diff options
| author | Adam Russell <ac.russell@live.com> | 2020-05-17 11:59:27 -0400 |
|---|---|---|
| committer | Adam Russell <ac.russell@live.com> | 2020-05-17 11:59:27 -0400 |
| commit | 315a1d397aadef8d5334cc9ef201119e6685f92f (patch) | |
| tree | 5fa151a6d49475ca26e116161066f9da89e39c90 /challenge-060 | |
| parent | 60ed360f69357766b595f9b47fb07c16e95913e3 (diff) | |
| download | perlweeklychallenge-club-315a1d397aadef8d5334cc9ef201119e6685f92f.tar.gz perlweeklychallenge-club-315a1d397aadef8d5334cc9ef201119e6685f92f.tar.bz2 perlweeklychallenge-club-315a1d397aadef8d5334cc9ef201119e6685f92f.zip | |
solutions for challenge 060
Diffstat (limited to 'challenge-060')
| -rw-r--r-- | challenge-060/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-060/adam-russell/perl/ch-1.pl | 37 | ||||
| -rw-r--r-- | challenge-060/adam-russell/perl/ch-2.pl | 39 | ||||
| -rw-r--r-- | challenge-060/adam-russell/prolog/ch-1.p | 43 | ||||
| -rw-r--r-- | challenge-060/adam-russell/prolog/ch-2.p | 54 |
5 files changed, 174 insertions, 0 deletions
diff --git a/challenge-060/adam-russell/blog.txt b/challenge-060/adam-russell/blog.txt new file mode 100644 index 0000000000..a1105934d6 --- /dev/null +++ b/challenge-060/adam-russell/blog.txt @@ -0,0 +1 @@ +https://adamcrussell.livejournal.com/16590.html diff --git a/challenge-060/adam-russell/perl/ch-1.pl b/challenge-060/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..01a8fa1cf3 --- /dev/null +++ b/challenge-060/adam-russell/perl/ch-1.pl @@ -0,0 +1,37 @@ +use strict; +use warnings; +## +# Write a script that accepts a number and returns +# the Excel Column Name it represents and vice-versa. +## +sub number_to_column{ + my($n) = @_; + my $s = ""; + while($n > 0){ + $s .= "A" if $n > 26; + $s .= chr($n + 64) if $n < 26; + $n -= 26; + } + return $s; +} + +sub column_to_number{ + my($s) = @_; + my $n = 0; + for my $c (split(//, $s)){ + $n += 1 if $c eq "A" && length($s) == 1; + $n += 26 if $c eq "A" && length($s) != 1; + $n += ord($c) - 64 if $c ne "A"; + } + return $n; +} + +MAIN:{ + print number_to_column(28) . "\n"; + print number_to_column(80) . "\n"; + print number_to_column(30) . "\n"; + print column_to_number("A") . "\n"; + print column_to_number("Z") . "\n"; + print column_to_number("AB") . "\n"; + print column_to_number("AAAZ") . "\n"; +} diff --git a/challenge-060/adam-russell/perl/ch-2.pl b/challenge-060/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..c48b86b830 --- /dev/null +++ b/challenge-060/adam-russell/perl/ch-2.pl @@ -0,0 +1,39 @@ +use strict; +use warnings; +## +# Write a script that accepts list of positive numbers +# and two positive numbers, a number of digits and a +# maximum value. The script should print all possible +# numbers made by concatenating the numbers from the +# list whose length is exactly the number of digits +# and value is less than the maximum. +## +use List::MoreUtils q/uniq/; +use Math::Prime::Util q/forperm/;; +use Algorithm::Combinatorics q/combinations_with_repetition/; + +sub find_numbers{ + my($numbers, $digits, $maximum) = @_; + my @results; + my @combinations = combinations_with_repetition($numbers, $digits); + for my $c (@combinations){ + forperm { + my @p = @{$c}[@_]; + my $i = 0; + my $d = pop @p; + my $sum = 0; + while(defined($d)){ + $sum += ($d * (10 ** $i)); + $i++; + $d = pop @p; + } + push @results, $sum if($sum < $maximum && length($sum) == $digits); + }@{$c}; + } + return uniq sort {$a <=> $b} @results; +} + +MAIN:{ + my @results = find_numbers([0,1,2,5], 2, 21); + print "@results"; +} diff --git a/challenge-060/adam-russell/prolog/ch-1.p b/challenge-060/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..6e74ba3cdf --- /dev/null +++ b/challenge-060/adam-russell/prolog/ch-1.p @@ -0,0 +1,43 @@ +number_column-label(X, L):- + number_column-label(X, "", L). +number_column-label(0, L, L). +number_column-label(X, C, L):- + (X > 26) -> + string_concat(C, "A", CAlpha), + number_column-label(X - 26, CAlpha, L); + N is X + 64, + char_code(Alpha, N), + string_concat(C, Alpha, CAlpha), + number_column-label(0, CAlpha, L). + +column-label_number(X, L):- + string_chars(L, Chars), + column-label_number(X, 0, Chars). +column-label_number(X, X, []). + column-label_number(X, C, [A | T]):- + (A = 'A', C = 0, length(T,0)) -> + column-label_number(X, 1, T); + (A = 'A') -> + C0 is C + 26, + column-label_number(X, C0, T); + char_code(A, Alpha), + C0 is Alpha - 64, + C1 is C + C0, + column-label_number(X, C1, T). + +main:- + number_column-label(5, S0), + writeln(S0), + number_column-label(28, S1), + writeln(S1), + number_column-label(80, S2), + writeln(S2), + number_column-label(30, S3), + writeln(S3), + column-label_number(X0, 'A'), + writeln(X0), + column-label_number(X1, S3), + writeln(X1), + column-label_number(X2, 'AAAZ'), + writeln(X2), + halt. diff --git a/challenge-060/adam-russell/prolog/ch-2.p b/challenge-060/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..6e4ef5f72d --- /dev/null +++ b/challenge-060/adam-russell/prolog/ch-2.p @@ -0,0 +1,54 @@ +number_digits(X, L):- + number_digits(X, 0, L). +number_digits(0, L, L):- !, true. +number_digits(X, S, A):- + L is S + 1, + Z is div(X, 10), + number_digits(Z, L, A). + +number_list(X, L):- + number_list(X, [], L). +number_list(0, L, L):- !, true. +number_list(X, S, A):- + Z is rem(X, 10), + Y is div(X, 10), + number_list(Y, [Z|S], A). + +list_number(L, N):- + list_number(L, 0, N). +list_number([], N, N). +list_number([Digit| T], Sum, N):- + length(T, D), + S is Sum + (Digit * (10 ^ D)), + list_number(T, S, N). + +permutation(List,[H|Permutation]):-delete(H,List,Rest),permutation(Rest,Permutation). +permutation([],[]). + +delete(X,[X|T],T). +delete(X,[H|T],[H|NT]):-delete(X,T,NT). + +combinations_of_length(_,[]). +combinations_of_length([X|T],[X|Combinations]):- + combinations_of_length([X|T],Combinations). +combinations_of_length([_|T],[X|Combinations]):- + combinations_of_length(T,[X|Combinations]). + +find_numbers(Digits, Length, Max, Matches):- + length(L, Length), + combinations_of_length(Digits,L), + permutation(L,LP), + find_numbers(LP, Length, Max, [], Matches). + +find_numbers([], _, _,Matches, Matches). +find_numbers(Digits, Length, Max, Acc, Matches):- + list_number(Digits, X), + number_digits(X, L), + L = Length, + X < Max, + find_numbers([], Length, Max, [X|Acc], Matches). + +main:- + setof(Matches, find_numbers([0,1,2,5], 2, 21, Matches), All_Matches), + writeln(All_Matches), + halt. |
