22
Last seen 5 years ago
Member for 8 years, 4 months
Difficulty Normal
Would like to receive comments/critics of my published solutions.
Not nearly enough cases.... See [here](https://py.checkio.org/mission/fizz-buzz/publications/jesaispas/python-3/i-know-switch-case-are-better/) :D :D
More
One of the better solutions I've seen so far (I'm doing random reviews for all published solutions)! Probably because I really like using sets whenever I can :)
Some minor things:
Line 6: no need to iterate through values, you can simply do:
all_users = sum(users.values())
Lines 17 and 24: `
More
Line 7: `total = sum(users.values())`
Line 12: `while nodes:`
Lines 18-20: `next` is a Python keyword, you should't use it as a name for your variables.
More
Instead of importing `chain` and creating a set `all_nodes`, wouldn't it be easier just to change the line 15 to:
disocnnected = users.keys() - connected
More
Function `make_graph` - instead of using `node[0]` and `node[1]`, it would be more elegant to use tuple unpacking in the for loop.
Also, you can use `defaultdict` to avoid checking if a key `node[0]` already exists (lines 5-8).
Putting those two things together:
from collections import defau
More
`sum` is a Python keyword, you shouldn't use keywords for naming your variables. The correct usage of `sum` (as a function) would be if you replaced lines 20-23 with:
return sum(users[node] for node in all_nodes.difference(next_nodes))
Lines 7-9: you can use tuple unpacking directly in the for
More
Lines 7-10: more pythonic way of iterating is by directly iterating through elements, not indices:
def customSet(word):
tmp = [word[0]]
for c in word[1:]:
if c != tmp[-1]:
tmp.append(c)
return "".join(tmp)
Line 14 is not needed.
Lines 15 and
More
Line 9 - why joining a split subject when you can just use the original one with `if subj.isupper()`? ;)
More
I took some liberty and refactored your solution:
Lines 4 and 6 can be checked with one `if` statement.
Line 9 is not needed if we use `subj.lower()` on line 10.
`reps` is always 1, so it is not needed.
Line 12: `join` accepts generator - no need to create a list (use `[` and `]`). It seems t
More
Lines 8-11 could/should be more pythonic if written as one line, by replacing `if` with `return`:
return subj.isupper() or subj[-3:] == '!!!' or any(word in ...
More
Some nice ideas here (hence upvotes), but not as speedy as I expected from a solution in this category (hence only two upvotes :))
More
Nice idea to limit the range just to the half of the length of the data (line 6)!
More
Instead of: `if not any(s for s in subj if s.islower())`, it would be easier just to check:
if subj.isupper()
Also, you can get rid of line 10 comment, if on line 11 you use a string method that is self-describing:
if subj.endswith('!!!')
More
The solution would be more elegant if we were allowed to return only one of the most critical points:
return min(users.keys(), key=subnetworks)
More
Line 4, more elegant way to see if string ends with some characters would be:
subj.endswith('!!!')
More
Instead of manually flooring each result of a division with /, there's a much simpler way - using floor division with //. For example:
data[len(data)//2]
More
There are several things that can be learnt/improved here:
1. I already mentioned in one other your solution - there's no need to use `len(a)-1`, just `-1` would be enough for reaching the last index.
2. You don't need to create a list in the comprehension just to find its sum. You can skip [ and ]
More
Instead of defining the lambda function for a key, there's a simpler solution that serves the same purpose:
key=abs
More
I've seen that you published some other solutions too, but just to have it for the future, maybe you didn't know:
There's no need to index the last index as `len(data)-1` - negative indices are possible, so you can get the last element of data with `data[-1]`.
More
There is a `hypot` function in the `math` module, that could have been more elegant than your line 4.
Line 13 would look like this:
else: replace = hypot(*subtract(pre, cur)) < hypot(*subtract(pre, nex))
More