RESOURCES

COMP150: Practice Exercises

Progression 7: Practice exercises

Do consult a demonstrator if you have any questions about this material

Download the files you need for these exercises: Progression 7 support files

Question 1

Write a function that takes the name of a file as a parameter.

The file will contain several lines of ints which are separated by commas.

The function should open the provided file and:

  • add up the even numbers on each line to produce a line total
  • concatenate the line total to a string, followed by a space
  • return the string after the whole file has been processed.

Given a file containing:

1,2,3,4
2,4,6
1,3,5,11,33,13,15,17
    

Your function should return:

'6 12 0 '

Remember to have all the required files in the same directory as the python file.

The function should pass the following doctests:

    def sum_evens(file_name):
       '''
    >>> sum_evens('numbers_1.txt')
    '164 12 0 '
    >>> sum_evens('numbers_2.txt')
    '7654 5360 1569200 46574 7635754 0 9859896 '
    '''

def sum_evens(file_name):
    file_in = open(file_name)
    lines = file_in.readlines()
    result = ''
    for line in lines:            

        str_numbers = line.split(',')
        total = 0
        for str_number in str_numbers:
            number = int(str_number)  

            if number %2 ==0:
                total += number
        result += str(total) + ' '
    return result

Question 2

Write a function that takes 2 parameters, the name of a file, and an int.

The file will contain several lines consisting of words separated by commas.

The function should print the following for each line of text in the file:

  • "Line " followed by the appropriate number, starting at 1
  • each word in the line with a length that is greater than the int parameter, followed by a space

Note: When printing

'A '\n' character is written at the end, unless the print statement ends with a comma.'

https://docs.python.org/2/reference/simple_stmts.html#the-print-statement

Remember to have all the required files in the same directory as the python file.

Your function should pass the doctests below:

           
def print_words_longer_than(file_name, x):
        '''
    >>> print_words_longer_than('words_1.txt', 5)
    Line 1: apricot nectarine rockmelon
    Line 2: parsnip turnip pumpkin carrot
    Line 3: oregano rosemary
    >>> print_words_longer_than('words_2.txt', 7)
    Line 1: elephant
    Line 2: crocodile alligator
    Line 3: blackbird
    '''

    
def print_words_longer_than(file_name, x):
    file_in = open(file_name)
    lines = file_in.readlines()
    i = 1
    while i <= len(lines):

        print 'Line', str(i)+':',
        line = lines[i-1]
        line = line.replace('\n','')# lose the end of line character           
        words = line.split(',')

        for word in words:
            if len(word) > x:
                print word,
        i+=1
        print      
    

Question 3

Write a function that returns a seating plan (in the form of a string) which is, thankfully, always rectangular. Rows are represented by capital letters and seats by numbers (starting from 1). Each seating position is followed by a space. The function should take, as its parameters, the number of rows required and the number of seats required in each row. The output of the function called with the arguments 3 and 4 should, when printed, look as follows:

A1 A2 A3 A4 
B1 B2 B3 B4 
C1 C2 C3 C4

You may assume(for now) that the number of rows will not exceed 26

Avoid using long string literals containing letters - that is no fun!

Remember:   ord('A') gives the ascii number of 'A' which is 65
	        chr(65) gives the char 'A'
	        Capital letters are contiguous.

Remember to have all the required files in the same directory as the python file.

Your function should pass the doctests below:

           
def seating_plan_simple(num_rows, num_seats):
        '''
    >>> seating_plan_simple(4,6)== open('school_play_simple.txt').read()
    True
    >>> seating_plan_simple(15,15)== open('opera_simple.txt').read()
    True
    '''

    
def seating_plan_simple(num_rows, num_seats):
    result = ""
    current_row = 0
    current_seat = 1

    while current_row < num_rows:
        while current_seat <= num_seats:
            result += chr(65 + current_row) + str(current_seat) + " "
            current_seat+=1

        result += "\n"
        current_row += 1
        current_seat = 1
    return result         

Question 4 - Option 1

To save confusion, the letters O and I are not used since they look too much like zero and one. The number 13 is not used since superstitious people refuse to sit there. For an easy life - assume that there will be no more than 24 rows.

Your function should pass the doctests below:

  
def seating_plan_opt1(num_rows, num_seats):
        '''
    >>> seating_plan_opt1(4, 6)== open('school_play_simple.txt').read()
    True
    >>> seating_plan_opt1(15,15)== open('school_play_opt1.txt').read()
    True
    >>> seating_plan_opt1(15, 15)== open('opera_opt1.txt').read()
    True
    '''
    

Question 4 - Option 2

If there are more than 24 rows you should move to lower case letters (a - z, again excluding i and o but also lower case L). This time you may assume there will not be more than 47 rows. Lower case letters are contiguous.

Your function should pass the doctests below:

           
def seating_plan_opt2(num_rows, num_seats):
        '''
    >>> seating_plan_opt2(15,15)== open('school_play_opt1.txt').read()
    True
    >>> seating_plan_opt2(47,23)== open('opera_mega.txt').read()
    True
    '''