Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit d7e73aa

Browse files
committed
Feature/sequences (#12)
* Longest unique characters sequence length Added get longest unique characters sequences length function. Added test cases for the function. * Updated documentation * Added sum strings Added implementation of "sum strings" challenge. The function gets two numbers as strings and return their sum as string. Test cases are added as well. * Updated sum of strings implementation Added more tests. Updated function implementation to include edge cases coverage.
1 parent ce814ce commit d7e73aa

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

src/sequences/func.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ def get_longest_palindrome(origin: str) -> str:
6363
:return: the longest palindrome
6464
:rtype: str
6565
66+
Usage examples:
67+
6668
>>> assert get_longest_palindrome("ABBABBC") == "BBABB"
6769
6870
"""
@@ -141,6 +143,18 @@ def get_fibonacci_number(idx: int) -> int:
141143
:return: a sequence's member
142144
:rtype: int
143145
146+
The Fibonacci number is a number from the Fibonacci sequence, in which
147+
each number is the sum of the two preceding ones. This sequence commonly
148+
starts from 0 and 1: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...
149+
150+
Usage examples:
151+
152+
>>> assert get_fibonacci_number(0) == 0
153+
>>> assert get_fibonacci_number(1) == 1
154+
>>> assert get_fibonacci_number(2) == 1
155+
>>> assert get_fibonacci_number(3) == 2
156+
>>> assert get_fibonacci_number(4) == 3
157+
144158
"""
145159

146160
if idx <= 0:
@@ -150,3 +164,92 @@ def get_fibonacci_number(idx: int) -> int:
150164
return 1
151165

152166
return get_fibonacci_number(idx - 1) + get_fibonacci_number(idx - 2)
167+
168+
169+
def get_longest_uniq_length(origin: str) -> int:
170+
"""Return the length of the longest on sequence of unique characters
171+
172+
Usage examples:
173+
174+
>>> assert get_longest_uniq_length("abcdefg") == 7
175+
>>> assert get_longest_uniq_length("abcacba") == 3
176+
177+
:param origin: original sequences
178+
:type origin: str
179+
180+
:return: the length of the longest unique characters sequence
181+
:rtype: int
182+
183+
"""
184+
185+
length: int = 0
186+
187+
size: int = len(origin)
188+
left_idx: int = 0
189+
190+
for right_idx in range(size):
191+
while origin[right_idx] in origin[left_idx:right_idx]:
192+
left_idx += 1
193+
length = max(length, right_idx - left_idx + 1)
194+
195+
return length
196+
197+
198+
def get_sum_of_strings(number_1: str, number_2: str) -> str:
199+
"""Return the sum of two numbers of string type as string
200+
201+
:param number_1: first number
202+
:type number_1: str
203+
:param number_2: second number
204+
:type number_2: str
205+
206+
:return: the sum of two numbers
207+
:rtype: str
208+
209+
Valid input is a string of any length containing numeric characters from
210+
0 to 9. Empty strings are allowed as well and should be considered as 0.
211+
212+
Usage examples:
213+
214+
>>> assert get_sum_of_strings("123", "456") == "579"
215+
>>> assert get_sum_of_strings("099", "001") == "100"
216+
>>> assert get_sum_of_strings("", "10") == "10"
217+
>>> assert get_sum_of_strings("", "") == "0"
218+
219+
"""
220+
221+
# remove leading zeros
222+
for digit in number_1:
223+
if digit != "0":
224+
break
225+
number_1 = number_1[1::]
226+
for digit in number_2:
227+
if digit != "0":
228+
break
229+
number_2 = number_2[1::]
230+
231+
if not number_1 or not number_2:
232+
return number_1 or number_2 or "0"
233+
234+
result: str = ""
235+
size: int = max(len(number_1), len(number_2))
236+
237+
# reverse numbers
238+
number_1, number_2 = number_1[::-1], number_2[::-1]
239+
240+
# make strings of the same lengths
241+
while len(number_1) < size:
242+
number_1 += "0"
243+
while len(number_2) < size:
244+
number_2 += "0"
245+
246+
carry: int = 0
247+
for digit_1, digit_2 in zip(number_1, number_2):
248+
sum_of_digits = int(digit_1) + int(digit_2) + carry
249+
result += str(sum_of_digits % 10)
250+
carry = sum_of_digits // 10
251+
252+
if carry:
253+
result += str(carry)
254+
255+
return result[::-1]

tests/sequences/test_func.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,36 @@ def test_fibonacci_number_getter():
6262
assert sequences.get_fibonacci_number(2) == 1
6363
assert sequences.get_fibonacci_number(3) == 2
6464
assert sequences.get_fibonacci_number(9) == 34
65+
66+
67+
# noinspection SpellCheckingInspection
68+
def test_get_longest_uniq_sequence_length():
69+
assert sequences.get_longest_uniq_length("abcdefg") == 7
70+
assert sequences.get_longest_uniq_length("abcacba") == 3
71+
assert sequences.get_longest_uniq_length("hwccjayhiszbmomlqkem") == 11
72+
73+
74+
def test_sum_of_strings():
75+
assert sequences.get_sum_of_strings("123", "456") == "579"
76+
assert sequences.get_sum_of_strings("123", "789") == "912"
77+
assert sequences.get_sum_of_strings("99", "1") == "100"
78+
79+
80+
def test_sum_of_strings_big_numbers():
81+
x, y, test_value = "490053634", "20846201", "510899835"
82+
assert sequences.get_sum_of_strings(x, y) == test_value
83+
84+
x, y, test_value = "616526149", "364644330", "981170479"
85+
assert sequences.get_sum_of_strings(x, y) == test_value
86+
87+
88+
def test_sum_of_strings_leading_zeros():
89+
assert sequences.get_sum_of_strings("009", "000001") == "10"
90+
assert sequences.get_sum_of_strings("020", "000000") == "20"
91+
assert sequences.get_sum_of_strings("000", "000000") == "0"
92+
93+
94+
def test_sum_of_strings_empty():
95+
assert sequences.get_sum_of_strings("123", "") == "123"
96+
assert sequences.get_sum_of_strings("", "456") == "456"
97+
assert sequences.get_sum_of_strings("", "") == "0"

0 commit comments

Comments
 (0)