mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
warmup, preparing to give interviews
This commit is contained in:
parent
9b8453e57e
commit
792434e3af
10 changed files with 390 additions and 0 deletions
40
short_phone_interview_problems/reverse_str.py
Normal file
40
short_phone_interview_problems/reverse_str.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
def reverse_str_inplace(_str):
|
||||
if len(_str) < 2:
|
||||
return _str
|
||||
return _str[-1] + reverse_str(_str[1:-1]) + _str[0]
|
||||
|
||||
|
||||
def reverse_str(_str):
|
||||
result = ''
|
||||
j = len(_str) - 1
|
||||
|
||||
while j >= 0:
|
||||
result += _str[j]
|
||||
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
_str = ''
|
||||
result = ''
|
||||
assert(reverse_str(_str) == result)
|
||||
assert(reverse_str_inplace(_str) == result)
|
||||
|
||||
_str = 'a'
|
||||
result = 'a'
|
||||
assert(reverse_str(_str) == result)
|
||||
assert(reverse_str_inplace(_str) == result)
|
||||
|
||||
_str = 'abcde'
|
||||
result = 'edcba'
|
||||
assert(reverse_str(_str) == result)
|
||||
assert(reverse_str_inplace(_str) == result)
|
||||
|
||||
_str = 'abcdef'
|
||||
result = 'fedcba'
|
||||
assert(reverse_str(_str) == result)
|
||||
assert(reverse_str_inplace(_str) == result)
|
Loading…
Add table
Add a link
Reference in a new issue