diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-11-19 11:05:57 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-11-19 11:05:57 +0000 |
| commit | bde0adaf7b8dfe99c4e494c932d8702eb8cf9a56 (patch) | |
| tree | 3f6e35b8583ff50174ec7fac6e2b8b60c463efe6 /challenge-191 | |
| parent | 1e1f2f3f3e08eba010e55eccb5678e215dfce7e9 (diff) | |
| download | perlweeklychallenge-club-bde0adaf7b8dfe99c4e494c932d8702eb8cf9a56.tar.gz perlweeklychallenge-club-bde0adaf7b8dfe99c4e494c932d8702eb8cf9a56.tar.bz2 perlweeklychallenge-club-bde0adaf7b8dfe99c4e494c932d8702eb8cf9a56.zip | |
- Added more solutions by Robert DiCicco.
Diffstat (limited to 'challenge-191')
| -rw-r--r-- | challenge-191/robert-dicicco/julia/ch-2.jl | 189 | ||||
| -rw-r--r-- | challenge-191/robert-dicicco/perl/ch-2.pl | 181 | ||||
| -rw-r--r-- | challenge-191/robert-dicicco/raku/ch-2.raku | 159 | ||||
| -rw-r--r-- | challenge-191/robert-dicicco/ruby/ch-2.rb | 163 |
4 files changed, 692 insertions, 0 deletions
diff --git a/challenge-191/robert-dicicco/julia/ch-2.jl b/challenge-191/robert-dicicco/julia/ch-2.jl new file mode 100644 index 0000000000..bb3f6973c7 --- /dev/null +++ b/challenge-191/robert-dicicco/julia/ch-2.jl @@ -0,0 +1,189 @@ +#!/usr/bin/env julia + + + +#= + +AUTHOR: Robert DiCicco + +DATE: 2022-11-17 + +Challenge 191 Cute List ( Julia ) + + + +You are given an integer, 0 < $n <= 15. + + + +Write a script to find the number of orderings of numbers that form a cute list. + + + +With an input @list = (1, 2, 3, .. $n) for positive integer $n, an ordering of @list is cute if for every entry, indexed with a base of 1, either + + + +1) $list[$i] is evenly divisible by $i + +or + +2) $i is evenly divisible by $list[$i] + + + +Example + + + +Input: $n = 2 + +Ouput: 2 + + + +Since $n = 2, the list can be made up of two integers only i.e. 1 and 2. + +Therefore we can have two list i.e. (1,2) and (2,1). + + + +@list = (1,2) is cute since $list[1] = 1 is divisible by 1 and $list[2] = 2 is divisible by 2. + +=# + + + +using Combinatorics + +using Printf + + + +function checkArgs(args) + + global num + + try + + num = parse(Int64, args) + + catch + + println("Error: Argument must be an integer") + + exit(0) + + finally + + if num < 1 || num > 15 + + println("Error: Argument must be > 0 and less than 16") + + exit(0) + + else + + main(num) + + end + + end + +end + + + +function main(arg) + +arr = collect(1:arg) + +@printf("\n\nInput: %s\n", arg) + +p = collect(permutations(arr,length(arr))) + +for suba in p + + res = 0 + + res2 = 0 + + success = 0 + + for x in 1:length(suba) + + res = suba[x] % x + + res2 = x % suba[x] + + if ((res == 0) || (res2 == 0) ) + + success += 1 + + end + + if success == length(suba) + + @printf("%s is cute!\n", suba) + + end + + end + +end + +end + + + +for mynum in ARGS + + checkArgs(mynum) + +end + + + +#= + +SAMPLE OUTPUT + +julia .\CuteList.jl 2 3 4 + +Input: 2 + +[1, 2] is cute! + +[2, 1] is cute! + + + +Input: 3 + +[1, 2, 3] is cute! + +[2, 1, 3] is cute! + +[3, 2, 1] is cute! + + + +Input: 4 + +[1, 2, 3, 4] is cute! + +[1, 4, 3, 2] is cute! + +[2, 1, 3, 4] is cute! + +[2, 4, 3, 1] is cute! + +[3, 2, 1, 4] is cute! + +[3, 4, 1, 2] is cute! + +[4, 1, 3, 2] is cute! + +[4, 2, 3, 1] is cute! + +=# diff --git a/challenge-191/robert-dicicco/perl/ch-2.pl b/challenge-191/robert-dicicco/perl/ch-2.pl new file mode 100644 index 0000000000..8510eee8b7 --- /dev/null +++ b/challenge-191/robert-dicicco/perl/ch-2.pl @@ -0,0 +1,181 @@ +#!/usr/bin/env perl + + + +=begin pod + +AUTHOR: Robert DiCicco + +DATE: 2022-11-17 + +Challenge 191 Cute List ( Perl ) + + + +You are given an integer, 0 < $n <= 15. + + + +Write a script to find the number of orderings of numbers that form a cute list. + + + +With an input @list = (1, 2, 3, .. $n) for positive integer $n, an ordering of @list is cute if for every entry, indexed with a base of 1, either + + + +1) $list[$i] is evenly divisible by $i + +or + +2) $i is evenly divisible by $list[$i] + + + +Example + + + +Input: $n = 2 + +Ouput: 2 + + + +Since $n = 2, the list can be made up of two integers only i.e. 1 and 2. + +Therefore we can have two list i.e. (1,2) and (2,1). + + + +@list = (1,2) is cute since $list[1] = 1 is divisible by 1 and $list[2] = 2 is divisible by 2. + +=cut + + + +use strict; + +use warnings; + +use Algorithm::Permute; + +use feature qw/say/; + + + +my @arr = ([1,2,3], [1,2,3,4], [1,2,3,4,5]); + + + +sub main { + + my $arg = shift; + + my @a = (1..$arg); + + my $len = scalar(@a); + + say "Input: $len"; + + my $p = Algorithm::Permute->new(\@a, $len); + + while ( my @suba = $p->next) { + + my $res = 0; + + my $res2 = 0; + + my $success = 0; + + for my $x (0..$len-1){ + + $res = $suba[$x] % ($x+1); + + $res2 = ($x+1) % $suba[$x]; + + if (($res == 0) || ($res2 == 0) ) { + + $success++; + + } + + if ($success == $len) { + + print("\[@suba\] is cute!\n"); + + } + + } + + } + + print("\n"); + +} + + + +for my $arg (@ARGV) { + + if (( $arg <1) || ($arg > 15)) { + + say "Error: supplied arg = $arg. arg must be greater than 0 and less than 16"; + + exit(1); + + } + + main($arg); + +} + + + +=begin pod + +SAMPLE OUTPUT + + + +perl .\CuteList.pl 2 3 4 + +Input: 2 + +[2 1] is cute! + +[1 2] is cute! + + + +Input: 3 + +[3 2 1] is cute! + +[2 1 3] is cute! + +[1 2 3] is cute! + + + +Input: 4 + +[3 2 1 4] is cute! + +[4 2 3 1] is cute! + +[2 4 3 1] is cute! + +[2 1 3 4] is cute! + +[3 4 1 2] is cute! + +[4 1 3 2] is cute! + +[1 4 3 2] is cute! + +[1 2 3 4] is cute! + + + +=cut diff --git a/challenge-191/robert-dicicco/raku/ch-2.raku b/challenge-191/robert-dicicco/raku/ch-2.raku new file mode 100644 index 0000000000..e19cf11c46 --- /dev/null +++ b/challenge-191/robert-dicicco/raku/ch-2.raku @@ -0,0 +1,159 @@ +use v6; + +#`{ + + AUTHOR: Robert DiCicco + + DATE: 2022-11-17 + + Challenge 191 Cute List ( Raku ) + + + + You are given an integer, 0 < $n <= 15. + + + + Write a script to find the number of orderings of numbers that form a cute list. + + + + With an input @list = (1, 2, 3, .. $n) for positive integer $n, an ordering of @list is cute if for every entry, indexed with a base of 1, either + + + + 1) $list[$i] is evenly divisible by $i + + or + + 2) $i is evenly divisible by $list[$i] + + + + Example + + + + Input: $n = 2 + + Ouput: 2 + + + + Since $n = 2, the list can be made up of two integers only i.e. 1 and 2. + + Therefore we can have two list i.e. (1,2) and (2,1). + + + + @list = (1,2) is cute since $list[1] = 1 is divisible by 1 and $list[2] = 2 is divisible by 2. + + + +} + + + +sub main($n) { + + print "Input: n = $n\n"; + + my @arr = 1..$n; + + my $len = @arr.elems; + + for @arr.permutations -> @suba { + + my $res = 0; + + my $res2 = 0; + + my $success = 0; + + for 0..($len-1) -> $x { + + $res = @suba[$x] % ($x+1); + + $res2 = ($x+1) % @suba[$x]; + + if (($res == 0) || ($res2 == 0)) { + + $success++; + + } + + if ($success == $len) { + + print "[" ~ @suba ~ "\] is cute!\n"; + + } + + } + + } + + put " "; + +} + + + +for @*ARGS -> $arg { + + if (( $arg < 1 ) || ( $arg > 15 )) { + + say "Error: supplied argument = $arg. Argument must be greater than 0 and less than 16"; + + exit(1); + + } + + main($arg); + +} + + + +#`{ + +SAMPLE OUTPUT + +raku .\CuteList.rk 2 3 4 + +Input: n = 2 + +[1 2] is cute! + +[2 1] is cute! + + + +Input: n = 3 + +[1 2 3] is cute! + +[2 1 3] is cute! + +[3 2 1] is cute! + + + +Input: n = 4 + +[1 2 3 4] is cute! + +[1 4 3 2] is cute! + +[2 1 3 4] is cute! + +[2 4 3 1] is cute! + +[3 2 1 4] is cute! + +[3 4 1 2] is cute! + +[4 1 3 2] is cute! + +[4 2 3 1] is cute! + +} diff --git a/challenge-191/robert-dicicco/ruby/ch-2.rb b/challenge-191/robert-dicicco/ruby/ch-2.rb new file mode 100644 index 0000000000..97fb0b01be --- /dev/null +++ b/challenge-191/robert-dicicco/ruby/ch-2.rb @@ -0,0 +1,163 @@ +#!/usr/bin/env ruby + + + +=begin + +AUTHOR: Robert DiCicco + +DATE: 2022-11-17 + +Challenge 191 Cute List ( Ruby ) + + + +You are given an integer, 0 < $n <= 15. + + + +Write a script to find the number of orderings of numbers that form a cute list. + + + +With an input @list = (1, 2, 3, .. $n) for positive integer $n, an ordering of @list is cute if for every entry, indexed with a base of 1, either + + + +1) $list[$i] is evenly divisible by $i + +or + +2) $i is evenly divisible by $list[$i] + + + +Example + + + +Input: $n = 2 + +Ouput: 2 + + + +Since $n = 2, the list can be made up of two integers only i.e. 1 and 2. + +Therefore we can have two list i.e. (1,2) and (2,1). + + + +@list = (1,2) is cute since $list[1] = 1 is divisible by 1 and $list[2] = 2 is divisible by 2. + +=end + + + +def main(n) + + arr = (1..n.to_i).to_a + + len = arr.length() + + puts "Input: #{len}" + + arr.permutation(len).to_a.each do |suba| + + res = 0 + + res2 = 0 + + success = 0 + + (0..len-1).each do |x| + + res = suba[x] % (x+1); + + res2 = (x+1) % suba[x]; + + if ((res == 0) || (res2 == 0) ) + + success += 1; + + end + + if (success == len) + + puts("\[#{suba}\] is cute!"); + + end + + end + + end + + puts " " + +end + + + +for arg in ARGV + + #puts arg + + if (( arg.to_i < 1) || (arg.to_i > 15)) + + puts "Error: supplied arg = #{arg}. arg must be greater than 0 and less than 16"; + + exit; + + end + + main(arg) + +end + + + +=begin + +SAMPLE OUTPUT + + + +ruby .\CuteList.rb 2 3 4 + +Input: 2 + +[[1, 2]] is cute! + +[[2, 1]] is cute! + + + +Input: 3 + +[[1, 2, 3]] is cute! + +[[2, 1, 3]] is cute! + +[[3, 2, 1]] is cute! + + + +Input: 4 + +[[1, 2, 3, 4]] is cute! + +[[1, 4, 3, 2]] is cute! + +[[2, 1, 3, 4]] is cute! + +[[2, 4, 3, 1]] is cute! + +[[3, 2, 1, 4]] is cute! + +[[3, 4, 1, 2]] is cute! + +[[4, 1, 3, 2]] is cute! + +[[4, 2, 3, 1]] is cute! + +=end |
