# Shifting digits

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'

• Is the input format that strict, or can I take the input for my program as, say, "5f6"\n16? Sep 1, 2017 at 1:02
• @ETHproductions: That would be acceptable. Sep 1, 2017 at 1:04
• What is the answer for ['999', '10']?
– tsh
Sep 1, 2017 at 1:22
• @tsh: That would be '000' Sep 1, 2017 at 1:22
• 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. Sep 2, 2017 at 23:50

# 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


# 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


# Python 2, 7774 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.

• 69 bytes
– ovs
Sep 1, 2017 at 9:33

# JavaScript, 57 bytes

(a,b)=>a.replace(/./g,v=>(-~parseInt(v,b)%b).toString(b))

• Save a byte with currying: a=>b=> Sep 1, 2017 at 13:51
• This doesn't function with base 1 Sep 1, 2017 at 16:34
• @Bálint I wouldn't expect any solution to work with base 1... Sep 1, 2017 at 21:05
• @ETHproductions Quoting one of the comments from OP "Base 1 is also an acceptable input" Sep 1, 2017 at 21:39
• @Bálint: Acceptable, not required. Also, comments do not a spec make. Sep 2, 2017 at 1:11

# 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.

• The notification I got of your answer said "Pyth, 0 bytes" and I almost did a spit take! 11 bytes is still great, though. :-) Sep 1, 2017 at 3:21
• @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 Sep 1, 2017 at 3:23
• 10 bytes (.rE<+jkUTG). I have absolutely no idea why you have a J in there, as you only use it once. Sep 1, 2017 at 8:45
• @Mr.Xcoder Whoa, I guess that's what golfing at 1am does to someone. Sep 1, 2017 at 13:43

# 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"

• ØBiÐ€⁸%⁹‘ịØB for 12 bytes (uppercase acceptable) Sep 2, 2017 at 1:19
• 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)? Sep 2, 2017 at 22:30
• @Zacharý added. Sep 3, 2017 at 3:42

# C, 7574 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!

# 05AB1E, 10 bytes

ö¹в>¹%¹β¹B


Try it online!

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

map.(%)
_%'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. • A multiline definition of % saves a byte. – nimi Sep 1, 2017 at 13:39 • @nimi Thanks! It's always nicer to have powers of 2 as byte count. Sep 1, 2017 at 14:58 • A shorter test for the second line: mod(fromEnum c-b)39==8. Sep 2, 2017 at 1:09 • @ØrjanJohansen Thanks! Switching to upper-case letters even saves another byte. Sep 2, 2017 at 7:52 • 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. Sep 2, 2017 at 8:21 # 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) • έ, pop n; 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! • MY is winning! Holy crap! Sep 2, 2017 at 22:25 • I can't believe MY is outgolfing the language named after base! Sep 3, 2017 at 0:48 # PHP, 83 bytes while(($c=$argv[1][$i++])>'')echo base_convert(intval($c,$b=$argv[2])+1,10,$b)[-1];


Try it online!

# Mathematica, 62 bytes

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


# 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.

# Bash, 71 bytes

b=({0..9} {a..z})
for c in fold -w1<<<$1;{ echo -n${b[-~$2#$c%$2]};}  Try it online! # Python, 81787487 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  • f('6f3', '32') get wrong output '7164' – tsh Sep 1, 2017 at 1:31 • @tsh: Absolutely right. Fixed. Sep 1, 2017 at 1:38 • (x+1)%y -> -~x%y, – tsh Sep 1, 2017 at 1:51 • @tsh: Beat me to it. I was trying to figure out that bit flip right now. Thanks :-) Sep 1, 2017 at 1:51 • What is [:b] used? – tsh Sep 1, 2017 at 1:56 # 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. • You can remove two spaces from the start. Sep 3, 2017 at 12:05 # Charcoal, 23 21 bytes ≔…⁺⪫ＥχＩιωβＮθＦＳ§θ⁺¹⌕θι  Try it online! Link is to verbose version of code. Edit: Saved 2 bytes by using CycleChop instead of Slice. Explanation:  Ｅχ Loop over the digits Ｉι Cast to string ⪫ ω Join together ⁺ β Append the lowercase letters Ｎ First input as a number … Take n characters from the start ≔ θ Assign to variable q ＦＳ 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: ≔…⁺⪫ＥχＩιωβＮθＦＳ§θ⊕⌕θι # Perl 5-lF, 61 53 bytes $l=(0..9,a..z,aa)[<>];say map{++$_;/$l/?0:s/10/a/r}@F


Try it online!

# 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.