Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ''' exercise - scramble words randomly from file then find matches '''
- def Stuart():
- from random import shuffle
- from random import choice
- from random import randint
- def generate_scrambled_words(quantity=10):
- ''' generate list of scrambled words from randomly selected words from file '''
- def scramble_words(word_list, quantity):
- ''' return list of <quantity> scrambled words randomly chosen from <wordlist> '''
- def scramble(word):
- ''' returns string with character order randomly shuffled'''
- scrambled = list(word)
- shuffle(scrambled)
- return ''.join(scrambled)
- scrambled_words = []
- for _ in range(quantity + 1):
- while True:
- word = choice(word_list)
- scrambled_word = scramble(word)
- if scrambled_word not in scrambled_words:
- break
- scrambled_words.append(scrambled_word)
- return scrambled_words
- def read_words_from_file():
- ''' read file of words '''
- with open('wordlist.txt', 'r') as f:
- # sorted string versions of words (keys), with original words (values)
- word_list = [word.strip() for word in f]
- return word_list
- word_list = read_words_from_file()
- scrambled_words = scramble_words(word_list, quantity)
- return scrambled_words
- def solve_scrambled_words(scrambled_words):
- ''' return list of unscrambled words for supplied scrambled words '''
- def read_words_from_file():
- ''' read file of words '''
- with open('wordlist.txt', 'r') as f:
- # sorted string versions of words (keys), with original words (values)
- word_dict = {}
- for word in f:
- sorted_word = ''.join(sorted(word.strip()))
- if not word_dict.get(sorted_word):
- word_dict[sorted_word] = word.strip()
- return word_dict
- word_dict = read_words_from_file()
- unscrambled_words = []
- for scrambled in scrambled_words:
- sorted_scrambled = ''.join(sorted(scrambled))
- found = word_dict.get(sorted_scrambled, '** not found **')
- unscrambled_words.append(found)
- return unscrambled_words
- scrambled_words = generate_scrambled_words(randint(5, 10))
- unscrambled_words = solve_scrambled_words(scrambled_words)
- print(f"\nScrambled words are: {', '.join(scrambled_words)}.\n")
- for scrambled, unscrambled in zip(scrambled_words, unscrambled_words):
- print(f'{scrambled} unscrambles to {unscrambled}')
- print()
- return scrambled_words, unscrambled_words
- ''' test framework for unscrambling challenge
- Copy this, using the raw view (or download), to an editor of your choice
- and insert your function(s) at the top of the code.
- Wrap your main code including the two required functions into a function named
- after you that returns the scambled and unscrambles word lists
- and add the function to the list of functions called in the last line below.
- Edit the last line, the call to the test function test_funcs() and place
- the name of each function, without its trailing (), in the call to test_funcs,
- with commas between each function name if testing more than one function.
- '''
- def test_funcs(*funcs):
- tests = [('Testing using external file', 'Simple comparison')]
- import traceback
- for func in funcs:
- print(f'\n\nTesting function: {func.__name__}\n')
- print('-----------')
- for test, assertion in tests:
- print(f'Testing: {test}')
- try:
- scrambled_words, unscrambled_words = func()
- except ValueError as errmsg:
- print(errmsg)
- break
- except Exception as e:
- print(f'ERROR: {traceback.format_exc()}')
- break
- try:
- if not (isinstance(scrambled_words, list) and isinstance(unscrambled_words, list)):
- raise TypeError('ERROR - failed to return two lists')
- if not (len(scrambled_words) > 0 and len(scrambled_words) == len(unscrambled_words)):
- raise ValueError('ERROR - lists are not of equal length with at least one entry')
- except (TypeError, ValueError) as errmsg:
- print(errmsg)
- break
- #testing
- scrambled_words.append('TESTING FAILURE')
- unscrambled_words.append('CONFIRMED')
- for scrambled, unscrambled in zip(scrambled_words, unscrambled_words):
- try:
- if not isinstance(scrambled, str):
- raise TypeError('Scrambled list contains non string entry')
- if not isinstance(unscrambled, str):
- raise TypeError('Unscrambled list contains non string entry')
- assert sorted(scrambled) == sorted(unscrambled)
- except AssertionError:
- print(f'ERROR: {scrambled} does not unscramble to {unscrambled}')
- except TypeError as errmsg:
- print(errmsg)
- if __name__ == "__main__":
- test_funcs(Stuart)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement