Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- #Accomodate for the number of rows to compute
- rowstocomp = input('How many rows shall we compute?: ')
- #check if input is numeric, if not, end program.
- try:
- # if rowstocomp.isnumeric(): -- python3
- rowstocomp = int(rowstocomp)
- except ValueError:
- print("Invalid input! ")
- sys.exit()
- print('_________________________________________')
- print("|x |y\t|" + u"\u0394" + "y\t|" + u"\u0394" + u"\u00B2" + "y\t|" + u"\u0394" + u"\u00B3" + "y\t|")
- print('-----------------------------------------')
- #x
- if rowstocomp < 7:
- target_range = range (0,7)
- else:
- target_range = range(0,rowstocomp)
- #y
- y = [x**3 - 145 * (x**2) + 5375 * x - 29375 for x in target_range]
- if rowstocomp > 7:
- # ycopy = y.copy() -- python3
- ycopy = y[:]
- #dy - 1st derivation
- deltay = []
- for x in range (0,7):
- if x is 6:
- dy = "<>"
- else:
- dy = y[x+1] - y[x]
- deltay.append(dy)
- #dy2 - 2nd derivation
- deltacubedy = []
- for x in range (0,7):
- if x in {5,6}:
- dcy = "<>"
- else:
- dcy = deltay[x+1] - deltay[x]
- deltacubedy.append(dcy)
- #dy3 - 3rd derivation
- deltasquaredy = []
- for x in range (0,7):
- if x in {4,5,6}:
- dsy = "<>"
- else:
- dsy = deltacubedy[x+1] - deltacubedy[x]
- deltasquaredy.append(dsy)
- #map
- results = dict()
- for x in range (0,7):
- results[x] = [str(y[x]),str(deltay[x]),str(deltacubedy[x]),str(deltasquaredy[x])]
- #rowstocomp > 7?
- grthn7 = False
- iter = rowstocomp
- if rowstocomp > 7:
- grthn7 = True
- iter = 7
- #print results
- for key in range (0, iter):
- y = results[key][0]
- deltay = results[key][1]
- deltacubedy = results[key][2]
- deltasquaredy = results[key][3]
- print ("|" + str(key) + " |" + y + " \t|" + deltay + " \t|" + deltacubedy + " \t|" + deltasquaredy + "\t|")
- #handle rows more than 7
- else:
- if grthn7:
- y = ycopy
- for x in range (7, rowstocomp):
- if x in {7,8,9}:
- print ("|" + str(x) + " |" + str(y[x]) + " \t|" + "<>" + " \t|" + "<>" + " \t|" + "<>" + "\t|")
- elif x is 100:
- print ("|" + str(x) + " |" + str(y[x]) + " \t|" + "<>" + " \t|" + "<>" + " \t|" + "<>" + "\t|")
- else:
- print ("|" + str(x) + " |" + str(y[x]) + " \t|" + "<>" + " \t|" + "<>" + " \t|" + "<>" + "\t|")
- #TODO - As a second test, figure out the first row of a table for the equation: y = x^4 and compute the values of y for x in the range [0,20]
- print('_________________________________________')
- print('\nSECOND TEST: y = x^4 in range [0,20]')
- print('_________________________________________')
- print("|x |y\t|\t\t\t|")
- print('-----------------------------------------')
- x = list(range(0, 21))
- y = list()
- for xc in x:
- if xc < 10:
- print("|" + str(xc) + " |" + str(xc**4) + "\t|")
- else:
- print("|" + str(xc) + " |" + str(xc**4) + "\t|")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement