2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -7,7 +7,7 @@ For this task, implement balanced ternary representation of integers with the fo
# Provide ways to convert to and from text strings, using digits '+', '-' and '0' (unless you are already using strings to represent balanced ternary; but see requirement 5).
# Provide ways to convert to and from native integer type (unless, improbably, your platform's native integer type ''is'' balanced ternary). If your native integers can't support arbitrary length, overflows during conversion must be indicated.
# Provide ways to perform addition, negation and multiplication directly on balanced ternary integers; do ''not'' convert to native integers first.
# Make your implementation efficient, with a reasonable definition of "effcient" (and with a reasonable definition of "reasonable").
# Make your implementation efficient, with a reasonable definition of "efficient" (and with a reasonable definition of "reasonable").
'''Test case''' With balanced ternaries ''a'' from string "+-0++0+", ''b'' from native integer -436, ''c'' "+-++-":
* write out ''a'', ''b'' and ''c'' in decimal notation;

View file

@ -1,10 +1,9 @@
data BalancedTernary = Bt [Int]
zeroTrim a = if null s then [0] else s where
s = f [] [] a
f x _ [] = x
f x y (0:zs) = f x (y++[0]) zs
f x y (z:zs) = f (x++y++[z]) [] zs
s = fst $ foldl f ([],[]) a
f (x,y) 0 = (x, y++[0])
f (x,y) z = (x++y++[z], [])
btList (Bt a) = a

View file

@ -5,10 +5,10 @@ class BT {
my %bt2co = %co2bt.invert;
multi method new (Str $s) {
self.bless(*, coeff => %bt2co{$s.flip.comb});
self.bless(coeff => %bt2co{$s.flip.comb});
}
multi method new (Int $i where $i >= 0) {
self.bless(*, coeff => carry $i.base(3).comb.reverse);
self.bless(coeff => carry $i.base(3).comb.reverse);
}
multi method new (Int $i where $i < 0) {
self.new(-$i).neg;
@ -35,7 +35,7 @@ multi prefix:<-> (BT $x) { $x.neg }
multi infix:<+> (BT $x, BT $y) {
my ($b,$a) = sort +*.coeff, $x, $y;
BT.new: coeff => carry $a.coeff Z+ $b.coeff, 0 xx *;
BT.new: coeff => carry ($a.coeff Z+ |$b.coeff, |(0 xx $a.coeff - $b.coeff));
}
multi infix:<-> (BT $x, BT $y) { $x + $y.neg }
@ -46,7 +46,7 @@ multi infix:<*> (BT $x, BT $y) {
my @z = 0 xx @x+@y-1;
my @safe;
for @x -> $xd {
@z = @z Z+ (@y X* $xd), 0 xx *;
@z = @z Z+ |(@y X* $xd), |(0 xx @z-@y);
@safe.push: @z.shift;
}
BT.new: coeff => carry @safe, @z;