From a870dd74a62f9f9c569f71d97344813e470ab8c3 Mon Sep 17 00:00:00 2001 From: Bernardo Sulzbach Date: Tue, 8 Jul 2014 15:15:02 -0300 Subject: [PATCH] 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)