mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
some new examples
This commit is contained in:
parent
5ed530430c
commit
b50dc5d69d
Binary file not shown.
Binary file not shown.
@ -1,45 +0,0 @@
|
|||||||
#!/usr/bin/python3
|
|
||||||
|
|
||||||
# Mari von Steinkirch @ 2013
|
|
||||||
# mari.wahl9@gmail.com
|
|
||||||
|
|
||||||
# Bernardo Sulzbach (mafagafo) @ 2014
|
|
||||||
# 1449441@gmail.com
|
|
||||||
|
|
||||||
|
|
||||||
def reverse_words(string):
|
|
||||||
"""
|
|
||||||
Reverse the order of the words in a sentence.
|
|
||||||
:param string: the string which words wil lbe reversed.
|
|
||||||
:return: the reversed string.
|
|
||||||
"""
|
|
||||||
l1 = string.split(' ')
|
|
||||||
l1.reverse()
|
|
||||||
return ' '.join(l1)
|
|
||||||
|
|
||||||
|
|
||||||
def reverse_words_brute(string):
|
|
||||||
"""
|
|
||||||
Reverse the order of the words in a sentence.
|
|
||||||
:param string: the string which words wil lbe reversed.
|
|
||||||
:return: the reversed string.
|
|
||||||
"""
|
|
||||||
word, sentence = [], []
|
|
||||||
for character in string:
|
|
||||||
if character != ' ':
|
|
||||||
word.append(character)
|
|
||||||
else:
|
|
||||||
# So we do not keep multiple whitespaces. An empty list evaluates to False.
|
|
||||||
if word:
|
|
||||||
sentence.append(''.join(word))
|
|
||||||
word = []
|
|
||||||
# So we do not keep multiple whitespaces. An empty list evaluates to False.
|
|
||||||
if word != '':
|
|
||||||
sentence.append(''.join(word))
|
|
||||||
sentence.reverse()
|
|
||||||
return ' '.join(sentence)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
phrase = 'I love Google, Fedora & Python!'
|
|
||||||
print(reverse_words(phrase))
|
|
||||||
print(reverse_words_brute(phrase))
|
|
@ -1,23 +0,0 @@
|
|||||||
#!/usr/bin/python3
|
|
||||||
# mari von steinkirch @2013
|
|
||||||
# steinkirch at gmail
|
|
||||||
|
|
||||||
|
|
||||||
def reversing_words_setence(str1):
|
|
||||||
''' reverse the words in a sentence'''
|
|
||||||
words = str1.split()
|
|
||||||
rev_set = " ".join(reversed(words))
|
|
||||||
return rev_set
|
|
||||||
|
|
||||||
|
|
||||||
def test_reversing_words_setence(module_name='this module'):
|
|
||||||
str1 = "Buffy is a Vampire Slayer"
|
|
||||||
assert(reversing_words_setence(str1) == "Slayer Vampire a is Buffy")
|
|
||||||
|
|
||||||
s = 'Tests in {name} have {con}!'
|
|
||||||
print(s.format(name=module_name, con='passed'))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
test_reversing_words_setence()
|
|
||||||
|
|
@ -1,12 +1,8 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/env python
|
||||||
|
|
||||||
# Mari von Steinkirch @ 2013
|
author = "Mari Wahl"
|
||||||
# mari.wahl9@gmail.com
|
email = "marina.w4hl@gmail.com"
|
||||||
|
|
||||||
# Bernardo Sulzbach (mafagafo) @ 2014
|
|
||||||
# 1449441@gmail.com
|
|
||||||
|
|
||||||
# This file defines and compares four different functions that reverse a string.
|
|
||||||
|
|
||||||
# timeit is used for benchmarking.
|
# timeit is used for benchmarking.
|
||||||
from timeit import timeit
|
from timeit import timeit
|
@ -1,6 +1,8 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/env python
|
||||||
# mari von steinkirch @2013
|
|
||||||
# steinkirch at gmail
|
author = "Mari Wahl"
|
||||||
|
email = "marina.w4hl@gmail.com"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def reverse_string_inplace_rec(s):
|
def reverse_string_inplace_rec(s):
|
||||||
@ -8,14 +10,14 @@ def reverse_string_inplace_rec(s):
|
|||||||
if s:
|
if s:
|
||||||
s = s[-1] + reverse_string_inplace_rec(s[:-1])
|
s = s[-1] + reverse_string_inplace_rec(s[:-1])
|
||||||
return s
|
return s
|
||||||
|
|
||||||
def reverse_string_inplace(s):
|
def reverse_string_inplace(s):
|
||||||
s = s[::-1]
|
s = s[::-1]
|
||||||
if s[0] == '\0':
|
if s[0] == '\0':
|
||||||
s = s[1:]
|
s = s[1:]
|
||||||
s += '\0'
|
s += '\0'
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_reverse_string_inplace_rec(module_name='this module'):
|
def test_reverse_string_inplace_rec(module_name='this module'):
|
||||||
@ -24,7 +26,7 @@ def test_reverse_string_inplace_rec(module_name='this module'):
|
|||||||
assert(reverse_string_inplace(s) == 'olleh')
|
assert(reverse_string_inplace(s) == 'olleh')
|
||||||
assert(reverse_string_inplace(s2) == 'yffub\0')
|
assert(reverse_string_inplace(s2) == 'yffub\0')
|
||||||
assert(reverse_string_inplace_rec(s) == 'olleh')
|
assert(reverse_string_inplace_rec(s) == 'olleh')
|
||||||
|
|
||||||
s = 'Tests in {name} have {con}!'
|
s = 'Tests in {name} have {con}!'
|
||||||
print(s.format(name=module_name, con='passed'))
|
print(s.format(name=module_name, con='passed'))
|
||||||
|
|
@ -0,0 +1,118 @@
|
|||||||
|
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
author = "Mari Wahl"
|
||||||
|
email = "marina.w4hl@gmail.com"
|
||||||
|
|
||||||
|
""" Here we want to invert the words in a string, without reverting
|
||||||
|
the words.
|
||||||
|
|
||||||
|
Important things to remember:
|
||||||
|
|
||||||
|
1. python strings are immutable
|
||||||
|
2. The last word doesn't not end by a space, so we need to make
|
||||||
|
sure we get the last word too
|
||||||
|
|
||||||
|
The solution consists of two loops,
|
||||||
|
1) revert all the characters with 2 pointers
|
||||||
|
2) search for spaces and revert the words, two pointers too
|
||||||
|
3) You can represent space as ' ' or as u'\u0020'
|
||||||
|
4) Do we want to look to ! ; , . etc?
|
||||||
|
|
||||||
|
In the solutions bellow, we show how to do this logic, and how to use
|
||||||
|
python's methods to do in a few lines
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
# EXAMPLE NUMBER 1
|
||||||
|
|
||||||
|
def reverser(string1, p1=0, p2=None):
|
||||||
|
if len(string1) < 2:
|
||||||
|
return string1
|
||||||
|
p2 = p2 or len(string1)-1
|
||||||
|
while p1 < p2:
|
||||||
|
aux = string1[p1]
|
||||||
|
string1[p1] = string1[p2]
|
||||||
|
string1[p2] = aux
|
||||||
|
p1 += 1
|
||||||
|
p2 -= 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def reversing_words_setence_logic(string1):
|
||||||
|
reverser(string1)
|
||||||
|
p = 0
|
||||||
|
start = 0
|
||||||
|
final = []
|
||||||
|
while p < len(string1):
|
||||||
|
if string1[p] == u"\u0020":
|
||||||
|
reverser(string1,start,p-1)
|
||||||
|
start = p+1
|
||||||
|
p += 1
|
||||||
|
reverser(string1,start,p-1)
|
||||||
|
|
||||||
|
return "".join(string1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# EXAMPLE NUMBER 2 AND 3 USING PYTHON AWESOMESAUCE
|
||||||
|
|
||||||
|
def reversing_words_setence_py(str1):
|
||||||
|
''' reverse the words in a sentence'''
|
||||||
|
words = str1.split()
|
||||||
|
rev_set = " ".join(reversed(words))
|
||||||
|
return rev_set
|
||||||
|
|
||||||
|
def reversing_words_setence_py2(str1):
|
||||||
|
"""
|
||||||
|
Reverse the order of the words in a sentence.
|
||||||
|
:param string: the string which words wilL be reversed.
|
||||||
|
:return: the reversed string.
|
||||||
|
"""
|
||||||
|
words = str1.split(' ')
|
||||||
|
words.reverse()
|
||||||
|
return ' '.join(words)
|
||||||
|
|
||||||
|
|
||||||
|
# EXAMPLE 4, VERY SILLY, USING BRUTE FORCE
|
||||||
|
#
|
||||||
|
def reverse_words_brute(string):
|
||||||
|
"""
|
||||||
|
Reverse the order of the words in a sentence.
|
||||||
|
:param string: the string which words wil lbe reversed.
|
||||||
|
:return: the reversed string.
|
||||||
|
"""
|
||||||
|
word, sentence = [], []
|
||||||
|
for character in string:
|
||||||
|
if character != ' ':
|
||||||
|
word.append(character)
|
||||||
|
else:
|
||||||
|
# So we do not keep multiple whitespaces. An empty list evaluates to False.
|
||||||
|
if word:
|
||||||
|
sentence.append(''.join(word))
|
||||||
|
word = []
|
||||||
|
# So we do not keep multiple whitespaces. An empty list evaluates to False.
|
||||||
|
if word != '':
|
||||||
|
sentence.append(''.join(word))
|
||||||
|
sentence.reverse()
|
||||||
|
return ' '.join(sentence)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# TESTS
|
||||||
|
|
||||||
|
def test_reversing_words_sentence():
|
||||||
|
str1 = "Buffy is a Vampire Slayer"
|
||||||
|
assert(reversing_words_setence_py(str1) == "Slayer Vampire a is Buffy")
|
||||||
|
assert(reversing_words_setence_py2(str1) == "Slayer Vampire a is Buffy")
|
||||||
|
assert(reverse_words_brute(str1) == "Slayer Vampire a is Buffy")
|
||||||
|
assert(reversing_words_setence_logic(list(str1)) == "Slayer Vampire a is Buffy")
|
||||||
|
|
||||||
|
print("Tests passed!")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test_reversing_words_sentence()
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user