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

Leave a Reply

Your email address will not be published. Required fields are marked *