6
\$\begingroup\$

Given a string representing a number and the base that number is in, shift each positional value upward, wrapping back to zero if the value cannot increase. You will never be provided invalid input, nor a base larger than 36.

For example, you may be provided ['5f6', '16'], which represents the number 1526 in base 16. The expected output in that case would be '607' (1543 in base 16).

Input

  • program: separate lines from STDIN: '5f6\n16'
  • function: a tuple or array: f(['5f6', '16']) or f(['5f6', 16])
  • function: two arguments f('5f6', '16') or f('5f6', 16)

Output

  • program: to STDOUT: 607
  • function: returned as a string: '607'

Test cases

['5f6', '16']
'607'

['abc', '36']
'bcd'

['1', '1'] or ['0', '1']
'1' or '0'

['9540', '10']
'0651' or '651'

['001', '2']
'110'
\$\endgroup\$
  • \$\begingroup\$ Is the input format that strict, or can I take the input for my program as, say, "5f6"\n16? \$\endgroup\$ – ETHproductions Sep 1 '17 at 1:02
  • \$\begingroup\$ @ETHproductions: That would be acceptable. \$\endgroup\$ – Zach Gates Sep 1 '17 at 1:04
  • \$\begingroup\$ What is the answer for ['999', '10']? \$\endgroup\$ – tsh Sep 1 '17 at 1:22
  • \$\begingroup\$ @tsh: That would be '000' \$\endgroup\$ – Zach Gates Sep 1 '17 at 1:22
  • 1
    \$\begingroup\$ You haven't specified what base 1 input will look like. Since unary isn't a positional number system I wouldn't require supporting it if I were you, but if you do you should specify what digit is used. \$\endgroup\$ – Jakob Sep 2 '17 at 23:50

19 Answers 19

7
\$\begingroup\$

Paradoc (v0.2.10), 10 bytes (CP-1252)

DaÅ+<:<oTr

Try it online!

Takes the string and the base as an integer from the stack, results in a string on the stack.

Whoops, I don't know why I don't have a 0-9A-Z constant yet. It would shave two bytes. (Assumes input is uppercase; for lowercase replace Å with La.)

Explanation:

Da         .. Push the digit alphabet
  Å        .. Push the uppercase alphabet
   +       .. Concatenate them
    <      .. Take the slice of the first (base) characters
     :     .. Duplicate
      <o   .. Left-rotate by one
        Tr .. Translate through the last two strings
\$\endgroup\$
4
\$\begingroup\$

Japt, 10 bytes

®nV Ä sV Ì

Test it online!

The 3-byters nV and sV are rather annoying...

Explanation

 ®   nV Ä  sV Ì     
UmZ{ZnV +1 sV gJ}   Ungolfed
                    Implicit: U = input string, V = base
UmZ{            }   Map each char in Z to
    ZnV               Z converted from base V to decimal,
        +1            plus 1,
           sV         converted back to base V,
              gJ      and shortened to only the last char. (J = -1)
                    Implicit: output result of last expression
\$\endgroup\$
3
\$\begingroup\$

Python 2, 77 74 72 bytes

lambda s,m:''.join(chr(c+[48,55][c>9])for c in[-~int(x,m)%m for x in s])

Try it online!

Takes a string and an int base. Works up to base 36.

\$\endgroup\$
  • \$\begingroup\$ 69 bytes \$\endgroup\$ – ovs Sep 1 '17 at 9:33
3
\$\begingroup\$

JavaScript, 57 bytes

(a,b)=>a.replace(/./g,v=>(-~parseInt(v,b)%b).toString(b))
\$\endgroup\$
  • 1
    \$\begingroup\$ Save a byte with currying: a=>b=> \$\endgroup\$ – Shaggy Sep 1 '17 at 13:51
  • \$\begingroup\$ This doesn't function with base 1 \$\endgroup\$ – Bálint Sep 1 '17 at 16:34
  • 1
    \$\begingroup\$ @Bálint I wouldn't expect any solution to work with base 1... \$\endgroup\$ – ETHproductions Sep 1 '17 at 21:05
  • \$\begingroup\$ @ETHproductions Quoting one of the comments from OP "Base 1 is also an acceptable input" \$\endgroup\$ – Bálint Sep 1 '17 at 21:39
  • \$\begingroup\$ @Bálint: Acceptable, not required. Also, comments do not a spec make. \$\endgroup\$ – Shaggy Sep 2 '17 at 1:11
3
\$\begingroup\$

Pyth, 11 10 bytes

.rE<+jkUTG

Try it here

Thanks to Mr. XCoder for saving 1 byte!

We use .r to cyclically rotate over the alphabet of the given base. We build that using G as the built in alphabet, and then a range of numbers, and slicing off the excess.

\$\endgroup\$
  • \$\begingroup\$ The notification I got of your answer said "Pyth, 0 bytes" and I almost did a spit take! 11 bytes is still great, though. :-) \$\endgroup\$ – Zach Gates Sep 1 '17 at 3:21
  • \$\begingroup\$ @ZachGates Haha, that was caused by me copying an empty TIO body link and then filling it out to save me the trouble of typesetting the answer. Unfortunately I forgot to change the byte count, but the edit that happened in the grace period doesn't get reflected in the notification I think. I hope I didn't startle you too much :P \$\endgroup\$ – FryAmTheEggman Sep 1 '17 at 3:23
  • 1
    \$\begingroup\$ 10 bytes (.rE<+jkUTG). I have absolutely no idea why you have a J in there, as you only use it once. \$\endgroup\$ – Mr. Xcoder Sep 1 '17 at 8:45
  • \$\begingroup\$ @Mr.Xcoder Whoa, I guess that's what golfing at 1am does to someone. \$\endgroup\$ – FryAmTheEggman Sep 1 '17 at 13:43
3
\$\begingroup\$

Jelly, 15 12 bytes

3 bytes thanks to Jonathan Allan.

ØBiЀ⁸%⁹‘ịØB

Try it online!

How it works

ØBiЀ⁸%⁹‘ịØB
ØB           "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  iЀ⁸       index of each in first input
      %⁹     modulo the second input
        ‘    increase by 1
         ị   index into
          ØB "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
\$\endgroup\$
  • \$\begingroup\$ ØBiЀ⁸%⁹‘ịØB for 12 bytes (uppercase acceptable) \$\endgroup\$ – Jonathan Allan Sep 2 '17 at 1:19
  • \$\begingroup\$ Nice answer, +1. Guess you wish Jelly had builtins for string <-> number base conversions, though, huh? Also, can you please explain how this works (mainly the order in which it's parsed)? \$\endgroup\$ – Zacharý Sep 2 '17 at 22:30
  • \$\begingroup\$ @Zacharý added. \$\endgroup\$ – Leaky Nun Sep 3 '17 at 3:42
2
\$\begingroup\$

C, 75 74 65 bytes

Thanks to @tsh for saving 9 bytes!

f(n,b)char*n;{for(;*n;++n)*n=*n-b-(b>10?86:47)?*n-57?*n+1:97:48;}

Modifies the input string directly.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 10 bytes

ö¹в>¹%¹β¹B

Try it online!

\$\endgroup\$
  • \$\begingroup\$ This seems pretty fitting, as 05AB1E came from base. Also, can you add an explanation of what all those b's mean? \$\endgroup\$ – Zacharý Sep 2 '17 at 22:33
  • \$\begingroup\$ @Zacharý They're all base stuff including the ö :p \$\endgroup\$ – Erik the Outgolfer Sep 3 '17 at 12:05
1
\$\begingroup\$

Haskell, 65 64 59 bytes

map.(%)
b%c|mod(fromEnum c-b)39==8='0'
_%'9'='a'
_%c=succ c

Try it online! Usage: map.(%) $ 16 "5f6" yields "607".

Edit: -1 byte thanks to nimi, -5 bytes thanks to Ørjan Johansen.

\$\endgroup\$
  • 1
    \$\begingroup\$ A multiline definition of % saves a byte. \$\endgroup\$ – nimi Sep 1 '17 at 13:39
  • \$\begingroup\$ @nimi Thanks! It's always nicer to have powers of 2 as byte count. \$\endgroup\$ – Laikoni Sep 1 '17 at 14:58
  • 1
    \$\begingroup\$ A shorter test for the second line: mod(fromEnum c-b)39==8. \$\endgroup\$ – Ørjan Johansen Sep 2 '17 at 1:09
  • \$\begingroup\$ @ØrjanJohansen Thanks! Switching to upper-case letters even saves another byte. \$\endgroup\$ – Laikoni Sep 2 '17 at 7:52
  • \$\begingroup\$ Um you cannot use upper-case letters for this trick - the modulus needs to be larger than the highest base supported, otherwise it gives false positives. \$\endgroup\$ – Ørjan Johansen Sep 2 '17 at 8:21
1
\$\begingroup\$

MY, 8 bytes

αωP‘%pέ←

I definitely made the right choice adding the basify (P) and stringify (p) atoms!

Try it online!

How it works, along with a reason for the codepoint

  • α, push the second command line argument (taken from APL, but it's the greek symbol instead of the APL one)
  • ω, push the first command line argument (also taken from APL)
  • P, pop n; push n basified (0-Z => 0-35, b flipped is p)
  • , pop n; push n + 1 (vectorizes ["vecifies" in MY], taken from jelly)
  • %, pop a; pop b; push a%b (vecifies, do I really need to give this to you?)
  • p, pop n; push n stringified (0-35 => 0-Z, vecifies, same origin as P)
  • έ, popn; push "".join(n) (ε is the empty string, and just put an accent mark over it)
  • , pop n; output n with no newline. (Pushing it away from the stack to STDOUT)

THIS is what MY was meant for!

\$\endgroup\$
  • \$\begingroup\$ MY is winning! Holy crap! \$\endgroup\$ – Zacharý Sep 2 '17 at 22:25
  • \$\begingroup\$ I can't believe MY is outgolfing the language named after base! \$\endgroup\$ – Zacharý Sep 3 '17 at 0:48
1
\$\begingroup\$

PHP, 83 bytes

while(($c=$argv[1][$i++])>'')echo base_convert(intval($c,$b=$argv[2])+1,10,$b)[-1];

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Mathematica, 62 bytes

(h=#2;""<>ToString/@(Mod[#~FromDigits~h+1,h]&/@Characters@#))&


Try it online!

\$\endgroup\$
0
\$\begingroup\$

PHP, 87 bytes

list(,$a,$b)=$argv;for(;$c=intval($a{$i++},$b);)echo$b>1?base_convert(++$c%$b,10,$b):1;

Run with -r like this:

php -r "list(,$a,$b)=$argv;for(;$c=intval($a{$i++},$b);)echo$b>1?base_convert(++$c%$b,10,$b):1;" xyz 36

Works with bases 1 to 36, inclusive.

\$\endgroup\$
0
\$\begingroup\$

Bash, 71 bytes

b=({0..9} {a..z})
for c in `fold -w1<<<$1`;{ echo -n ${b[-~$2#$c%$2]};}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python, 81 78 74 87 86 bytes

import string
lambda a,b:''.join(string.printable[-~int(i,b)%b]for i in a)if b>1else a

Alternatively:

lambda a,b:''.join(__import__('string').printable[-~int(i,b)%b]for i in a)if b>1else a
\$\endgroup\$
  • \$\begingroup\$ f('6f3', '32') get wrong output '7164' \$\endgroup\$ – tsh Sep 1 '17 at 1:31
  • \$\begingroup\$ @tsh: Absolutely right. Fixed. \$\endgroup\$ – Zach Gates Sep 1 '17 at 1:38
  • \$\begingroup\$ (x+1)%y -> -~x%y, \$\endgroup\$ – tsh Sep 1 '17 at 1:51
  • \$\begingroup\$ @tsh: Beat me to it. I was trying to figure out that bit flip right now. Thanks :-) \$\endgroup\$ – Zach Gates Sep 1 '17 at 1:51
  • \$\begingroup\$ What is [:b] used? \$\endgroup\$ – tsh Sep 1 '17 at 1:56
0
\$\begingroup\$

Ruby, 53 59 bytes

->n,b{b<2?n:n.chars.map{|c|(c.to_i(b)+1).to_s(b)[-1]}.join}

Try it online!

Originally 6 bytes less, but Ruby doesn't like using a base of 1.

Could save 5 bytes by removing .join if the output did not need to be a string.

\$\endgroup\$
  • \$\begingroup\$ You can remove two spaces from the start. \$\endgroup\$ – Erik the Outgolfer Sep 3 '17 at 12:05
0
\$\begingroup\$

Charcoal, 23 21 bytes

≔…⁺⪫EχIιωβNθFS§θ⁺¹⌕θι

Try it online! Link is to verbose version of code. Edit: Saved 2 bytes by using CycleChop instead of Slice. Explanation:

    Eχ                  Loop over the digits
      Iι                Cast to string
   ⪫    ω               Join together
  ⁺      β              Append the lowercase letters
          N             First input as a number
 …                      Take `n` characters from the start
≔          θ            Assign to variable `q`
            FS          Loop over the characters in the second input
                  ⌕θι   Find the character index in `q`
                ⁺¹      Plus 1
              §θ        Cyclically index in `q`
                        Implicitly print

Note: Later versions of Charcoal reduce this to 20 bytes: ≔…⁺⪫EχIιωβNθFS§θ⊕⌕θι

\$\endgroup\$
0
\$\begingroup\$

Perl 5 -lF, 61 53 bytes

$l=(0..9,a..z,aa)[<>];say map{++$_;/$l/?0:s/10/a/r}@F

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Stax, 6 bytes

╕W─░ì]

Run and debug it

Procedure:

  1. Push constant "0123456789abcdefghijklmnopqrstuvwxyz".
  2. Get and eval base, now two spots down the stack
  3. Truncate digit string to specified count.
  4. "Ring" translate using remaining characters. This maps each character to the next, wrapping around to "0" at the end.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.