Practice 6 (Year 2 Sem 1)

Practice 6 is the sixth practice in semester 1 of year 2, and the sixth practice in the total of year 2. It is a practice for lists, and is considered to be of medium difficulty.

Description
No description was provided for this practice.

Unlocked after completion

 * Practice makes perfect! (Score at least 60% for Practice 6 [Chapter 9])
 * Intern of the Year (Score at least 60% for Practice 6 [Chapter 9])

Question
Write a function called  which will take in a list and return


 * the total of all values in the list,


 * and the average of all values in the list.

Note: Refer to extra reading on how to perform multiple return.

Solution
To return multiple values, simply type in return a, b to return a and b inside a single function.

Use the built in functions sum and len (calculates length of list) to help you.

Question
You will be given binary numbers stored in lists, with each element containing one digit. For example, the binary number 110101 will be given as such:. You are then supposed to convert the binary number to decimal (i.e. 110101 is 53 in decimal) and return the decimal value.

There will not be any negative numbers. Also, you should not hard-code this question; you will fail the very large test cases if you do.

Solution
Iterate over lst backwards. Then store the value of a number in a variable, multiplying that by 2 as you go. When you encounter a 1, add that variable to your total.

Question
Now we shall convert a sentence to pig latin.

The split function returns a list of the words in a string.

For example  returns

Write a function to convert a sentence to pig latin.

Rules for converting to pig latin:


 * For words that begin with a vowel (a, e, i, o, u), add "way"


 * Otherwise, move all letters before the first vowel to the end and add "ay"

For simplicity, no punctuation will be present in the inputs.

Solution
Split your sentence using the str.split function described above.

Now, refer back to Lab 8 Question 7 to convert each word into the list using your solution to Lab 8 Question 7.

Question
Write a function named  which will take in a list, and return 2 list which denotes the elements in the list (in sorted order), and the respective frequency of the elements.

For example, if the following code is executed: mylist = [0,0,1,2,1,2,0,4,3,1,0,2,3,4,5,9,1] [dataList, frequencyList] = frequencyCount(mylist) print(dataList) print(frequencyList)

Output will be:

[ [0, 1, 2, 3, 4, 5, 9], [4, 4, 3, 2, 2, 1, 1] ]

[updated on 4 April 2018], the correct template is

def frequencyCount(mylist): data = [] freq = [] #add your code below return [data, freq]

Solution
First, sort mylist and iterate over it to get a list of all elements in mylist.

Then remove duplicates from that list.

(Alternatively, use list(set(mylist)) to get the above.)

Set that list to data.

Then, for each element in that new list, use mylist.count(item) to determine the number of items, and append it onto freq.

NOTE: Using collections.Counter will not work due to private test cases.