7
\$\begingroup\$

Create a function which takes an integer and returns an array of integers representing the number in balanced base twelve. Specifically, the representation should follow these rules regarding 6 & -6 (from the above link):

  1. If the last digit of a number could be written with either 6 or ϑ, we use the one whose sign is opposite the sign of the first digit. For example, we write decimal 30 as dozenal , but decimal -30 as as dozenal ε6, not ζϑ.
  2. If a 6 or ϑ occurs before the last digit of a number, we use the one whose sign is opposite the next digit after it. For example, we write decimal 71 as dozenal , but decimal 73 as as dozenal 1ϑ1, not 61.
  3. If a 6 or ϑ is followed by a 0, we keep looking to the right until we can apply one of the first two rules. For example, we write decimal -72 as dozenal τ60, not ϑ0.

Otherwise, the only restrictions are to use only digits in [-6..6], and for the output, when interpreted as big-endian base twelve, to be equivalent to the input.

Example of use:

f(215) => [1,6,-1]
f(216) => [2,-6,0]
f(-82) => [-1,5,2]
f(0)   => [0]

Winner is the shortest, with upvotes being the tiebreaker.

\$\endgroup\$

3 Answers 3

3
\$\begingroup\$

J, 74 72 characters

Here's my J implementation (f =. not included in character count):

f =. **}.^:(*@#*0={.)@([:((12-~[),0>:@{.@]`[`]}])`,@.(6>[)/&.|.0,12&#.^:_1)@|

And some tests (each result is boxed, and underscore means negative):

   NB. Integers [-10..10]:
   (<@f"0)i:10
┌────┬────┬────┬────┬────┬──┬──┬──┬──┬──┬─┬─┬─┬─┬─┬─┬────┬────┬────┬────┬────┐
│_1 2│_1 3│_1 4│_1 5│_1 6│_5│_4│_3│_2│_1│0│1│2│3│4│5│1 _6│1 _5│1 _4│1 _3│1 _2│
└────┴────┴────┴────┴────┴──┴──┴──┴──┴──┴─┴─┴─┴─┴─┴─┴────┴────┴────┴────┴────┘
   NB. Some boundary values:
   (<@f"0)215 216 217 71 72 73 
┌──────┬──────┬──────┬────┬──────┬──────┐
│1 6 _1│2 _6 0│2 _6 1│6 _1│1 _6 0│1 _6 1│
└──────┴──────┴──────┴────┴──────┴──────┘

Interestingly, the rules for 6 have no special place in the function. They work that way just by handling all digits >= 6 together and only working on positive numbers.

I made another version, which doesn't rely on #.^:_1, but it's longer:

**_2&([:}.^:(0={.)+/\)@(((<.@%&12,(1,-&12)`(0,])@.(6>])@(12&|))@{.,}.)^:(0~:{.)^:_)@(,&0)@|
\$\endgroup\$
1
\$\begingroup\$

GolfScript, 69 53 49 characters

[.0<\{.12:^%.3$5+>{^-\^+\}*@1$.0<@if@^/.}do;;]-1%

GolfScript expression which takes top of the stack and replaces it by the result. You can play with the code online.

\$\endgroup\$
1
  • \$\begingroup\$ Working on a different solution which should be much shorter - but unfortunately doesn't work for several corner cases... \$\endgroup\$
    – Howard
    Commented Dec 31, 2013 at 18:42
0
\$\begingroup\$

APL(NARS), 71 chars, 142 bytes

{⍵=0:,0⋄⍵{⍵=0:⍬⋄((m+⍺×m=0)∇⌊12÷⍨⍵+q),m←t-q←12×(6<t)+(⍺>0)×6=t←12∣⍵}⍨×⍵}

test:

  z←{⍵=0:,0⋄⍵{⍵=0:⍬⋄((m+⍺×m=0)∇⌊12÷⍨⍵+q),m←t-q←12×(6<t)+(⍺>0)×6=t←12∣⍵}⍨×⍵}
  ⎕fmt z¨215 216 ¯82 0 30 ¯30 71 72 73 ¯72
┌10──────────────────────────────────────────────────────────────────────────────────────┐
│┌3──────┐ ┌3──────┐ ┌3──────┐ ┌1─┐ ┌2────┐ ┌2────┐ ┌2────┐ ┌3──────┐ ┌3──────┐ ┌3──────┐│
││ 1 6 ¯1│ │ 2 ¯6 0│ │ ¯1 5 2│ │ 0│ │ 3 ¯6│ │ ¯3 6│ │ 6 ¯1│ │ 1 ¯6 0│ │ 1 ¯6 1│ │ ¯1 6 0││
│└~──────┘ └~──────┘ └~──────┘ └~─┘ └~────┘ └~────┘ └~────┘ └~──────┘ └~──────┘ └~──────┘2
└∊───────────────────────────────────────────────────────────────────────────────────────┘
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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