diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2021-11-20 13:15:26 +0100 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2021-11-20 13:15:26 +0100 |
| commit | 9b94fe8766e532231729219e74e7a05be81ce8a5 (patch) | |
| tree | 6bbf571385ecc0b0a4e4114a212094d773db29be | |
| parent | fb6439e9b29281803424ed0c44a3c83f031d18cf (diff) | |
| download | perlweeklychallenge-club-9b94fe8766e532231729219e74e7a05be81ce8a5.tar.gz perlweeklychallenge-club-9b94fe8766e532231729219e74e7a05be81ce8a5.tar.bz2 perlweeklychallenge-club-9b94fe8766e532231729219e74e7a05be81ce8a5.zip | |
Challenge 139 Task 1 2 LK Perl
| -rw-r--r-- | challenge-139/lubos-kolouch/perl/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-139/lubos-kolouch/perl/ch-2.pl | 54 |
2 files changed, 76 insertions, 0 deletions
diff --git a/challenge-139/lubos-kolouch/perl/ch-1.pl b/challenge-139/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..f155ef7bda --- /dev/null +++ b/challenge-139/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; +use Data::Dumper; + +sub is_jort_sorted { + my $what = shift; + + my @sorted_arr = sort @$what; + + my $str1 = join ' ', @$what; + my $str2 = join ' ', @sorted_arr; + + return $str1 eq $str2 ? 1 : 0; + +} + +use Test::More; + +is( is_jort_sorted( [ 1, 2, 3, 4, 5 ] ), 1 ); +is( is_jort_sorted( [ 1, 3, 2, 4, 5 ] ), 0 ); + +done_testing; diff --git a/challenge-139/lubos-kolouch/perl/ch-2.pl b/challenge-139/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..9e7f17caa9 --- /dev/null +++ b/challenge-139/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,54 @@ +use strict; +use warnings; +use bignum ( p => -100 ); +use Math::Prime::Util qw/next_prime/; + +sub get_repeating_pattern { + my $what = shift; + + my $big_reverse = 1 / $what; + my $repeating = $1 if ($big_reverse) =~ /(.+?)\1/msx; + + return length($repeating); +} + +sub is_long_prime { + my $what = shift; + + my $repeats = get_repeating_pattern($what); + + return 1 if ( $repeats > 1 ) and ( $repeats == $what - 1 ); + + return 0; +} + +sub generate_long_primes { + + my $primes_count = 0; + my $at_prime = 0; + + my @result; + + while ( $primes_count < 5 ) { + $at_prime = next_prime($at_prime); + + if ( is_long_prime($at_prime) ) { + $primes_count++; + push @result, $at_prime; + } + } + + return \@result; +} + +use Test::More; + +is( get_repeating_pattern(7), 6 ); +is( get_repeating_pattern(17), 16 ); + +is( is_long_prime(7), 1 ); +is( is_long_prime(17), 1 ); +is( is_long_prime(2), 0 ); +is_deeply( generate_long_primes(), [ 7, 17, 19, 23, 29 ] ); + +done_testing; |
