Please help with this task..I come from a C background and don\'t fully understa
ID: 3848507 • Letter: P
Question
Please help with this task..I come from a C background and don't fully understand Python just yet
We are using Python 3
We must include at least these two functions: buildList() and filterList()
2.1 Task 1: Random Numbers This program uses Python's random number generator to generate a random list of numbers. It filters out certain numbers, and then prints both versions of the list. It will give you practice with building lists, filtering values from one list to another, and loops. You will also practice with writing simple functions. Name this file randList.py It must use inputs C) to read two integers. (Do not print out any prompt for the user; simply read them The first number will be the "seed" to the random number generator (https://en.wikipedia You are allowed to assume that the user will actually print type integers as input you do not have to do any error checking.Explanation / Answer
buildList function creates the list using random.randint() and filterList function filters the list using list comprehension like below
L=[x for x in L if x!=0 and (x-1)%3!=0]
Finally sum() builtin function of python is used for the sum and for average dividing the sum by length of list (using builtin function len)
code:
import random
def main():
m=input()
n=input()
if int(n)<0:
print("ERROR: List length is negative ")
sys.exit(1)
random.seed(int(m))
lst=buildList(n)
filteredList=filterList(lst)
for x in filteredList:
print(x)
print(lst)
if len(filteredList)!=0:
sumlst=sum(filteredList)
avg=sumlst/float(len(filteredList))
print("sum=",sumlst,"average=",avg)
else:
print("Cannot print out the sum and average, as the filtered list is empty")
def filterList(L):
L=[x for x in L if x!=0 and (x-1)%3!=0]
return L
def buildList(n):
lst=[]
for i in range(int(n)):
lst.append(random.randint(-25,75))
#print(lst)
return lst
if __name__=="__main__":
main()
input:
4
15
output:
printing filtered List:
5
-12
36
-6
26
45
12
72
-18
printing actual list:
[5, 13, -12, 67, 25, 36, -6, -14, -17, -23, 26, 45, 12, 72, -18]
sum= 160 average= 17.77777777777778