In: Computer Science
Focus on: Basic list operations, methods, use of functions, and good programming style. Program should be in basic python.
Part 1. Write a program that does the following:
1. Create a list of length N where N is a randomly selected integer
between 10 and 20 and
whose elements are randomly selected integers between 0 and
19.
2. Print out the list.
3. Create a copy of the list. Sort the copy into decreasing order
and print it.
4. Report the distinct elements in the list
Before continuing with the next two parts, add a function
user_input(lo, hi) to your
program. It should ask the user for an integer between lo and hi
and return the value. Use
appropriate error handling and input validation.
Part 2.
Write a function named how_many(a_list, a_num) that that expects a
list of numbers
and a number as arguments. It returns how many times the number is
found in the list.
If the number is not in the list, return 0. For example
how_many( [12, 15, 16, 4, 10, 3, 5, 7, 9, 15], 15 )
returns 2
And how_many( [12, 15, 16, 4, 10, 3, 5, 7, 9, 15], 11 )
returns 0
Ask the user for an integer between 0 and 19. If the number is in
the list, report how
many times it occurs.
Part 3.
Write a function named find_bigger(a_list, a_num) that expects as
arguments a list of
numbers and a number. It should return a new list consisting of all
the numbers in the list
that are greater than the number. For example
find_bigger( [12, 15, 16, 4, 10, 3, 5, 7, 9, 15], 11 )
returns [12, 15, 16, 15]
And find_bigger( [12, 15, 16, 4, 10, 3, 5, 7, 9, 15], 15 )
returns [16]
Ask the user for an integer between 0 and 19 and then report how
many of the items in the
list are greater than that number.
Below are the codes with comments for the above three Parts with output screens
Part1-
Code with comments-
#using laibrary called random
import random
#generatin a random integer
myrandom=random.randint(10,20)
#creating empty list
myrandomlist=[]
#generating random elements
for i in range(0,myrandom):
myrandomlist.append(random.randint(0,19))
print("Random list is")
print(myrandomlist)
print()
#copying the list
myrandomlistcopy=myrandomlist
myrandomlistcopy.sort(reverse=True)
print("Sort of list in decreasing order is")
#soring the list in descending order
print(myrandomlistcopy)
print()
#generating distinctelements
distinctelements=list(set(myrandomlistcopy))
print("The distinct elements in the list are")
#printing distinctelements
print(distinctelements)
print()
#our user_input function
def user_input(lo,hi):
#asking user to input between lo and hi
print("Please enter a value between ",lo,"and",hi)
inputt=int(input())
#checking if number in range
if(inputt<hi and inputt>lo):
num=random.randint(lo,hi)
#checking if guessed number is right or not
if(inputt==num):
z="Yes your guess is right"
else:
z="Oops your guess is wrong,the correct number is "+str(num)
#if number not in range program will exit
else:
print("Please enter the number in given range next time")
exit()
#returning the value z
return z
#calling the function with parameters lo and hi
print(user_input(10,20))
Output screens with code-
Please note the spelling mistake in comment 1 i.r library not laibrary for importing the library "random"
Part2-
Note-You can remove multiline comments from line 9 to 19 if you want to test with your desired values and pass the values to the function
Code with comments-
#function how_many
def how_many(a_list,a_num):
#checking if numbe ris in list if present return the count of the element
if(a_num in list(set(a_list))):
return a_list.count(a_num)
#else return 0
else:
return 0
"""
print("Please enter How many elements do you want to enter")
c=int(input())
mylist=[]
for i in range(c):
num=int(input())
mylist.append(num)
print("Please enter Which element do you want to check")
e=int(input())
print()
"""
#printing the result of the function how_many by passing a list and a number
print(how_many( [12, 15, 16, 4, 10, 3, 5, 7, 9, 15], 15 ))
print()
#asking the user to enter a number
print("Please enter a element between 0 and 19")
aa=int(input())
if(aa<19 and aa>0):
print(how_many([12, 15, 16, 4, 10, 3, 5, 7, 9, 15],aa))
else:
print("Please enter in between given range nexttime")
Output screens with code-
Part3-
Note-You can remove multiline comments from line 13 to 21 if you want to test with your desired values and pass the values to the function
Code with comments-
#function find_bigger
def find_bigger(a_list,a_num):
#creating a empty list
nlist=[]
#iterating through the list
for m in a_list:
#if element in list is greater than number append to new list
if(m>a_num):
nlist.append(m)
#return the list
return nlist
"""print("Please enter How many elements do you want to enter")
c=int(input())
mylist=[]
for i in range(c):
num=int(input())
mylist.append(num)
print("Please enter Which element do you want to check")
e=int(input())
print()"""
#calling our function find_bigger
print(find_bigger( [12, 15, 16, 4, 10, 3, 5, 7, 9, 15], 11 ))
print()
#asking user to enter a element
print("Please enter a element between 0 and 19")
aa=int(input())
#checking if they are in range
if(aa<19 and aa>0):
#passing our function find_bigger and finding the length of the result list
z=find_bigger([12, 15, 16, 4, 10, 3, 5, 7, 9, 15],aa)
#printing length of the result list
print(len(z))
#program exits if user does not enter number in correct range
else:
print("Please enter in between given range nexttime")
Output screens with code-
(Hope you like the work if you have any doubt Please comment,I will definately help you).