Time Add Seconds
Programming Interview
Medium
9 views
Problem Description
Time hh mm ss and extra seconds x are provided. Create Time class add(x) and output new time in HH:MM:SS (24h).
Input Format
One line: hh mm ss x.
Output Format
One time string.
Official Solution
import sys
p=sys.stdin.read().strip().split()
if len(p)<4: sys.exit(0)
h=int(p[0]); m=int(p[1]); s=int(p[2]); x=int(p[3])
class Time:
def __init__(self,h,m,s):
self.t=h*3600+m*60+s
def add(self,x):
self.t=(self.t+x)%(24*3600)
def fmt(self):
h=self.t//3600
m=(self.t%3600)//60
s=self.t%60
return f'{h:02d}:{m:02d}:{s:02d}'
t=Time(h,m,s)
t.add(x)
print(t.fmt())
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!