Algorithms: Creating Usernames

usernamesThe following starter code is loosely based on the flowchart shown. The function provided is used to check whether a username already exists.

Use the starter code to implement the sections marked #TODO.

The text file, with usernames can be downloaded here.

 

 

'''
creating usernames
uses a file of existing usernames
this file is read in and then new usernames are checked against it
complete sections marked #TODO
'''

# reading in existing usernames
def read_usernames():
    with open('usernames.csv') as ufile:
        data = ufile.readlines()
    
    usernames = []
    for line in data:
        line = line.strip().split(',')
        usernames.append(line[4]) # only appends usernames, rest discarded
    
    return usernames

# checking if username exists
# the if condition in this function is equivalent to the one-line return    
def exists(username):
    '''
    if username in read_usernames():
        return True
    '''    
    return username in read_usernames()

# this writes the data back to the file
def write_userdata(udata):
    with open('usernames.csv', 'a') as ufile:
        ufile.write(udata)
        
    print(udata, 'has been written to the file.')
    print('Open the file: "usernames.csv" to see the data in the file')

    
#TODO write your code to construct a username below
#use the exists() function to check if a username already exists





#TODO write the code to construct a comma separated string of the format:
# yearofentry, firstname, surname, initial, username\n

 


#TODO call the function write_userdata() with your string



Algorithms – big exam question

This reads a csv file to get data on stations and miles. Use the starter code below and the linked csv file to solve this.

'''
big exam question starter code
implment your algorithm below
complete the sections starting # TODO
'''

# read file and clean up the data
def getstationdata():
    with open("stationlookupkingston.csv") as tfile:
        data = tfile.readlines()
            
        stationlist = []
        for line in data:
            line = line.strip().split(',')
            line[1] = int(line[1])   # convert the miles to int
            stationlist.append(line)
            
        return stationlist #send back a 2d list
        
        
ListOfStations = getstationdata() # list of stations and miles

# intro for the user
print('''
***********************************************
This section will simulate the card being read.
We will assume journey ends at Kingston'

Instead of IDs we will use station names
***********************************************

''')

# TODO change start to an input from the user
start = 'Barnes' #card being read

miles = 0 # initialise miles

# TODO add a for-loop to iterate through the elements in ListOfStations
# TODO find the miles for start - remember it is a 2d list 



#TODO display the fare based on miles

Download the csv file for the above starter code.

Algorithms Practice6

Students need to practice reading and completing algorithms given to them as pseudo code and flowcharts. The following problem has been modified from a past paper. Use the starter code to complete the program and check that the algorithm works:

'''
Rail passengers carry a contactless smartcard to pay for train journeys. A processor in the exit barrier calculates the rail fare. The card is swiped at the start and end of each journey. If there is enough money on the card at the end of the journey, the balance is updated and displayed on the barrier screen and the exit barrier opens. If not, the message, “Need to top-up” is displayed and the barrier does not open.
'''

# stations and costs for ending Kingston
# only london journeys

def calculateCost(startStation):
    zone1 ='waterloo westminster greenpark marble arch'
    zone2 = 'archway arsenal bermondsey bow hampstead'
    zone3 = 'barnes bellingham bowe charlton harlesden highgate'
    zone4 = 'barking brentford eltham greenford hanwell'
    zone5 = 'becontree brimsdown cheam eastcote hayes'
    zone6 = 'hillinngdon heathrow ickenham purley romford'
    
    zone1 = zone1.split()
    zone2 = zone2.split()
    zone3 = zone3.split()
    zone4 = zone4.split()
    zone5 = zone5.split()
    zone6 = zone6.split()
    
    if startStation in zone1:
        return 5.5
    if startStation in zone2:
        return 4.5
    if startStation in zone3:
        return 4
    if startStation in zone4:
        return 3.75
    if startStation in zone5:
        return 3.5
    if startStation in zone6:
        return 3
        
    return 0 #if station not found
    
# end of above function

# TODO change startStation to be an input
startStation = 'barnes' #for testing only
# TODO get balance as a decimal number input

payment = calculateCost(startStation)

# TODO implement the rest of your algorithm