28
9
Implement a simple integer operation scriptable calculator.
Concept
The accumulator starts at 0 and has operations performed on it. At the end of the program output the value of the accumulator.
Operations:
+
adds1
to the accumulator-
subtracts1
from the accumulator*
multiplies the accumulator by2
/
divides the accumulator by2
Sample script
The input ++**--/
should give the output 3
.
Example implementation
def calc(s)
i = 0
s.chars.each do |o|
case o
when '+'
i += 1
when '-'
i -= 1
when '*'
i *= 2
when '/'
i /= 2
end
end
return i
end
Rules
- This is code-golf, so lowest answer in bytes wins, but is not selected.
- Creative implementations are encouraged.
- Standard loopholes are prohibited.
- You get the program via stdin or arguments, and you can output the answer via return value or stdout.
- Have fun.
- Division truncates down because it is integer division.
- The program
-/
returns-1
.
Test cases
*///*-*+-+
-1
/*+/*+++/*///*/+-+//*+-+-/----*-*-+++*+**+/*--///+*-/+//*//-+++--++/-**--/+--/*-/+*//*+-*-*/*+*+/+*-
-17
+++-+--/-*/---++/-+*-//+/++-*--+*+/*/*/++--++-+//++--*/***-*+++--+-*//-*/+*/+-*++**+--*/*//-*--**-/-*+**-/*-**/*+*-*/--+/+/+//-+*/---///+**////-*//+-+-/+--/**///*+//+++/+*++**++//**+**+-*/+/*/*++-/+**+--+*++++/-*-/*+--/++*/-++/-**++++/-/+/--*/-/+---**//*///-//*+-*----+//--/-/+*/-+++-+*-*+*+-/-//*-//+/*-+//+/+/*-/-/+//+**/-****/-**-//+/+-+/*-+*++*/-/++*/-//*--+*--/-+-+/+/**/-***+/-/++-++*+*-+*+*-+-//+/-++*+/*//*-+/+*/-+/-/*/-/-+*+**/*//*+/+---+*+++*+/+-**/-+-/+*---/-*+/-++*//*/-+-*+--**/-////*/--/*--//-**/*++*+/*+/-+/--**/*-+*+/+-*+*+--*///+-++/+//+*/-+/**--//*/+++/*+*////+-*-//--*+/*/-+**/*//+*+-//+--+*-+/-**-*/+//*+---*+//*/+**/*--/--+/*-*+*++--*+//+*+-++--+-*-*-+--**+/+*-/+*+-/---+-*+-+-/++/+*///*/*-+-*//-+-++/++/*/-++/**--+-////-//+/*//+**/*+-+/+/+///*+*///*-/+/*/-//-*-**//-/-+--+/-*--+-++**++//*--/*++--*-/-///-+/+//--+*//-**-/*-*/+*/-*-*//--++*//-*/++//+/-++-+-*/*-+++**-/-*++++**+-+++-+-***-+//+-/**-+/*+****-*+++*/-*-/***/-/*+/*****++*+/-/-**-+-*-*-++**/*+-/*-+*++-/+/-++*-/*-****-*
18773342
2So... it's not strictly integer, since
/
can yield non-integers. – Conor O'Brien – 2016-08-28T22:52:56.3232Then you should specify this explicitly. – Conor O'Brien – 2016-08-28T22:55:16.790
5What should
-/
return? – Dennis – 2016-08-28T23:32:09.290@Dennis Well, C truncation says it should round to 0, so that should be
0
... – LegionMammal978 – 2016-08-28T23:58:22.433@LegionMammal978 Yet the reference implementation returns -1.
– Dennis – 2016-08-29T00:07:28.2904I can't help but notice that the snippet of code featured on the home page of rust-lang solves this challenge. – Zwei – 2016-08-29T03:23:45.177
1@Zwei That's exactly where I got this challenge. – dkudriavtsev – 2016-08-29T04:13:31.417
My recursive solution returns an floating point number. Is this allowed? The results are correct, but they contain
.0
at the end. – Yytsi – 2016-08-29T06:41:21.163Does the program have to handle the empty input string ? – Ton Hospel – 2016-08-29T07:11:12.430
4Please add more test cases. – Martin Ender – 2016-08-29T07:38:50.117
2Curse that nasty round-down requirement! I had a nice 41-byte GNU sed + dc answer:
s/[+-]/1&/g;s/[*/]/2&/g;s/.*/dc -e 0d&p/e
, but it rounds towards zero when you divide a negative :-( – Toby Speight – 2016-08-29T13:23:11.8631May we substitute the symbols with
+-×÷
or 'PMTD'? – Adám – 2016-08-30T06:54:28.963@TobySpeight you can divide this way in dc to have the desired rounding:
– seshoumara – 2016-08-30T08:09:24.723[1-]S@d0>@2/
, because the default output precision is 0 (see my answer)@TonHospel It outputs 0 because the accumulator starts at 0 and changes only with operations. – dkudriavtsev – 2016-09-03T21:43:45.490
@MartinEnder xnor did that for me – dkudriavtsev – 2016-09-03T21:44:09.183
@Adám no, they must be exactly as specified. Sorry. – dkudriavtsev – 2016-09-03T21:44:34.840