Starting from:

$30

EECS1015-Lab 6 Lists, Dictionaries, and Tuples Solved

TASK 1 – Printing and sorting a ragged list
A "ragged list" is a list of lists where the length of the lists nested inside the main list is not of the same size.  

Variable rList is a ragged list that has already been defined for you (see above). 

For Task 1, you need to implement and call the following two (2) functions: 

1)  printRaggedList(param: list) - no return 

Loop through each item in the ragged list (which is a list) and print out each list as follows: 

 

Row 0: [item1, item2, item3, …, itemN] Row 1: [item1, item2, …, itemN] 

In Python, you can print a list after a formatted string as follows:   print("format string".format(arg1, arg2), list) 

2)  sortRaggedList(param: list) - no return (but mutates list) 

This function will sort each list in the ragged list.   The function should be passed the variable rList.  

 

TASK 1 ACTION:    

(1) Print the ragged list by passing rList as a parameter to printRaggedList(). 

 

(2) Sort the ragged list by passing rList as a parameter to sortRaggedList(). 

 

(3) Print the ragged list again after sorting using printRaggedList().  

               -We should see that the contents of the lists are sorted. 

 

                

TASK 2 – Print an encoded ASCII Art
This task processes the data in the variables encodedData1 and  encodedData2. 

These variables are bound to a "list of lists of tuples".  Specifically, each item in the list is another list, that list stores several tuples. 

The tuples have two values, the first is a number, the second is a single character. 

The tuple is encoding a "run" of characters.  That is the number tells you how many times you could repeat the character. 

The idea is that you should "decode" the list of tuples to construct a string that you can print out.  Each list represents  a single line of "Test Art" (or what we call ASCII Art). 

 

See example here: 

[ [(5, '*')],         
 
# row 0 
decodes to: *****  A run of 5 '*' 
 [(2, ' '), (1, '*')], 
 
# row 1 
decodes to:   *   A run of 2 ' ', 1 '*' 
 [(2, ' '), (1, '*')], 
 
# row 2 
decodes to:   *   A run of 2 ' ', 1 '*' 
 [(1, ' '), (3, '*')]] 
 
# row 3 
decodes to:  ***  A run of 1 ' ', and 3 '*' 
 

To perform this task, define two functions: 

1)  decodeTupleList(param: list of tuples) - string 

This will take a list of tuples in the form [(number, character), (number, character), …].  

You should "decode" the list and its tuples to build a single string. 

For example: 

decodeTupleList( [ (5, '.'), (3, '-'), (5,'.') ] ) returns ".....---....." 

 

 "."+"."+"."+"."+"."  + "-"+"-"+"-"  + "."+"."+"."+"."+"." - ".....---....."    5 "." chars           3 "-" chars      5 "." chars            final string 

 

2)  printEncodedAsciiImage(param: list) - no return 

This function will print the ASCII art encoded in lists bound to variables encodedData1 and encodedData2. 

When you pass the variable to this function, you should loop through the list.  Recall that each item in the list is another list of tuples.  Call decodeTupleList(item) to decode the string.  decoupleTupleList() returns a string.  Print the returned string. This will print the Ascii Art encoded one line at a time.  When you are done, you should have a nice picture. 

In the lab, I will only show you the result of encodedData1 , you have to implement the program to see encodedData2. 

Task 2 ACTIONS 
(1) call printEncodedAsciiImage(encodedData1)  

-An example of the output of this is shown in the video and in the output below. 

(2) call printEncodedAsciiImage(encodedData2)  

- You need output this too, but it is not shown (you have to implement the task to see it!) 

 

 

 

        

TASK 3 – Element string to a dictionary
This task processes the data in the string variable stringData. 

stringData is a long string that encodes the information on the first 25 elements from the periodic table.   

To perform this task, define two functions: 

1)  buildElementDictionary(param: string) - dictionary 

This function processes the stringData  to build a dictionary.  The string has the following form. 

"1 H Hydrogen,2 He Helium,3 Li Lithium,4 Be Beryllium,5 B Boron,6 C Carbon,…" 

Split the string to get a list of each element as follows: 

["1 H Hydrogen", "2 He Helium", "3 Li Lithium, …] 

Now, for each string in this list, split it to get 

"1", "H", "Hydrogen" 

Add this information to your dictionary as follows: 

key='H', value = ['Hydrogen' ,'1']  i.e. {'H', ['Hydrogen', '1']} key='He', value = ['Helium', '2']   i.e. {'He', 'Helium', '2']} 

 

Process each element and return the final dictionary with all 25 elements.   

2)  printElements(param: dictionary) - no return 

This function takes the dictionary created by buildElementDictionary() as a parameter.   Print out the dictionary as follows: 

H [Hydrogen] #1           'H' is the key to the dictionary.  Hydrogen and '1' are the 1st and 2nd items in the list paired with the key. 

He [Helium] #2                             

Li [Lithium] #3 

Be [Beryllium] #4 

Task 3 ACTIONS
(1) Call buildElementDictionary(stringData) 

-  This will generated the dictionary from the stringData that stores the elements. 

(2) Print the dictionary using print(). 

-  Print the dictionary out so its contents and verify that it is OK. 

(3) Call printElements() by passing your dictionary.  

-  This will print out the contents as described above. 

Finally, put all your tasks in the main() function. 

main(parameters: none) - no return 

Your main function will be used to test the functionality above.  The skeleton code for your main() is: 

def main(): 

  print("Task 1 - Sorting and printing a ragged list ")          print("Task 2 - Decoding Ascii Art ")           print("Task 3 - Elements String to Dictionary ")  

See the next page for an example output of Lab 6. 

Task 1 - Sorting and printing a ragged list  

--List before sorting-- 

 

Row 3: [2, 9, 44, 88, 100] 

Row 4: [2, 4, 9, 19] 

 

Task 2 - Decodng Ascii Art 

 

 

Task 3 - Elements String to Dictionary 

{'H': ['Hydrogen', '1'], 'He': ['Helium', '2'], 'Li': ['Lithium', '3'], 'Be': ['Beryllium', '4'], 'B': ['Boron', '5'], 'C': ['Carbon', '6'], 'N': ['Nitrogen', '7'], 'O': ['Oxygen', '8'], 'F': ['Fluorine', '9'], 'Ne': ['Neon', '10'], 'Na': 

['Sodium', '11'], 'Mg': ['Magnesium', '12'], 'Al': ['Aluminum', '13'], 'Si': ['Silicon', '14'], 'P': ['Phosphorus', '15'], 'S': ['Sulfur', '16'], 'Cl': ['Chlorine', '17'], 'Ar': ['Argon', '18'], 'K': ['Potassium', '19'], 'Ca': ['Calcium', '20'], 'Sc': ['Scandium', '21'], 'Ti': ['Titanium', '22'], 'V': ['Vanadium', '23'], 'Cr': ['Chromium', '24'], 'Mn': ['Manganese', 

'25']} 

 

Al [Aluminum] #13 

Si [Silicon] #14 

P [Phosphorus] #15 

S [Sulfur] #16 

Cl [Chlorine] #17 Ar [Argon] #18 

K [Potassium] #19 Ca [Calcium] #20 

Sc [Scandium] #21 

Ti [Titanium] #22 

V [Vanadium] #23 

Cr [Chromium] #24 

Mn [Manganese] #25 

More products