diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-04-12 06:47:01 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-12 06:47:01 +0100 |
| commit | b0634f8e0d0c77c0d92de8c619b31d89a0c13a0d (patch) | |
| tree | 6eb35018b224a6345c799e7eff7a95f09a3cb06e | |
| parent | 3e3892a127e0393baed83fc53fbf8523ce9f20e7 (diff) | |
| parent | f8962b11d7445b396285c9ac8916835277c8fa36 (diff) | |
| download | perlweeklychallenge-club-b0634f8e0d0c77c0d92de8c619b31d89a0c13a0d.tar.gz perlweeklychallenge-club-b0634f8e0d0c77c0d92de8c619b31d89a0c13a0d.tar.bz2 perlweeklychallenge-club-b0634f8e0d0c77c0d92de8c619b31d89a0c13a0d.zip | |
Merge pull request #1549 from Doomtrain14/master
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
| -rw-r--r-- | challenge-055/yet-ebreo/perl/ch-2.pl | 30 | ||||
| -rw-r--r-- | challenge-055/yet-ebreo/perl/ch-2a.pl | 66 | ||||
| -rw-r--r-- | challenge-055/yet-ebreo/python/ch-1.py | 47 | ||||
| -rw-r--r-- | challenge-055/yet-ebreo/python/ch-2.py | 71 | ||||
| -rw-r--r-- | challenge-055/yet-ebreo/ruby/ch-2.rb | 29 |
5 files changed, 238 insertions, 5 deletions
diff --git a/challenge-055/yet-ebreo/perl/ch-2.pl b/challenge-055/yet-ebreo/perl/ch-2.pl index a9473ed815..5012501c10 100644 --- a/challenge-055/yet-ebreo/perl/ch-2.pl +++ b/challenge-055/yet-ebreo/perl/ch-2.pl @@ -2,8 +2,9 @@ use strict; use warnings; use feature 'say'; - - +use Time::HiRes 'time'; + +my $start_time = time; my @n = sort {$a - $b} @ARGV ? @ARGV : (1, 2, 3, 4) ; my %h; sub wave { @@ -27,6 +28,9 @@ sub wave { } wave(\@n, 0, $#n); + +my $elapsed = time - $start_time; +printf ("Execution Time: %.6f seconds",$elapsed); =begin perl .\ch-2.pl 2 1 4 3 @@ -34,6 +38,7 @@ perl .\ch-2.pl 3 1 4 2 4 2 3 1 4 1 3 2 +Execution Time: 0.001008 seconds perl .\ch-2.pl 1 2 2 3 4 2 1 3 2 4 @@ -48,4 +53,25 @@ perl .\ch-2.pl 1 2 2 3 4 4 2 3 1 2 4 1 2 2 3 4 1 3 2 2 +Execution Time: 0.002978 seconds + +perl .\ch-2.pl 1 2 3 4 5 6 7 8 9 +... +9 1 8 2 7 3 5 4 6 +9 1 8 2 7 3 6 4 5 +9 1 8 2 7 4 5 3 6 +9 1 8 2 7 4 6 3 5 +9 1 8 2 4 3 7 5 6 +9 1 8 2 4 3 6 5 7 +Execution Time: 7.181952 seconds + +perl .\ch-2.pl 1 2 3 4 5 6 7 8 9 10 +... +10 1 9 2 8 4 6 3 7 5 +10 1 9 2 4 3 7 6 8 5 +10 1 9 2 4 3 7 5 8 6 +10 1 9 2 4 3 8 6 7 5 +10 1 9 2 4 3 8 5 7 6 +10 1 9 2 4 3 6 5 8 7 +Execution Time: 52.477920 seconds =cut diff --git a/challenge-055/yet-ebreo/perl/ch-2a.pl b/challenge-055/yet-ebreo/perl/ch-2a.pl new file mode 100644 index 0000000000..0c56e624df --- /dev/null +++ b/challenge-055/yet-ebreo/perl/ch-2a.pl @@ -0,0 +1,66 @@ +#!/usr/bin/perl +use strict; +use warnings; +use feature 'say'; +use Time::HiRes 'time'; +use Math::Combinatorics; + +my $start_time = time; +my @n = sort {$a - $b} @ARGV ? @ARGV : (1, 2, 3, 4) ; +my %h; + +for my $elem (permute(@n)) { + my $flag = 1; + for my $e (1..$#{$elem}) { + !($flag &= $e % 2? $elem->[$e]<=$elem->[$e-1] : $elem->[$e]>=$elem->[$e-1]) && last; + } + #Only unique permutation will be printed + $flag && !$h{"@{$elem}"}++ && say "@{$elem}" +} + +my $elapsed = time - $start_time; +printf ("Execution Time: %.6f seconds",$elapsed); +=begin +perl .\ch-2a.pl +3 1 4 2 +3 2 4 1 +2 1 4 3 +4 1 3 2 +4 2 3 1 +Execution Time: 0.000916 seconds + +perl .\ch-2a.pl 1 2 2 3 4 +2 1 4 2 3 +2 1 3 2 4 +2 2 4 1 3 +2 2 3 1 4 +4 1 2 2 3 +4 1 3 2 2 +4 2 2 1 3 +4 2 3 1 2 +3 1 2 2 4 +3 1 4 2 2 +3 2 2 1 4 +3 2 4 1 2 +Execution Time: 0.002716 seconds + +perl .\ch-2a.pl 1 2 3 4 5 6 7 8 9 +... +8 6 9 4 5 2 7 1 3 +8 6 9 4 7 1 3 2 5 +8 6 9 4 7 1 5 2 3 +8 6 9 4 7 3 5 1 2 +8 6 9 4 7 2 3 1 5 +8 6 9 4 7 2 5 1 3 +Execution Time: 20.922912 seconds + +perl .\ch-2a.pl 1 2 3 4 5 6 7 8 9 10 +... +10 8 9 6 7 2 5 1 4 3 +10 8 9 6 7 2 5 3 4 1 +10 8 9 6 7 2 4 1 5 3 +10 8 9 6 7 2 4 3 5 1 +10 8 9 6 7 4 5 1 3 2 +10 8 9 6 7 4 5 2 3 1 +Execution Time: 219.184307 seconds +=cut diff --git a/challenge-055/yet-ebreo/python/ch-1.py b/challenge-055/yet-ebreo/python/ch-1.py new file mode 100644 index 0000000000..6bf2fc0c52 --- /dev/null +++ b/challenge-055/yet-ebreo/python/ch-1.py @@ -0,0 +1,47 @@ +import sys + +if len(sys.argv) < 2: + bin_str = '010' +else: + bin_str = sys.argv[1] + +size = len(bin_str) +num = int(bin_str, 2) + +res = [] +max = 0 + +for left in list(range(0,size)): + for right in list(range(left,size)): + bin_int = num + for number in list(range(left,right+1)): + bin_int ^= 1 << size - number -1 + + ones = bin(bin_int).count("1") + + if ones > max: + max = ones + res.clear() + + if ones == max: + res.append([left,right]) + +print ("Pair of L-R (one's = " + str(max) + "):") +for out in res: + print (out) + +""" +python .\ch-1.py +Pair of L-R (one's = 2): +[0, 0] +[0, 2] +[2, 2] + +python .\ch-1.py 0101101101 +Pair of L-R (one's = 7): +[0, 0] +[0, 2] +[2, 2] +[5, 5] +[8, 8] +""" diff --git a/challenge-055/yet-ebreo/python/ch-2.py b/challenge-055/yet-ebreo/python/ch-2.py new file mode 100644 index 0000000000..8d40a6f06e --- /dev/null +++ b/challenge-055/yet-ebreo/python/ch-2.py @@ -0,0 +1,71 @@ +from itertools import permutations +import sys +import time + +start_time = time.time() + +if len(sys.argv) < 2: + narray = [1, 2, 3, 4] +else: + narray = list(map(int, sys.argv[1:])) + +narray.sort() +dict = {} +for elem in list(permutations(narray)): + flag = 1 + for e in list(range(1, len(elem))): + flag &= (elem[e] >= elem[e-1], elem[e] <= elem[e-1])[e % 2 > 0] + if not flag: + break + + if flag > 0: + hold = str(elem) + if (not hold in dict): + print (hold) + dict[hold] = 1 + +print("Execution Time: %s seconds" % (time.time() - start_time)) +""" +python .\ch-2.py +(2, 1, 4, 3) +(3, 1, 4, 2) +(3, 2, 4, 1) +(4, 1, 3, 2) +(4, 2, 3, 1) +Execution Time: 0.02042078971862793 seconds + +python .\ch-2.py 1 2 2 3 4 +(2, 1, 3, 2, 4) +(2, 1, 4, 2, 3) +(2, 2, 3, 1, 4) +(2, 2, 4, 1, 3) +(3, 1, 2, 2, 4) +(3, 1, 4, 2, 2) +(3, 2, 2, 1, 4) +(3, 2, 4, 1, 2) +(4, 1, 2, 2, 3) +(4, 1, 3, 2, 2) +(4, 2, 2, 1, 3) +(4, 2, 3, 1, 2) +Execution Time: 0.10867619514465332 seconds + +python .\ch-2.py 1 2 3 4 5 6 7 8 9 +... +(9, 7, 8, 4, 6, 3, 5, 1, 2) +(9, 7, 8, 5, 6, 1, 3, 2, 4) +(9, 7, 8, 5, 6, 1, 4, 2, 3) +(9, 7, 8, 5, 6, 2, 3, 1, 4) +(9, 7, 8, 5, 6, 2, 4, 1, 3) +(9, 7, 8, 5, 6, 3, 4, 1, 2) +Execution Time: 14.091261386871338 seconds + +python .\ch-2.py 1 2 3 4 5 6 7 8 9 10 +... +(10, 8, 9, 6, 7, 3, 4, 1, 5, 2) +(10, 8, 9, 6, 7, 3, 4, 2, 5, 1) +(10, 8, 9, 6, 7, 3, 5, 1, 4, 2) +(10, 8, 9, 6, 7, 3, 5, 2, 4, 1) +(10, 8, 9, 6, 7, 4, 5, 1, 3, 2) +(10, 8, 9, 6, 7, 4, 5, 2, 3, 1) +Execution Time: 145.47170519828796 seconds +"""
\ No newline at end of file diff --git a/challenge-055/yet-ebreo/ruby/ch-2.rb b/challenge-055/yet-ebreo/ruby/ch-2.rb index fb1358e50d..1b93feeb25 100644 --- a/challenge-055/yet-ebreo/ruby/ch-2.rb +++ b/challenge-055/yet-ebreo/ruby/ch-2.rb @@ -1,5 +1,6 @@ #!/usr/bin/ruby +start_time = Time.now narray = (ARGV.sort) hash = {} for elem in narray.permutation.to_a @@ -18,16 +19,17 @@ for elem in narray.permutation.to_a hash[hold] = 1 end end - +puts "Execution Time: #{Time.now - start_time} seconds" =begin -ruby .\ch-2.rb 1 2 3 4 +ruby .\ch-2.rb 1 2 3 4 2 1 4 3 3 1 4 2 3 2 4 1 4 1 3 2 4 2 3 1 +Execution Time: 0.001842 seconds -ruby .\ch-2.rb 1 2 2 3 4 +ruby .\ch-2.rb 1 2 2 3 4 2 1 3 2 4 2 1 4 2 3 2 2 3 1 4 @@ -40,4 +42,25 @@ ruby .\ch-2.rb 1 2 2 3 4 4 1 3 2 2 4 2 2 1 3 4 2 3 1 2 +Execution Time: 0.0028966 seconds + +ruby .\ch-2.rb 1 2 3 4 5 6 7 8 9 +.. +9 7 8 4 6 3 5 1 2 +9 7 8 5 6 1 3 2 4 +9 7 8 5 6 1 4 2 3 +9 7 8 5 6 2 3 1 4 +9 7 8 5 6 2 4 1 3 +9 7 8 5 6 3 4 1 2 +Execution Time: 7.6230353 seconds + +ruby .\ch-2.rb 1 2 3 4 5 6 7 8 9 10 +.. +9 7 8 5 6 2 3 1 4 10 +9 7 8 5 6 2 3 10 4 1 +9 7 8 5 6 2 4 1 3 10 +9 7 8 5 6 2 4 10 3 1 +9 7 8 5 6 3 4 1 2 10 +9 7 8 5 6 3 4 10 2 1 +Execution Time: 60.9326917 seconds =end
\ No newline at end of file |
