AtCoder Beginners Contest 084[A to C]

Tasks

 

A. New Year … Basic calculation only

print(-int(input())+48)

 

B. Postal Code … I used string method .split(‘-‘) to split the given string into two part, and method .isdecimal() to check if respective part is composed of only 0-9.

a, b = list(map(int, input().split()))
s = input()

sl = s.split('-')
if len(sl) != 2:
    print('No')
elif ( sl[0].isdecimal() and len(sl[0]) == a ) and ( sl[1].isdecimal() and len(sl[1]) == b):
   print('Yes')
else:
    print('No')

 

C. Special Trains … I created class for stations to store each parameters. It costs N^2 as there are two for loops, but N is only up to 500 and should not be a problem.

class station:
    def __init__(self, speed, first, interval):
        self.speed = speed
        self.first = first
        self.interval = interval


def onboard_duration(station, start_time, first=False):
    # return the arrival_time at the next_station
    if first or start_time < station.first: # starting station or the first train is not departed from the station
        return station.first + station.speed
    elif start_time % station.interval == 0:
        return start_time + station.speed
    else:
        return start_time + station.interval - (start_time % station.interval) + station.speed


stations = []
for _ in range(int(input())-1):
    stations.append(station(*list(map(int, input().split()))))

for i in range(len(stations)):
    elapsed_time = 0
    for j in range(i, len(stations)):
        if elapsed_time == 0:
            elapsed_time = onboard_duration(stations[j], elapsed_time, first=True)
        else:
            elapsed_time = onboard_duration(stations[j], elapsed_time)
    print(elapsed_time)

print('0')