diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-12-06 14:55:30 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-12-06 14:55:30 +0000 |
| commit | cd04687f29d4ebea2fd5ec165585b21f2951f413 (patch) | |
| tree | 5c17b623d6b7a8cb803a4464f19d157152143e62 /challenge-141/mohammad-anwar | |
| parent | fe57a0405720a40beaeb75100e069aaa13e4c49e (diff) | |
| parent | b9773f5c38387d865a093d2ecdfd1b01b4452c34 (diff) | |
| download | perlweeklychallenge-club-cd04687f29d4ebea2fd5ec165585b21f2951f413.tar.gz perlweeklychallenge-club-cd04687f29d4ebea2fd5ec165585b21f2951f413.tar.bz2 perlweeklychallenge-club-cd04687f29d4ebea2fd5ec165585b21f2951f413.zip | |
Merge branch 'master' into devel
Diffstat (limited to 'challenge-141/mohammad-anwar')
| -rw-r--r-- | challenge-141/mohammad-anwar/perl/ch-1.pl | 50 | ||||
| -rw-r--r-- | challenge-141/mohammad-anwar/perl/ch-2.pl | 51 | ||||
| -rw-r--r-- | challenge-141/mohammad-anwar/python/ch-1.py | 46 | ||||
| -rw-r--r-- | challenge-141/mohammad-anwar/python/ch-2.py | 51 | ||||
| -rw-r--r-- | challenge-141/mohammad-anwar/raku/ch-1.raku | 49 | ||||
| -rw-r--r-- | challenge-141/mohammad-anwar/raku/ch-2.raku | 51 |
6 files changed, 298 insertions, 0 deletions
diff --git a/challenge-141/mohammad-anwar/perl/ch-1.pl b/challenge-141/mohammad-anwar/perl/ch-1.pl new file mode 100644 index 0000000000..6cf7326d00 --- /dev/null +++ b/challenge-141/mohammad-anwar/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl + +=head1 + +Week 141: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-141 + +Task #1: Number Divisors + + Write a script to find lowest 10 positive integers having exactly 8 divisors. + +=cut + +use strict; +use warnings; +use Test::More; +use Test::Deep; + +is_deeply( + number_divisors(10, 8), + [24, 30, 40, 42, 54, 56, 66, 70, 78, 88], + 'Example 1' +); + +done_testing; + +sub number_divisors { + my ($count, $number) = @_; + + my @numbers = (); + my $i = 1; + while (@numbers < $count) { + + my @divisors = (); + foreach my $j (1 .. $i) { + if ($i % $j == 0) { + push @divisors, $j; + } + } + + if (@divisors == $number) { + push @numbers, $i; + } + + $i++; + } + + return \@numbers; +} diff --git a/challenge-141/mohammad-anwar/perl/ch-2.pl b/challenge-141/mohammad-anwar/perl/ch-2.pl new file mode 100644 index 0000000000..b1005410d2 --- /dev/null +++ b/challenge-141/mohammad-anwar/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/perl + +=head1 + +Week 141: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-141 + +Task #2: Like Numbers + + You are given positive integers, $m and $n. + + Write a script to find total count of integers created using the digits of $m which is also divisible by $n. + +=cut + +use strict; +use warnings; +use Test::More; +use Test::Deep; +use Algorithm::Combinatorics qw(combinations); + +is_deeply( + like_numbers(1234, 2), + [2, 4, 12, 14, 24, 34, 124, 134, 234], + 'Example 1' +); + +is_deeply( + like_numbers(768, 4), + [8, 76, 68], + 'Example 2' +); + +done_testing; + +sub like_numbers { + my ($m, $n) = @_; + + my @numbers = (); + foreach my $i (1 .. length($m)-1) { + foreach my $j (combinations([ split //, $m ], $i)) { + my $k = join('', @$j); + if ($k % $n == 0) { + push @numbers, $k; + } + } + } + + return \@numbers; +} diff --git a/challenge-141/mohammad-anwar/python/ch-1.py b/challenge-141/mohammad-anwar/python/ch-1.py new file mode 100644 index 0000000000..b07284649b --- /dev/null +++ b/challenge-141/mohammad-anwar/python/ch-1.py @@ -0,0 +1,46 @@ +#!/usr/bin/python3 + +''' + +Week 141: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-141 + +Task #1: Number Divisors + + Write a script to find lowest 10 positive integers having exactly 8 divisors. + +''' + +import unittest + +def number_divisors(count, number): + numbers = [] + i = 1 + + while len(numbers) < count: + divisors = [] + for j in range(1, i+1): + if i % j == 0: + divisors.append(j) + + if len(divisors) == number: + numbers.append(i) + + i += 1 + + return numbers + +# +# +# Unit test class + +class TestJortSort(unittest.TestCase): + + def test_example_1(self): + self.assertEqual( + number_divisors(10, 8), + [24, 30, 40, 42, 54, 56, 66, 70, 78, 88], + 'Example 1') + +unittest.main() diff --git a/challenge-141/mohammad-anwar/python/ch-2.py b/challenge-141/mohammad-anwar/python/ch-2.py new file mode 100644 index 0000000000..96b013c73c --- /dev/null +++ b/challenge-141/mohammad-anwar/python/ch-2.py @@ -0,0 +1,51 @@ +#!/usr/bin/python3 + +''' + +Week 141: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-141 + +Task #1: Like Numbers + + You are given positive integers, $m and $n. + + Write a script to find total count of integers created using the digits of $m which is also divisible by $n. + +''' + +from itertools import combinations +import unittest + +def like_numbers(m, n): + numbers = [] + m_digits = [int(a) for a in str(m)] + + for i in range(1, len(m_digits)): + divisors = [] + for j in combinations(m_digits, i): + num = int(''.join(str(x) for x in (j))) + if num % n == 0: + numbers.append(num) + + return numbers + +# +# +# Unit test class + +class TestJortSort(unittest.TestCase): + + def test_example_1(self): + self.assertEqual( + like_numbers(1234, 2), + [2, 4, 12, 14, 24, 34, 124, 134, 234], + 'Example 1') + + def test_example_2(self): + self.assertEqual( + like_numbers(768, 4), + [8, 76, 68], + 'Example 2') + +unittest.main() diff --git a/challenge-141/mohammad-anwar/raku/ch-1.raku b/challenge-141/mohammad-anwar/raku/ch-1.raku new file mode 100644 index 0000000000..a637f810dc --- /dev/null +++ b/challenge-141/mohammad-anwar/raku/ch-1.raku @@ -0,0 +1,49 @@ +#!/usr/bin/env raku + +=begin pod + +Week 141: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-141 + +Task #1: Number Divisors + + Write a script to find lowest 10 positive integers having exactly 8 divisors. + +=end pod + +use Test; + +is-deeply + number-divisors(10, 8), + [24, 30, 40, 42, 54, 56, 66, 70, 78, 88], + 'Example 1'; + +done-testing; + +# +# +# METHODS + +sub number-divisors(Int $count, Int $number) { + + my @numbers = (); + my Int $i = 1; + while @numbers.elems < $count { + + my @divisors = []; + for 1 .. $i -> $j { + if $i mod $j == 0 { + push @divisors: $j; + } + } + + if @divisors.elems == $number { + push @numbers: $i; + } + + $i++; + } + + return @numbers; +} diff --git a/challenge-141/mohammad-anwar/raku/ch-2.raku b/challenge-141/mohammad-anwar/raku/ch-2.raku new file mode 100644 index 0000000000..0082f845e1 --- /dev/null +++ b/challenge-141/mohammad-anwar/raku/ch-2.raku @@ -0,0 +1,51 @@ +#!/usr/bin/env raku + +=begin pod + +Week 141: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-141 + +Task #2: Like Numbers + + You are given positive integers, $m and $n. + + Write a script to find total count of integers created using the digits of $m which is also divisible by $n. + +=end pod + +use Test; + +is-deeply( + like-numbers(1234, 2), + [2, 4, 12, 14, 24, 34, 124, 134, 234], + 'Example 1' +); + +is-deeply( + like-numbers(768, 4), + [8, 76, 68], + 'Example 2' +); + +done-testing; + +# +# +# METHODS + +sub like-numbers(Int $m, Int $n) { + + my @numbers = (); + for $m.comb.combinations -> $i { + my Int $j = $i.join('').Int; + next if $j == 0; + next if $j.chars == $m.chars; + + if $j mod $n == 0 { + push @numbers: $j; + } + } + + return @numbers; +} |
