Balanced ternary: Difference between revisions
→{{header|11l}}: Static type variables and static methods are supported now
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
Alextretyak (talk | contribs) (→{{header|11l}}: Static type variables and static methods are supported now) |
||
Line 33:
R a I/ b
R (a - b + 1) I/ b
I n == 0▼
R [Int]()▼
V n3 = ((n % 3) + 3) % 3▼
I n3 == 0 {R [0] [+] BalancedTernary_int2ternary(py_idiv(n, 3))}▼
I n3 == 1 {R [1] [+] BalancedTernary_int2ternary(py_idiv(n, 3))}▼
I n3 == 2 {R [-1] [+] BalancedTernary_int2ternary(py_idiv((n + 1), 3))}▼
X RuntimeError(‘’)▼
R digs.map(d -> -d)▼
V table = [(0, -1), (1, -1), (-1, 0), (0, 0), (1, 0), (-1, 1), (0, 1)]▼
F BalancedTernary_add(a, b, =c = 0)▼
I !(!a.empty & !b.empty)▼
I c == 0▼
R I !a.empty {a} E b▼
E▼
R BalancedTernary_add([c], I !a.empty {a} E b)▼
(V d, c) = :table[3 + (I !a.empty {a[0]} E 0) + (I !b.empty {b[0]} E 0) + c]▼
V res = BalancedTernary_add(a[1..], b[1..], c)▼
I !res.empty | d != 0▼
R [d] [+] res▼
E▼
R res▼
V BalancedTernary_str2dig = [‘+’ = 1, ‘-’ = -1, ‘0’ = 0]▼
V BalancedTernary_dig2str = [1 = ‘+’, -1 = ‘-’, 0 = ‘0’]▼
T BalancedTernary
[Int] digits
Line 73 ⟶ 46:
E
X ValueError(‘BalancedTernary: Wrong input digits.’)
F :int2ternary(n)
▲ I n == 0
▲ R [Int]()
▲ V n3 = ((n % 3) + 3) % 3
▲ X RuntimeError(‘’)
F to_int()
Line 80 ⟶ 62:
I .digits.empty
R ‘0’
R reversed(.digits).map(d ->
F :neg(digs)
▲ R digs.map(d -> -d)
F -()
R BalancedTernary(.:neg(.digits))
▲ I !(!a.empty & !b.empty)
▲ I c == 0
▲ R I !a.empty {a} E b
▲ E
▲ E
▲ (V d, c) = .:table[3 + (I !a.empty {a[0]} E 0) + (I !b.empty {b[0]} E 0) + c]
▲ I !res.empty | d != 0
▲ R [d] [+] res
E
▲ R res
F +(b)
R BalancedTernary(
F -(b)
R (.) +
F *(b)
Line 94 ⟶ 96:
E
[Int] x
I a[0] == -1 {x =
E I a[0] == 0 {}
E I a[0] == 1 {x = b}
Line 100 ⟶ 102:
assert(0B)
V y = [0] [+] @_mul(a[1..], b)
R
R BalancedTernary(_mul(.digits, b.digits))
F createBalancedTernaryFromStr(inp)
R BalancedTernary(reversed(inp).map(c ->
F createBalancedTernaryFromInt(inp)
R BalancedTernary(
V a = createBalancedTernaryFromStr(‘+-0++0+’)
|