Skip to content

Instantly share code, notes, and snippets.

@precious
Created June 22, 2019 18:32
Show Gist options
  • Save precious/cf486445bb2a84a68ec444ac1a7138e3 to your computer and use it in GitHub Desktop.
Save precious/cf486445bb2a84a68ec444ac1a7138e3 to your computer and use it in GitHub Desktop.
import re
def roman_to_int(r):
r = r.strip().lower()
r_original = r
r = r.rstrip('i')
right_i_num = len(r_original) - len(r)
def parse_group(g):
return {'x': 10, 'v': 5}[g[-1]] - (len(g) - 1)
groups = re.findall(r'(i*[xv])', r) # to check!!!!!!!!
print(groups)
the_sum = right_i_num
for g in groups:
the_sum += parse_group(g)
return the_sum
#########################################################
class Node:
def __init__(self, left=None, right=None, i=0):
self.left = left
self.right = right
self.i = i
def __repr__(self):
return 'Node(i={}, l={}, r={})'.format(self.i, self.left, self.right)
def leftfurthest(tree, current_distance=0):
if tree.left is not None:
best_node_1, best_node_distance_1 = leftfurthest(tree.left, current_distance - 1)
else:
best_node_1, best_node_distance_1 = None, float('inf')
if tree.right is not None:
best_node_2, best_node_distance_2 = leftfurthest(tree.right, current_distance + 1)
else:
best_node_2, best_node_distance_2 = None, float('inf')
if best_node_distance_1 <= best_node_distance_2:
best_child = best_node_1
best_distance = best_node_distance_1
else:
best_child = best_node_2
best_distance = best_node_distance_2
if current_distance < best_distance:
best_child = tree
best_distance = current_distance
return best_child, best_distance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment