From 5c41dd06dd52f6e065b05e90733560ca3194cfc1 Mon Sep 17 00:00:00 2001 From: Bernardo Sulzbach Date: Mon, 7 Jul 2014 13:48:18 -0300 Subject: [PATCH 1/9] To Python 3 And some error handling. --- src/further_examples/useful/version_info.py | 27 +++++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/further_examples/useful/version_info.py b/src/further_examples/useful/version_info.py index 3050fa1..de6f925 100644 --- a/src/further_examples/useful/version_info.py +++ b/src/further_examples/useful/version_info.py @@ -1,9 +1,20 @@ -import sys -print "System version:",sys.version -import numpy as np -print "Numpy version:",np.version.version -import matplotlib -print "Matplotlib version:",matplotlib.__version__ -import pandas as pd -print "Pandas version:",pd.__version__ +import sys +print("System version:", sys.version, '\n') +try: + import numpy + print("\nNumpy version:", numpy.version.version) +except ImportError as e: + print(e) + +try: + import matplotlib + print("\nMatplotlib version:", matplotlib.__version__) +except ImportError as e: + print(e) + +try: + import pandas + print("\nPandas version:", pd.__version__) +except ImportError as e: + print(e) From 4a32dac35d8c913d543194a377d1e0fc4a67bc50 Mon Sep 17 00:00:00 2001 From: Bernardo Sulzbach Date: Mon, 7 Jul 2014 13:52:56 -0300 Subject: [PATCH 2/9] Fix on last commit --- src/further_examples/useful/version_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/further_examples/useful/version_info.py b/src/further_examples/useful/version_info.py index de6f925..7d8e175 100644 --- a/src/further_examples/useful/version_info.py +++ b/src/further_examples/useful/version_info.py @@ -15,6 +15,6 @@ except ImportError as e: try: import pandas - print("\nPandas version:", pd.__version__) + print("\nPandas version:", pandas.__version__) except ImportError as e: print(e) From 7b79f5816a0bcd85103610ff8572c2b8d56557b2 Mon Sep 17 00:00:00 2001 From: Bernardo Sulzbach Date: Mon, 7 Jul 2014 16:21:33 -0300 Subject: [PATCH 3/9] Faster, better, stronger --- .../arrays_and_strings/reverse_str.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/further_examples/arrays_and_strings/reverse_str.py b/src/further_examples/arrays_and_strings/reverse_str.py index 442877f..e00425f 100644 --- a/src/further_examples/arrays_and_strings/reverse_str.py +++ b/src/further_examples/arrays_and_strings/reverse_str.py @@ -1,25 +1,17 @@ #!/usr/bin/python3 # mari von steinkirch @2013 # steinkirch at gmail - - -def reverse_str(s): - ''' in place ''' - sr_ls = [] - for i in range(len(s)-1, -1, -1): - sr_ls.append(s[i]) - return ''.join(sr_ls) - +reverse_string = lambda s: s[::-1] def main(): s1 = 'abcdefg' s2 = 'buffy' s3 = '' - print(reverse_str(s1)) - print(reverse_str(s2)) - print(reverse_str(s3)) + print(reverse_string(s1)) + print(reverse_string(s2)) + print(reverse_string(s3)) if __name__ == '__main__': main() From ac7a68da17f300d7589b30dbf14ee2335a30b1d6 Mon Sep 17 00:00:00 2001 From: Bernardo Sulzbach Date: Mon, 7 Jul 2014 19:55:00 -0300 Subject: [PATCH 4/9] Now I am happy --- .../arrays_and_strings/reverse_str.py | 73 +++++++++++++++---- 1 file changed, 59 insertions(+), 14 deletions(-) diff --git a/src/further_examples/arrays_and_strings/reverse_str.py b/src/further_examples/arrays_and_strings/reverse_str.py index e00425f..4d7543f 100644 --- a/src/further_examples/arrays_and_strings/reverse_str.py +++ b/src/further_examples/arrays_and_strings/reverse_str.py @@ -1,18 +1,63 @@ #!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail - -reverse_string = lambda s: s[::-1] + +# Mari von Steinkirch @ 2013 +# mari.wahl9@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. +from timeit import timeit -def main(): - s1 = 'abcdefg' - s2 = 'buffy' - s3 = '' - print(reverse_string(s1)) - print(reverse_string(s2)) - print(reverse_string(s3)) - -if __name__ == '__main__': - main() +def reverse_1(string): + """ + Slowest function. Use a list and str.join to reverse the string. + :param string: the string to be reversed. + :return: a reversed string. + """ + reversed_string = [] + # Iterates from the last to the first character. + for i in range(len(string) - 1, -1, -1): + # Appends the character to the list. + reversed_string.append(string[i]) + return ''.join(reversed_string) + +def reverse_2(string): + """ + Same principle as reverse_1. One-liner cousin. + :param string: the string to be reversed. + :return: a reversed string. + """ + return ''.join([character for character in [string[i] for i in range(len(string) - 1, -1, -1)]]) + + +def reverse_3(string): + """ + Another one-liner. We make a list from the characters of the string and reverse it. + :param string: the string to be reversed. + :return: a reversed string. + """ + return ''.join([character for character in string][::-1]) + +# Overkill of elegance. A bit too passionate but it defines this lambda function well. +# Simply returns the string backwards. As fast as concise. +reverse_lambda = lambda s: s[::-1] + +# Import a lowercase ASCII alphabet from the string module. +from string import ascii_lowercase +# We define some short strings to test our functions. +strings = (ascii_lowercase, 'buffy', 'foo', 'bar', 'SPAM') +# We announce what we are doing. +print(', '.join(strings), ' should appear reversed four times if all the functions are working.\n') +print('{:<30}:'.format('Function name'), 'benchmarking result (lower is better):') +# Iterate over a tuple of functions. +for function in (reverse_1, reverse_2, reverse_3, reverse_lambda): + name = function.__name__ if function.__name__ != "" else 'reverse_lambda' + # We print the function's name and its benchmark result. + print("{:<30}:".format(name), timeit(name + "('string')", setup='from __main__ import ' + name)) + # We print the output so that we can check if the function is working as expected. + print(', '.join(map(function, strings)), '\n') \ No newline at end of file From a071c64242c01e6cfd212960796556945bd225d5 Mon Sep 17 00:00:00 2001 From: Bernardo Sulzbach Date: Mon, 7 Jul 2014 19:56:54 -0300 Subject: [PATCH 5/9] Another commit --- .../arrays_and_strings/reverse_str.py | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/further_examples/arrays_and_strings/reverse_str.py b/src/further_examples/arrays_and_strings/reverse_str.py index 4d7543f..83c70d0 100644 --- a/src/further_examples/arrays_and_strings/reverse_str.py +++ b/src/further_examples/arrays_and_strings/reverse_str.py @@ -1,4 +1,31 @@ #!/usr/bin/python3 +<<<<<<< HEAD +======= +# mari von steinkirch @2013 +# steinkirch at gmail + + +def reverse_str(s): + ''' in place ''' + sr_ls = [] + for i in range(len(s)-1, -1, -1): + sr_ls.append(s[i]) + return ''.join(sr_ls) + + + + +def main(): + s1 = 'abcdefg' + s2 = 'buffy' + s3 = '' + print(reverse_str(s1)) + print(reverse_str(s2)) + print(reverse_str(s3)) + +if __name__ == '__main__': + main() +>>>>>>> parent of 7b79f58... Faster, better, stronger # Mari von Steinkirch @ 2013 # mari.wahl9@gmail.com @@ -60,4 +87,4 @@ for function in (reverse_1, reverse_2, reverse_3, reverse_lambda): # We print the function's name and its benchmark result. print("{:<30}:".format(name), timeit(name + "('string')", setup='from __main__ import ' + name)) # We print the output so that we can check if the function is working as expected. - print(', '.join(map(function, strings)), '\n') \ No newline at end of file + print(', '.join(map(function, strings)), '\n') From e3bc4378aa08a7d755f91cd3c25d8a606ce93e58 Mon Sep 17 00:00:00 2001 From: Bernardo Sulzbach Date: Mon, 7 Jul 2014 20:00:13 -0300 Subject: [PATCH 6/9] Commits are free anyways --- .../arrays_and_strings/reverse_str.py | 38 +++---------------- 1 file changed, 5 insertions(+), 33 deletions(-) diff --git a/src/further_examples/arrays_and_strings/reverse_str.py b/src/further_examples/arrays_and_strings/reverse_str.py index 83c70d0..14f4a22 100644 --- a/src/further_examples/arrays_and_strings/reverse_str.py +++ b/src/further_examples/arrays_and_strings/reverse_str.py @@ -1,32 +1,3 @@ -#!/usr/bin/python3 -<<<<<<< HEAD -======= -# mari von steinkirch @2013 -# steinkirch at gmail - - -def reverse_str(s): - ''' in place ''' - sr_ls = [] - for i in range(len(s)-1, -1, -1): - sr_ls.append(s[i]) - return ''.join(sr_ls) - - - - -def main(): - s1 = 'abcdefg' - s2 = 'buffy' - s3 = '' - print(reverse_str(s1)) - print(reverse_str(s2)) - print(reverse_str(s3)) - -if __name__ == '__main__': - main() ->>>>>>> parent of 7b79f58... Faster, better, stronger - # Mari von Steinkirch @ 2013 # mari.wahl9@gmail.com @@ -74,12 +45,10 @@ def reverse_3(string): # Simply returns the string backwards. As fast as concise. reverse_lambda = lambda s: s[::-1] -# Import a lowercase ASCII alphabet from the string module. -from string import ascii_lowercase # We define some short strings to test our functions. -strings = (ascii_lowercase, 'buffy', 'foo', 'bar', 'SPAM') +strings = ('buffy', 'foo', 'bar') # We announce what we are doing. -print(', '.join(strings), ' should appear reversed four times if all the functions are working.\n') +print(', '.join(strings), ' should appear reversed four times if the function is working.\n') print('{:<30}:'.format('Function name'), 'benchmarking result (lower is better):') # Iterate over a tuple of functions. for function in (reverse_1, reverse_2, reverse_3, reverse_lambda): @@ -88,3 +57,6 @@ for function in (reverse_1, reverse_2, reverse_3, reverse_lambda): print("{:<30}:".format(name), timeit(name + "('string')", setup='from __main__ import ' + name)) # We print the output so that we can check if the function is working as expected. print(', '.join(map(function, strings)), '\n') +# So that the console stays open and waits the user to kill it. +from os import system +system('pause') From f8236d49cbce99ebe08df4370e2019fb4bae3090 Mon Sep 17 00:00:00 2001 From: Bernardo Sulzbach Date: Mon, 7 Jul 2014 20:04:46 -0300 Subject: [PATCH 7/9] Finished --- src/further_examples/arrays_and_strings/reverse_str.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/further_examples/arrays_and_strings/reverse_str.py b/src/further_examples/arrays_and_strings/reverse_str.py index 14f4a22..e058305 100644 --- a/src/further_examples/arrays_and_strings/reverse_str.py +++ b/src/further_examples/arrays_and_strings/reverse_str.py @@ -48,7 +48,7 @@ reverse_lambda = lambda s: s[::-1] # We define some short strings to test our functions. strings = ('buffy', 'foo', 'bar') # We announce what we are doing. -print(', '.join(strings), ' should appear reversed four times if the function is working.\n') +print(', '.join(strings), ' should appear reversed if the function is working.\n') print('{:<30}:'.format('Function name'), 'benchmarking result (lower is better):') # Iterate over a tuple of functions. for function in (reverse_1, reverse_2, reverse_3, reverse_lambda): @@ -57,6 +57,5 @@ for function in (reverse_1, reverse_2, reverse_3, reverse_lambda): print("{:<30}:".format(name), timeit(name + "('string')", setup='from __main__ import ' + name)) # We print the output so that we can check if the function is working as expected. print(', '.join(map(function, strings)), '\n') -# So that the console stays open and waits the user to kill it. -from os import system -system('pause') +# We wait until the user wants to quit. +input('Press Return to quit.') From 2835a0d27ee50064f5262e8e708488d2422f7a04 Mon Sep 17 00:00:00 2001 From: Bernardo Sulzbach Date: Mon, 7 Jul 2014 20:06:42 -0300 Subject: [PATCH 8/9] Ignore my IDE --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index ea5bb31..e5095d8 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,6 @@ nosetests.xml .mr.developer.cfg .project .pydevproject + +# PyCharm +.idea \ No newline at end of file From a870dd74a62f9f9c569f71d97344813e470ab8c3 Mon Sep 17 00:00:00 2001 From: Bernardo Sulzbach Date: Tue, 8 Jul 2014 15:15:02 -0300 Subject: [PATCH 9/9] More changes --- .../sorting/bubble_sort.py | 16 +++++-- .../arrays_and_strings/reverse_str.py | 4 +- .../arrays_and_strings/reverse_words.py | 45 +++++++++++++++++++ .../reverse_words_setence.py | 44 ------------------ src/further_examples/useful/version_info.py | 2 +- 5 files changed, 61 insertions(+), 50 deletions(-) create mode 100644 src/further_examples/arrays_and_strings/reverse_words.py delete mode 100644 src/further_examples/arrays_and_strings/reverse_words_setence.py diff --git a/src/examples_in_my_book/sorting/bubble_sort.py b/src/examples_in_my_book/sorting/bubble_sort.py index c48fb7b..cf95285 100644 --- a/src/examples_in_my_book/sorting/bubble_sort.py +++ b/src/examples_in_my_book/sorting/bubble_sort.py @@ -1,10 +1,19 @@ #!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail + +# Mari von Steinkirch @ 2013 +# mari.wahl9@gmail.com + +# Bernardo Sulzbach (mafagafo) @ 2014 +# 1449441@gmail.com def bubble_sort(seq): - ''' implement bubble sort, a O(n^2) not efficient algorithm''' + """ + Implementation of bubble sort. + O(n²) and thus highly ineffective. + :param seq: the sequence to be sorted. + :return: the sorted sequence. + """ size = len(seq) -1 for num in range(size, 0, -1): for i in range(num): @@ -13,7 +22,6 @@ def bubble_sort(seq): seq[i] = seq[i+1] seq[i+1] = temp return seq - def test_bubble_sort(module_name='this module'): diff --git a/src/further_examples/arrays_and_strings/reverse_str.py b/src/further_examples/arrays_and_strings/reverse_str.py index e058305..e14ad79 100644 --- a/src/further_examples/arrays_and_strings/reverse_str.py +++ b/src/further_examples/arrays_and_strings/reverse_str.py @@ -1,4 +1,6 @@ -# Mari von Steinkirch @ 2013 +#!/usr/bin/python3 + +# Mari von Steinkirch @ 2013 # mari.wahl9@gmail.com # Bernardo Sulzbach (mafagafo) @ 2014 diff --git a/src/further_examples/arrays_and_strings/reverse_words.py b/src/further_examples/arrays_and_strings/reverse_words.py new file mode 100644 index 0000000..0055cec --- /dev/null +++ b/src/further_examples/arrays_and_strings/reverse_words.py @@ -0,0 +1,45 @@ +#!/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)) diff --git a/src/further_examples/arrays_and_strings/reverse_words_setence.py b/src/further_examples/arrays_and_strings/reverse_words_setence.py deleted file mode 100644 index 110eb33..0000000 --- a/src/further_examples/arrays_and_strings/reverse_words_setence.py +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail - -''' reverse words in a setence, keeping the words right - >>> str1 = 'I love Google and Python!' - >>> reverse_words(str1) - 'Python! and Google love I' - >>> reverse_words('bla') - 'bla' - >>> reverse_words('') - '' - >>> reverse_words_brute(str1) - 'Python! and Google love I' - >>> reverse_words_brute('bla') - 'bla' - >>> reverse_words_brute('') - '' -''' - -def reverse_words(str1): - l1 = str1.split(' ') - l1.reverse() - return ' '.join(l1) - -def reverse_words_brute(str1): - aux_lt = [] - aux_str = '' - for i, c in enumerate(str1): - if c != ' ': - aux_str += c # WE COULD HAVE USED LT HERE, MORE EFFICIENT - elif c == ' ': - aux_lt.append(aux_str) # WE COULD HAVE USED STR BUT NOT EFFICIENT! - aux_str = '' - aux_lt.append(aux_str) # REMEMBER THAT THE LAST ONE DOEN'T HAVE SPACE! - aux_lt.reverse() - return ' '.join(aux_lt) - - - -if __name__ == '__main__': - import doctest - doctest.testmod() - diff --git a/src/further_examples/useful/version_info.py b/src/further_examples/useful/version_info.py index 7d8e175..504a204 100644 --- a/src/further_examples/useful/version_info.py +++ b/src/further_examples/useful/version_info.py @@ -3,7 +3,7 @@ print("System version:", sys.version, '\n') try: import numpy - print("\nNumpy version:", numpy.version.version) + print("\nNumpy version:", numpy.__version__) except ImportError as e: print(e)