diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-01-03 11:47:07 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-01-03 11:47:07 +0000 |
| commit | fb55c47fe6cba78cf8bac12ff0fc9caa1fbe44c8 (patch) | |
| tree | a5619f5107206319babdc76d8c2a1c0a6b16f26c | |
| parent | 64a0a51857212b0264b7b193830a93c3dd5ff80e (diff) | |
| download | perlweeklychallenge-club-fb55c47fe6cba78cf8bac12ff0fc9caa1fbe44c8.tar.gz perlweeklychallenge-club-fb55c47fe6cba78cf8bac12ff0fc9caa1fbe44c8.tar.bz2 perlweeklychallenge-club-fb55c47fe6cba78cf8bac12ff0fc9caa1fbe44c8.zip | |
solution to fraction tree
| -rw-r--r-- | challenge-146/james-smith/perl/ch-2.pl | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/challenge-146/james-smith/perl/ch-2.pl b/challenge-146/james-smith/perl/ch-2.pl index 2348c8b946..d3f552e849 100644 --- a/challenge-146/james-smith/perl/ch-2.pl +++ b/challenge-146/james-smith/perl/ch-2.pl @@ -9,14 +9,22 @@ use Benchmark qw(cmpthese timethis); use Data::Dumper qw(Dumper); my @TESTS = ( - [ 0, 1 ], + [ 3,5, '3/5 3/2 1/2 1/1' ], + [ 4,3, '4/3 1/3 1/2 1/1' ], + [ 101,45, '101/45 56/45 11/45 11/34 11/23 11/12 11/1 10/1 9/1 8/1 7/1 6/1 5/1 4/1 3/1 2/1 1/1' ], ); -is( my_function($_->[0]), $_->[1] ) foreach @TESTS; +is( stringify( tree($_->[0],$_->[1]) ), $_->[2] ) foreach @TESTS; done_testing(); -sub my_function { - return 1; +sub tree { + my $tree = [[ my($n,$d)=@_ ]]; + push @{$tree}, [($n,$d)=$d>$n?($n,$d-$n):($n-$d,$d)] while $n>1||$d>1; + $tree; +} + +sub stringify { + join ' ', map { "$_->[0]/$_->[1]" } @{$_[0]}; } |
