Coding Games and Programming Challenges to Code Better
Send your feedback or ask for help here!
Created by @ByteWolf1,validated by @TheMagicShop,@MonkeyFeathers and @UnicornP.
If you have any issues, feel free to ping them.
Coding Games and Programming Challenges to Code Better
Send your feedback or ask for help here!
Created by @ByteWolf1,validated by @TheMagicShop,@MonkeyFeathers and @UnicornP.
If you have any issues, feel free to ping them.
Good, tricky and annoying. Definitely not an easy task.
Hello, thanks for this puzzle, it took me a while to pass all the test. Everything is green on my side now but the last validator is not good. Can you have a look please?
import sys
import math
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
def calculator(nbr1,nbr2,operationSign):
a = float(nbr1)
b = float(nbr2)
if operationSign == 0:
res = a+b
elif operationSign == 1:
res = a-b
elif operationSign == 2:
res = a*b
else:
res = a/b
if res - math.floor(res)==0:
res = int(res)
return round(res,3)
n = int(input())
numbers = ['0','1','2','3','4','5','6','7','8','9']
operationSign = ['+','-','x','/']
AC = 'AC'
equal = '='
keys = []
for i in range(n):
keys.append(input())
i=0
j=0
nbr1 = ''
nbr2 = ''
res = ''
operation = -1
operationPrev = -1
while i<n:
key = keys[i]
#Reset if AC
if key == AC:
nbr1 = ''
nbr2 = ''
res = ''
j = 0
operation = -1
operationPrev = -1
print('0')
#Check if operation pressed
elif key in operationSign:
change = False
#deal with multiple hit
if i<n:
if keys[i+1] in operationSign:
changeNbr = 0
while keys[i+1] in operationSign:
change = True
changeNbr += 1
i += 1
key = keys[i]
operation = operationSign.index(key)
j += 1
if j==1:
if change:
for k in range(changeNbr+1):
print(nbr1)
else:
print(nbr1)
#Update nbr1 and nbr2
elif (key in numbers):
if j>=1:
nbr2 += key
prevNbr2 = nbr2
print(nbr2)
else:
nbr1 += key
print(nbr1)
if j>1 and nbr2!= '':
nbr1 = str(calculator(nbr1,nbr2,operationPrev))
prevNbr2 = nbr2
print(nbr1)
if change:
change = False
print(nbr1)
nbr2 = ''
operationPrev = operation
if key == equal:
multipleEqual = False
if i<n-1:
if keys[i+1] == equal:
while keys[i+1] == equal:
multipleEqual = True
nbr1 = str(calculator(nbr1,prevNbr2,operationPrev))
print(nbr1)
i+=1
j=0
if i==n-1:
break
if nbr2 != '':
nbr1 = str(calculator(nbr1,nbr2,operationPrev))
prevNbr2 = nbr2
nbr2 = ''
print(nbr1)
if i<n-1:
if keys[i+1] in numbers:
nbr1 = ''
j = 0
else:
if nbr2 != '':
nbr1 = str(calculator(nbr1,nbr2,operationPrev))
print(nbr1)
i+=1
Try this test case:
8
8
0
-
3
x
1
0
=
The final answer should be 770.
Your code also fails in similar cases where other operators are involved.
thank you I figured out what was wrong