diff options
23 files changed, 823 insertions, 0 deletions
diff --git a/challenge-280/roger-bell-west/crystal/ch-1.cr b/challenge-280/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..7533649fda --- /dev/null +++ b/challenge-280/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,24 @@ +#! /usr/bin/crystal +require "spec" +describe "twiceappearance" do + it "test_ex1" do + twiceappearance("acbddbca").should eq 'd' + end + it "test_ex2" do + twiceappearance("abccd").should eq 'c' + end + it "test_ex3" do + twiceappearance("abcdabbb").should eq 'a' + end +end + +def twiceappearance(a) + m = Set(Char).new + a.chars.each do |c| + if m.includes?(c) + return c + end + m.add(c) + end + return 'x' +end diff --git a/challenge-280/roger-bell-west/crystal/ch-2.cr b/challenge-280/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..fa7582a5b2 --- /dev/null +++ b/challenge-280/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,29 @@ +#! /usr/bin/crystal +require "spec" +describe "countasterisks" do + it "test_ex1" do + countasterisks("p|*e*rl|w**e|*ekly|").should eq 2 + end + it "test_ex2" do + countasterisks("perl").should eq 0 + end + it "test_ex3" do + countasterisks("th|ewe|e**|k|l***ych|alleng|e").should eq 5 + end +end + +def countasterisks(a) + out = 0 + active = true + a.chars.each do |c| + case c + when '|' + active = !active + when '*' + if active + out += 1 + end + end + end + out +end diff --git a/challenge-280/roger-bell-west/javascript/ch-1.js b/challenge-280/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..e480c21218 --- /dev/null +++ b/challenge-280/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,34 @@ +#! /usr/bin/node + +"use strict" + +function twiceappearance(a) { + let m = new Set; + for (let c of a.split("")) { + if (m.has(c)) { + return c; + } else { + m.add(c); + } + } + return 'x'; +} + +if (twiceappearance('acbddbca') == 'd') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (twiceappearance('abccd') == 'c') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (twiceappearance('abcdabbb') == 'a') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-280/roger-bell-west/javascript/ch-2.js b/challenge-280/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..c23c15ab27 --- /dev/null +++ b/challenge-280/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,39 @@ +#! /usr/bin/node + +"use strict" + +function countasterisks(a) { + let out = 0 + let active = true + for (let c of a.split("")) { + switch (c) { + case '|': + active = !active; + break; + case '*': + if (active) { + out += 1; + } + } + } + return out +} + +if (countasterisks('p|*e*rl|w**e|*ekly|') == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (countasterisks('perl') == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (countasterisks('th|ewe|e**|k|l***ych|alleng|e') == 5) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-280/roger-bell-west/kotlin/ch-1.kt b/challenge-280/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..e7ee6c12b6 --- /dev/null +++ b/challenge-280/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,34 @@ +fun twiceappearance(a: String): Char { + var m = mutableSetOf<Char>() + for (c in a.toList()) { + if (m.contains(c)) { + return c + } else { + m.add(c) + } + } + return 'x' +} + +fun main() { + + if (twiceappearance("acbddbca") == 'd') { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (twiceappearance("abccd") == 'c') { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (twiceappearance("abcdabbb") == 'a') { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-280/roger-bell-west/kotlin/ch-2.kt b/challenge-280/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..0c9e46a8a8 --- /dev/null +++ b/challenge-280/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,38 @@ +fun countasterisks(a: String): Int { + var out = 0 + var active = true + for (c in a.toList()) { + when(c) { + '|' -> { active = !active } + '*' -> { + if (active) { + out += 1 + } + } + } + } + return out +} + +fun main() { + + if (countasterisks("p|*e*rl|w**e|*ekly|") == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (countasterisks("perl") == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (countasterisks("th|ewe|e**|k|l***ych|alleng|e") == 5) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-280/roger-bell-west/lua/ch-1.lua b/challenge-280/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..fddcb8f968 --- /dev/null +++ b/challenge-280/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,45 @@ +#! /usr/bin/lua + +function split(t) + local cl = {} + string.gsub(t, + "(.)", + function(c) + table.insert(cl, c) + end + ) + return cl +end + +function twiceappearance(a) + local m = {} + for _, c in ipairs(split(a)) do + if m[c] ~= nil then + return c + end + m[c] = true + end + return 'x' +end + +if twiceappearance("acbddbca") == "d" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if twiceappearance("abccd") == "c" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if twiceappearance("abcdabbb") == "a" then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-280/roger-bell-west/lua/ch-2.lua b/challenge-280/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..df83492871 --- /dev/null +++ b/challenge-280/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,51 @@ +#! /usr/bin/lua + + +function split(t) + local cl = {} + string.gsub(t, + "(.)", + function(c) + table.insert(cl, c) + end + ) + return cl +end + +function countasterisks(a) + local out = 0 + local active = true + for _, c in ipairs(split(a)) do + if c == '|' then + active = not active + end + if c == '*' then + if active then + out = out + 1 + end + end + end + return out +end + +if countasterisks("p|*e*rl|w**e|*ekly|") == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if countasterisks("perl") == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if countasterisks("th|ewe|e**|k|l***ych|alleng|e") == 5 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-280/roger-bell-west/perl/ch-1.pl b/challenge-280/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..b459221848 --- /dev/null +++ b/challenge-280/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,22 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(twiceappearance('acbddbca'), 'd', 'example 1'); +is(twiceappearance('abccd'), 'c', 'example 2'); +is(twiceappearance('abcdabbb'), 'a', 'example 3'); + +sub twiceappearance($a) { + my %m; + foreach my $c (split '', $a) { + if (exists $m{$c}) { + return $c; + } + $m{$c} = 1; + } + return 'x'; +} diff --git a/challenge-280/roger-bell-west/perl/ch-2.pl b/challenge-280/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..709b441406 --- /dev/null +++ b/challenge-280/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,26 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(countasterisks('p|*e*rl|w**e|*ekly|'), 2, 'example 1'); +is(countasterisks('perl'), 0, 'example 2'); +is(countasterisks('th|ewe|e**|k|l***ych|alleng|e'), 5, 'example 3'); + +sub countasterisks($a) { + my $out = 0; + my $active = 1; + foreach my $c (split '', $a) { + if ($c eq '|') { + $active = 1 - $active; + } elsif ($c eq '*') { + if ($active) { + $out++; + } + } + } + $out; +} diff --git a/challenge-280/roger-bell-west/postscript/ch-1.ps b/challenge-280/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..7e3c46aec8 --- /dev/null +++ b/challenge-280/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,74 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/s2a { + [ exch { } forall ] +} bind def + +/a2s { + 2 dict begin + /i exch def + i length dup string /o exch def + 1 sub 0 exch 1 exch { + dup i 3 -1 roll get o 3 1 roll put + } for + o + end +} 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 + +/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 + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + + +% end included library code + +/twiceappearance { + 0 dict begin + /m 0 dict def + (x) exch + s2a { + /c exch def + m c known { + pop [ c ] a2s + exit + } { + m c true put + } ifelse + } forall + end +} bind def + +(twiceappearance) test.start +(acbddbca) twiceappearance (d) eq test +(abccd) twiceappearance (c) eq test +(abcdabbb) twiceappearance (a) eq test +test.end diff --git a/challenge-280/roger-bell-west/postscript/ch-2.ps b/challenge-280/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..301b6ea926 --- /dev/null +++ b/challenge-280/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,64 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/s2a { + [ exch { } forall ] +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} 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 + +/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 + + +% end included library code + +/countasterisks { + 0 dict begin + /out 0 def + /active true def + s2a { + /c exch def + c 124 eq { + /active active not def + } if + c 42 eq active and { + /out out 1 add def + } if + } forall + out + end +} bind def + +(countasterisks) test.start +(p|*e*rl|w**e|*ekly|) countasterisks 2 eq test +(perl) countasterisks 0 eq test +(th|ewe|e**|k|l***ych|alleng|e) countasterisks 5 eq test +test.end diff --git a/challenge-280/roger-bell-west/python/ch-1.py b/challenge-280/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..c4c8287c5a --- /dev/null +++ b/challenge-280/roger-bell-west/python/ch-1.py @@ -0,0 +1,24 @@ +#! /usr/bin/python3 + +def twiceappearance(a): + m = set() + for c in a: + if c in m: + return c + m.add(c) + return 'x' + +import unittest + +class TestTwiceappearance(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(twiceappearance("acbddbca"), "d", 'example 1') + + def test_ex2(self): + self.assertEqual(twiceappearance("abccd"), "c", 'example 2') + + def test_ex3(self): + self.assertEqual(twiceappearance("abcdabbb"), "a", 'example 3') + +unittest.main() diff --git a/challenge-280/roger-bell-west/python/ch-2.py b/challenge-280/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..c03eba6bab --- /dev/null +++ b/challenge-280/roger-bell-west/python/ch-2.py @@ -0,0 +1,28 @@ +#! /usr/bin/python3 + +def countasterisks(a): + out = 0 + active = True + for c in a: + match c: + case '|': + active = not active + case '*': + if active: + out += 1 + return out + +import unittest + +class TestCountasterisks(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(countasterisks("p|*e*rl|w**e|*ekly|"), 2, 'example 1') + + def test_ex2(self): + self.assertEqual(countasterisks("perl"), 0, 'example 2') + + def test_ex3(self): + self.assertEqual(countasterisks("th|ewe|e**|k|l***ych|alleng|e"), 5, 'example 3') + +unittest.main() diff --git a/challenge-280/roger-bell-west/raku/ch-1.p6 b/challenge-280/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..3ee18151de --- /dev/null +++ b/challenge-280/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,20 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(twiceappearance('acbddbca'), 'd', 'example 1'); +is(twiceappearance('abccd'), 'c', 'example 2'); +is(twiceappearance('abcdabbb'), 'a', 'example 3'); + +sub twiceappearance($a) { + my %m = SetHash.new; + for $a.comb -> $c { + if (%m{$c}:exists) { + return $c + } + %m{$c}++; + } + return 'x'; +} diff --git a/challenge-280/roger-bell-west/raku/ch-2.p6 b/challenge-280/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..59f966d656 --- /dev/null +++ b/challenge-280/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,27 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(countasterisks('p|*e*rl|w**e|*ekly|'), 2, 'example 1'); +is(countasterisks('perl'), 0, 'example 2'); +is(countasterisks('th|ewe|e**|k|l***ych|alleng|e'), 5, 'example 3'); + +sub countasterisks($a) { + my $out = 0; + my $active = True; + for $a.comb -> $c { + given $c { + when '|' { + $active = !$active; + } + when '*' { + if ($active) { + $out++; + } + } + } + } + $out; +} diff --git a/challenge-280/roger-bell-west/ruby/ch-1.rb b/challenge-280/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..a23d04a809 --- /dev/null +++ b/challenge-280/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,32 @@ +#! /usr/bin/ruby + +require 'set' + +def twiceappearance(a) + m = Set.new + a.chars.each do |c| + if m.include?(c) + return c + end + m.add(c) + end + return 'x' +end + +require 'test/unit' + +class TestTwiceappearance < Test::Unit::TestCase + + def test_ex1 + assert_equal('d', twiceappearance('acbddbca')) + end + + def test_ex2 + assert_equal('c', twiceappearance('abccd')) + end + + def test_ex3 + assert_equal('a', twiceappearance('abcdabbb')) + end + +end diff --git a/challenge-280/roger-bell-west/ruby/ch-2.rb b/challenge-280/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..1987b0273f --- /dev/null +++ b/challenge-280/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,35 @@ +#! /usr/bin/ruby + +def countasterisks(a) + out = 0 + active = true + a.chars.each do |c| + case c + when '|' + active = !active + when '*' + if active + out += 1 + end + end + end + out +end + +require 'test/unit' + +class TestCountasterisks < Test::Unit::TestCase + + def test_ex1 + assert_equal(2, countasterisks('p|*e*rl|w**e|*ekly|')) + end + + def test_ex2 + assert_equal(0, countasterisks('perl')) + end + + def test_ex3 + assert_equal(5, countasterisks('th|ewe|e**|k|l***ych|alleng|e')) + end + +end diff --git a/challenge-280/roger-bell-west/rust/ch-1.rs b/challenge-280/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..d1a67d45f2 --- /dev/null +++ b/challenge-280/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,30 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +use std::collections::HashSet; + +#[test] +fn test_ex1() { + assert_eq!(twiceappearance("acbddbca"), 'd'); +} + +#[test] +fn test_ex2() { + assert_eq!(twiceappearance("abccd"), 'c'); +} + +#[test] +fn test_ex3() { + assert_eq!(twiceappearance("abcdabbb"), 'a'); +} + +fn twiceappearance(a: &str) -> char { + let mut m = HashSet::new(); + for c in a.chars() { + if m.contains(&c) { + return c; + } + m.insert(c); + } + 'x' +} diff --git a/challenge-280/roger-bell-west/rust/ch-2.rs b/challenge-280/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..23d1818ebb --- /dev/null +++ b/challenge-280/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,34 @@ +#! /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!(countasterisks("p|*e*rl|w**e|*ekly|"), 2); +} + +#[test] +fn test_ex2() { + assert_eq!(countasterisks("perl"), 0); +} + +#[test] +fn test_ex3() { + assert_eq!(countasterisks("th|ewe|e**|k|l***ych|alleng|e"), 5); +} + +fn countasterisks(a: &str) -> u32 { + let mut out = 0; + let mut active = true; + for c in a.chars() { + match c { + '|' => active = !active, + '*' => { + if active { + out += 1 + } + } + _ => {} + } + } + out +} diff --git a/challenge-280/roger-bell-west/scala/ch-1.scala b/challenge-280/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..0852a2f09d --- /dev/null +++ b/challenge-280/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,41 @@ +import scala.collection.mutable + +object Twiceappearance { + def twiceappearance(a: String): Char = { + var m = mutable.Set.empty[Char] + var ex = false + var out = 'x' + for (c <- a.toList) { + if (!ex) { + if (m.contains(c)) { + out = c + ex = true + } else { + m += c + } + } + } + return out + } + def main(args: Array[String]) { + if (twiceappearance("acbddbca") == 'd') { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (twiceappearance("abccd") == 'c') { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (twiceappearance("abcdabbb") == 'a') { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-280/roger-bell-west/scala/ch-2.scala b/challenge-280/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..4e10fd755b --- /dev/null +++ b/challenge-280/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,40 @@ + +object Countasterisks { + def countasterisks(a: String): Int = { + var out = 0 + var active = true + for (c <- a.toList) { + c match { + case '|' => { active = !active } + case '*' => { + if (active) { + out += 1 + } + } + case _ => { } + } + } + return out + } + def main(args: Array[String]) { + if (countasterisks("p|*e*rl|w**e|*ekly|") == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (countasterisks("perl") == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (countasterisks("th|ewe|e**|k|l***ych|alleng|e") == 5) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-280/roger-bell-west/tests.json b/challenge-280/roger-bell-west/tests.json new file mode 100644 index 0000000000..23527596b2 --- /dev/null +++ b/challenge-280/roger-bell-west/tests.json @@ -0,0 +1,32 @@ +{ + "ch-1" : [ + { + "function" : "twiceappearance", + "arguments" : "acbddbca", + "result" : "d" + }, + { + "arguments" : "abccd", + "result" : "c" + }, + { + "arguments" : "abcdabbb", + "result" : "a" + } + ], + "ch-2" : [ + { + "function" : "countasterisks", + "arguments" : "p|*e*rl|w**e|*ekly|", + "result" : 2 + }, + { + "arguments" : "perl", + "result" : 0 + }, + { + "arguments" : "th|ewe|e**|k|l***ych|alleng|e", + "result" : 5 + } + ] +} |
