Question
I would like somebody to check my python code and its output and help me make sure that the output is correct and displays the
I would like somebody to check my python code and its output and help me make sure that the output is correct and displays the following (if something is missing I would appreciate if somebody helps me fix the code).
Populate an array of size n = 20 with random integers in the range 10 ---- 50 inclusive (see Lab 06 and lab 11) but do NOT include any duplicates. The list of n integers should include only unique integers in the range 10 25
Strategy
Generate a random integer (1)
If it NOT in the list add to the list
Else return to (1) that is generate another random integer
USE the fill() function in the template below
Display the list using the display() function
Display the list in sorted order (largest to smallest) by calling a function sorted that you code Use the sorted() function
Display the minimum integer in the list using your function min() from
Display the maximum integer in the list using your function max() from
Display the number of integers that are evenly divisible by 3 (no remainder) using your function div3() . Integers in the array that are evenly divisible be 3 are { 12, 15, 18, 21, 24}
Please use x mod 3 as in
Select an integer from your list call it x. If x mod 3 == 0 then add 1 to a counter
Use the div3() in the template
Display the number of integers in the list that are multiples of 5 (10, 15, 20,25) by coding a function mult5()
Use the mult5() in the template
Ask the user for an integer and display YES if the integer appears in the list or NO if it is in the list by calling the yesNo() function as noted in the lab
CODE:
import random def display(a): print("the list == ", a) # DONE def sorted(a): # sort the array a from largest to smallest # Consider using the python sort function t = 0 for i in range(len(a)): for j in range(i + 1, len(a)): if a[i] < a[j]: t = a[i] a[i] = a[j] a[j] = t return a def fill(a, n): # fill the list a with n random integers in the range 10 --- 25 inclusive BUT NO duplicates THIS code NEEDS to be fixed ar = 10 br = 50 j = 0 while (len(a) < 20): x = random.randint(ar, br) # Return a random integer . if x not in a: a.append(x) return a def div3(a): v = 0; for x in a: if (x % 3 == 0): v = v + 1 return v def multi5(a): m = 0 for x in a: if (x % 5 == 0): m = m + 1 return m def yesNo(a): s = "YES" n = "NO" num = int(input("Enter a number:")) if num in a: return s else: return n def max(a): # return the largest value in the list mx = a[0] # place holder only for num in a: if (num > mx): mx = num return mx def min(a): # return the smallest value in the list mn = a[-1] for num in a: if (num < mn): mn = num return mn # ========================================== # NOW we call the functions a = [] n = 20 # array size array = fill(a, n) display(array) mx = max(array) mn = min(array) d = div3(array) m = multi5(array) print("Maximum is : ", mx, "Minimum is :", mn) print("div3 return is : ", d, " multi5 return value is :", m) str = yesNo(array) print(str) print('Sorted Array ', sorted(array))
---------------------------------------------------------------------------------------------------------------------------------------
OUTPUT
the list == [15, 40, 19, 22, 45, 27, 13, 14, 41, 12, 49, 25, 18, 30, 21, 23, 24, 20, 17, 26] Maximum is : 49 Minimum is : 12 div3 return is : 8 multi5 return value is : 6 Enter a number: 8 NO Sorted Array [49, 45, 41, 40, 30, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 15, 14, 13, 12]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started