Lists - Exercises Discussion


Question 1
Download a copy of the file www.py4e.com/code3/romeo.txt.
Write a program to open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split function.
For each word, check to see if the word is already in a list. If the word is not in the list, add it to the list. When the program completes, sort and print the resulting words in alphabetical order.
Enter file: romeo.txt
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief',
'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window','with', 'yonder']
  • Create an empty list, we'll call it romeo
  • read line by line
  • strip the newline character
  • split the line into a list of words using split()
  • Use extend() to add the list of words in each line to the list, romeo
  • sort() it
  • print
Remember : extend() and sort() modify a list

fhand = open('romeo.txt')

romeo=[]
for line in fhand:
    line=line.rstrip()
    words=line.split()
    romeo.extend(words)

romeo.sort()
print(romeo)
Question 2
Write a program to read through the mail box data and when you find line that starts with “From”, you will split the line into words using the split function. We are interested in who sent the message, which is the second word on the From line.
From stephen.marquard@uct.ac.za Sat Jan 5 
You will parse the From line and print out the second word for each From line, then you will also count the number of From (not From:) lines and print out a count at the end. This is a good sample output with a few lines removed:

fhand = open('mbox.txt')

count=0
for line in fhand:
    if line.startswith('From '):
        count+=1
        words=line.split()
        print(words[1])

print("There were %d lines in the file with From as the first word" % count)

Output:
....
jzaremba@unicon.net
dlhaines@umich.edu
chmaurer@iupui.edu
zach.thomas@txstate.edu
dlhaines@umich.edu
There were 1797 lines in the file with From as the first word
Question 3
Rewrite the program that prompts the user for a list of numbers and prints out the maximum and minimum of the numbers at the end when the user enters “done”. Write the program to store the numbers the user enters in a list and use the max() and min() functions to compute the maximum and minimum numbers after the loop completes.

  • This is an exercise to practice appending to a list.
  • append() also modifies a list
  • use min(), max() function to find min and max after the loop finishes.
numbers=[]

while True:
    inp=input("Enter a number: ")
    if inp == 'done':
        break

    try:
        numbers.append(float(inp))
    except:
        print('Invalid number')

minimum = min(numbers)
maximum = max(numbers)

print ('Maximum: %0.1f \nMinimum: %0.1f' % (minimum, maximum) )

Output:

Enter a number: 5
Enter a number: 9
Enter a number: 7
Enter a number: f
Invalid number
Enter a number: 6
Enter a number: 2
Enter a number: 4
Enter a number: done
Maximum: 2.0 
Minimum: 9.0






Comments

Popular posts from this blog

TYL - Food Corner Program

Classes and objects solution

TYL - Salary Hike - Python Problem