diff options
| author | hstejas <tejashs@rocketmail.com> | 2020-11-29 22:27:52 +0530 |
|---|---|---|
| committer | hstejas <tejashs@rocketmail.com> | 2020-11-29 22:30:09 +0530 |
| commit | b86ae93a0073163f62b59d076faca0a108dd430d (patch) | |
| tree | 2c8f2db21a3e607cf9a4ca28dd8995c1874ce0e2 /challenge-088 | |
| parent | e5f5309fd45b7e190c3bf574304d3b9b42ce185a (diff) | |
| download | perlweeklychallenge-club-b86ae93a0073163f62b59d076faca0a108dd430d.tar.gz perlweeklychallenge-club-b86ae93a0073163f62b59d076faca0a108dd430d.tar.bz2 perlweeklychallenge-club-b86ae93a0073163f62b59d076faca0a108dd430d.zip | |
Challenge 88
Diffstat (limited to 'challenge-088')
| -rw-r--r-- | challenge-088/hstejas/cpp/ch1.cpp | 35 | ||||
| -rwxr-xr-x | challenge-088/hstejas/perl/ch1.pl | 24 | ||||
| -rw-r--r-- | challenge-088/hstejas/perl/ch2.pl | 53 |
3 files changed, 112 insertions, 0 deletions
diff --git a/challenge-088/hstejas/cpp/ch1.cpp b/challenge-088/hstejas/cpp/ch1.cpp new file mode 100644 index 0000000000..a12760aac0 --- /dev/null +++ b/challenge-088/hstejas/cpp/ch1.cpp @@ -0,0 +1,35 @@ +#include<iostream> +#include<algorithm> +#include<vector> + +std::ostream& operator<<(std::ostream& out, const std::vector<int>& in) +{ + if(in.empty()) + return out; + + out << in[0]; + for(size_t i = 1; i < in.size(); i++) + out << ", " << in[i]; + + return out; +} + +std::vector<int> arrayOfProduct(const std::vector<int>& arr) +{ + int product = 1; + for(const int& n : arr) + product *= n; + + std::vector<int> ret(arr.size(), product); + + for(size_t i = 0; i < arr.size(); i++) + ret[i] /= arr[i]; + + return ret; +} + +int main() +{ + std::cout << arrayOfProduct({5, 2, 1, 4, 3}) << std::endl; + std::cout << arrayOfProduct({2, 1, 4, 3}) << std::endl; +}
\ No newline at end of file diff --git a/challenge-088/hstejas/perl/ch1.pl b/challenge-088/hstejas/perl/ch1.pl new file mode 100755 index 0000000000..17fc4436b4 --- /dev/null +++ b/challenge-088/hstejas/perl/ch1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use v5.30; + + +sub array_of_product { + my $n = shift; + my @m; + my $product = 1; + map { $product *= $_ } @{$n}; + + for(my $idx = 0; $idx < scalar @{$n}; $idx++) { + push @m, $product / $n->[$idx]; + } + return \@m; +} + +my @n = (5, 2, 1, 4, 3); +say join ', ', @{array_of_product(\@n)}; + +@n = (2, 1, 4, 3); +say join ', ', @{array_of_product(\@n)};
\ No newline at end of file diff --git a/challenge-088/hstejas/perl/ch2.pl b/challenge-088/hstejas/perl/ch2.pl new file mode 100644 index 0000000000..c2038bf430 --- /dev/null +++ b/challenge-088/hstejas/perl/ch2.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use v5.30; +use Data::Dumper; + + + +sub print_spiral { + my $in = shift; + my $m = scalar @{$in}; + my $n = scalar @{$in->[0]}; + my @res; + # only works with $m == $n + for my $done (0..$m) { + # Right + for(my $i = $done; $i < ($m - $done); $i++) { + push @res, $in->[$done][$i]; + } + # Down + for(my $i = $done + 1; $i < ($n - $done); $i++) { + push @res, $in->[$i][$n - $done - 1]; + } + # Left + for(my $i = $m - $done - 2; $i >= $done; $i--) { + push @res, $in->[$m - $done - 1][$i]; + } + # Up + for(my $i = $n - $done - 2; $i > $done; $i--) { + push @res, $in->[$i][$done]; + } + } + say join ', ', @res; +} + +my $in1 = [ + [ 1, 2, 3 ], + [ 4, 5, 6 ], + [ 7, 8, 9 ], +]; + +print_spiral($in1); + + +my $in2 = [ + [ 1, 2, 3, 4 ], + [ 5, 6, 7, 8 ], + [ 9, 10, 11, 12 ], + [ 13, 14, 15, 16 ], +]; + +print_spiral($in2);
\ No newline at end of file |
