diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-10-17 18:49:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-17 18:49:15 +0100 |
| commit | f2cb497943700a2388973d02148af213a85e67b4 (patch) | |
| tree | 7fae8974c34d5ffd6bf6398491041a7d999c39ef | |
| parent | 6848b8b1b4ddcc3f7bf637e077758d1214f1cd5d (diff) | |
| parent | e9dbf6d87ca9e091612a88bbc72abd6ba3c56831 (diff) | |
| download | perlweeklychallenge-club-f2cb497943700a2388973d02148af213a85e67b4.tar.gz perlweeklychallenge-club-f2cb497943700a2388973d02148af213a85e67b4.tar.bz2 perlweeklychallenge-club-f2cb497943700a2388973d02148af213a85e67b4.zip | |
Merge pull request #5042 from adamcrussell/challenge-134
Challenge 134
| -rw-r--r-- | challenge-134/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-134/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-134/adam-russell/cxx/ch-1.cxx | 57 | ||||
| -rw-r--r-- | challenge-134/adam-russell/cxx/ch-2.cxx | 55 | ||||
| -rw-r--r-- | challenge-134/adam-russell/nuweb/ch-1.w | 168 | ||||
| -rw-r--r-- | challenge-134/adam-russell/nuweb/ch-2.w | 164 | ||||
| -rw-r--r-- | challenge-134/adam-russell/perl/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-134/adam-russell/perl/ch-2.pl | 30 | ||||
| -rw-r--r-- | challenge-134/adam-russell/prolog/ch-1.p | 28 | ||||
| -rw-r--r-- | challenge-134/adam-russell/prolog/ch-2.p | 59 |
10 files changed, 603 insertions, 0 deletions
diff --git a/challenge-134/adam-russell/blog.txt b/challenge-134/adam-russell/blog.txt new file mode 100644 index 0000000000..0f05a0a21e --- /dev/null +++ b/challenge-134/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/2021/10/17/perl diff --git a/challenge-134/adam-russell/blog1.txt b/challenge-134/adam-russell/blog1.txt new file mode 100644 index 0000000000..3ca465e417 --- /dev/null +++ b/challenge-134/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/2021/10/17/prolog diff --git a/challenge-134/adam-russell/cxx/ch-1.cxx b/challenge-134/adam-russell/cxx/ch-1.cxx new file mode 100644 index 0000000000..4a32d8255e --- /dev/null +++ b/challenge-134/adam-russell/cxx/ch-1.cxx @@ -0,0 +1,57 @@ + + +#include<iostream> +#include<vector> +#include<algorithm> +/* +* Write a script to generate first 5 Pandigital Numbers in base 10. +*/ + + +class Pandigital { + public: + std::vector<int> first_n_pandigitals(int); + std::vector<int> first_5_pandigitals(); +}; + + +std::vector<int> Pandigital::first_n_pandigitals(int n) { + std::vector<int> pandigitals; + int x = 1000000000; + do { + int test = x; + std::vector<int> test_vector; + do { + test_vector.push_back(test % 10); + test = test / 10; + } while(test > 0); + if(std::count(test_vector.begin(), test_vector.end(), 0) && + std::count(test_vector.begin(), test_vector.end(), 1) && + std::count(test_vector.begin(), test_vector.end(), 2) && + std::count(test_vector.begin(), test_vector.end(), 3) && + std::count(test_vector.begin(), test_vector.end(), 4) && + std::count(test_vector.begin(), test_vector.end(), 5) && + std::count(test_vector.begin(), test_vector.end(), 6) && + std::count(test_vector.begin(), test_vector.end(), 7) && + std::count(test_vector.begin(), test_vector.end(), 8) && + std::count(test_vector.begin(), test_vector.end(), 9)) + pandigitals.push_back(x); + x++; + } while(pandigitals.size() < n); + return pandigitals; +} + + +std::vector<int> Pandigital::first_5_pandigitals() { + return this->first_n_pandigitals(5); +} + + +int main(int argc, char** argv) { + Pandigital p; + std::vector<int> pandigitals = p.first_5_pandigitals(); + for(int i = 0; i < pandigitals.size(); i++) { + std::cout << pandigitals[i] << std::endl; + } +} + diff --git a/challenge-134/adam-russell/cxx/ch-2.cxx b/challenge-134/adam-russell/cxx/ch-2.cxx new file mode 100644 index 0000000000..3818621da5 --- /dev/null +++ b/challenge-134/adam-russell/cxx/ch-2.cxx @@ -0,0 +1,55 @@ + + +#include<iostream> +#include<vector> +#include<algorithm> +/* +* You are given 2 positive numbers, m and n. +* Write a script to generate multiplcation table and display count of distinct terms. +*/ + + +class DistinctTermsCount { + public: + std::vector<int> compute_print(int, int); +}; + + +std::vector<int> DistinctTermsCount::compute_print(int m, int n) { + std::vector<int> distinct; + std::cout << " x | "; + for(int k = 1; k <= n; k++) { + std::cout << " " << k; + } + std::cout << std::endl; + std::cout << "---+-"; + for(int k = 1; k <= n * 2; k++) { + std::cout << "-"; + } + std::cout << std::endl; + for(int i = 1; i <= m; i++) { + std::cout << " " << i << " | "; + for(int j = 1; j <= n; j++) { + int x = i * j; + std::cout << " " << x; + if(std::count(distinct.begin(), distinct.end(), x) == 0) { + distinct.push_back(x); + } + } + std::cout << std::endl; + } + return distinct; +} + + +int main(int argc, char** argv) { + DistinctTermsCount dtc; + std::vector<int> distinct = dtc.compute_print(3, 3); + std::cout << "Distinct Terms: "; + for(int i = 0; i < distinct.size() - 1; i++) { + std::cout << distinct[i] << ", "; + } + std::cout << distinct[distinct.size() - 1] << std::endl; + std::cout << "Count: " << distinct.size() << std::endl; +} + diff --git a/challenge-134/adam-russell/nuweb/ch-1.w b/challenge-134/adam-russell/nuweb/ch-1.w new file mode 100644 index 0000000000..c0052ff3d2 --- /dev/null +++ b/challenge-134/adam-russell/nuweb/ch-1.w @@ -0,0 +1,168 @@ +\documentclass{article} + +\usepackage{listings} +\lstset{extendedchars=true , keepspaces=true , language=perl} + +\begin{document} + +Challenge 134 + +\section{Pandigital Numbers} + +Write a script to generate first 5 Pandigital Numbers in base 10. + +\section{Pandigital Numbers: Perl} +@s +@o perl/ch-1.pl +@{ + @<preamble...@> + @<first n pandigital numbers@> + @<first 5 pandigital numbers@> + @<main@> +@} + +@d preamble: use statements, introductory comments, etc. +@{ + use strict; + use warnings; + ## + # Write a script to generate first 5 Pandigital Numbers in base 10. + ## + use boolean; +@} + +@d check if pandigital +@{ + my $test = $x; + push @@{$pandigitals}, $x if + ($test =~ tr/0//d) > 0 && + ($test =~ tr/1//d) > 0 && + ($test =~ tr/2//d) > 0 && + ($test =~ tr/3//d) > 0 && + ($test =~ tr/4//d) > 0 && + ($test =~ tr/5//d) > 0 && + ($test =~ tr/6//d) > 0 && + ($test =~ tr/7//d) > 0 && + ($test =~ tr/8//d) > 0 && + ($test =~ tr/9//d) > 0; + $found = (@@{$pandigitals} == $n); +@} + +@d first n pandigital numbers +@{ + sub first_n_pandigitals{ + my($n) = @@_; + my $found = false; + my $pandigitals = []; + my $x = 1_000_000_000; + do{ + @<check if pandigital@> + $x++; + } while(!$found); + return $pandigitals; + } +@} + +@d first 5 pandigital numbers +@{ + sub first_5_pandigitals{ + return first_n_pandigitals(5); + } +@} + +@d main +@{ + MAIN:{ + my $pandigitals = first_5_pandigitals; + for my $x (@@{$pandigitals}){ + print "$x\n"; + } + } + +@} +@S + +\section{Pandigital Numbers: C++} +@s +@s +@o cxx/ch-1.cxx +@{ + @<preamble: #...@> + @<class definition@> + @<first n pandigital numbers@> + @<first 5 pandigital numbers@> + @<main@> +@} + +@d preamble: #include statements, introductory comments, etc. +@{ + #include<iostream> + #include<vector> + #include<algorithm> + /* + * Write a script to generate first 5 Pandigital Numbers in base 10. + */ +@} + +@d class definition +@{ + class Pandigital{ + public: + std::vector<int> first_n_pandigitals(int); + std::vector<int> first_5_pandigitals(); + }; +@} + +@d first 5 pandigital numbers +@{ + std::vector<int> Pandigital::first_5_pandigitals(){ + return this->first_n_pandigitals(5); + } +@} + +@d first n pandigital numbers +@{ + std::vector<int> Pandigital::first_n_pandigitals(int n){ + std::vector<int> pandigitals; + int x = 1000000000; + do{ + @<check if pandigital@> + x++; + }while(pandigitals.size() < n); + return pandigitals; + } +@} + +@d check if pandigital +@{ + int test = x; + std::vector<int> test_vector; + do{ + test_vector.push_back(test % 10); + test = test / 10; + }while(test > 0); + if(std::count(test_vector.begin(), test_vector.end(), 0) && + std::count(test_vector.begin(), test_vector.end(), 1) && + std::count(test_vector.begin(), test_vector.end(), 2) && + std::count(test_vector.begin(), test_vector.end(), 3) && + std::count(test_vector.begin(), test_vector.end(), 4) && + std::count(test_vector.begin(), test_vector.end(), 5) && + std::count(test_vector.begin(), test_vector.end(), 6) && + std::count(test_vector.begin(), test_vector.end(), 7) && + std::count(test_vector.begin(), test_vector.end(), 8) && + std::count(test_vector.begin(), test_vector.end(), 9)) + pandigitals.push_back(x); +@} + +@d main +@{ + int main(int argc, char** argv){ + Pandigital p; + std::vector<int> pandigitals = p.first_5_pandigitals(); + for(int i = 0; i < pandigitals.size(); i++){ + std::cout << pandigitals[i] << std::endl; + } + } +@} + +\end{document} diff --git a/challenge-134/adam-russell/nuweb/ch-2.w b/challenge-134/adam-russell/nuweb/ch-2.w new file mode 100644 index 0000000000..f12a074828 --- /dev/null +++ b/challenge-134/adam-russell/nuweb/ch-2.w @@ -0,0 +1,164 @@ +\documentclass{article} + +\usepackage{listings} +\lstset{extendedchars=true , keepspaces=true , language=perl} + +\begin{document} + +Challenge 134 + +\section{Distinct Terms Count} + +You are given 2 positive numbers, $m and $n. +Write a script to generate multiplcation table and display count of distinct terms. + +\section{Distinct Terms Count: Perl} +@s +@o perl/ch-2.pl +@{ + @<preamble...@> + @<compute and print table@> + @<main@> +@} + +@d preamble: use statements, introductory comments, etc. +@{ + use strict; + use warnings; + ## + # You are given 2 positive numbers, $m and $n. + # Write a script to generate multiplcation table and display count of distinct terms. + ## +@} + +@d print header +@{ + print " x | " . join(" ", (1 .. $n)) . "\n"; + print "---+-" . "-" x ($n * 2 - 1) . "\n"; +@} + +@d print row +@{ + print " $i | " . join(" ", map {$i * $_} (1 .. $n)) . "\n"; +@} + +@d compute and print table +@{ + sub compute_print{ + my($m, $n) = @@_; + my $distinct = {}; + @<print header@> + for my $i (1 .. $m){ + @<print row@> + for my $j (1 .. $n){ + $distinct->{$i * $j} = undef; + } + } + return $distinct; + } +@} + +@d main +@{ + MAIN:{ + my $distinct = compute_print(3, 3); + print "Distinct Terms: " . join(", ", sort {$a <=> $b} keys %{$distinct}) . "\n"; + print "Count: " . keys(%{$distinct}) . "\n"; + print "\n\n"; + $distinct = compute_print(3, 5); + print "Distinct Terms: " . join(", ", sort {$a <=> $b} keys %{$distinct}) . "\n"; + print "Count: " . keys(%{$distinct}) . "\n"; + } + +@} +@S + +\section{Distinct Terms Count: C++} +@s +@o cxx/ch-2.cxx +@{ + @<preamble: #...@> + @<class definition@> + @<compute and print table@> + @<main@> +@} + +@d preamble: #include statements, introductory comments, etc. +@{ + #include<iostream> + #include<vector> + #include<algorithm> + /* + * You are given 2 positive numbers, m and n. + * Write a script to generate multiplcation table and display count of distinct terms. + */ +@} + +@d class definition +@{ + class DistinctTermsCount{ + public: + std::vector<int> compute_print(int, int); + }; +@} + +@d compute and print table +@{ + std::vector<int> DistinctTermsCount::compute_print(int m, int n){ + std::vector<int> distinct; + @<print header@> + for(int i = 1; i <= m; i++){ + @<print start of row@> + for(int j = 1; j <= n; j++) + @<print row@> + } + return distinct; + } +@} + +@d print header +@{ + std::cout << " x | "; + for(int k = 1; k <= n; k++){ + std::cout << " " << k; + } + std::cout << std::endl; + std::cout << "---+-"; + for(int k = 1; k <= n * 2; k++){ + std::cout << "-"; + } + std::cout << std::endl; +@} + +@d print start of row +@{ + std::cout << " " << i << " | "; +@} + +@d print row +@{ + { + int x = i * j; + std::cout << " " << x; + if(std::count(distinct.begin(), distinct.end(), x) == 0) { + distinct.push_back(x); + } + } + std::cout << std::endl; +@} + +@d main +@{ + int main(int argc, char** argv){ + DistinctTermsCount dtc; + std::vector<int> distinct = dtc.compute_print(3, 3); + std::cout << "Distinct Terms: "; + for(int i = 0; i < distinct.size() - 1; i++){ + std::cout << distinct[i] << ", "; + } + std::cout << distinct[distinct.size() - 1] << std::endl; + std::cout << "Count: " << distinct.size() << std::endl; + } +@} + +\end{document}
\ No newline at end of file diff --git a/challenge-134/adam-russell/perl/ch-1.pl b/challenge-134/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..8ac31c3031 --- /dev/null +++ b/challenge-134/adam-russell/perl/ch-1.pl @@ -0,0 +1,40 @@ +use strict; +use warnings; +## +# Write a script to generate first 5 Pandigital Numbers in base 10. +## +use boolean; + +sub first_n_pandigitals { + my ($n) = @_; + my $found = false; + my $pandigitals = []; + my $x = 1_000_000_000; + do { + my $test = $x; + push @{$pandigitals}, $x + if ( $test =~ tr/0//d ) > 0 + && ( $test =~ tr/1//d ) > 0 + && ( $test =~ tr/2//d ) > 0 + && ( $test =~ tr/3//d ) > 0 + && ( $test =~ tr/4//d ) > 0 + && ( $test =~ tr/5//d ) > 0 + && ( $test =~ tr/6//d ) > 0 + && ( $test =~ tr/7//d ) > 0 + && ( $test =~ tr/8//d ) > 0 + && ( $test =~ tr/9//d ) > 0; + $found = ( @{$pandigitals} == $n ); + $x++; + } while ( !$found ); + return $pandigitals; +} + +sub first_5_pandigitals { + return first_n_pandigitals(5); +} +MAIN: { + my $pandigitals = first_5_pandigitals; + for my $x ( @{$pandigitals} ) { + print "$x\n"; + } +} diff --git a/challenge-134/adam-russell/perl/ch-2.pl b/challenge-134/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..ef5b41615b --- /dev/null +++ b/challenge-134/adam-russell/perl/ch-2.pl @@ -0,0 +1,30 @@ +use strict; +use warnings; +## +# You are given 2 positive numbers, $m and $n. +# Write a script to generate multiplcation table and display count of distinct terms. +## +sub compute_print { + my ( $m, $n ) = @_; + my $distinct = {}; + print " x | " . join( " ", ( 1 .. $n ) ) . "\n"; + print "---+-" . "-" x ( $n * 2 - 1 ) . "\n"; + for my $i ( 1 .. $m ) { + print " $i | " . join( " ", map { $i * $_ } ( 1 .. $n ) ) . "\n"; + for my $j ( 1 .. $n ) { + $distinct->{ $i * $j } = undef; + } + } + return $distinct; +} +MAIN: { + my $distinct = compute_print( 3, 3 ); + print "Distinct Terms: " + . join( ", ", sort { $a <=> $b } keys %{$distinct} ) . "\n"; + print "Count: " . keys( %{$distinct} ) . "\n"; + print "\n\n"; + $distinct = compute_print( 3, 5 ); + print "Distinct Terms: " + . join( ", ", sort { $a <=> $b } keys %{$distinct} ) . "\n"; + print "Count: " . keys( %{$distinct} ) . "\n"; +} diff --git a/challenge-134/adam-russell/prolog/ch-1.p b/challenge-134/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..62d13baa3c --- /dev/null +++ b/challenge-134/adam-russell/prolog/ch-1.p @@ -0,0 +1,28 @@ +:-initialization(first_5_pandigitals). + +all_unique(_, []). +all_unique(L, [V|T]):- + fd_exactly(1, L, V), + all_unique(L, T). + +pandigital(Pandigitals):- + Digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + Pandigitals = [A, B, C, D, E, F, G, H, I, J], + fd_domain([A, B, C, D, E, F, G, H, I, J], Digits), + A #= 1, + B #= 0, + all_unique([A, B, C, D, E, F, G, H, I, J], Digits), + fd_labeling(Pandigitals). + +first_5_pandigitals:- + setof(P, pandigital(P), Pandigitals), + sort(Pandigitals, [A, B, C, D, E | _ ]), + print_pandigitals([A, B, C, D, E]). + +print_pandigitals([]). +print_pandigitals([H|T]):- + maplist(number_codes, H, Codes), + flatten(Codes, DigitCodes), + number_codes(Number, DigitCodes), + write(Number), nl, + print_pandigitals(T).
\ No newline at end of file diff --git a/challenge-134/adam-russell/prolog/ch-2.p b/challenge-134/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..5b0bd6e8e6 --- /dev/null +++ b/challenge-134/adam-russell/prolog/ch-2.p @@ -0,0 +1,59 @@ +:-initialization(main). + +table(M, N, K):- + between(1, M, I), + between(1, N, J), + K is I * J. + +print_table(M, N, Distinct):- + findall(K, table(M, N, K), Ks), + setof(K, table(M, N, K), Distinct), + print_header(M, N), + print_seperator(M, N), + print_rows(M, N, Ks), + maplist(number_chars, Distinct, DistinctRow), + maplist(add_space, DistinctRow, DistinctSpaced), + flatten(DistinctSpaced, DistinctSpacedFlattened), + format("~nDistinct Terms: ~S~n", [DistinctSpacedFlattened]), + length(Distinct, Count), + format("Count: ~d ~n", [Count]). + +print_rows(0, _, _). +print_rows(M, N, Products):- + length(Row, N), + append(Rest, Row, Products), + M0 is M - 1, + print_rows(M0, N, Rest), + maplist(number_chars, Row, RowChars), + maplist(add_space, RowChars, CharsSpaced), + flatten(CharsSpaced, CharsSpacedFlattened), + format(" ~n ~d | ~S ", [M, CharsSpacedFlattened]). + +print_header(_, N):- + format(" x | ", _), + print_header(N). +print_header(0). +print_header(N):- + N0 is N - 1, + print_header(N0), + format("~d ", [N]). + +print_seperator(_, N):- + format("~n --+-", _), + print_seperator(N). +print_seperator(0). +print_seperator(N):- + N0 is N - 1, + print_seperator(N0), + format("~a", ['--']). + +add_space(C, CS):- + flatten(C, F), + append(F, [' '], FS), + atom_chars(A, FS), + atom_chars(A, CS). + +main:- + print_table(3, 3, _), nl, nl, + print_table(3, 5, _), + halt.
\ No newline at end of file |
