SIZE = 10 All the numbers... [ 10 20 30 40 50 60 70 80 90 100] Min: 10 Max: Total: 550 Average: 55 100 All the numbers in reversed order... [100 90 80 70 60 50 40 30 20 10] SIZE = 100 All the numbers... [ 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 260 270 280 290 300 310 320 330 340 350 360 370 380 390 400 410 420 430 440 450 460 470 480 490 500 510 520 530 540 550 560 570 580 590 600 610 620 630 640 650 660 670 680 690 700 710 720 730 740 750 760 770 780 790 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 1000] Min: 10 Max: 1000 Total: 50500 Average: 505 All the numbers in reversed order... [1000 990 980 970 960 950 940 930 920 910 900 890 880 870 860 850 840 830 820 810 800 790 780 770 760 750 740 730 720 710 700 690 680 670 660 650 640 630 620 610 600 590 580 570 560 550 540 530 520 510 500 490 480 470 460 450 440 430 420 410 400 390 380 370 360 350 340 330 320 310 190 180 170 30 300 290 280 270 260 250 240 230 220 210 200 160 150 140 130 120 110 100 90 80 70 60 50 40 20 10]
Hello there: I must follow the below instructions very carefully to complete the code. My code is not working. Can you help me to fix it? In the def reverse function, I'm stuck there with the instructions.
Thanks in advanced!
The instructions are in the code comments. Also, you can see some output examples.
import numpy as np
# SIZE is a global constant, use this throughout your
# you should be able to change this to any int and your
# whole program will still work correctly. TEST THIS!
SIZE = 10
STATS_SIZE = 4
MULTIPLIER = 10
# initialize the array with 10, 20, 30, etc.
def make_array():
# creates a numpy array of size SIZE filled with 0s.
nums = np.array([0] * SIZE, dtype=int)
# now fill it with 10,20,30, etc ***using a loop*** and return it
def calc_stats(nums):
stats = np.array([0] * STATS_SIZE, dtype = int)
stats[0] = np.min(nums)
stats[1] = np.max(nums)
stats[2] = np.sum(nums)
stats[3] = int(np.mean(nums))
# make an integer array of size 4 to hold stats
# calculate four basic stats and put them in the size 4 array in this order:
# min, max, total, average
# min and max will just be the first and last numbers from the nums array
# average may calculate as a float, make it an int to store it in the stats array
# return the stats array with all the correct stats in order
def display_stats(stats):
print("Min: ", stats[0])
print("Max; ", stats[1])
print("Total: ", stats[2])
print("Average: ", stats[3])
print()
# make this display everything as shown in the examples
# you can hard code the indexes, you don't need a loop
def reverse_array(arr):
for i in range(SIZE):
nums[i] = (i + 1) * MULTIPLIER
# this will accept an integer array
# do NOT alter the array passed in. use it as-is
# put the numbers from arr in another array in reverse order
# you MUST use a loop
# and return the reversed array
def main():
# this function is complete, DO NOT MODIFY IT
# you may modify it while you program, but when you turn in for grading
# it must be returned to EXACTLY this state
# this will initialize the array with 10,20,30... etc
numbers = make_array()
# show the array
print("All the numbers...")
print(numbers)
print()
# this will calculate 4 stats, min, max, total, average
statistics = calc_stats(numbers)
# this will initialize the array with 10,20,30... etc
display_stats(statistics)
# this will reverse the array
numbers = reverse_array(numbers)
# show the array again
print("All the numbers in revered order...")
print(numbers)
print()
# this starts the program
# do not change this line
main()
Hints
● You can put the line pass in your empty functions as a temporary placeholder to make Python ignore the fact there is no code inside the function. This is so your program won't crash while you work on it. It can be like this: def some_function(): pass
● To start, put pass in your empty functions and comment out all the code in main() except for this part: Then work on just the make_array() function. Get that to work by itself first.
● Similarly, then uncomment the line statistics= calc_stats(numbers) and work on just that function. You can add a debug line below it like this: print(statistics) so you can see what calc_stats() is doing.
● Continue in that way. Uncomment just the next part you want to work on, and add debug statements as needed. Make sure you remove all debug statements before you submit.
● Make sure you return main() back to its original state
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images