diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-05-08 08:04:08 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-05-08 08:04:08 +0100 |
| commit | 2814f8bf07f0bf1b654057526b6cdfd760057fb1 (patch) | |
| tree | 710d312e0720c6df3bcf1ad9b5c256ec92f2ada1 /challenge-163 | |
| parent | 3bf06ce0bf215eadf1248a2f292b5968d239ef4d (diff) | |
| parent | 33eb8631c58b8a4510362e5563eec0ba86c9c9e0 (diff) | |
| download | perlweeklychallenge-club-2814f8bf07f0bf1b654057526b6cdfd760057fb1.tar.gz perlweeklychallenge-club-2814f8bf07f0bf1b654057526b6cdfd760057fb1.tar.bz2 perlweeklychallenge-club-2814f8bf07f0bf1b654057526b6cdfd760057fb1.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-163')
| -rw-r--r-- | challenge-163/cheok-yin-fung/julia/ch-2.jl | 24 | ||||
| -rw-r--r-- | challenge-163/cheok-yin-fung/perl/ch-1.pl | 28 | ||||
| -rw-r--r-- | challenge-163/cheok-yin-fung/perl/ch-2.pl | 31 | ||||
| -rw-r--r-- | challenge-163/cheok-yin-fung/python/ch-2.py | 37 | ||||
| -rw-r--r-- | challenge-163/mohammad-anwar/perl/ch-1.pl | 41 | ||||
| -rw-r--r-- | challenge-163/mohammad-anwar/perl/ch-2.pl | 44 |
6 files changed, 205 insertions, 0 deletions
diff --git a/challenge-163/cheok-yin-fung/julia/ch-2.jl b/challenge-163/cheok-yin-fung/julia/ch-2.jl new file mode 100644 index 0000000000..61b164b10a --- /dev/null +++ b/challenge-163/cheok-yin-fung/julia/ch-2.jl @@ -0,0 +1,24 @@ +# The Weekly Challenge 163 +# Task 2 Summation +# Julia Solution + +function compute(arr) + v = arr + NUM = length(arr) + new_v = [] + + for r in 1:(NUM-1) + new_v = [] + push!(new_v, v[2]) + for nxt in 2:(NUM-r) + push!( new_v, new_v[nxt-1]+v[nxt+1] ) + end + v = new_v + end + return new_v[1] +end + +print(compute([1,2,3,4,5])) +println() +print(compute([1,3,5,7,9])) +println() diff --git a/challenge-163/cheok-yin-fung/perl/ch-1.pl b/challenge-163/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..7d8e711cd3 --- /dev/null +++ b/challenge-163/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl +# The Weekly Challenge 163 +# Task 1 Sum Bitwise Operator +# Usage: $ ch-1.pl @n +use v5.22.0; +use warnings; +use List::Util qw/sum pairmap first/; + +if (defined($ARGV[0])) { + say sum_bitwise(@ARGV); +} + +sub sum_bitwise { + my @n = @_; + my $sum = 0; + for my $i (0..$#n) { + for my $j (0..$#n) { + $sum += $n[$i] & $n[$j] if $i != $j && $n[$i] != $n[$j]; + } + } + $sum /= 2; + return $sum; +} + +use Test::More tests=>2; +ok sum_bitwise(1,2,3) == 3, "Example 1"; +ok sum_bitwise(2,3,4) == 2, "Example 2"; + diff --git a/challenge-163/cheok-yin-fung/perl/ch-2.pl b/challenge-163/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..e323f20714 --- /dev/null +++ b/challenge-163/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl +# The Weekly Challenge 163 +# Task 2 Summation +# Usage: $ ch-2.pl @n +use v5.22.0; +use warnings; + + +say compute(@ARGV) if defined($ARGV[0]); + +sub compute { + my @v = @_; + my $NUM = scalar @v; + + my @new_v; + + for my $r (1..$NUM-1) { + @new_v = (); + $new_v[0] = $v[1]; + for my $next (1..$NUM-$r-1) { + $new_v[$next] = $new_v[$next-1] + $v[$next+1]; + } + @v = @new_v; + } + + return $new_v[0]; +} + +use Test::More tests => 2; +ok compute(1,2,3,4,5) == 42; +ok compute(1,3,5,7,9) == 70; diff --git a/challenge-163/cheok-yin-fung/python/ch-2.py b/challenge-163/cheok-yin-fung/python/ch-2.py new file mode 100644 index 0000000000..e88feacc8e --- /dev/null +++ b/challenge-163/cheok-yin-fung/python/ch-2.py @@ -0,0 +1,37 @@ +# The Weekly Challenge 163 +# Task 2 Summation +# Python script +# Usage: $ python3 ch-2.py [parameter] + + +import unittest +import sys + + +def compute(array): + NUM = len(array) + v = array + new_v = [] + for r in range(1, NUM): + new_v = [] + new_v.append(v[1]) + for nxt in range(1, NUM-r): + new_v.append(new_v[nxt-1] + v[nxt+1]) + v = new_v + return new_v[0] + +class SimpleTest(unittest.TestCase): + def test_example(self): + self.assertEqual(compute([1,2,3,4,5]), 42) + self.assertEqual(compute([1,3,5,7,9]), 70) + + +if __name__ == "__main__": + myArr = [] + if len(sys.argv) > 1: + for i in range(1, len(sys.argv)): + myArr.append(int(sys.argv[i])) + ans = compute(myArr) + print(ans) + else: + unittest.main() diff --git a/challenge-163/mohammad-anwar/perl/ch-1.pl b/challenge-163/mohammad-anwar/perl/ch-1.pl new file mode 100644 index 0000000000..92b6043dd0 --- /dev/null +++ b/challenge-163/mohammad-anwar/perl/ch-1.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl + +=head1 + +Week 163: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-163 + +Task #1: Sum Bitwise Operator + + You are given list positive numbers, @n. + + Write script to calculate the sum of bitwise & operator for all unique pairs. + +=cut + +use strict; +use warnings; + +use Test::More; +use Algorithm::Combinatorics qw(combinations); + +is bitwise_and([1, 2, 3]), 3, 'Example 1'; +is bitwise_and([2, 3, 4]), 2, 'Example 2'; + +done_testing; + +# +# +# METHOD + +sub bitwise_and { + my ($numbers) = @_; + + return unless @$numbers; + + my $sum = 0; + $sum += $_->[0] & $_->[1] for (combinations($numbers, 2)); + + return $sum; +} diff --git a/challenge-163/mohammad-anwar/perl/ch-2.pl b/challenge-163/mohammad-anwar/perl/ch-2.pl new file mode 100644 index 0000000000..638b5d7a1a --- /dev/null +++ b/challenge-163/mohammad-anwar/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl + +=head1 + +Week 163: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-163 + +Task #2: Summations + + You are given a list of positive numbers, @n. + + Write a script to find out the summations. + +=cut + +use strict; +use warnings; + +use Test::More; + +is summations([1, 2, 3, 4, 5]), 42, 'Example 1'; +is summations([1, 3, 5, 7, 9]), 70, 'Example 2'; + +done_testing; + +# +# +# METHOD + +sub summations { + my ($numbers) = @_; + + return unless @$numbers; + + while (@$numbers > 2) { + my @_numbers = ($numbers->[1]); + $_numbers[$_] = $numbers->[$_ + 1] + $_numbers[$_ - 1] + for (1 .. @$numbers-2); + $numbers = \@_numbers; + } + + return $numbers->[-1]; +} |
