9
Last seen 7 years ago
Member for 7 years, 7 months, 25 days
Difficulty Normal
Using array slicing would make this simpler. The cool thing with array slicing is that it does bounds checking for us.
>> array = []
>> array[100:200] # grab element from 100 to 199
[] # We just get an empty list back and that is all good
Getting the last entry of a list is also simple
More
Pretty much perfect. You can drop the last return though and just return the value inside the `try/catch`.
try:
return array[n] ** n
except IndexError:
return -1
More
To be a nitpicker: Spaces around named function arguments normally don't have spaces around `=`.
return sorted(numbers_array, key=abs)
More
`bin` already returns a string, so using `str()` here is not needed. We also know that the prefix will always be `0b`, so we could either just run `count` on the entire string or use slicing.
return bin(number).count("1")
or...
return bin(number)[2:].count("1")
.. to make sure we only co
More
Optionally you can drop the generator and do the replace on the joined string.
More
`bin` already returns a string, so `str()` is not needed. Optionally you can use `str.count()` to skip the loop as well.
More
You can use list comprehensions to solve this in one simple line.
[unit for unit in data if ... etc]
Also having spaces around assignments and operators is the standard style in python.
newdata = []
if data.count(unit) != 1:
More
Isn't the `for` loop here a bit overkill? You create the `numbers` list that is identical to the `args` list. If you print them out, that's probably what you will see. A shorter version is :
if args:
return max(args) - min(args)
else:
return 0
*args is a list arbitrary posi
More
Looks good. The `uppers` could have been a generator instead so python don't have to construct an actual list in memory.
uppers = (letter for letter in text if letter.isupper())
Not sure if removing the parenthesis works in python 2.
return "".join(letter for letter in text if letter.isup
More
Nice and simple. I would be careful with introducing temporary variables when not needed. Extra variables means we have to mentally map then when reading the code.
This version might be easier to read:
calculate = 1
for n in str(number):
if n != '0':
calculate *= int(n)
More
Pretty much perfect except that the more pythonic way is :
return ",".join(phrases).replace('right', 'left')
If you can express the code in a sentence it can go in the same line: "Join the phrases into a string and replace all occurrences of 'left' with 'right'". If it gets a lot more complex
More
It's the old way of doing it and it definitely works, but it's still an extremely convoluted way of expressing "int(str_number, radix) :)
More
A bit too complicated solution, but definitely creative :D Remember that python likes spaces between operators and assignment. See pep8. "p = 1", "num = number % 10".. etc
More
Use sum() with a list comprehension to dramatically reduce the complexity :)
More
A few things:
- `i = 0` is not needed.
- `return "{}".format(out)` can just be `return out`
More
Nice to see that generators are used.
Optionally you can skip the list comprehension completely and do a:
return ','.join(phrases).replace('right','left')
More
Every time you see something like this:
if count == 3:
return True
else:
return False
.. there is a better way.
return count == 3
Another variant:
count = 0
for word in words.split():
if word.isalpha():
count += 1
if count == 3
More