40
Last seen 4 days ago
Member for 9 years, 10 months, 19 days
Difficulty Advanced
Hi, _bin()_ return __str__ which is iterable, you don't need those indices to access elements:
def checkio(number):
b_str = bin(number)
count = 0
for e in b_str:
if e == '1':
count += 1
return count
# or use str.count() metho
More
Hi,
1. __str__ is iterable
2. `d` id redundant.
3. Line 8 is redundant.
def checkio(number):
res = 1
s = str(number)
for e in s:
if e != '0':
res * int(s[i])
return res
# or even
for e in str(number):
More
Hi,
n in range(len(array))
is not a good way. Check this:
0 <= n < len(array)
More
Hi,
1. this:
if gb:
flag = True
else:
flag = False
return flag
you can simply return `gb`:
return gb
2. You can replace all that `#search` code with _str.isalpha()_.
3. In fact you don't need `gb` and `flag` variables:
def checkio(words):
count
More
Hi, Python has __set__ which has _set.intersection()_ method and there's even a set intersection operation `&`. Also look at _sorted()_.
def checkio(first, second):
set1 = set(first.split(","))
set2 = set(second.split(","))
return ",".join(sorted(set1 & set2))
More
Hi, __str__ is not good type for accumulating values as it's immutable. Look at _filter()_ function.
More
Hi, look at _filter()_ built-in and the _str.join()_ method.
return "".join(filter(str.isupper, text))
More
This solution is for demonstration purposes ([see](https://py.checkio.org/forum/post/10113/help-please/)).
More
Hi,
1. Instead of lines 10-14 you can write: `return sum(x[0]!=x[1] for x in zip(*(map(int, i) for i in (x, y))))`.
2. Look at `^` (alias `xor`) operator.
Regards,
Gabriel
More
Hi,
1. look at the _sum()_ builtin:
sum(int(digit) for digit in xnorm)
2. In fact, you just need to count the number of "1" in xnorm, therefore:
return bin(n^m).count('1')
More
Hi,
1. this is quite inefficient as __str__ is immutable.
2. Look at negative indices. For non-empty string/list/tuple... `seq[:len(seq)-1] = seq[-1]`.
3. Look at _str.join()_.
More
Hi,
1. You could write:
matchstring = ",".join(matchlist)
# instead of:
matchesstring = ""
for match in matcheslist:
matchesstring = matchesstring + match + ","
matchesstring = matchesstring[:len(matchesstring)-1]
2. Look at __set__ built-in type and set operations:
More