Created
February 11, 2021 17:01
-
-
Save wilfreddesert/524a8eaff6cee5e3aa872addccc07943 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def divisible(begin, end): | |
""" | |
:param begin: int, positive integer | |
:param end: int, positive integer | |
:return: str, string of space separated integers | |
Examples of usage: | |
>>> divisible(1, 10) | |
'7' | |
>>> divisible(1, 17) | |
'7 14' | |
>>> len(divisible(100, 1000)) | |
407 | |
>>> divisible(29, 60) | |
'42 49 56' | |
>>> len(divisible(300, 3000).split()) | |
309 | |
>>> ns = [int(n) for n in divisible(300, 10000).split()] | |
>>> seven_mask = [not bool(n % 7) for n in ns] | |
>>> all(seven_mask) | |
True | |
>>> any(seven_mask) | |
True | |
>>> five_mask = [not bool(n % 5) for n in ns] | |
>>> all(five_mask) | |
False | |
>>> any(five_mask) | |
False | |
>>> divisible(2, 5) | |
'' | |
>>> 1329 not in ns | |
True | |
""" | |
answer = [ | |
str(item) for item in range(begin, end + 1) if item % 7 == 0 and item % 5 != 0 | |
] | |
return " ".join(answer) | |
def register_count(string): | |
""" | |
:param string: str, input string | |
:return: dict, dict of lower and upper letter counts | |
>>> register_count("Mama") == {'UPPER': 1, 'LOWER': 3} | |
True | |
>>> register_count("Your Name") == {'UPPER': 2, 'LOWER': 6} | |
True | |
>>> register_count("LingvoX!!!") == {'UPPER': 2, 'LOWER': 5} | |
True | |
>>> register_count("Trud, mir, mai! Zvahka!") == {'UPPER': 2, 'LOWER': 14} | |
True | |
>>> register_count("Coi ZIV!,,,,,") == {'UPPER': 4, 'LOWER': 2} | |
True | |
""" | |
lower_count = 0 | |
upper_count = 0 | |
for char in string: | |
if not char.isalpha(): | |
continue | |
if char == char.lower(): | |
lower_count += 1 | |
else: | |
upper_count += 1 | |
return dict(UPPER=upper_count, LOWER=lower_count) | |
def pairwise_diff(first, second): | |
""" | |
:param first: str, first input string | |
:param second: str, second input string | |
:return: float, percentage of different letters | |
>>> pairwise_diff('ABSD', 'ABCD') | |
0.25 | |
>>> pairwise_diff('aBSD', 'ABCD') | |
0.5 | |
>>> pairwise_diff('LingvX', 'SpaceX') | |
0.83 | |
>>> pairwise_diff('LingvoX', 'SpaceX') | |
Traceback (most recent call last): | |
... | |
AssertionError | |
>>> pairwise_diff('abc', 'ab') | |
Traceback (most recent call last): | |
... | |
AssertionError | |
>>> first = 'Salaman..'; second = 'Salaman.!' | |
>>> round(1. / len(first), 2) == pairwise_diff(first, second) | |
True | |
>>> pairwise_diff(first + second, first + first) | |
0.06 | |
>>> pairwise_diff(first * 3, second * 2 + first) | |
0.07 | |
""" | |
assert len(first) == len(second) | |
length = len(first) | |
unequal = 0 | |
for f, s in zip(first, second): | |
if f != s: | |
unequal += 1 | |
return round(unequal / length, 2) | |
def run_robot(): | |
""" | |
Uses input() inside. | |
:return: int, rounded euclidean distance from origin | |
""" | |
actions = [] | |
current_x = 0 | |
current_y = 0 | |
while True: | |
line = input() | |
if line: | |
actions.append(line) | |
else: | |
break | |
for item in actions: | |
direction = item.split()[0] | |
step = int(item.split()[1]) | |
if direction == "DOWN": | |
current_y -= step | |
elif direction == "UP": | |
current_y += step | |
elif direction == "RIGHT": | |
current_x += step | |
elif direction == "LEFT": | |
current_x -= step | |
return round(sqrt(current_x ** 2 + current_y ** 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment