20
\$\begingroup\$

This is a code golf version of a similar question I asked on stack earlier but thought it'd be an interesting puzzle.

Given a string of length 10 which represents a base 36 number, increment it by one and return the resulting string.

This means the strings will only contain digits from 0 to 9 and letters from a to z.

Base 36 works as follows:

The right most digit is incremented, first by using 0 to 9

0000000000 > 9 iterations > 0000000009

and after that a to z is used:

000000000a > 25 iterations > 000000000z

If z needs to be incremented it loops back to zero and the digit to its left is incremented:

000000010

Further rules:

  • You may use upper case or lower case letters.
  • You may not drop leading zeros. Both input and output are strings of length 10.
  • You do not need to handle zzzzzzzzzz as input.

Test Cases:

"0000000000" -> "0000000001"
"0000000009" -> "000000000a"
"000000000z" -> "0000000010"
"123456zzzz" -> "1234570000"
"00codegolf" -> "00codegolg"
\$\endgroup\$
  • \$\begingroup\$ @JoKing Code-golf, cool ideas, and efficiency I guess. \$\endgroup\$ – Jack Hales Jul 28 '18 at 3:08
  • 7
    \$\begingroup\$ I like the idea of implementing just the increment operation because it has the potential for strategies other than base-converting there and back. \$\endgroup\$ – xnor Jul 28 '18 at 3:16
  • 2
    \$\begingroup\$ Welcome to PPCG! This is a nice challenge idea, however as some comments have pointed out, some parts of the specification are unclear. For the future I recommend using our sandbox where you can get feedback on a challenge before posting it. \$\endgroup\$ – Laikoni Jul 28 '18 at 10:15
  • 1
    \$\begingroup\$ suggest you add something like "0zzzzzzzzz" (modify the most signficant digit) as a test case. It tripped up my C solution because of an off-by-one-error. \$\endgroup\$ – O.O.Balance Jul 28 '18 at 13:11
  • 1
    \$\begingroup\$ added an entry assuming it's ok -- a C entry already does it as well. \$\endgroup\$ – Felix Palmen Jul 30 '18 at 9:58

34 Answers 34

0
\$\begingroup\$

Elixir, 70 bytes

fn x->"1"<>x=Integer.to_string 1+String.to_integer("1"<>x,36),36;x end

Explanation:

Prepends 1 to the input, converts it to an integer in base 36, increments it, converts it to a string in base 36, and matches it to "1" <> x (reverse concatenation), then returns x.

Try it online!

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

Perl 5 -F, 47 bytes

($_=/z/?0:/9/?a:++$_)&&last for reverse@F;say@F

Try it online!

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

Powershell, 79 78 82 bytes

+4 bytes: z and 9 inside an argument string fixed

$i=1
"$args"[9..0]|%{$r=([char]($i+$_)+'0a')[2*($i*$_-eq57)+($i*=$_-eq'z')]+$r};$r

Less golfed test script:

$f = {

$i=1                    # increment = 1
"$args"[9..0]|%{        # for chars in positions 0..9 in the argument string (in reverse order)
    $c=[char]($i+$_)    # Important! A Powershell calculates from left to right
                        # Therefore the subexpression ($i+$_) gets a value before the subexpression ($i=$_-eq122)
    $j=2*($i*$_-eq57)+  # j = 2 if the current char is '9' and previous i is 1
        ($i*=$_-eq122)  # j = 1 if the current char is 'z' and previous i is 1
                        # j = 0 othewise
                        # side effect is: i = 1 if the current char is 'z' and previous i is 1, i = 0 othewise
    $c=($c+'0a')[$j]    # get element with index j from the array
    $r=$c+$r            # accumulate the result string
}
$r                      # push the result to a pipe

}

@(
    ,("09fizzbuzz" , "09fizzbv00")
    ,("0000000000" , "0000000001")
    ,("0000000009" , "000000000a")
    ,("000000000z" , "0000000010")
    ,("123456zzzz" , "1234570000")
    ,("00codegolf" , "00codegolg")
) | % {
    $s,$expected = $_
    $result = &$f $s
    "$($result-eq$expected): $result"
}

Output:

True: 09fizzBv00
True: 0000000001
True: 000000000a
True: 0000000010
True: 1234570000
True: 00codegolg
\$\endgroup\$
0
\$\begingroup\$

C# (.NET Core), 125 bytes

a=>{var x="";for(int i=9;;i--){var y=a[i]==90?'0':a[i]==57?'A':(char)(a[i]+1);x=y+x;a=a.Remove(i);if(y>48)break;}return a+x;}

Try it online!

Ungolfed:

a => {
    var x = "";                 // initialize x
    for (int i = 9; ; i--)      // starting at 9 (length of string minus one) and decrementing i indefinitely
    {
        var y = a[i] == 90 ?        // if the last character of a is 'Z'
            '0' :                       // y = '0'
            a[i] == 57 ?                // if the last character of a is '9'
                'A':                        // y = 'A'
                (char)(a[i] + 1);           // y = the ascii value of last character plus 1
        x = y + x;                  // prepend y to x
        a = a.Remove(i);            // remove the last character of a
        if (y > 48)                 // if y is a character other than '0',
            break;                      // break from the loop
    }
    return a + x;               // return the remainder of a, with x appended
}
\$\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.