Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. import sys
  2.  
  3. #Accomodate for the number of rows to compute
  4. rowstocomp = input('How many rows shall we compute?: ')
  5.  
  6. #check if input is numeric, if not, end program.
  7. try:
  8. # if rowstocomp.isnumeric(): -- python3
  9. rowstocomp = int(rowstocomp)
  10. except ValueError:
  11. print("Invalid input! ")
  12. sys.exit()
  13.  
  14. print('_________________________________________')
  15. print("|x |y\t|" + u"\u0394" + "y\t|" + u"\u0394" + u"\u00B2" + "y\t|" + u"\u0394" + u"\u00B3" + "y\t|")
  16. print('-----------------------------------------')
  17.  
  18. #x
  19. if rowstocomp < 7:
  20. target_range = range (0,7)
  21. else:
  22. target_range = range(0,rowstocomp)
  23.  
  24. #y
  25. y = [x**3 - 145 * (x**2) + 5375 * x - 29375 for x in target_range]
  26. if rowstocomp > 7:
  27. # ycopy = y.copy() -- python3
  28. ycopy = y[:]
  29.  
  30. #dy - 1st derivation
  31. deltay = []
  32. for x in range (0,7):
  33. if x is 6:
  34. dy = "<>"
  35. else:
  36. dy = y[x+1] - y[x]
  37. deltay.append(dy)
  38.  
  39. #dy2 - 2nd derivation
  40. deltacubedy = []
  41. for x in range (0,7):
  42. if x in {5,6}:
  43. dcy = "<>"
  44. else:
  45. dcy = deltay[x+1] - deltay[x]
  46. deltacubedy.append(dcy)
  47.  
  48. #dy3 - 3rd derivation
  49. deltasquaredy = []
  50. for x in range (0,7):
  51. if x in {4,5,6}:
  52. dsy = "<>"
  53. else:
  54. dsy = deltacubedy[x+1] - deltacubedy[x]
  55. deltasquaredy.append(dsy)
  56.  
  57. #map
  58. results = dict()
  59. for x in range (0,7):
  60. results[x] = [str(y[x]),str(deltay[x]),str(deltacubedy[x]),str(deltasquaredy[x])]
  61.  
  62. #rowstocomp > 7?
  63. grthn7 = False
  64. iter = rowstocomp
  65. if rowstocomp > 7:
  66. grthn7 = True
  67. iter = 7
  68.  
  69. #print results
  70. for key in range (0, iter):
  71. y = results[key][0]
  72. deltay = results[key][1]
  73. deltacubedy = results[key][2]
  74. deltasquaredy = results[key][3]
  75. print ("|" + str(key) + " |" + y + " \t|" + deltay + " \t|" + deltacubedy + " \t|" + deltasquaredy + "\t|")
  76. #handle rows more than 7
  77. else:
  78. if grthn7:
  79. y = ycopy
  80. for x in range (7, rowstocomp):
  81. if x in {7,8,9}:
  82. print ("|" + str(x) + " |" + str(y[x]) + " \t|" + "<>" + " \t|" + "<>" + " \t|" + "<>" + "\t|")
  83. elif x is 100:
  84. print ("|" + str(x) + " |" + str(y[x]) + " \t|" + "<>" + " \t|" + "<>" + " \t|" + "<>" + "\t|")
  85. else:
  86. print ("|" + str(x) + " |" + str(y[x]) + " \t|" + "<>" + " \t|" + "<>" + " \t|" + "<>" + "\t|")
  87.  
  88. #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]
  89. print('_________________________________________')
  90. print('\nSECOND TEST: y = x^4 in range [0,20]')
  91. print('_________________________________________')
  92. print("|x |y\t|\t\t\t|")
  93. print('-----------------------------------------')
  94.  
  95. x = list(range(0, 21))
  96. y = list()
  97.  
  98. for xc in x:
  99. if xc < 10:
  100. print("|" + str(xc) + " |" + str(xc**4) + "\t|")
  101. else:
  102. print("|" + str(xc) + " |" + str(xc**4) + "\t|")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement