9
eforselv
6 25
380/ 445
Last seen 7 years ago
Member for 7 years, 7 months, 25 days
Difficulty Normal
Best reviews / Newest reviews
Explain-a-lot-Sandra_Svensson 1 1
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
First-Sandra_Svensson 1
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
join-SeraFinN 1
Optionally you can drop the generator and do the replace on the joined string. More
First Binary count-Mapkn3 1
`bin` already returns a string, so no need to use `str()` here. More
sorted()-Sandra_Svensson 1
To be a nitpicker: Spaces around named function arguments normally don't have spaces around `=`. return sorted(numbers_array, key=abs) More
not first, wont be last-greg.p.mueller 1
`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
First-jpursell 1
`if array:` is enough to check if the list contains any elements. More
meh-Krischtopp 1
Optionally you can drop the generator and do the replace on the joined string. More
First-tofguerrier 1
`bin` already returns a string, so `str()` is not needed. Optionally you can use `str.count()` to skip the loop as well. More
First-Fkudue 1 1
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
Arbitrary arguments-Sandra_Svensson 1
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
Trying to be more efficient-Sandra_Svensson 1
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
Eight lines-Sandra_Svensson 1
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
Left is always right-Sandra_Svensson 1
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
Exponent of radix == enumerate reversed position-Kyiran 1
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
First-sanuskaria 1
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
First-Victor_Horbunkoff 1
Use sum() with a list comprehension to dramatically reduce the complexity :) More
First-GaidarOS 1 1
A few things: - `i = 0` is not needed. - `return "{}".format(out)` can just be `return out` More
First-rafal.zbytniewski 1
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
I wish this was shorter-Sandra_Svensson 1
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
1
2 3 4 5 6