17
Last seen 1 year ago
Member for 8 years, 8 months, 11 days
Difficulty Normal
One of those cases where it is much nicer to use nested for loops rather than tricky list comprehenssions.
More
Rather than
ascii_lowercase[ascii_lowercase.index(col) - 1]
you might prefer to use
chr(ord(col) - 1)
This is (in my opinion) slightly cleaner regarding the possibility of negative index
More
Two points:
- To build your dictionary, it is inefficient to iterate through lower_text, as every letter will be counted multiple times. This is n^2, bad news for (very) long texts. Better use string.ascii_lowercase
- You can also find the key for the (first) max value of a dictionary by
result
More
Just a nit:
symbol_one*digit
is a lot shorter and works as well as
''.join([symbol_one for _ in range(digit)])
More
Does this work in every case?
Suppose the network is [a-b, c-d, a-c]
Then your for loop iterations of the root directory are
1: a-b
root = {a:a, b:a, c:c, d:d}
size = {a:2, b:1, c:1, d:1}
2: c-d
root = {a:a, b:a, c:c, d:c}
size = {a:2, b:1, c:2, d:1}
3:
More
To compute the set of rings,
rings = set.union(*cons)
would be simpler.
Also, all the breaks and continues in the main loop look a bit cumbersome. You could get around this by refactoring, i.e. defining a function which checks a set of broken_rings, called from the second for loop. You
More
for some reason, lower case characters have not been included in the problem statement. So, line 3 should also check c.isupper()
More
Two small points:
* The line largest = max(...) can be drawn outside the if / else block
* ant_c = does not need to be repeated for every c. Could place it inside the else branch
More
The use of map in line 5 does not make the code shorter, faster or more readable, so I would prefer to use the straightforward
return [sorted(young_or_old), sorted(middle_age)]
Matter of taste though ...
More
Instead of checking for leap years yourself, you could ask datetime for help:
Number of days is
(datetime.datetime(year + 1, 1, 1) - datetime.datetime(year, 1, 1)).days
More
- d3, d5 and text are not used.
- rather than testing x = int(number), you could put the whole if block into a try.
- tiny nit: testing for x%15 == 0 rather than x%3==0 and x%5==0 is a bit shorter
More
The inequality checks can be left out, because they have already been covered by the first (divisible by 15) check.
More