44
Last seen 5 months ago
Member for 10 years, 3 months, 4 days
Difficulty Normal
Line 5:
bins = [re.sub('(?P\d+)(\.)?', binarize, a) for a in adds]
Line 8:
pat = ','.join(['(?P\d+)\d*'] + ['\\1\d*'] * (len(adds) - 1))
More
def all_routes_from_node(m, node):
possibilities = []
for arc in m:
if node in arc:
possibilities.append(arc)
return possibilities
How about using **list comprehension** here?
def all_routes_from_node(m, node):
return [arc fo
More
def distance(x,y):
return math.sqrt((x*x+y*y))
You can use **hypot** instead of sqrt of sum of squares:
distance = math.hypot
More
I've tried to let **ConsistentDict** inherit **dict**, but it doesn't work:
class ConsistentDict(dict):
def __setitem__(self, key, item):
if not key in self.keys(): dict.__setitem__(self, key, item)
elif self[key] != item: raise ValueError #prohibit updates
More
You can write simpler by using **math.ceil** (if you don't hate importing **math**).
**FROM**:
x = 0
while x < radius:
y = 0
while y < radius:
#(skip)
y+=1
x+=1
**TO**:
for x in range(ceil(radius)):
for y in range
More
if "1" in chemin and "2" in chemin and ...
You can check **chemin** using **set**:
if len(set(chemin)) == 8 and chemin[-1] == "1":
----
teleports_map = [(int(x), int(y)) for x, y in teleports_string.split(",")]
You don't need to convert strings to **int**:
teleports_map = telepo
More
To improve my code, I've used [Eldin's](http://www.checkio.org/mission/house-password/publications/Eldin/python-3/using-regex/#comment-outer-10787) as a reference. Thanks.
More
I'll modify post-processing.
FROM:
if match and all(a != b for a, b in combinations(match.groups(), 2)):
return structure_grid_from(candidate_words)
TO(delete **structure\_grid\_from**):
if match and all(a != b for a, b in combinations(match.groups(), 2)):
_dict
More
pos = int(len(wrk)/2)
You can use **//** instead of **int()**:
pos = len(wrk) // 2
More
adjacent = [set() for _ in range(i)]
How about **collections.defaultdict**?
adjacent = defaultdict(set)
More
cand = set(data)
for c in cand:
You can write as follows(del **cand**):
for c in set(data):
More
distance = lambda x, y: math.sqrt(x**2 + y**2)
You can use **hypot**:
distance = math.hypot
More
for i in word2:
if i in word1:
if word1.count(i) == word2.count(i):
word1 = word1.replace(i, "")
You don't need first **if** because second is False if i is not in word1(left=0, right>0).
More
[revised](http://www.checkio.org/mission/the-square-chest/publications/gyahun_dash/python-3/5th/)
More
Appendix:
font = 27499, 11410, 29415, 29327, 23497, 31118, 14827, 29348, 31727, 15310
More
**string.ascii_uppercase** is helpful for your code.
pair = [(chr(x), l.count(chr(x))) for x in range(ord('a'), ord('z')+1)]
a = max(pair, key = lambda x:x[1])
You can use **l.count** in **lambda function** as follows. By doing so, you don't need to create the list of pairs.
a
More
def threeof(string):
return (string[0] != ".") & (string[0] == string[1]) & (string[0] == string[2])
You can integrate the expressions.
def threeof(string):
return (string[0] == string[1] == string[2] != ".")
More