Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Commonmark migration
Source Link

#DUP , 37 bytes

DUP , 37 bytes

#DUP , 37 bytes

DUP , 37 bytes

Source Link
M L
  • 3.3k
  • 1
  • 20
  • 19

#DUP, 37 bytes

[0[`32ドル-][10円*(48ドル-)+\%]#%]⇒II(I)+.

Both integers have to be space terminated (ASCII 32)

Explanation, taking example 11+3:

[0[`32ドル-][10円*(48ドル-)+\%]#%]⇒I {I defines an operator that converts space-terminated character strings to integer numbers}
[ ]⇒I {Definition of operator I}
 0 {push 0 on ds (data stack)}
 [cond.][ block ]# {while condition nonzero do block}
 ` {read character from STDIN to ds}
 $ {DUP ds top value}
 32- {push 32 on ds, SUB}
 \ {SWAP topmost ds values}
 10* {push 10 on ds, MUL}
 ( {move ds top value to rs (return stack)}
 $ {DUP}
 48- {push 48, SUB}
 ) {move rs top back to ds}
 + {ADD}
 \% {SWAP, POP}
 % {POP}

I reads in numbers as single characters and converts them to decimal numbers (basically by a base 10 shift left, add loop). I also checks if a terminating space is found, and if that’s the case, exits the read in loop and removes junk from the stack.

Actual program:

I {Read in first integer character by character}
 ( {move ds top to rs}
 I {read in second integer}
 ) {move first integer back from rs to ds}
 + {ADD}
 . {output sum to STDOUT}

Try it out in this online DUP interpreter.

AltStyle によって変換されたページ (->オリジナル) /