Asked on 11 Mar 2020

Given the following main program

#main
a = [5,2,9,6,3] doubleList(a) print a

Implement the function named doubleList that takes one list argument and modifies the list so that each element in the list is doubled. The statement print(a) in the main program will then have to print [10,4,18,12,6]

Given the following main program

#main
a = [5,2,9,6,3]
b = reversedList(a) print b
print a

Implement the function named reversedList that takes one list argument and returns a list that contains the elements in the list argument in reverse order. The statement print(b) in the main program will then have to print [3,6,9,2,5]. The statement print(a) must print the original list [5,2,9,6,3] unaffected. This means your function should not modify the list argument.

Given the following main program

#main
a = [5,2,9,6,3] reverseList(a) print a

Implement the function named reverseList that takes one list argument and reverses the list so that the modified list contains the same elements in reverse order. The statement print(a) in the main program will then have to print [3,6,9,2,5]

What is the return value of the functions doubleList, reversedList and reverseList in questions 1, 2, and 3 above?

Given the following main program

#main
a = [5,2,9,6,3]
b = evenElements(a) print b

Implement the function named evenElements that takes one list argument and returns a list that contains the elements in the list argument that are even integers. The statement print(b) in the main program will then have to print [2,6]. What is the return value of the function? Your function should not modify the list argument.

Given the following main program

#main
a = [5, 2, 9, 6, 3, 7, 19, 4, 8, 17, 21, 20, 15] removeOddElements(a)
print a

Implement the function named removeOddElements that takes one list argument and modifies the list so that all odd integers in the list are removed. The statement print(a) in the main program will then have to print [2,6,4,8,20]. What is the return value of the function?

Part A
#Main program A=[5,8,2,0,-5,3] n=-5
answer = isFound(A, n) if answer == True:

print "The integer is found in the list" else:

print "The integer is not found in the list."

Consider the main program shown above. Write a Python function named isFound that takes a list of integers and an integer as arguments and returns True if the integer is found in the list; otherwise it returns False.

Remark: You are NOT allowed to use the in operator or any other built in function when you write this function. Instead, you must write a loop to go through the elements of the list.

Part B

Give the following main program

#main
A = [5,2,9,6,3]
B = [2,4,3,9,7,1,6,5]
flag = isList1InList2(A, B) if flag:

print "The list", A, "is contained in the list", B else:

print "The list", A, "is not contained in the list", B

Implement the function named isList1InList2 that takes two list arguments and returns True if and only if each element of the first list argument are found in the second list argument; otherwise it returns False.

Remark: You are NOT allowed to use the in operator or any other built in function when you write this function. Instead, if you wish, you can use the function you wrote in Part A.

For the lists shown in the main program, your function must return True because each element of list A is found in list B.

A given List is known as Palindrome List if the reverse of the List contains the same elements in the same order of the original List. For example the List of integers [2, 5, 0, 3, 3, 0, 5, 2] is a Palindrome because the reverse of the List which is [2, 5, 0, 3, 3, 0, 5, 2] contains the same elements in the same order as the given List. However the List [1, 2, '2', 1] is not Palindrome because its reverse which is [1, '2', 2, 1] does not contain the same elements in the same order as the given List.

Write a Python function named isPalindrome that takes a List as argument and returns True if the argument is a Palindrome List; otherwise it returns False. You don't need to write the main program; just write the function. If you would like to test your function, then it is ok to write the main program too but you will not get any mark for the main program.

Given your function in question #7 above, what is the output of the following code: a=[]

b = [5,2,9,6,3]

print isList1InList2(a,b)

Given your function in question #7 above, what is the output of the following code:

a=[]
b=[]
print isList1InList2(a,b)

Given your function in question #7 above, what is the output of the following code: a = [5,2,9,6,3]

b=[]

print isList1InList2(a,b)

Given the following main program

#main
a=[]
n = readListElements(a)
print "The user entered", n, "elements to the list" print a

Implement the function named readListElements that takes one list argument and modifies the list by repeatedly asking the user to enter integer values and then appending them to the list. The process stops when the user enters -1000. The stopping value -1000 should not be appended to the list. Moreover your function must count the appended elements and return it. Example if the user enters the integers 31, -4, 9, 1,-1000 then your function must modify the list a to a = [31, -4, 9, 1] and return the number elements added which is 4.

Write a function named createRandomList that takes an integer argument n and returns a list with n elements. The elements must be random numbers in the range [0,100]

Given the following main program:

#main

n = int(input("How many elements do you want in your list? ")) a = createList(n) s1 = sumOfElementsAtEvenIndexes(a) print s1 s2 = sumOfElementsAtOddIndexes(a) print s2 s3 = sumOfAllElements1(a) print s3 s4 = sumOfEvenElements(a) print s4 s5 = sumOfOddElements(a) print s5 s6 = sumOfAllElements2(a) print s6 if (s3 != s6): print "Something is wrong in your program." else: print "Looks like you have done a good job."

Implement the functions as follows:

 createList: returns a list object with n elements. The elements are generated randomly so that each element is in the range [15,45]

sumOfElementsAtEvenIndexes: returns the sum of the elements at even indexes. Use a for loop or a while loop.

sumOfElementsAtOddIndexes: returns the sum of the elements at odd indexes. Use a for loop or a while loop.

sumOfAllElements_A: returns the sum of all the elements in the list using the functionssumOfElementsAtEvenIndexes and sumOfElementsAtOddIndexes. Here you are NOT allowed to use any loop; instead make function calls as described.

sumOfEvenElements: returns the sum of the even integer elements in the list. Use a for loop or a while loop.

sumOfOddElements: returns the sum of the odd integer elements in the list. Use a for loop or a while loop.

sumOfAllElementsB: returns the sum of all the elements in the list using the functions sumOfEvenElements and sumOfOddElements. Here you are NOT allowed to use any loop; instead make function calls as described.

17. Most data compression algorithms like winzip, gzip, lzip and etc are based on the frequency distribution of characters in a given string. Write a function named getFreqDistribution that takes a string argument and returns a LIST that has length 26 such that the first element of the list is the number of appearance of 'a' or 'A' in the string, the second element is number of appearance of 'b' or 'B', and so on so forth.

I'm studying python 2 and have a lot of troubles with solving these homework questions.

Answered on 11 Mar 2020

Unlock this answer

Get 1 free homework help answers
Access 3.7 million verified answers.
Get access
Already have an account? Log in