mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-06-18 03:49:22 -04:00
some new examples
This commit is contained in:
parent
5ed530430c
commit
b50dc5d69d
84 changed files with 130 additions and 82 deletions
book
src
builtin_structures
neat_builtin_examples
dicts
Counter_example.pyOrderedDict_example.py__init__.pycount_unique_words_.pydefaultdict_example.pydelete_duplicate_char_str.pyfind_anagram_hash_function.pyfind_dice_probabilities.pyfind_top_N_recurring_words.pyruntime_dicts_with_timeit_module.pysetdeault_example.pyveirfy_two_strings_are_anagrams.py
lists_and_strings
__init__.pycomb_str.pyconv_str2int.pycount_unique_words.pyfind_0_MxN_replace_cols_rows.pyfind_all_permutations_string.pyfind_closest_num_seq.pyfind_duplicate_num_array.pyfind_edit_distance.pyfind_first_non_repetead_char.pyfind_if_is_substr.pyfind_if_only_unique_chars.pyfind_long_con_inc_subseq.pyfind_majority_in_seq.pyfind_max_profit.pyfind_max_subarray.pyfind_palindrome_rec.pyfind_product_without_division.pyfind_subst_in_str.pyfind_two_missing_numbers_in_sequence.pygreatest_sum_sub_array.pylongest_common_substring.pymerge_two_sorted_arrays.pyperm_str.pyprint_all_seq_with_cont_num.pyremove_specified_char_from_str.pyremoving_duplicates_seq.pyreverse_str.pyreverse_string_inplace_rec.pyreverse_words_sentence.pyrotate_NxN.pyruntime_lists_with_timeit_module.pysimple_str_comprension.pysum_two_numbers_sequence.pyverify_if_perm.py
numbers
__init__.pyconvert_dec_to_any_base_rec.pyconvert_from_decimal.pyconvert_from_decimal_larger_bases.pyconvert_to_decimal.pyfind_fibonacci_seq.pyfinding_gcd.pyfinding_if_prime.pygenerate_prime.pysearch_entry_matrix.pytesting_floats.pytesting_numpy.pytesting_numpy_speed.pytesting_random.py
sets
__init__.py
bit_operations
bit_array.pyclear_bits.pyfind_bit_len.pyfind_how_many_1_binary.pyget_bit.pyget_float_rep_bin.pyinsert_small_bin_into_big_bin.pynext_with_same_num_1s.pynum_bits_to_convert_2_nums.pyset_bit.pyswap_odd_even.pyupdate_bit.py
removing_duplicates_seq_.pyset_operations_dict.pyset_operations_with_dict.pyset_operations_with_lists.pytuples
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
|
||||
# mari.wahl9@gmail.com
|
||||
author = "Mari Wahl"
|
||||
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.
|
||||
from timeit import timeit
|
|
@ -1,6 +1,8 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/usr/bin/env python
|
||||
|
||||
author = "Mari Wahl"
|
||||
email = "marina.w4hl@gmail.com"
|
||||
|
||||
|
||||
|
||||
def reverse_string_inplace_rec(s):
|
|
@ -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…
Add table
Add a link
Reference in a new issue