27
Last seen seconds ago
Member for 1 year, 6 months, 29 days
Difficulty Normal
Nice :)
You could even go for
from collections.abc import Iterable
def duplicate_zeros(donuts: list[int]) -> Iterable[int]:
# your code here
for i in donuts:
yield from [i] + [0] * (not i)
More
You could shorten it to
from collections.abc import Iterable
def duplicate_zeros(donuts: list[int]) -> Iterable[int]:
# your code here
dup_donuts = list()
for x in donuts:
dup_donuts += [x] + [0] * (x == 0)
return dup_donuts
More
You can flatten the list by just sum() it with an empty list:
from collections.abc import Iterable
def duplicate_zeros(donuts: list[int]) -> Iterable[int]:
# your code here
return sum([[num] if num else [num, num] for num in donuts], [])
More
No need for a counter i, just directly iterate over the list:
from collections.abc import Iterable
def duplicate_zeros(donuts: list[int]) -> Iterable[int]:
ans = []
for d in donuts:
if d == 0:
ans.append(0)
ans.append(d)
retur
More
No need for an 'else' clause:
from collections.abc import Iterable
def duplicate_zeros(donuts: list[int]) -> Iterable[int]:
res = []
for x in donuts:
res.append(x)
if x == 0:
res.append(x)
return res
More
Very nice! Thanks for reminding me that map can take more than one iterable.
More
I have some issues with this solution..
In combinations_with_replacement you take range(1, 18) -> 1 to 17, but max(die) is <= 18.
Also, you could optimize that to range(1, min(19, sum(enemy_die) - len(enemy_die) + 2)
More
Thanks for that solution. I didn't know you could flatten lists/sets like that.
sum(matrix, tuple())
More
Oh wow, now THIS one is quick :D
Takes 0.00 seconds for 1000 runs of is_perfect(100000000). Mine takes 0.40 seconds.. Nice one!
More
it's pretty slow though.. Takes 2.5 seconds on my computer for is_perfect(100000000). You could at least go for
def is_perfect(n: int) -> bool:
def find_divisors(m):
for i in range(1, (m + 2) // 2):
if m % i == 0:
yield i
return sum(find_divisors(n))
More
It's very slow though.. Takes over 4 seconds for n == 100'000'000 on my computer. You could at least go for
divisors = filter(lambda x: n % x == 0, range(1, (n + 2) // 2))
More
Loved your "Clear" solution, but now this is just a nightmare! ;)
More
Loved this one! Lots of things to learn here.
code[i] or '0'
Nice!
'01'[i in rs]
Very nice! :)
More
Yeah, thats the good stuff! I would give you points for it, but sadly that doesn't work in 'random soulution'.. ;)
More
This code could need some cleanups..
For example, all the print statements are redundant. You could replace the 'items[len(items)-1]'-like statements with just 'items[-1].
More
if s2[0] >s1[0]+s1[2] or s2[0] < s1[0]:
if s2[1] > s1[1]+s1[2] or s2[1]s1[0]+s1[2] or s2[0] < s1[0]) and (s2[1] > s1[1]+s1[2] or s2[1]More
if s2[0]<=s1[0]+s1[2] and s2[0]>=s1[0]-s2[2]:
return True
else:
return False
could be
return s2[0]<=s1[0]+s1[2] and s2[0]>=s1[0]-s2[2]
More