20
6
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"
@JoKing Code-golf, cool ideas, and efficiency I guess. – Jack Hales – 2018-07-28T03:08:43.587
@JoKing I just edited the question to explain the golf puzzle a bit better, the aim is to create the function or "cycler" to turn one string into the next, the
i = i + 1
part of a for loop. – Jack Hales – 2018-07-28T03:15:07.173@JoKing changed to bytes. – Jack Hales – 2018-07-28T03:15:57.620
7I like the idea of implementing just the increment operation because it has the potential for strategies other than base-converting there and back. – xnor – 2018-07-28T03:16:56.660
@xnor I was thinking the same, I am trying to do the puzzle now and I was thinking of the ability to use it in a for-loop or while.
for (i = "000"; i == "zzz"; i = up(i)) {}
or something. – Jack Hales – 2018-07-28T03:18:16.343@JoKing you can use uppercase, you can add to the base-X as much as you want, it just has to be an iterating function that works with string AND integer, capital or not. And no you cannot truncate leading 0's. Will explain the bonus better now. – Jack Hales – 2018-07-28T03:25:39.827
2
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.
– Laikoni – 2018-07-28T10:15:25.0771suggest 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. – O.O.Balance – 2018-07-28T13:11:45.297Is there a codegolf style that gives points for readability? – TJR – 2018-07-30T03:02:55.170
"increment it by one and return the resulting string." <- is an in-place operation ok as well? – Felix Palmen – 2018-07-30T08:24:15.840
1added an entry assuming it's ok -- a C entry already does it as well. – Felix Palmen – 2018-07-30T09:58:12.493
@FelixPalmen yes :D – Jack Hales – 2018-07-31T22:07:18.113
Do I have to return leading zeros? – Magic Octopus Urn – 2018-10-31T14:00:18.220
@MagicOctopusUrn "You may not drop leading zeros." – Ørjan Johansen – 2018-10-31T17:03:48.277