diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-10-01 16:58:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-01 16:58:43 +0100 |
| commit | 49257d99a5acd537cfda0082cba75637b0924b0d (patch) | |
| tree | 13bdf338c50868a5f09c04b7979653d1e33c8268 | |
| parent | 0f10ae532192b44c8c6291dfd0050f58e18b2be0 (diff) | |
| parent | e940402e4a351ef8bdcd19fd1d77836e49a8d5e8 (diff) | |
| download | perlweeklychallenge-club-49257d99a5acd537cfda0082cba75637b0924b0d.tar.gz perlweeklychallenge-club-49257d99a5acd537cfda0082cba75637b0924b0d.tar.bz2 perlweeklychallenge-club-49257d99a5acd537cfda0082cba75637b0924b0d.zip | |
Merge pull request #10940 from Firedrake/rogerbw-challenge-289
RogerBW solutions for challenge no. 289
22 files changed, 687 insertions, 0 deletions
diff --git a/challenge-289/roger-bell-west/crystal/ch-1.cr b/challenge-289/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..c26dcc9ad2 --- /dev/null +++ b/challenge-289/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,23 @@ +#! /usr/bin/crystal + +def thirdmaximum(a) + p = Set.new(a).to_a.sort.reverse + if p.size > 2 + p[2] + else + p[0] + end +end + +require "spec" +describe "thirdmaximum" do + it "test_ex1" do + thirdmaximum([5, 6, 4, 1]).should eq 4 + end + it "test_ex2" do + thirdmaximum([4, 5]).should eq 5 + end + it "test_ex3" do + thirdmaximum([1, 2, 2, 3]).should eq 1 + end +end diff --git a/challenge-289/roger-bell-west/crystal/ch-2.cr b/challenge-289/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..05000bd481 --- /dev/null +++ b/challenge-289/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,17 @@ +#! /usr/bin/crystal + +module JumbledLetters + wordre = Regex.new("([A-Za-z])([A-Za-z][A-Za-z]+)([A-Za-z])") + begin + while line = read_line() + l = line.gsub(wordre) { |_x, s| + s[1] + jumble(s[2]) + s[3] } + print("#{l}\n") + end + rescue IO::EOFError + end +end + +def jumble(a : String) + a.chars().shuffle.join +end diff --git a/challenge-289/roger-bell-west/javascript/ch-1.js b/challenge-289/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..ed70d52d1f --- /dev/null +++ b/challenge-289/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,34 @@ +#! /usr/bin/node + +"use strict" + +function thirdmaximum(a) { + let p = [...new Set([...a])]; + p.sort(function(a,b) { + return b - a; + }); + if (p.length > 2) { + return p[2]; + } else { + return p[0]; + } +} + +if (thirdmaximum([5, 6, 4, 1]) == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (thirdmaximum([4, 5]) == 5) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (thirdmaximum([1, 2, 2, 3]) == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-289/roger-bell-west/javascript/ch-2.js b/challenge-289/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..aca855f01d --- /dev/null +++ b/challenge-289/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,31 @@ +#! /usr/bin/node + +"use strict" + +function shuffleArray(input) { + let keys = Array(input.length).fill().map((element, index) => Math.random()); + let ix = Array(input.length).fill().map((element, index) => index); + ix.sort(function(a, b) { + return keys[a] - keys[b]; + }); + return ix.map(n => input[n]); +} + +function jumble(a) { + return shuffleArray(a.split("")).join(""); +} + +const wordrx = /([A-Za-z])([A-Za-z][A-Za-z]+)([A-Za-z])/g; +const readable = process.stdin; +let buffer = ""; +readable.on('readable', () => { + let chunk; + while (null !== (chunk = readable.read())) { + buffer += chunk; + } +}) +readable.on('end', () => { + buffer.split(/[\r\n]+/).forEach(line => { + process.stdout.write(line.replace(wordrx, (t, x, y, z) => x + jumble(y) + z) + "\n"); + }); +}); diff --git a/challenge-289/roger-bell-west/kotlin/ch-1.kt b/challenge-289/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..cdd86257a9 --- /dev/null +++ b/challenge-289/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,31 @@ +fun thirdmaximum(a: List<Int>): Int { + val p = a.distinct().sorted().reversed() + if (p.size > 2) { + return p[2] + } else { + return p[0] + } +} + +fun main() { + + if (thirdmaximum(listOf(5, 6, 4, 1)) == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (thirdmaximum(listOf(4, 5)) == 5) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (thirdmaximum(listOf(1, 2, 2, 3)) == 1) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-289/roger-bell-west/kotlin/ch-2.kt b/challenge-289/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..49a139a181 --- /dev/null +++ b/challenge-289/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,17 @@ +fun jumble(a: String): String { + return a.split("").shuffled().joinToString("") +} + +fun main() { + val wordrx = """([A-Za-z])([A-Za-z][A-Za-z]+)([A-Za-z])""".toRegex() + while(true) { + var line = readLine() + if (line == null) { + break + } + println( + line.replace(wordrx) { val s = it.groupValues + s[1] + jumble(s[2]) + s[3] } + ) + } +} diff --git a/challenge-289/roger-bell-west/lua/ch-1.lua b/challenge-289/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..75a5f2a343 --- /dev/null +++ b/challenge-289/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,40 @@ +#! /usr/bin/lua + +function thirdmaximum(a) + local q = {} + for i, n in ipairs(a) do + q[n] = true + end + local p = {} + for k, v in pairs(q) do + table.insert(p, k) + end + table.sort(p, function (i, j) return i > j end) + if #p > 2 then + return p[3] + else + return p[1] + end +end + +if thirdmaximum({5, 6, 4, 1}) == 4 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if thirdmaximum({4, 5}) == 5 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if thirdmaximum({1, 2, 2, 3}) == 1 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-289/roger-bell-west/lua/ch-2.lua b/challenge-289/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..a80e95ba6f --- /dev/null +++ b/challenge-289/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,50 @@ +#! /usr/bin/lua + +function shuffle(input) + local ix = {} + local keys = {} + for n = 1, #input do + table.insert(ix, n) + table.insert(keys, math.random()) + end + table.sort(ix, function (i, j) return keys[i] < keys[j] end) + local out = {} + for _, v in ipairs(ix) do + table.insert(out, input[v]) + end + return out +end + +function split(t) + local cl = {} + string.gsub(t, + "(.)", + function(c) + table.insert(cl, c) + end + ) + return cl +end + +function join(t) + local out="" + for i, v in ipairs(t) do + out = out .. v + end + return out +end + +function jumble(st) + return join(shuffle(split(st))) +end + +math.randomseed(os.time()) +while true do + local line = io.read() + if line == nil then break end + local x = line + local l = string.gsub(line, "(%a)(%a%a+)(%a)", function(a, b, c) + return a .. jumble(b) .. c + end) + print(l) +end diff --git a/challenge-289/roger-bell-west/perl/ch-1.pl b/challenge-289/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..36f070ff4b --- /dev/null +++ b/challenge-289/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,20 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(thirdmaximum([5, 6, 4, 1]), 4, 'example 1'); +is(thirdmaximum([4, 5]), 5, 'example 2'); +is(thirdmaximum([1, 2, 2, 3]), 1, 'example 3'); + +sub thirdmaximum($a) { + my @p = sort {$::b <=> $::a} keys %{{map {$_ => 1} @{$a}}}; + if (scalar @p > 2) { + $p[2]; + } else { + $p[0]; + } +} diff --git a/challenge-289/roger-bell-west/perl/ch-2.pl b/challenge-289/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..546128f848 --- /dev/null +++ b/challenge-289/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,18 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use List::Util qw(shuffle); + +sub jumble($a) { + return join('', shuffle(split('', $a))); +} + +while (<>) { + chomp; + my $line = $_; + $line =~ s/([A-Za-z])([A-Za-z][A-Za-z]+)([A-Za-z])/$1.jumble($2).$3/eg; + print "$line\n"; +} diff --git a/challenge-289/roger-bell-west/postscript/ch-1.ps b/challenge-289/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..e91e533cc4 --- /dev/null +++ b/challenge-289/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,169 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/toset { % array -> dict of (value, true) + << exch + { + true + } forall + >> +} bind def + +/quicksort.partition { + 3 dict begin + /pivot arr hi lo add 2 idiv get def + /i lo 1 sub def + /j hi 1 add def + { + { + /i i 1 add def + arr i get pivot cmp 0 ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot cmp 0 le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + end +} bind def + +/quicksort { + { quicksort.cmp } quicksort.with_comparator +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/quicksort.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/quicksort.swap { + 2 dict begin + /bi exch def + /ai exch def + arr ai get + arr bi get + arr exch ai exch put + arr exch bi exch put + end +} bind def + +/reverse { + 1 dict begin + dup length /l exch def + [ exch + aload pop + 2 1 l { + -1 roll + } for + ] + end +} bind def + +/quicksort.main { % lo hi -> (null) + 3 dict begin + /hi exch def + /lo exch def + /xit false def + lo 0 lt { + /xit true def + } if + hi 0 lt { + /xit true def + } if + lo hi ge { + /xit true def + } if + xit not { + /p quicksort.partition def + lo p quicksort.main + p 1 add hi quicksort.main + } if + end +} bind def + +/quicksort.with_comparator { % [ a c b ] { comparator } -> [ a b c ] + 2 dict begin + /cmp exch def + /arr exch def + arr length 0 gt { + 0 arr length 1 sub quicksort.main + } if + arr + end +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} bind def + + +% end included library code + +/thirdmaximum { + toset keys quicksort reverse + dup length 2 gt { + 2 + } { + 0 + } ifelse + get +} bind def + +(thirdmaximum) test.start +[5 6 4 1] thirdmaximum 4 eq test +[4 5] thirdmaximum 5 eq test +[1 2 2 3] thirdmaximum 1 eq test +test.end diff --git a/challenge-289/roger-bell-west/python/ch-1.py b/challenge-289/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..9c24614665 --- /dev/null +++ b/challenge-289/roger-bell-west/python/ch-1.py @@ -0,0 +1,24 @@ +#! /usr/bin/python3 + +def thirdmaximum(a): + p = list(set(a)) + p.sort(reverse = True) + if len(p) > 2: + return p[2] + else: + return p[0] + +import unittest + +class TestThirdmaximum(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(thirdmaximum([5, 6, 4, 1]), 4, 'example 1') + + def test_ex2(self): + self.assertEqual(thirdmaximum([4, 5]), 5, 'example 2') + + def test_ex3(self): + self.assertEqual(thirdmaximum([1, 2, 2, 3]), 1, 'example 3') + +unittest.main() diff --git a/challenge-289/roger-bell-west/python/ch-2.py b/challenge-289/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..18f42d7121 --- /dev/null +++ b/challenge-289/roger-bell-west/python/ch-2.py @@ -0,0 +1,20 @@ +#! /usr/bin/python3 + +import fileinput +import re +import random + +def jumble(a): + b = list(a) + random.shuffle(b) + return "".join(b) + +def replace(mt): + return mt.group(1) + jumble(mt.group(2)) + mt.group(3) + +wordre = re.compile(r"([A-Za-z])([A-Za-z][A-Za-z]+)([A-Za-z])") + +for line in fileinput.input(): + line = line.rstrip() + l = re.sub(wordre, replace, line) + print(l) diff --git a/challenge-289/roger-bell-west/raku/ch-1.p6 b/challenge-289/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..a9be677a0e --- /dev/null +++ b/challenge-289/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,18 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(thirdmaximum([5, 6, 4, 1]), 4, 'example 1'); +is(thirdmaximum([4, 5]), 5, 'example 2'); +is(thirdmaximum([1, 2, 2, 3]), 1, 'example 3'); + +sub thirdmaximum(@a) { + my @p = @a.unique.sort({$^b <=> $^a}); + if (@p.elems > 2) { + @p[2]; + } else { + @p[0]; + } +} diff --git a/challenge-289/roger-bell-west/raku/ch-2.p6 b/challenge-289/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..322b64c5e0 --- /dev/null +++ b/challenge-289/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,13 @@ +#! /usr/bin/raku + +sub jumble($a) { + my @p = $a.comb; + return @p.pick(@p.elems).join(''); +} + +for lines() { + .chomp; + my $line = $_; + $line ~~ s:g[(<[A..Za..z]>)(<[A..Za..z]><[A..Za..z]>+)(<[A..Za..z]>)] = $0 ~ jumble($1) ~ $2; + say $line; +} diff --git a/challenge-289/roger-bell-west/ruby/ch-1.rb b/challenge-289/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..6084ad7afc --- /dev/null +++ b/challenge-289/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,30 @@ +#! /usr/bin/ruby + +require 'set' + +def thirdmaximum(a) + p = Set.new(a).to_a.sort.reverse + if p.size > 2 + p[2] + else + p[0] + end +end + +require 'test/unit' + +class TestThirdmaximum < Test::Unit::TestCase + + def test_ex1 + assert_equal(4, thirdmaximum([5, 6, 4, 1])) + end + + def test_ex2 + assert_equal(5, thirdmaximum([4, 5])) + end + + def test_ex3 + assert_equal(1, thirdmaximum([1, 2, 2, 3])) + end + +end diff --git a/challenge-289/roger-bell-west/ruby/ch-2.rb b/challenge-289/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..1cc0fcf984 --- /dev/null +++ b/challenge-289/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,12 @@ +#! /usr/bin/ruby + +def jumble(a) + a.chars().shuffle.join +end + +wordre = Regexp.new("([A-Za-z])([A-Za-z][A-Za-z]+)([A-Za-z])") +while line = gets + line = line.chomp + l = line.gsub(wordre) { $1 + jumble($2) + $3 } + print("#{l}\n") +end diff --git a/challenge-289/roger-bell-west/rust/ch-1.rs b/challenge-289/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..1f694d9775 --- /dev/null +++ b/challenge-289/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,29 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(thirdmaximum(vec![5, 6, 4, 1]), 4); +} + +#[test] +fn test_ex2() { + assert_eq!(thirdmaximum(vec![4, 5]), 5); +} + +#[test] +fn test_ex3() { + assert_eq!(thirdmaximum(vec![1, 2, 2, 3]), 1); +} + +fn thirdmaximum(a: Vec<u32>) -> u32 { + let mut p = a.clone(); + p.sort(); + p.reverse(); + p.dedup(); + if p.len() > 2 { + p[2] + } else { + p[0] + } +} diff --git a/challenge-289/roger-bell-west/rust/ch-2.rs b/challenge-289/roger-bell-west/rust/ch-2.rs new file mode 100644 index 0000000000..ac883964b5 --- /dev/null +++ b/challenge-289/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,24 @@ +use rand::seq::SliceRandom; +use rand::thread_rng; +use regex::Captures; +use regex::Regex; +use std::io::{self, BufRead, BufReader}; + +fn jumble(a: &str) -> String { + let mut p = a.chars().collect::<Vec<_>>(); + p.shuffle(&mut thread_rng()); + p.into_iter().collect() +} + +fn main() { + let reader = BufReader::new(io::stdin()); + let wordre = + Regex::new(r"([A-Za-z])([A-Za-z][A-Za-z]+)([A-Za-z])").unwrap(); + for l in reader.lines() { + let line = l.unwrap(); + let result = wordre.replace_all(&line, |caps: &Captures| { + format!("{}{}{}", &caps[1], jumble(&caps[2]), &caps[3]) + }); + println!("{}", result); + } +} diff --git a/challenge-289/roger-bell-west/scala/ch-1.scala b/challenge-289/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..8778808a03 --- /dev/null +++ b/challenge-289/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,32 @@ + +object Thirdmaximum { + def thirdmaximum(a: List[Int]): Int = { + val p = a.distinct.sortWith(_ > _) + if (p.size > 2) { + return p(2) + } else { + return p(0) + } + } + def main(args: Array[String]) { + if (thirdmaximum(List(5, 6, 4, 1)) == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (thirdmaximum(List(4, 5)) == 5) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (thirdmaximum(List(1, 2, 2, 3)) == 1) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-289/roger-bell-west/scala/ch-2.scala b/challenge-289/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..41e317dfd8 --- /dev/null +++ b/challenge-289/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,18 @@ +import scala.util.Random +import scala.util.matching.Regex +import scala.io.Source +// import scala.io.StdIn.readLine + +object Jumbledletters { + def jumble (a: String): String = { + return Random.shuffle(a.split("").toList).mkString("") + } + def main(args: Array[String]) { + val wordre = raw"([A-Za-z])([A-Za-z][A-Za-z]+)([A-Za-z])".r + val input = Source.fromInputStream(System.in); + for (line <- input.getLines) { + println(wordre.replaceAllIn(line, + m => m.group(1) + jumble(m.group(2)) + m.group(3))) + } + } +} diff --git a/challenge-289/roger-bell-west/tests.json b/challenge-289/roger-bell-west/tests.json new file mode 100644 index 0000000000..140b5e0577 --- /dev/null +++ b/challenge-289/roger-bell-west/tests.json @@ -0,0 +1,17 @@ +{ + "ch-1" : [ + { + "function" : "thirdmaximum", + "arguments" : [ 5, 6, 4, 1 ], + "result" : 4 + }, + { + "arguments" : [ 4, 5 ], + "result" : 5 + }, + { + "arguments" : [ 1, 2, 2, 3 ], + "result" : 1 + } + ] +} |
