diff options
23 files changed, 648 insertions, 0 deletions
diff --git a/challenge-272/roger-bell-west/crystal/ch-1.cr b/challenge-272/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..032e4213b7 --- /dev/null +++ b/challenge-272/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,15 @@ +#! /usr/bin/crystal + +def defrangip(a) + return a.gsub(".", "[.]") +end + +require "spec" +describe "defrangip" do + it "test_ex1" do + defrangip("1.1.1.1").should eq "1[.]1[.]1[.]1" + end + it "test_ex2" do + defrangip("255.101.1.0").should eq "255[.]101[.]1[.]0" + end +end diff --git a/challenge-272/roger-bell-west/crystal/ch-2.cr b/challenge-272/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..bcea3f7fe1 --- /dev/null +++ b/challenge-272/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,22 @@ +#! /usr/bin/crystal + +def stringscore(a) + out = 0 + a.chars.each_cons(2) do |c| + out += (c[0].ord() - c[1].ord()).abs + end + out +end + +require "spec" +describe "stringscore" do + it "test_ex1" do + stringscore("hello").should eq 13 + end + it "test_ex2" do + stringscore("perl").should eq 30 + end + it "test_ex3" do + stringscore("raku").should eq 37 + end +end diff --git a/challenge-272/roger-bell-west/javascript/ch-1.js b/challenge-272/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..27d2fa1a36 --- /dev/null +++ b/challenge-272/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,20 @@ +#! /usr/bin/node + +"use strict" + +function defrangip(a) { + return a.replace(/\./g, "[.]"); +} + +if (defrangip('1.1.1.1') == '1[.]1[.]1[.]1') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (defrangip('255.101.1.0') == '255[.]101[.]1[.]0') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-272/roger-bell-west/javascript/ch-2.js b/challenge-272/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..96364dc7c8 --- /dev/null +++ b/challenge-272/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,39 @@ +#! /usr/bin/node + +"use strict" + +// by VLAZ +// https://stackoverflow.com/a/59322890 +function toWindows(inputArray, size) { + return Array.from( + {length: inputArray.length - (size - 1)}, //get the appropriate length + (_, index) => inputArray.slice(index, index+size) //create the windows + ) +} + +function stringscore(a) { + let out = 0; + for (let t = 0; t < a.length - 1; t++) { + out += Math.abs(a.charCodeAt(t) - a.charCodeAt(t + 1)); + } + return out; +} + +if (stringscore('hello') == 13) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (stringscore('perl') == 30) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (stringscore('raku') == 37) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-272/roger-bell-west/kotlin/ch-1.kt b/challenge-272/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..495b14c77b --- /dev/null +++ b/challenge-272/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,20 @@ +fun defrangip(a: String): String { + return a.replace(".", "[.]") +} + +fun main() { + + if (defrangip("1.1.1.1") == "1[.]1[.]1[.]1") { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (defrangip("255.101.1.0") == "255[.]101[.]1[.]0") { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-272/roger-bell-west/kotlin/ch-2.kt b/challenge-272/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..ee575717b9 --- /dev/null +++ b/challenge-272/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,30 @@ +fun stringscore(a: String): Int { + var out = 0 + for (i in a.toList().windowed(size = 2)) { + out += Math.abs(i[0].code - i[1].code) + } + return out +} + +fun main() { + + if (stringscore("hello") == 13) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (stringscore("perl") == 30) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (stringscore("raku") == 37) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-272/roger-bell-west/lua/ch-1.lua b/challenge-272/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..f8b4f261b7 --- /dev/null +++ b/challenge-272/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,20 @@ +#! /usr/bin/lua + +function defrangip(a) + return string.gsub(a, "[.]", "[.]") + end + +if defrangip("1.1.1.1") == "1[.]1[.]1[.]1" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if defrangip("255.101.1.0") == "255[.]101[.]1[.]0" then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-272/roger-bell-west/lua/ch-2.lua b/challenge-272/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..7bf1100382 --- /dev/null +++ b/challenge-272/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,43 @@ +#! /usr/bin/lua + +function split(t) + local cl = {} + string.gsub(t, + "(.)", + function(c) + table.insert(cl, c) + end + ) + return cl +end + +function stringscore(a) + local out = 0 + local c = split(a) + for n = 1, #c - 1 do + out = out + math.abs(string.byte(c[n]) - string.byte(c[n + 1])) + end + return out +end + +if stringscore("hello") == 13 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if stringscore("perl") == 30 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if stringscore("raku") == 37 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-272/roger-bell-west/perl/ch-1.pl b/challenge-272/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..bd91e55ec6 --- /dev/null +++ b/challenge-272/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,15 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(defrangip('1.1.1.1'), '1[.]1[.]1[.]1', 'example 1'); +is(defrangip('255.101.1.0'), '255[.]101[.]1[.]0', 'example 2'); + +sub defrangip($a) { + (my $b = $a) =~ s/\./[.]/g; + return $b; +} diff --git a/challenge-272/roger-bell-west/perl/ch-2.pl b/challenge-272/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..f14041e844 --- /dev/null +++ b/challenge-272/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,20 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(stringscore('hello'), 13, 'example 1'); +is(stringscore('perl'), 30, 'example 2'); +is(stringscore('raku'), 37, 'example 3'); + +sub stringscore($a) { + my $out = 0; + my @c = split '',$a; + foreach my $i (0 .. $#c - 1) { + $out += abs(ord($c[$i]) - ord($c[$i+1])); + } + return $out; +} diff --git a/challenge-272/roger-bell-west/postscript/ch-1.ps b/challenge-272/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..71a4deae7c --- /dev/null +++ b/challenge-272/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,70 @@ +%!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.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 + +/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 + +/defrangip { + [ exch + s2a + { + dup 46 eq { + 91 exch + 93 + } if + } forall + ] + a2s +} bind def + +(defrangip) test.start +(1.1.1.1) defrangip (1[.]1[.]1[.]1) eq test +(255.101.1.0) defrangip (255[.]101[.]1[.]0) eq test +test.end diff --git a/challenge-272/roger-bell-west/postscript/ch-2.ps b/challenge-272/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..c1de213b51 --- /dev/null +++ b/challenge-272/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,75 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + +/rotor { + 5 dict begin + /delta exch def + /size exch def + dup length /len exch def + /ar exch def + /ix 0 def + [ + { + ix size add len gt { + exit + } if + ar ix size getinterval + /ix ix size delta add add def + } loop + ] + 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 + +/s2a { + [ exch { } forall ] +} bind def + + +% end included library code + +/stringscore { + 0 exch + s2a + 2 -1 rotor + { + aload pop sub abs add + } forall +} bind def + +(stringscore) test.start +(hello) stringscore 13 eq test +(perl) stringscore 30 eq test +(raku) stringscore 37 eq test +test.end diff --git a/challenge-272/roger-bell-west/python/ch-1.py b/challenge-272/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..2fbe357488 --- /dev/null +++ b/challenge-272/roger-bell-west/python/ch-1.py @@ -0,0 +1,16 @@ +#! /usr/bin/python3 + +def defrangip(a): + return a.replace(".", "[.]") + +import unittest + +class TestDefrangip(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(defrangip("1.1.1.1"), "1[.]1[.]1[.]1", 'example 1') + + def test_ex2(self): + self.assertEqual(defrangip("255.101.1.0"), "255[.]101[.]1[.]0", 'example 2') + +unittest.main() diff --git a/challenge-272/roger-bell-west/python/ch-2.py b/challenge-272/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..4332977373 --- /dev/null +++ b/challenge-272/roger-bell-west/python/ch-2.py @@ -0,0 +1,36 @@ +#! /usr/bin/python3 + +import collections +from itertools import islice + +# https://docs.python.org/3/library/itertools.html +def sliding_window(iterable, n): + # sliding_window('ABCDEFG', 4) --> ABCD BCDE CDEF DEFG + it = iter(iterable) + window = collections.deque(islice(it, n), maxlen=n) + if len(window) == n: + yield tuple(window) + for x in it: + window.append(x) + yield tuple(window) + +def stringscore(a): + out = 0 + for i in sliding_window(a, 2): + out += abs(ord(i[0]) - ord(i[1])) + return out + +import unittest + +class TestStringscore(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(stringscore("hello"), 13, 'example 1') + + def test_ex2(self): + self.assertEqual(stringscore("perl"), 30, 'example 2') + + def test_ex3(self): + self.assertEqual(stringscore("raku"), 37, 'example 3') + +unittest.main() diff --git a/challenge-272/roger-bell-west/raku/ch-1.p6 b/challenge-272/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..fe923d38d0 --- /dev/null +++ b/challenge-272/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,13 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(defrangip('1.1.1.1'), '1[.]1[.]1[.]1', 'example 1'); +is(defrangip('255.101.1.0'), '255[.]101[.]1[.]0', 'example 2'); + +sub defrangip($a) { + (my $b = $a) ~~ s:g/\./[.]/; + return $b; +} diff --git a/challenge-272/roger-bell-west/raku/ch-2.p6 b/challenge-272/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..7f67141e39 --- /dev/null +++ b/challenge-272/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,18 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(stringscore('hello'), 13, 'example 1'); +is(stringscore('perl'), 30, 'example 2'); +is(stringscore('raku'), 37, 'example 3'); + +sub stringscore($a) { + my $out = 0; + my @c = $a.comb; + for @c.rotor(2 => -1) -> @i { + $out += abs(ord(@i[0]) - ord(@i[1])); + } + return $out; +} diff --git a/challenge-272/roger-bell-west/ruby/ch-1.rb b/challenge-272/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..82152f9fd3 --- /dev/null +++ b/challenge-272/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,19 @@ +#! /usr/bin/ruby + +def defrangip(a) + return a.gsub(".", "[.]") +end + +require 'test/unit' + +class TestDefrangip < Test::Unit::TestCase + + def test_ex1 + assert_equal('1[.]1[.]1[.]1', defrangip('1.1.1.1')) + end + + def test_ex2 + assert_equal('255[.]101[.]1[.]0', defrangip('255.101.1.0')) + end + +end diff --git a/challenge-272/roger-bell-west/ruby/ch-2.rb b/challenge-272/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..f198380624 --- /dev/null +++ b/challenge-272/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,27 @@ +#! /usr/bin/ruby + +def stringscore(a) + out = 0 + a.chars.each_cons(2) do |c| + out += (c[0].ord() - c[1].ord()).abs + end + out +end + +require 'test/unit' + +class TestStringscore < Test::Unit::TestCase + + def test_ex1 + assert_equal(13, stringscore('hello')) + end + + def test_ex2 + assert_equal(30, stringscore('perl')) + end + + def test_ex3 + assert_equal(37, stringscore('raku')) + end + +end diff --git a/challenge-272/roger-bell-west/rust/ch-1.rs b/challenge-272/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..ef13992b11 --- /dev/null +++ b/challenge-272/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,24 @@ +#! /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!(defrangip("1.1.1.1"), "1[.]1[.]1[.]1"); +} + +#[test] +fn test_ex2() { + assert_eq!(defrangip("255.101.1.0"), "255[.]101[.]1[.]0"); +} + +fn defrangip(a: &str) -> String { + let mut out = String::new(); + for c in a.chars() { + if c == '.' { + out.push_str("[.]"); + } else { + out.push(c); + } + } + out +} diff --git a/challenge-272/roger-bell-west/rust/ch-2.rs b/challenge-272/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..c95b015b88 --- /dev/null +++ b/challenge-272/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,25 @@ +#! /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!(stringscore("hello"), 13); +} + +#[test] +fn test_ex2() { + assert_eq!(stringscore("perl"), 30); +} + +#[test] +fn test_ex3() { + assert_eq!(stringscore("raku"), 37); +} + +fn stringscore(a: &str) -> u32 { + let mut out = 0; + for i in a.chars().collect::<Vec<char>>().windows(2) { + out += ((i[0] as i32) - (i[1] as i32)).abs(); + } + out as u32 +} diff --git a/challenge-272/roger-bell-west/scala/ch-1.scala b/challenge-272/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..9cfa5dcc61 --- /dev/null +++ b/challenge-272/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,20 @@ +object Defrangip { + def defrangip(a: String): String = { + a.replaceAll("\\.", "[.]") + } + def main(args: Array[String]) { + if (defrangip("1.1.1.1") == "1[.]1[.]1[.]1") { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (defrangip("255.101.1.0") == "255[.]101[.]1[.]0") { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-272/roger-bell-west/scala/ch-2.scala b/challenge-272/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..d7bebf840d --- /dev/null +++ b/challenge-272/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,32 @@ + +object Stringscore { + def stringscore(a: String): Int = { + var out = 0 + val c = a.toList + for (i <- c.sliding(2)) { + out += (i(0).toInt - i(1).toInt).abs + } + out + } + def main(args: Array[String]) { + if (stringscore("hello") == 13) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (stringscore("perl") == 30) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (stringscore("raku") == 37) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-272/roger-bell-west/tests.json b/challenge-272/roger-bell-west/tests.json new file mode 100644 index 0000000000..2eb8bad37e --- /dev/null +++ b/challenge-272/roger-bell-west/tests.json @@ -0,0 +1,29 @@ +{ + "ch-1" : [ + { + "function" : "defrangip", + "arguments" : "1.1.1.1", + "result" : "1[.]1[.]1[.]1" + }, + { + "function" : "defrangip", + "arguments" : "255.101.1.0", + "result" : "255[.]101[.]1[.]0" + } + ], + "ch-2" : [ + { + "function" : "stringscore", + "arguments" : "hello", + "result" : 13 + }, + { + "arguments" : "perl", + "result" : 30 + }, + { + "arguments" : "raku", + "result" : 37 + } + ] +} |
