a. Implement a function create_permutation (n) that creates and returns a list c
ID: 3713011 • Letter: A
Question
a. Implement a function create_permutation (n) that creates and returns a list containing a random permutation of the numbers: 0, 1, 2, ..., (n-1) For example, one call to create_permutation (6) could return the list: [3, 2, 5, 4, 0, 1, another call to create permutation (6) could return the list: [2, 0, For more detail on permutation, check this link: https://en wikipedia.org/wiki/Permutation b. Implement a function scramble_word (word) that is given a word (a string), and returns a scrambled version of word, that is new string containing a random reordering of the letters of word. For example, one call to scramble_word('pokemon) could return okonmpe ', another call to scramble_word('pokemon) could return 'mpeoonk Implementation requirement: To determine the new order of the letters, use the function create_permutation (n). For example, for the word 'pokemon ',the scrambled word implied by the permutation [1, 4, 5, 2, 3, 0, 6] is 'omokepn (the first letter is the letter from index 1, the second letter is the letter from index 4, the third letter is the letter from index 5, and so on). c. You are given a text file containing a bank of words (one word in each line). Write a main) function that randomly chooses a word from the file, scrambles it, prints the scrambled word to the user, and allows them to find the unscrambled word 3 times Have your program interact with the user as demonstrated below: Unscramble the word: om okepn Try #1: openkom Wrong! Try #2: pokemon Yay you got it! Notes: 1. Your main should use the function/s you implemented in the previous sections. When printing the letters of the scrambled word, add a space between each two letters. 2.Explanation / Answer
#python code for the same. Create file called "input_file" and insert words in it to run the program.
import itertools
import random
def create_permutation(n):
tmp = [i for i in range(n)]
tp = itertools.permutations(tmp)
x = []
for i in tp:
x.append(i)
ran = x[random.randint(1,len(x))-1]
return ran
if __name__=="__main__":
f = file("input_file","r") #reading from a file
words = f.read().split()
rand = random.randint(1,len(words))
random_word = words[rand-1] #selecting random word
gen = create_permutation(len(random_word))
random_permutation = ""
for i in xrange(len(random_word)):
random_permutation += random_word[gen[i]]
print "Unscramble the word:",
for i in random_permutation:
print i,
Tries = 0
while(Tries!=3):
print "Try #"+str(Tries+1)+":",
s = raw_input()
if(s==random_word):
print "Yay you got it!"
break
else:
print "Wrong!"
Tries+=1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.