diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-04-10 18:54:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-10 18:54:15 +0100 |
| commit | 1a14099d8fc40b7cf6c522c3c120059ed396c5a6 (patch) | |
| tree | f5216ea3d461ef8e261bd026258b1fc322632e56 | |
| parent | 10fd83d769f61c2ce4f91da43588e9818a122e7b (diff) | |
| parent | 04ad75f4916cf2358f48c720b44e67b25f2da063 (diff) | |
| download | perlweeklychallenge-club-1a14099d8fc40b7cf6c522c3c120059ed396c5a6.tar.gz perlweeklychallenge-club-1a14099d8fc40b7cf6c522c3c120059ed396c5a6.tar.bz2 perlweeklychallenge-club-1a14099d8fc40b7cf6c522c3c120059ed396c5a6.zip | |
Merge pull request #1546 from Doomtrain14/master
Ruby solutions for ch#55
| -rw-r--r-- | challenge-055/yet-ebreo/ruby/ch-1.rb | 47 | ||||
| -rw-r--r-- | challenge-055/yet-ebreo/ruby/ch-2.rb | 43 |
2 files changed, 90 insertions, 0 deletions
diff --git a/challenge-055/yet-ebreo/ruby/ch-1.rb b/challenge-055/yet-ebreo/ruby/ch-1.rb new file mode 100644 index 0000000000..bd2c19a892 --- /dev/null +++ b/challenge-055/yet-ebreo/ruby/ch-1.rb @@ -0,0 +1,47 @@ +#!/usr/bin/ruby + +bin_str = ARGV[0] || '010' +len = bin_str.length +num = bin_str.to_i(2) + +res = [] +max = 0 + +for left in 0..len-1 + for right in left..len-1 + bin = num + for number in left..right + bin ^= 1 << len - number - 1 + end + + ones = bin.to_s(2).count('1') + if ones > max then + max = ones + res.clear + end + if ones == max then + res.push("#{left} #{right}") + end + end +end + +puts "Pair of L-R (one's = #{max}):"; +for out in res + puts out +end + +=begin +ruby .\ch-1.rb +Pair of L-R (one's = 2): +0 0 +0 2 +2 2 + +ruby .\ch-1.rb 0101101101 +Pair of L-R (one's = 7): +0 0 +0 2 +2 2 +5 5 +8 8 +=end
\ 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 new file mode 100644 index 0000000000..fb1358e50d --- /dev/null +++ b/challenge-055/yet-ebreo/ruby/ch-2.rb @@ -0,0 +1,43 @@ +#!/usr/bin/ruby + +narray = (ARGV.sort) +hash = {} +for elem in narray.permutation.to_a + flag = 1 + for e in 1..elem.length-1 + e % 2 > 0? + flag &= (elem[e] <= elem[e-1] ? 1 : 0) + : + flag &= (elem[e] >= elem[e-1] ? 1 : 0) + + break if !flag + end + if flag > 0 + hold = elem.join(' ') + puts hold if !hash[hold] + hash[hold] = 1 + end +end + +=begin +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 + +ruby .\ch-2.rb 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 +=end
\ No newline at end of file |
