[Community Puzzle] Calculator

Coding Games and Programming Challenges to Code Better

Send your feedback or ask for help here!

Created by @ByteWolf,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.

2 Likes

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 :slight_smile:

1 Like

Hello,

This sequential puzzle is quite not oftenly seen in easy section, that’s quite interesting.
I have found my way on test cases but the validators 3 and 7 are still not ok with my solution…
I tried multiple combination with AC instruction placements but no issue appears.
Could any one give me any specificity of the validator vs the equivalent test case?

thank you !

Validator 3: The structure of the input is similar to Test 3. I guess there shouldn’t be an issue before the “AC” part? For the part after “AC”, you may create custom cases to test out different single-digit number combinations (I won’t say more, otherwise a bit too spoilery…) and see if your code produces the correct output.

Validator 7: It looks like it’s the only case where the user presses = consecutively, gets a result, then types another operation, and presses = consecutively again. That is to say, another operation is performed on the earlier result immediately repeatedly. You may check whether your code handles such a situation correctly.

for Validator7:
I tried the following pattern and it works :

9
2
5
+
6
=
=
-
=
=

type or paste code here

results:
2
25
25
6
31
37
37
31
25

Would I be mistaken in my understanding how to handle this situation?

There is an operand after the second operator, e.g.

9
2
5
+
6
=
=
-
8
=
=

Hi,
There are 10 inputs so I changed 9 to 10.
My program is giving these output:
2
25
25
6
31
37
37
8
29
21
Which I think are quite ok, may I missed or miss understood a rule.

Yes, you’re right. So, there’s something else wrong with your code. You may PM your code to me and I can take a look.

Did you ever get to the bottom of this? I too am stuck on validator steps 3 and 7 despite passing the test cases and trying the suggestions here.

Hi,
I’m still trying to figure out, if it make sens, I’ll inform you once found my issue.

Hi,
Ok I went through, the possible link between both validators is the management of small results.
I’d prefer avoid to go in more details, if you don’t figure out, lets PM.

1 Like

Thanks I’ll give that a try :slight_smile:

Nice game ! Validators 3 and 5 and 7 are red for me while all tests are green.
I have woked this with the help of Rust, then not very easy to read.