Question

In: Computer Science

Focus on: Basic list operations, methods, use of functions, and good programming style. Program should be...

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.

Solutions

Expert Solution

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).


Related Solutions

This is an exercise to design and write a Python program in good programming style for...
This is an exercise to design and write a Python program in good programming style for a simulation of stock price over a period of 100 days. In this exercise, you are asked to simulate the stock price starting at $100.00 for 100 days with a daily fluctuation based on the Normal Distribution with mean = 0.0 & sigma = 0.0125. The program will show the daily stock price, the 7-day minimum, the 7-day maximum, the 7-day average, and the...
USE BASIC PYTHON Focus on Basic file operations, exception handling. Save a copy of the file...
USE BASIC PYTHON Focus on Basic file operations, exception handling. Save a copy of the file module6data.txt (Below) Write a program that will a. Open the file module6data.txt b. Create a second file named processed. txt c. Read the numbers from the first file one at a time. For each number write into second file, if possible, its I. Square ii. Square root iii. Reciprocal (1/number)use a math module function for the square root. use exception handling to trap possible...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program that displays the following menu: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a rectangle 3. Calculate the area of a triangle 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for the radius of the circle and then display the area. Use the following formula to calculate the circle’s area: ?...
programming in python Design a program that initializes a list of 5 items to zero (use...
programming in python Design a program that initializes a list of 5 items to zero (use repetition operator). It then updates that list with a series of 5 random numbers. The program should find and display the following data: -The lowest number in the list - Average of the numbers stored in the list
Please use C language and use link list to do this program. This program should ask...
Please use C language and use link list to do this program. This program should ask user to enter two fraction polynomials. Then user chocie if he want add it or multiple it. I need you please to test it to see if it work with these two fraction polynomials 1-  Left Poly Pointer: 1/1x2 + 3/4x + 5/12 2-Right Poly Pointer: 1/1x4 – 3/7x2 + 4/9x + 2/11 AND AFTER ADDING 3- Resulting Poly Pointer: 1/1x4 + 4/7x2 + 43/36x...
Why should service firms focus their efforts? Describe the basic focus options, and illustrate them with...
Why should service firms focus their efforts? Describe the basic focus options, and illustrate them with examples.
List and briefly explain the three basic appraisal methods
List and briefly explain the three basic appraisal methods
Create a message encoder/decoder. PLEASE USE BASIC PYTHON METHODS/FUNCTIONS. The user enters a message that could...
Create a message encoder/decoder. PLEASE USE BASIC PYTHON METHODS/FUNCTIONS. The user enters a message that could only include alphabetic letters and space. There are 26 alphabetic letters. Consider space the 27th letter. The user then enters a shift code that should be an integer between -26 and 26. The application will show the encoded/decoded message based on the shift code entered. If you encode a message, each letter in the message will be moved forward through the alphabet according to...
This program will focus on utilizing functions, parameters, Python's math and random modules, and if-statements! To...
This program will focus on utilizing functions, parameters, Python's math and random modules, and if-statements! To Start: You should fork the provided REPL, run the program and observe the output, and then take a look through the code. You may not 100% understand the code, but taking a quick look through what's there will be helpful in the future. You are going to systematically replace the TODO comments with code so that the program will work as intended. TODO 0...
HASKELL PROGRAMMING Use the functions concatenate AND concatList to write the functions below: 1) WRITE the...
HASKELL PROGRAMMING Use the functions concatenate AND concatList to write the functions below: 1) WRITE the function: concatStringsUsingDelimiter (can use fold instruction and a lambda expression to define) Sample run = *Main> concatStringsUsingDelimiter '#' ["The", "quick" , "boy" , "ran."] "The#quick#boy#ran.#" 2) WRITE the function: unconcatStringWithDelimiter (inverse of function above. can use haskell's let construct and/or where construct) Sample run = *Main> unconcatStringWithDelimiter '#' "The#quick#boy#ran.#" ["The" , "quick" , "boy" , "ran."] SIDE NOTE: Please write comments describing each function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT