I have the following (correct) solution to Project Euler problem 24. I'm relatively new to Python, and am stumped on a couple of Python points.
First, the code:
# A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. # If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. # The lexicographic permutations of 0, 1 and 2 are: 012 021 102 120 201 210 # What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? permutations =  def getLexicographicPermutationsOf(digits, state): if len(digits) == 0: permutations.append(str(state)) for i in range(len(digits)): state.append(digits[i]) rest = digits[:i] + digits[i+1:] getLexicographicPermutationsOf(rest, state) state.pop() digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] getLexicographicPermutationsOf(digits, ) print(permutations)
My first query is regarding the use of the yield statement. Instead of defining the permutations list at the top, my first design was to replace the
permutations.append line with
yield state. I would then assign the return value of the method to a variable. I checked, and the return value was a generator, as expected. However, looping over its contents indicated that no values were being generated. Am I missing something here?
My second query is about the final line - printing a value from the list. When I run this, it outputs the values as though it was a list, whereas it should be a string. In fact, replacing
print(type(permutations)) results in
< class str>. So why is it being printed like a list (with square brackets, separated by commas)?