diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-06-02 11:57:37 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-02 11:57:37 +0100 |
| commit | 52a65ea1a9b45337f70fc4c70a5bcf0c4b381975 (patch) | |
| tree | 72a2e67d477cbc7092a30902b5f5077d89457805 | |
| parent | 4f01c70b1b1c6d1d41ae3068408ba24983175834 (diff) | |
| parent | 640b2248d9135318bfbac1407594e7454fcdd962 (diff) | |
| download | perlweeklychallenge-club-52a65ea1a9b45337f70fc4c70a5bcf0c4b381975.tar.gz perlweeklychallenge-club-52a65ea1a9b45337f70fc4c70a5bcf0c4b381975.tar.bz2 perlweeklychallenge-club-52a65ea1a9b45337f70fc4c70a5bcf0c4b381975.zip | |
Merge pull request #1786 from sangeetkar/ch63
Perl and Python
| -rwxr-xr-x | challenge-063/sangeet-kar/perl/ch-1.pl | 21 | ||||
| -rwxr-xr-x | challenge-063/sangeet-kar/perl/ch-2.pl | 18 | ||||
| -rwxr-xr-x | challenge-063/sangeet-kar/python/ch-1.py | 12 | ||||
| -rwxr-xr-x | challenge-063/sangeet-kar/python/ch-2.py | 16 |
4 files changed, 67 insertions, 0 deletions
diff --git a/challenge-063/sangeet-kar/perl/ch-1.pl b/challenge-063/sangeet-kar/perl/ch-1.pl new file mode 100755 index 0000000000..986f399b2d --- /dev/null +++ b/challenge-063/sangeet-kar/perl/ch-1.pl @@ -0,0 +1,21 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; + +sub last_word { + my ( $string, $regex ) = @_; + my @words = split /\s+/, $string; + foreach ( reverse @words ) { + return $_ if $_ =~ $regex; + } + return undef; +} + +is( last_word( ' hello world', qr/[ea]l/ ), 'hello', 'hello world' ); +is( last_word( "Don't match too much, Chet!", qr/ch.t/i ), 'Chet!', 'Chet!' ); +is( last_word( "spaces in regexp won't match", qr/in re/ ), undef, 'undef' ); +is( last_word( join( ' ', 1 .. 1e6 ), qr/^(3.*?){3}/ ), + '399933', 'long list of nums' ); +done_testing; diff --git a/challenge-063/sangeet-kar/perl/ch-2.pl b/challenge-063/sangeet-kar/perl/ch-2.pl new file mode 100755 index 0000000000..0d250f739d --- /dev/null +++ b/challenge-063/sangeet-kar/perl/ch-2.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl + +sub rot { + my ( $string, $n ) = @_; + substr( $string, $n ) . substr( $string, 0, $n ); +} + +my $input = $ARGV[0] // 'xyxx'; + +my $tmp = $input; +for ( my $i = 1 ; ; $i++ ) { + $tmp = rot( $tmp, $i % length($input) ); + if ( $tmp eq $input ) { + print $i; + last; + } +} + diff --git a/challenge-063/sangeet-kar/python/ch-1.py b/challenge-063/sangeet-kar/python/ch-1.py new file mode 100755 index 0000000000..0817cecb17 --- /dev/null +++ b/challenge-063/sangeet-kar/python/ch-1.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python + +import re + +def last_word(string, regex): + return next((word for word in reversed(string.split()) if re.search(regex, word)), None) + +assert last_word(' hello world', r"[ea]l") == 'hello' +assert last_word("Don't match too much, Chet!", re.compile(r"ch.t", re.I)) == 'Chet!' +assert last_word("spaces in regexp won't match", r"in re") == None +assert last_word( ' '.join(str(x) for x in range(1, 1000000)), r"^(3.*?){3}") == '399933' +print("All tests passed") diff --git a/challenge-063/sangeet-kar/python/ch-2.py b/challenge-063/sangeet-kar/python/ch-2.py new file mode 100755 index 0000000000..af0d5c6382 --- /dev/null +++ b/challenge-063/sangeet-kar/python/ch-2.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python + +import sys +from itertools import count + +def rot(string, n=1): + return string[n:] + string[:n] + +inp = sys.argv[1] if len(sys.argv) > 1 else 'xyxx' + +tmp = inp +for i in count(1): + tmp = rot(tmp, i % len(inp)) + if tmp == inp: + print(i) + break |
