Add 'universe/' from commit '8ad51b24dd'
git-subtree-dir: universe git-subtree-mainline:15110e6de9git-subtree-split:8ad51b24dd
This commit is contained in:
commit
fb9380ba26
131 changed files with 13792 additions and 0 deletions
22
universe/data_structures_and_algorithms/string-reverse.py
Normal file
22
universe/data_structures_and_algorithms/string-reverse.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
|
||||
# swap :: Int -> Int -> [Char] -> IO ()
|
||||
def swap(ia, iz, xs):
|
||||
# handle swap when ia == iz
|
||||
assert ia <= iz
|
||||
xs[ia], xs[iz] = xs[iz], xs[ia]
|
||||
|
||||
|
||||
# reverse :: [Char] -> IO ()
|
||||
def reverse(xs):
|
||||
ia = 0
|
||||
iz = len(xs) - 1
|
||||
|
||||
while ia <= iz:
|
||||
swap(ia, iz, xs)
|
||||
ia += 1
|
||||
iz -= 1
|
||||
|
||||
x = list("superduperpooper")
|
||||
reverse(x)
|
||||
print(x)
|
||||
print("Tests pass")
|
||||
Loading…
Add table
Add a link
Reference in a new issue