

32 hole peg solitaire fastest solution code#
Your program should include some test code that draws three flowers, all of them facing up, on three different positions of the screen: (a) a simple flower of size 200 (b) a fractal flower of depth 3 and size 250 and (c) a fractal flower of depth 4 and size 300. For example, a fractal flower with a maximum recursion level of 4 will result in a picture like this one:

Your function will paint a recursive fractal flower with the same basic shape outlined above, but with each petal recursively replaced by a scaled down version of the flower itself. Next, write the function fractalFlower that takes an integer representing the size of the flower, and an integer representing the maximum level (depth) of recursion, and again whatever else you need to do turtle graphics. When your function terminates, the turtle should be back to the same position and the same orientation it started. The origin of the flower is the bottom part of the stem, and its length develops straight ahead on the current direction of the turtle. The position and orientation of the flower will depend on the initial position and orientation of the turtle. Your function will draw the following figure: This still requires a non-recursive wrapper function (like above) that drops all the permutations that are not of length k, but since we never produce any permutations larger than length k, this is much, much faster than the code above.įirst, write the function simpleFlower that takes an integer number representing the size of the flower (and whatever else you need to do turtle graphics - see Try it, if you want to wait a very, very long time!īig Hint: one way to solve this is to modify the permutations code from class to take a second parameter, k, and to return all the permutations of a that are no larger than length k.

This huge amount of extra work makes it intractable to use this code to compute permutations(range(30),2), let alone permutations(range(50),3). However, the code above will generate all 362,880 permutations of length 9 to find these 72 permutations. The answer contains 72 permutations of two numbers each. Why is this unacceptable? Well, say we are finding permutations(range(9), 2). # returns a list of all permutations of the list aįor subPermutation in allPermutations(a):ĪllPerms += + ] + subPermutation] # renamed permutations code from class notes Perm = fullLengthPerm # not ok: essentially "removes" (n-k) elements # is already in our list (which is also not ok). # or add an expensive O(n) check inside our loop to check if a permutation # so either have to remove all the duplicates at the end (which is also not ok), # and then, assuming len(a)>=k, uses only first k elements of each permutation, # inefficient version that uses permutations code from class, unmodified,
