diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-02-03 20:41:34 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-02-03 20:41:34 +0000 |
| commit | bd279cc88842b2b3d46ce276d4487c1482e5d7f0 (patch) | |
| tree | 33a1c171ba12289800b50f8be3ebf12273d00561 /challenge-150 | |
| parent | d904513eeb1c9fc8c958cab0db7f7fd2d34bed8b (diff) | |
| download | perlweeklychallenge-club-bd279cc88842b2b3d46ce276d4487c1482e5d7f0.tar.gz perlweeklychallenge-club-bd279cc88842b2b3d46ce276d4487c1482e5d7f0.tar.bz2 perlweeklychallenge-club-bd279cc88842b2b3d46ce276d4487c1482e5d7f0.zip | |
- Added Perl and Raku solutions to task 1 of week 150.
Diffstat (limited to 'challenge-150')
| -rw-r--r-- | challenge-150/mohammad-anwar/perl/ch-1.pl | 37 | ||||
| -rw-r--r-- | challenge-150/mohammad-anwar/python/ch-1.py | 34 | ||||
| -rw-r--r-- | challenge-150/mohammad-anwar/raku/ch-1.raku | 40 |
3 files changed, 111 insertions, 0 deletions
diff --git a/challenge-150/mohammad-anwar/perl/ch-1.pl b/challenge-150/mohammad-anwar/perl/ch-1.pl new file mode 100644 index 0000000000..dfdbced2ad --- /dev/null +++ b/challenge-150/mohammad-anwar/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl + +=head1 + +Week 150: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-150 + +Task #1: Fibonacci Words + + You are given two strings having same number of digits, $a and $b. + + Write a script to generate Fibonacci Words by concatenation of the previous two strings. Finally print 51st digit of the first term having at least 51 digits. + +=cut + +use strict; +use warnings; +use Test::More; + +is(fibonacci_words('1234', '5678', 51), 7, 'Example'); + +done_testing; + +# +# +# METHOD + +sub fibonacci_words { + my ($term_1, $term_2, $index) = @_; + + while (length($term_1 . $term_2) <= $index) { + ($term_1, $term_2) = ($term_2, $term_1 . $term_2); + } + + return substr($term_1 . $term_2, --$index, 1); +} diff --git a/challenge-150/mohammad-anwar/python/ch-1.py b/challenge-150/mohammad-anwar/python/ch-1.py new file mode 100644 index 0000000000..4761089c0f --- /dev/null +++ b/challenge-150/mohammad-anwar/python/ch-1.py @@ -0,0 +1,34 @@ +#!/usr/bin/python3 + +''' + +Week 150: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-150 + +Task #1: Fibonacci Words + + You are given two strings having same number of digits, $a and $b. + + Write a script to generate Fibonacci Words by concatenation of the previous two strings. Finally print 51st digit of the first term having at least 51 digits. + +''' + +import unittest + +def fibonacci_words(term1, term2, index): + while (len(term1 + term2) <= index): + term1, term2 = term2, term1 + term2 + + return (term1 + term2)[index-1] + +# +# +# Unit test class + +class TestTruncatablePrime(unittest.TestCase): + + def test_example(self): + self.assertEqual(fibonacci_words('1234', '5678', 51), '7', 'Example') + +unittest.main() diff --git a/challenge-150/mohammad-anwar/raku/ch-1.raku b/challenge-150/mohammad-anwar/raku/ch-1.raku new file mode 100644 index 0000000000..ddf167cce9 --- /dev/null +++ b/challenge-150/mohammad-anwar/raku/ch-1.raku @@ -0,0 +1,40 @@ +#!/usr/bin/env raku + +=begin pod + +Week 150: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-150 + +Task #1: Fibonacci Words + + You are given two strings having same number of digits, $a and $b. + + Write a script to generate Fibonacci Words by concatenation of the previous two strings. Finally print 51st digit of the first term having at least 51 digits. + +=end pod + +use Test; + +my Str $term1 = '1234'; +my Str $term2 = '5678'; +my Int $index = 51; + +is(fibonacci-words($term1, $term2, $index), '7', 'Example'); + +done-testing; + +# +# +# METHOD + +sub fibonacci-words(Str $term1 is rw, + Str $term2 is rw, + Int $index is rw --> Str) { + + while ($term1 ~ $term2).codes <= $index { + ($term1, $term2) = ($term2, $term1 ~ $term2); + } + + return ($term1 ~ $term2).substr(--$index,1); +} |
