After working on the folding problem in lecture, I decided that the best way to show the solution was to build a function that would return the pattern of folds if the paper is folded x number of times. I noticed in class that the pattern of one fold is one down fold. For two folds it is up, down, down. After observing the patterns, I concluded that the next pattern of folds can be predicted from the previous fold. There is a down fold in the center, the previous fold pattern to the right of that, and to the left is the previous pattern but all down folds are up folds and vice versa.
The following function written in python lists all the fold patterns up to 'numfolds' number of folds.
def iextend(lst, z, x): #This extends a list by another list
lst[z:z] = x
def fold_pattern(numfolds):
result = ['d'] #Current fold pattern, starts out as a single down-fold
prevresult = [] #The result for the previous pattern
i = 0
while i < numfolds: #Do this loop for each number of times the paper is 'folded'
oppresult = [] #Same as previous result but up-folds and down-folds are switched
result = ['d'] #Set the middle of the current fold pattern to down-fold
result.extend(prevresult) #Place the previous fold pattern after the middle fold
for c in prevresult: #Create the opposite fold pattern
#Switching out d's for u's and vice versa
if c == 'd':
iextend(oppresult, len(oppresult), 'u')
elif c == 'u':
iextend(oppresult, len(oppresult), 'd')
if i > 0:
iextend(result, 0, oppresult) #Add opposite of previous fold pattern to beginning of the current one
prevresult = result
print(result) #Print current pattern
i = i + 1
return
No comments:
Post a Comment