.split() Exercises

Instructions:

Your task is to write a python program to find the 5 answers below. You should upload your .py file and also put your answers to 1 through 5 in the comments-to-teacher box.

Note: I am not providing you test cases. However, for your own benefit in writing and debugging your code, and making sure it works correctly, I strongly recommend you write your own tests.

Hints

  1. The data we're using today has multiple lines, and each line has multiple pieces of information on it. You'll need to use .split() multiple times to get it into a list of lists
  2. S="""app,box,cat
    dog,ear,fit"""
    LofS = S.split("\n")      # [ "app,box,cat" , "dog,ear,fit" ]
    LofL = []
    for string in LofS:
        sublist = string.split(",")  # e.g.:  ["app" , "box", "cat"]
        LofL += [ sublist ]  
        # NOT LofL += sublist! Sublists should stay separate, not merged
    

  3. Test your code on a short string (e.g. only the first few students). You don't need all 100 or so names right off the bat, switch to the full data once you've got the basics debugged.

  4. Remember: when sorting a list of lists, the sort order is determined by the first element of each list; To sort by a specific quantity, copy it to the front of each sublist before sorting, then remove it after you're done
    # We want to sort by the number in each sublist, e.g. 1, 4, 9
    L = [  ["A", 4], ["X", 1], ["K", 9]  ]
    
    for i in range(len(L)):  #Copy number to front of each sublist
        num = L[i][1] 
        L[i] = [ num ] + L[i]   
    # L is now  [  [4,"A",4], [1,"X",1], [9,"K",9]  ]
    
    L.sort() # Sort by first element
    
    for i in range(len(L)):  #Drop first element of each sublist
        L[i] = L[i][1:] 
    
    # Now L is sorted in order of the numbers:  [  ["X",1], ["A",4], ["K",9]  ]
    


Assignment

You have a list of data consisting of last name, first name, and test score.
1: How many names are in the list?
2: How long is the longest first name? What is it?
3: How long is the longest last name?
4: What is the mean of the test scores?
5: what are the top 3 test scores, and which students got them?

Challenge: Can you find the second-longest last name, without sorting the list, 
and in a single pass through the list (i.e. loop over it exactly once)


Copy and paste the following lines directly into thonny:
arr="""FOMIN,SAQIF,6
PAPERNO,XINNI,12
WILLIAMS,SKAI,14
GINDIS,MOZEN,11
MORSE,ANDREA,17
LOW,ARIEL,12
ZAMAN,ALEXANDRIA,12
HUMAIDEE,AFIA,19
SHAN,NATALIA,5
SHAHID,EDWARD,5
TEDESCO,CARINA,14
HLINKA,ZAMIN,9
STACY,JIA,9
SHIN,JIAYI,11
ZHAN,MALIKA,10
LETON,HEPZIBAH,14
KUCHINAD,VIANA,12
LEUSSING,REGINA,8
CHEN,EBTESHAM,17
AUDHY,ELAINE,10
WIELAARD,ANI,8
MARUF,VIOLETA,15
CUNNINGHAM,TANISHA,6
TAMEEM,AQIB,17
BISTA,ANGELA,4
KHALIQUE,RAMEZ,12
LOU,CYRUS,17
PERAZZO,KRIPAMOYE,3
FRID,JOSEPH,17
HAQUE,ANNE,9
FANTOZZI,ALVI,7
LANDA,ISABELLE,8
VALLEJO,VICKY,13
XHETANI,ZAINAB,8
LAKUSTA,MANVEER,12
FIROOZAN,OLIVIER,9
KARIM,SOPHIA,6
SCHWARZ,ESTEAK,10
KROP-SIEGMUND,TAASEEN,12
MUI,YE-HEE,16
SHAPIRO,MIM,13
GUTKOVICH,JAY,11
CHIU,HUMAIRA,17
BAHUTSKI,KAIDEN,11
FUKUOKA,RIFAH,10
CASTLE,SHAFKATH,3
VAYSBURD,KAZI,10
ANDREWS,MICHA,10
CAOTHIEN,DREW,10
RABINOVICH,DANIEL,8
OR,LUCINDA,16
KABWA,WEIBIN,6
MEDINA,SAKURA,17
ANANT,TERREN,13
RUZZIER,GITAE,13
SY,IQRA,11
PULAWSKA,ROXY,13
REN,YUVAL,12
BACCHUS,OLUWATOBI,9
JEA,FLORENCE,11
BAE,IRENE,13
HUGHES,LEON,16
COWAN,INARA,10
SHEN,KEN,16
ILYANOK,CHENHUI,8
COLLINS,SELENA,10
MIKHALEVSKY,MADELYN,12
MAI,NAFISA,4
GRAEBER,SAYAN,20
BOIS,ALIND,8
TSAHALIS,MANOLEE,6
GALAI,QINA,13
ROHAN,ALFAYED,7
KRONMAN,MORRIS,9
KYI,GRETA,11
TAWHID,INFINITY,4
KRANICH,WAI,4
KOLENOVIC,DORIN,3
LEUNG,XINQING,18
BELL,SAARAH,14
TYLO,MISA,16
TASNEEM,DAVALYNN,7
AZIZ,MELODY,16
SOIEFER,KANJUDA,17
FUENTES,MENDY,2
SAI,CALISTA,10
BACON,ANIKAH,17
ALLEN,KHUJISTA,16
JIA,KEIRA,5
LEVINE,TAMARA,10
EISEN,PULINDU,10
ASHIKIN,FATIHA,3
LAMANY,AIDEN,6
SCHWARTZ,ATHENA,6
RUSSO,FARIHAH,17
KATARI,SUBYETA,16
ZOARDAR,MISHEL,17
HAMEED,YVETTE,15
HARVEY,CALVIN,10
SELECTOR,DANTE,6"""