Hi I have 2 questions 1- Is there a way to search this site for answers to previous questions? 2 - I don't understand the following in Problem set 6 from fall 2008, Now, given a hand, here's how to use that dict to find a word that can be made from that hand: To find some word that can be made out of the letters in HAND: For each subset S of the letters of HAND: Let w = (string containing the letters of S in sorted order) If w in d: return d[w] Are we being asked to test every possible subset of letters from the HAND? What is the best way to generate a full set of these subsets?
Exercise 12.4 in thinking in python looks similar - maybe thats my way forward
The generally accepted method for searching this site for past questions is with google. In the search box you will type "keywords I'm looking for site:openstudy.com" to limit the results to OpenStudy.
"Are we being asked to test every possible subset of letters from the HAND? " - yes, combinations if using the 'sorted dictionary' and permutations if not. "What is the best way to generate a full set of these subsets?" itertools Combinatoric generators: http://docs.python.org/2.7/library/itertools.html
Thanks for your answers. I wanted to think about how the professors envisaged we would answer the questions, so I wanted to use methods covered in the lectures and readings. I feel I learned the most from the exercises and it has certainly exposed some misundersandings on my part. I couldn't figure out a neat way to do this. The best I could come up with was to use binary sequences, but would the professors expect the students to do this given that its 6 lectures into an introductory course. anyway here is my solution import math #Test Hand hand = ['a','b','c','d','e','f','g'] hand_size = 7 #the number of possible subsets is 2^^hand_size -1 #perhaps small benefit in counting backwards from 1111111 to 0000001 #don't need to consider 0000000 for count in range(pow(2,hand_size)-1,1,-1): #convert each count to a binary string bin_index = bin(count) # format the string remove the leading '0b' # pad with 0 so each string is hand_size chars # convert string to list bin_list = list(bin_index[2:].zfill(hand_size)) # create a tuple, esch element is a character from the binary list and the hand e.g (1, 'a') t=zip(bin_list, hand) subset = [] # initialise / empty the subset for index, letter in t: #if 1 in binary sequence add character to the subset if index == '1': subset.append(letter) print count, subset
please use a code pasting site: - http://dpaste.com - http://pastebin.com - https://gist.github.com/ - http://pastie.org - http://codepad.org - http://ideone.com - http://www.repl.it/ paste your code there and post the link here. select Python syntax highlighting when u paste.
does yours make combinations or permutations? here is one i made (combinations) - http://dpaste.com/1315241/ search for powerset for other examples
Join our real-time social learning platform and learn together with your friends!