B.2 [24 Points] The Towers of Providence is a variation of the classical Towers
ID: 3603311 • Letter: B
Question
B.2 [24 Points] The Towers of Providence is a variation of the classical Towers of Hanoi problem. There are four pegs, denoted A, B, C, and D, and n disks of different sizes. Originally, all the disks are on peg A, stacked in decreasing size from bottom to top. Our goal is to transfer all the disks to peg D, and the rules are that we can only move one disk at a time, and no disk can be moved onto a smaller one. Write a recursive algorithm in pseudo code that solves the Towers of Providence problem Hint: use the operator '&' to concatenate elements and lists, the functions first (L) which returns the first element of a list L, rest(L) which return the list L without its first element, and the predicate empty(L) which is true if L does not contain an element.Explanation / Answer
import numpy as np
import scipy.linalg as linalg
import matplotlib.pyplot as plt
import csv #needed to read csv file
import least_square as LS #will use the least square functions there for linear regression
#----------------------------------------------
def plot_GLOTI(datafile="GLB.Ts+dSST.csv"):
#
# read from the datafile, skip any comment lines that start with #
#
with open(datafile) as csvf:
LOTIdata = csv.DictReader(filter(lambda row: row[0]!='#', csvf))
#
# you are asked to get the data for the following 4 months
#
Jan=[]; Apr=[]; Jul=[]; Aug=[]
# you also need the Year for the xlabel
Year=[]
#
#study the online manual for the csv.DictReader() function, then finish the following part
#to save appropriate data in the above empty lists, remember you readin only str, but you
#need float datatype for the plots
#
for line in LOTIdata:
#
# plot the temperature deviation data of the four months you obtained from the csv file
#
#
## do linear regression on the Jan and Aug data you obtained, using the
## least_square_poly() function from the imported LS module
## (two sample lines are provided below, you need to modify accordingly)
#
xv = np.arange(Year[0], Year[-1]+0.5, 0.5) #extrapolate over the Years
#JulLS, Julstd = LS.least_square_poly(Year, Jul, 1, xv) #change this line to fit the Jan and Aug data
#
# plot the linear regressions for the Jan and Aug data
#
#
# set legends, title, labels properly
#
plt.savefig("plot_GLOTI.png")
plt.show()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.