In: Computer Science
Suppose you have ridden a bicycle from New York City to Key West, Florida. Your bicycle odometer shows the total miles you have travelled thus far, which you make a note of each day with paper and pencil. Your first two entries might be ‘55’ and ‘120’, indicating that you rode your bike 55 miles on day 1 and 65 miles on day 2. Your task is to create a NumPy array wherein you can record the cumulative miles you recorded each day during your trip “by hand”. Then use your Python skills to show the total miles that you rode each day. Assume the trip took a total of 35 days and included at least 4 non-consecutive days where no cycling was possible due to either weather conditions or personal fatigue/soreness. For the remaining 31 days, you would have covered the entire distance between NYC and Key West. Complete your work within your Jupyter Notebook for this assignment and be sure to include a writeup explaining your approach to this problem, including the ways in which you decided to make use of NumPy.
First the cumulative distance is recorded into the program by asking the user to enter the details. Numpy's 1D array is used to store the cumulative values when the user enters it. These values are then convereted into everyday distance by subtracting each element with its previous element. Then the array is traversed and the daily distance is printed. Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code. Below is the output of the program: Below is the code to copy: #CODE STARTS HERE----------------
import numpy as np # # Following numpy array contains 34 days values(for testing) # miles = np.array([55,120,180,180,265,320,350,456,566,566 # ,623,698,742,780,802,845,899,964,1022,1100, # 1120,1180,1180,1206,1287,1331,1371,1405,1455,1500, # 1546,1587,1600,1611]) miles = np.array([]) for x in range(34): inp = int(input("Enter reading for day "+str(x+1)+": ")) miles = np.append(miles,inp) #Convert cumulative array to non cumulative numpy array miles[1:] -= miles[:-1].copy() for mile in range(len(miles)): #loop through the array print("Day "+str(mile+1)+": ",miles[mile],"miles") #Display miles for everyday #CODE ENDS HERE------------------