Basically, the purpose of this code is to break up strings that have camelCases into different strings, and I looked up some code samples online, and I found this solution. The code works, however, I don't quite understand what some of the syntax actually does, even after some research. I made a # comment on each line I didn't understand, explaining my confusion.
I simply just don't want to mindlessly copy code off the internet without understanding what it does!
Also, if possible, are there any lessons online you recommend I goto to learn about this concept to avoid further confusion? Thanks!
def camelCase(str):
words = [[str[0]]]
for c in str[1: ]: #What does str[1: ] do? I tried looking it up but Google isn't helpful
if words[-1][-1].islower() and c.isupper(): #I'm not quite sure what function [-1][-1] plays in this function
words.append(list(c))
else:
words[-1].append(c) #I once again don't understand the function of [-1] in this else statement
return [' '.join(word) for word in words]
Why does Python start at index -1 (as opposed to 0) when indexing a list from the end? - Stack Overflow
Why is the index -1 always denoted as the last element of a list in python?
python - Negative list index? - Stack Overflow
Embarrassingly, i don't understand how list indexing works
To explain it in another way, because -0 is equal to 0, if backward starts from 0, it is ambiguous to the interpreter.
If you are confused about -, and looking for another way to index backwards more understandably, you can try ~, it is a mirror of forward:
arr = ["a", "b", "c", "d"]
print(arr[~0]) # d
print(arr[~1]) # c
The typical usages for ~ are like "swap mirror node" or "find median in a sort list":
"""swap mirror node"""
def reverse(arr: List[int]) -> None:
for i in range(len(arr) // 2):
arr[i], arr[~i] = arr[~i], arr[i]
"""find median in a sort list"""
def median(arr: List[float]) -> float:
mid = len(arr) // 2
return (arr[mid] + arr[~mid]) / 2
"""deal with mirror pairs"""
# verify the number is strobogrammatic, strobogrammatic number looks the same when rotated 180 degrees
def is_strobogrammatic(num: str) -> bool:
return all(num[i] + num[~i] in '696 00 11 88' for i in range(len(num) // 2 + 1))
~ actually is a math trick of inverse code and complement code, and it is more easy to understand in some situations.
Discussion about whether should use python tricks like ~:
In my opinion, if it is a code maintained by yourself, you can use any trick to avoid potential bug or achieve goal easier, because of maybe a high readability and usability. But in team work, avoid using 'too clever' code, may bring troubles to your co-workers.
For example, here is one concise code from Stefan Pochmann to solve this problem. I learned a lot from his code. But some are just for fun, too hackish to use.
# a strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down)
# find all strobogrammatic numbers that are of length = n
def findStrobogrammatic(self, n):
nums = n % 2 * list('018') or ['']
while n > 1:
n -= 2
# n < 2 is so genius here
nums = [a + num + b for a, b in '00 11 88 69 96'.split()[n < 2:] for num in nums]
return nums
I have summarized python tricks like this, in case you are interested.
list[-1]
Is short hand for:
list[len(list)-1]
The len(list) part is implicit. That's why the -1 is the last element. That goes for any negative index - the subtraction from len(list) is always implicit
Today I was learning lists in python and I came across an easy way to find the last element of a list, which is using the index -1, and I was wondering why is it so? could it be random or there is a specific reason for it?
Negative numbers mean that you count from the right instead of the left. So, list[-1] refers to the last element, list[-2] is the second-last, and so on.
List indexes of -x mean the xth item from the end of the list, so n[-1] means the last item in the list n. Any good Python tutorial should have told you this.
It's an unusual convention that only a few other languages besides Python have adopted, but it is extraordinarily useful; in any other language you'll spend a lot of time writing n[n.length-1] to access the last item of a list.