diff options
| -rw-r--r-- | challenge-023/yet-ebreo/perl5/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-023/yet-ebreo/perl5/ch-2.pl | 42 | ||||
| -rw-r--r-- | challenge-023/yet-ebreo/perl5/ch-3.pl | 28 | ||||
| -rw-r--r-- | challenge-023/yet-ebreo/perl6/ch-1.p6 | 19 | ||||
| -rw-r--r-- | challenge-023/yet-ebreo/perl6/ch-2.p6 | 31 |
5 files changed, 143 insertions, 0 deletions
diff --git a/challenge-023/yet-ebreo/perl5/ch-1.pl b/challenge-023/yet-ebreo/perl5/ch-1.pl new file mode 100644 index 0000000000..36198da740 --- /dev/null +++ b/challenge-023/yet-ebreo/perl5/ch-1.pl @@ -0,0 +1,23 @@ +# Create a script that prints nth order forward difference series. +# You should be a able to pass the list of numbers and order number as command line parameters. +# Let me show you with an example. +# Suppose we have list (X) of numbers: 5, 9, 2, 8, 1, 6 and we would like to +# create 1st order forward difference series (Y). So using the formula Y(i) = X(i+1) - X(i), +# we get the following numbers: (9-5), (2-9), (8-2), (1-8), (6-1). In short, +# the final series would be: 4, -7, 6, -7, 5. If you noticed, it has one less number +# than the original series. Similary you can carry on 2nd order forward difference series like: +# (-7-4), (6+7), (-7-6), (5+7) => -11, 13, -13, 12. + +use strict; +use warnings; +use 5.010; + +my ($n, @list) = @ARGV; + +die "Usage: ch-1.pl <n> <list>\n\tn must be less than the number of elements in list\n\tlist values are space separated\n" if !@ARGV; +die "n($n) must be less than the number(".@list.") of elements in list\n" if $n>= @list; + + +(@list = map $list[$_]-$list[$_-1],1..$#list) for 1..$n; + +say "@list";
\ No newline at end of file diff --git a/challenge-023/yet-ebreo/perl5/ch-2.pl b/challenge-023/yet-ebreo/perl5/ch-2.pl new file mode 100644 index 0000000000..8af3986a73 --- /dev/null +++ b/challenge-023/yet-ebreo/perl5/ch-2.pl @@ -0,0 +1,42 @@ +# Create a script that prints Prime Decomposition of a given number. +# The prime decomposition of a number is defined as a list of prime numbers +# which when all multiplied together, are equal to that number. For example, +# the Prime decomposition of 228 is 2,2,3,19 as 228 = 2 * 2 * 3 * 19. +use strict; +use warnings; +use 5.010; + +my @r; +my $n = $ARGV[0]; +my $m = $n; +die "Usage: ch-2.pl <n> \n\tn is a postive number\n" if !@ARGV; +die "n must be a postive number\n" if $n<1; + +#Backticks solution using factor, works both on windows and linux :D +say "Using backticks and factor system command:"; +say `factor $n`=~s/.+: //r; + +#Using ntheory module +say "Using module nthoery"; +use ntheory 'factor'; +@r = factor $m; +say "@r\n"; + +#But I feel like I should do a non-backtick/module solution so here we go: +#It's slow(and inaccurate) on very large numbers +say "Using non-backticks solution (trial division/modulo):"; +@r = (); +while ($n % 2<1) { + push @r, 2; + $n /= 2; +} +my $f = 3; +while ($f*$f<=$n) { + while ($n % $f<1) { + push @r, $f; + $n /= $f; + } + $f+=2; +} +$n>1 && push @r, $n; +say "@r\n"; diff --git a/challenge-023/yet-ebreo/perl5/ch-3.pl b/challenge-023/yet-ebreo/perl5/ch-3.pl new file mode 100644 index 0000000000..7d4bd97110 --- /dev/null +++ b/challenge-023/yet-ebreo/perl5/ch-3.pl @@ -0,0 +1,28 @@ +# Write a script to use Random Poems API. This is the easiset API, +# I have come across so far. You don’t need API key for this. +# They have only route to work with (GET). +# The API task is optional but we would love to see your solution. + +use strict; +use warnings; +use 5.010; + +use LWP::Simple qw(get); +use JSON; +binmode STDOUT, ":encoding(UTF-8)"; + +my $api_content = get "https://www.poemist.com/api/v1/randompoems"; +my @data; +push @data, @{$_} for JSON->new->utf8->decode($api_content); + +#Printing the info of the first poem +say "$data[0]{title} - $data[0]{poet}{name} - $data[0]{url}\n"; + +#Accessing all(5) poems +# for (@data) { +# say "$_->{title} - $_->{poet}{name} - $_->{url}"; +# } + +#Printing the content of the first poem +say "$data[0]{content}"; + diff --git a/challenge-023/yet-ebreo/perl6/ch-1.p6 b/challenge-023/yet-ebreo/perl6/ch-1.p6 new file mode 100644 index 0000000000..2bfc4c59df --- /dev/null +++ b/challenge-023/yet-ebreo/perl6/ch-1.p6 @@ -0,0 +1,19 @@ +# Create a script that prints nth order forward difference series. +# You should be a able to pass the list of numbers and order number as command line parameters. +# Let me show you with an example. +# Suppose we have list (X) of numbers: 5, 9, 2, 8, 1, 6 and we would like to +# create 1st order forward difference series (Y). So using the formula Y(i) = X(i+1) - X(i), +# we get the following numbers: (9-5), (2-9), (8-2), (1-8), (6-1). In short, +# the final series would be: 4, -7, 6, -7, 5. If you noticed, it has one less number +# than the original series. Similary you can carry on 2nd order forward difference series like: +# (-7-4), (6+7), (-7-6), (5+7) => -11, 13, -13, 12. + +my ($n, @list) = @*ARGS; + +die "Usage: ch-1.p6 <n> <list>\n\tn\tmust be less than the number of elements in list\n\tlist\tvalues are space separated\n" if !@*ARGS; +die "n($n) must be less than the number("~@list.end+1~") of elements in list\n" if $n> @list.end; + + +(@list = map {@list[$_]-@list[$_-1]},1..@list.end) for 1..$n; + +say "@list[]"; diff --git a/challenge-023/yet-ebreo/perl6/ch-2.p6 b/challenge-023/yet-ebreo/perl6/ch-2.p6 new file mode 100644 index 0000000000..07c92eaf53 --- /dev/null +++ b/challenge-023/yet-ebreo/perl6/ch-2.p6 @@ -0,0 +1,31 @@ +# Create a script that prints Prime Decomposition of a given number. +# The prime decomposition of a number is defined as a list of prime numbers +# which when all multiplied together, are equal to that number. For example, +# the Prime decomposition of 228 is 2,2,3,19 as 228 = 2 * 2 * 3 * 19. + + +my $n = @*ARGS[0]; +my $m = $n; +die "Usage: ch-2.p6 <n> \n\tn is a postive number\n" if !@*ARGS; +die "n must be a postive number\n" if $n < 1; + +#Backticks solution using factor, works both on windows and linux :D +say "Using backticks and factor system command:"; +say qqx{factor $n}.subst(/.+\:\s/, ''); + +say "Using non-backticks solution (trial division/modulo):"; +my @r; +while ($n % 2 < 1) { + push @r, 2; + $n /= 2; +} +my $f = 3; +while ($f*$f <= $n) { + while ($n % $f < 1) { + push @r, $f; + $n /= $f; + } + $f+=2; +} +$n > 1 && push @r, $n; +say "@r[]\n"; |
