-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
74 lines (56 loc) · 2.18 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
|| HackerRank
Problem: Append and Delete
Level: Easy
Author: zemen
Implementation: apexDev37
"""
def append_and_delete(initial: str, desired: str, moves: int) -> str:
response = append_by_empty_string(initial, desired, moves)
if matching_chars(initial, desired):
response = append_by_matching_chars(initial, desired, moves)
if response == 'No':
response = append_by_substrings(initial, desired, moves)
return response
def append_by_empty_string(initial: str, desired: str, moves: int) -> str:
delete_moves = len(initial)
append_moves = len(desired)
total_moves = delete_moves + append_moves
return 'Yes' if (total_moves <= moves) else 'No'
# TODO: Refactor function
def matching_chars(initial: str, desired: str) -> bool:
return (
initial == len(initial) * initial[0]
and desired == len(desired) * desired[0]
and initial[0] == desired[0]
)
def append_by_matching_chars(initial: str, desired: str, moves: int) -> str:
response = 'No'
difference = abs(len(desired) - len(initial))
if ((difference % 2) == (moves % 2)
or initial == desired):
response = 'Yes'
return response
def append_by_substrings(initial: str, desired: str, moves: int) -> str:
index = get_substring_index(initial, desired)
substring_lengths = get_substring_lengths(index, initial, desired)
response = 'Yes' if (substring_lengths == moves) else 'No'
return response
def get_substring_index(initial: str, desired: str) -> int:
substring_index = 0
for tuple in list(enumerate(zip(initial, desired)
)): # Tuple format: [0, ('h', 'h')]
chars = tuple[1]
if chars[0] != chars[1]:
substring_index = tuple[0]
break
return substring_index
def get_substring_lengths(index: int, initial: str, desired: str) -> int:
initial_substring = initial[index:]
desired_substring = desired[index:]
return len(initial_substring) + len(desired_substring)
def main() -> None:
print('Can append and delete: ',
append_and_delete(initial='aba', desired='aaa', moves=7))
if __name__ == "__main__":
main()