Simple Counter
Programming Interview
Easy
9 views
Problem Description
You have q commands: INC, DEC. Create Counter class. DEC cannot go below 0. Output final value.
Input Format
First line q. Next q lines commands.
Output Format
One integer value.
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines: sys.exit(0)
q=int(lines[0].strip())
class Counter:
def __init__(self):
self.v=0
def inc(self):
self.v+=1
def dec(self):
if self.v>0:
self.v-=1
c=Counter()
for i in range(q):
cmd=(lines[1+i] if 1+i<len(lines) else '').strip()
if cmd=='INC':
c.inc()
elif cmd=='DEC':
c.dec()
sys.stdout.write(str(c.v))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!