diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-25 22:27:26 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-25 22:27:26 +0100 |
| commit | 9a8f0b8043c806ea406fdaddc718ccd263fbc093 (patch) | |
| tree | 6084b8d9d56b7370d12c2de83e952ae23bb4eb90 | |
| parent | 815048f1183179a10ebb61c46e2c4160925b837b (diff) | |
| parent | 508b49f4cef1ec360e5a9ec738c46f60a92c3c0b (diff) | |
| download | perlweeklychallenge-club-9a8f0b8043c806ea406fdaddc718ccd263fbc093.tar.gz perlweeklychallenge-club-9a8f0b8043c806ea406fdaddc718ccd263fbc093.tar.bz2 perlweeklychallenge-club-9a8f0b8043c806ea406fdaddc718ccd263fbc093.zip | |
Merge pull request #10325 from Firedrake/rogerbw-challenge-275
RogerBW solutions for challenge no. 275
23 files changed, 1003 insertions, 0 deletions
diff --git a/challenge-275/roger-bell-west/crystal/ch-1.cr b/challenge-275/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..3358f9d540 --- /dev/null +++ b/challenge-275/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,26 @@ +#! /usr/bin/crystal + +def brokenkeys(a, k) + out = 0 + failset = Set.new(k.map{ |c| c.downcase }) + a.split(" ").each do |word| + wordset = Set.new(word.downcase.split("")) + if (wordset & failset).size == 0 + out += 1 + end + end + out +end + +require "spec" +describe "brokenkeys" do + it "test_ex1" do + brokenkeys("Perl Weekly Challenge", ["l", "a"]).should eq 0 + end + it "test_ex2" do + brokenkeys("Perl and Raku", ["a"]).should eq 1 + end + it "test_ex3" do + brokenkeys("Well done Team PWC", ["l", "o"]).should eq 2 + end +end diff --git a/challenge-275/roger-bell-west/crystal/ch-2.cr b/challenge-275/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..136354dfd9 --- /dev/null +++ b/challenge-275/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,32 @@ +#! /usr/bin/crystal + +def replacedigits(a) + out = "" + prev = 0 + a.chars.each do |c| + out += case c + when .number? + (prev + c.to_i).chr + else + prev = c.ord + c + end + end + out +end + +require "spec" +describe "replacedigits" do + it "test_ex1" do + replacedigits("a1c1e1").should eq "abcdef" + end + it "test_ex2" do + replacedigits("a1b2c3d4").should eq "abbdcfdh" + end + it "test_ex3" do + replacedigits("b2b").should eq "bdb" + end + it "test_ex4" do + replacedigits("a16z").should eq "abgz" + end +end diff --git a/challenge-275/roger-bell-west/javascript/ch-1.js b/challenge-275/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..08d76e96ab --- /dev/null +++ b/challenge-275/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,35 @@ +#! /usr/bin/node + +"use strict" + +function brokenkeys(a, k) { + let out = 0; + const failset = new Set(k.map(c => c.toLowerCase())) + for (let word of a.split(" ")) { + const wordset = new Set(word.toLowerCase().split("")); + const intersect = new Set([...failset].filter(i => wordset.has(i))); + if (intersect.size == 0) { + out++; + } + } + return out; +} + +if (brokenkeys('Perl Weekly Challenge', ['l', 'a']) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (brokenkeys('Perl and Raku', ['a']) == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (brokenkeys('Well done Team PWC', ['l', 'o']) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-275/roger-bell-west/javascript/ch-2.js b/challenge-275/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..db3ee7311a --- /dev/null +++ b/challenge-275/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,42 @@ +#! /usr/bin/node + +"use strict" + +function replacedigits(a) { + let out = ""; + let prev = 0; + for (let c of a.split("")) { + if (c >= '0' && c <= '9') { + out += String.fromCharCode(prev + parseInt(c, 10)); + } else { + prev = c.charCodeAt(0); + out += c; + } + } + return out; +} + +if (replacedigits('a1c1e1') == 'abcdef') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (replacedigits('a1b2c3d4') == 'abbdcfdh') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (replacedigits('b2b') == 'bdb') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (replacedigits('a16z') == 'abgz') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-275/roger-bell-west/kotlin/ch-1.kt b/challenge-275/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..d657dc5156 --- /dev/null +++ b/challenge-275/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,34 @@ +fun brokenkeys(a: String, k: List<Char>): Int { + var out = 0 + val failset: Set<Char> = k.map{it.lowercaseChar()}.toSet() + for (word in a.split(" ")) { + val wordset: Set<Char> = word.lowercase().toSet() + if (failset.intersect(wordset).isEmpty()) { + out += 1 + } + } + return out +} + +fun main() { + + if (brokenkeys("Perl Weekly Challenge", listOf('l', 'a')) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (brokenkeys("Perl and Raku", listOf('a')) == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (brokenkeys("Well done Team PWC", listOf('l', 'o')) == 2) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-275/roger-bell-west/kotlin/ch-2.kt b/challenge-275/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..78838490d3 --- /dev/null +++ b/challenge-275/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,42 @@ +fun replacedigits(a: String): String { + var out = "" + var prev = 0 + for (c in a.toList()) { + if (c >= '0' && c <= '9') { + out += (prev + c.digitToInt()).toChar() + } else { + prev = c.code + out += c + } + } + return out +} + +fun main() { + + if (replacedigits("a1c1e1") == "abcdef") { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (replacedigits("a1b2c3d4") == "abbdcfdh") { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (replacedigits("b2b") == "bdb") { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (replacedigits("a16z") == "abgz") { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-275/roger-bell-west/lua/ch-1.lua b/challenge-275/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..456f420de6 --- /dev/null +++ b/challenge-275/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,70 @@ +#! /usr/bin/lua + +-- bart at https://stackoverflow.com/questions/1426954/split-string-in-lua +function splits(inputstr, sep) + sep=sep or '%s' + local t={} + for field,s in string.gmatch(inputstr, "([^"..sep.."]*)("..sep.."?)") do + table.insert(t,field) + if s=="" then + return t + end + end +end + +function split(t) + local cl = {} + string.gsub(t, + "(.)", + function(c) + table.insert(cl, c) + end + ) + return cl +end + +function brokenkeys(a, k) + local out = 0 + local failset = {} + for _, c in ipairs(k) do + failset[string.lower(c)] = true + end + for _, word in ipairs(splits(string.lower(a), " ")) do + local wordset = {} + for _b, c in ipairs(split(word)) do + wordset[c] = true + end + local intersect = {} + for k, v in pairs(failset) do + if wordset[k] ~= nil then + table.insert(intersect, k) + end + end + if #intersect == 0 then + out = out + 1 + end + end + return out +end + +if brokenkeys("Perl Weekly Challenge", {"l", "a"}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if brokenkeys("Perl and Raku", {"a"}) == 1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if brokenkeys("Well done Team PWC", {"l", "o"}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-275/roger-bell-west/lua/ch-2.lua b/challenge-275/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..021d1aa368 --- /dev/null +++ b/challenge-275/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,63 @@ +#! /usr/bin/lua + +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 replacedigits(a) + local out = {} + local prev = 0 + for _, c in ipairs(split(a)) do + if c >= "0" and c <= "9" then + table.insert(out, string.char(prev + c)) + else + prev = string.byte(c) + table.insert(out, c) + end + end + return join(out) +end + +if replacedigits("a1c1e1") == "abcdef" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if replacedigits("a1b2c3d4") == "abbdcfdh" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if replacedigits("b2b") == "bdb" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if replacedigits("a16z") == "abgz" then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-275/roger-bell-west/perl/ch-1.pl b/challenge-275/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..3d94697a39 --- /dev/null +++ b/challenge-275/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,24 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(brokenkeys('Perl Weekly Challenge', ['l', 'a']), 0, 'example 1'); +is(brokenkeys('Perl and Raku', ['a']), 1, 'example 2'); +is(brokenkeys('Well done Team PWC', ['l', 'o']), 2, 'example 3'); + +sub brokenkeys($a, $k) { + my $out = 0; + my %failset = map {lc($_) => 1} @{$k}; + foreach my $word (split ' ', $a) { + my %wordset = map {$_ => 1} split '',lc($word); + my @intersect = grep {exists $wordset{$_}} keys %failset; + if (scalar @intersect == 0) { + $out++; + } + } + return $out; +} diff --git a/challenge-275/roger-bell-west/perl/ch-2.pl b/challenge-275/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..8b2e458e23 --- /dev/null +++ b/challenge-275/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 => 4; + +is(replacedigits('a1c1e1'), 'abcdef', 'example 1'); +is(replacedigits('a1b2c3d4'), 'abbdcfdh', 'example 2'); +is(replacedigits('b2b'), 'bdb', 'example 3'); +is(replacedigits('a16z'), 'abgz', 'example 4'); + +sub replacedigits($a) { + my @out; + my $prev = 0; + foreach my $c (split '', $a) { + if ($c ge '0' && $c le '9') { + push @out, chr($prev + $c); + } else { + $prev = ord($c); + push @out, $c; + } + } + return join('', @out); +} diff --git a/challenge-275/roger-bell-west/postscript/ch-1.ps b/challenge-275/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..f8a2e44f93 --- /dev/null +++ b/challenge-275/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,152 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} bind def + +/strsplit % (ajbjc) (j) -> [ (a) (b) (c) ] +{ + 1 dict begin + /sep exch def + [ exch + { + dup length 0 eq { + pop + exit + } { + sep search { + exch pop + dup length 0 eq { + pop + } { + exch + } ifelse + } { + () + } ifelse + } ifelse + } loop + ] + end +} 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.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/tolower { + s2a + [ exch + { + dup dup 65 ge exch 90 le and { + 32 add + } if + } forall + ] a2s +} bind def + +/toset { % array -> dict of (value, true) + << exch + { + true + } forall + >> +} bind def + +/set.intersection { + 4 dict begin + /s 0 dict def + /b exch def + /a exch def + a keys { + /k exch def + b k known { + s k true put + } if + } forall + s + end +} bind def + +/s2a { + [ exch { } forall ] +} bind def + + +/map { % array proc -> array + 2 dict begin + /p exch def + [ exch + { + p + } forall + ] + end +} bind def + + +% end included library code + +/brokenkeys { + 0 dict begin + { tolower s2a 0 get } map toset /failset exch def + 0 exch + tolower + ( ) strsplit { + s2a toset /wordset exch def + failset wordset set.intersection length 0 eq { + 1 add + } if + } forall + end +} bind def + +(brokenkeys) test.start +(Perl Weekly Challenge) [(l) (a)] brokenkeys 0 eq test +(Perl and Raku) [(a)] brokenkeys 1 eq test +(Well done Team PWC) [(l) (o)] brokenkeys 2 eq test +test.end diff --git a/challenge-275/roger-bell-west/postscript/ch-2.ps b/challenge-275/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..ecfd8b9846 --- /dev/null +++ b/challenge-275/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,77 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + +/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 + + +% end included library code + +/replacedigits { + 0 dict begin + /prev 0 def + [ exch + s2a { + /c exch def + c 48 ge c 57 le and { + c 48 sub prev add + } { + /prev c def + c + } ifelse + } forall + ] + a2s + end +} bind def + +(replacedigits) test.start +(a1c1e1) replacedigits (abcdef) eq test +(a1b2c3d4) replacedigits (abbdcfdh) eq test +(b2b) replacedigits (bdb) eq test +(a16z) replacedigits (abgz) eq test +test.end diff --git a/challenge-275/roger-bell-west/python/ch-1.py b/challenge-275/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..0ad687feea --- /dev/null +++ b/challenge-275/roger-bell-west/python/ch-1.py @@ -0,0 +1,25 @@ +#! /usr/bin/python3 + +def brokenkeys(a, k): + out = 0 + failset = set(c.lower() for c in k) + for word in a.lower().split(" "): + wordset = set(word) + if len(failset.intersection(wordset)) == 0: + out += 1 + return out + +import unittest + +class TestBrokenkeys(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(brokenkeys("Perl Weekly Challenge", ["l", "a"]), 0, 'example 1') + + def test_ex2(self): + self.assertEqual(brokenkeys("Perl and Raku", ["a"]), 1, 'example 2') + + def test_ex3(self): + self.assertEqual(brokenkeys("Well done Team PWC", ["l", "o"]), 2, 'example 3') + +unittest.main() diff --git a/challenge-275/roger-bell-west/python/ch-2.py b/challenge-275/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..fe1a9c1eee --- /dev/null +++ b/challenge-275/roger-bell-west/python/ch-2.py @@ -0,0 +1,32 @@ +#! /usr/bin/python3 + +def replacedigits(a): + out = "" + prev = 0 + digits = {chr(asc) for asc in list(range(ord('0'), ord('9')+1))} + for c in a: + if c >= '0' and c <= '9': + out += chr(prev + int(c)) + else: + prev = ord(c) + out += c + return out + + +import unittest + +class TestReplacedigits(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(replacedigits("a1c1e1"), "abcdef", 'example 1') + + def test_ex2(self): + self.assertEqual(replacedigits("a1b2c3d4"), "abbdcfdh", 'example 2') + + def test_ex3(self): + self.assertEqual(replacedigits("b2b"), "bdb", 'example 3') + + def test_ex4(self): + self.assertEqual(replacedigits("a16z"), "abgz", 'example 4') + +unittest.main() diff --git a/challenge-275/roger-bell-west/raku/ch-1.p6 b/challenge-275/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..57ca12d0d0 --- /dev/null +++ b/challenge-275/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,21 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(brokenkeys('Perl Weekly Challenge', ['l', 'a']), 0, 'example 1'); +is(brokenkeys('Perl and Raku', ['a']), 1, 'example 2'); +is(brokenkeys('Well done Team PWC', ['l', 'o']), 2, 'example 3'); + +sub brokenkeys($a, @k) { + my $out = 0; + my %failset = Set(@k.map({$_.lc})); + for $a.split(" ") -> $word { + my %wordset = Set($word.lc.comb); + if ((%wordset (&) %failset).elems == 0) { + $out++; + } + } + return $out; +} diff --git a/challenge-275/roger-bell-west/raku/ch-2.p6 b/challenge-275/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..cccabd6226 --- /dev/null +++ b/challenge-275/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,24 @@ +#! /usr/bin/raku + +use Test; + +plan 4; + +is(replacedigits('a1c1e1'), 'abcdef', 'example 1'); +is(replacedigits('a1b2c3d4'), 'abbdcfdh', 'example 2'); +is(replacedigits('b2b'), 'bdb', 'example 3'); +is(replacedigits('a16z'), 'abgz', 'example 4'); + +sub replacedigits($a) { + my @out; + my $prev = 0; + for $a.comb -> $c { + if ($c ge '0' && $c le '9') { + @out.push(chr($prev + $c)); + } else { + $prev = ord($c); + @out.push($c); + } + } + return @out.join(''); +} diff --git a/challenge-275/roger-bell-west/ruby/ch-1.rb b/challenge-275/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..59ae2e5c1f --- /dev/null +++ b/challenge-275/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,33 @@ +#! /usr/bin/ruby + +require 'set' + +def brokenkeys(a, k) + out = 0 + failset = Set.new(k.map{ |c| c.downcase }) + a.split(" ").each do |word| + wordset = Set.new(word.downcase.split("")) + if (wordset & failset).size == 0 + out += 1 + end + end + out +end + +require 'test/unit' + +class TestBrokenkeys < Test::Unit::TestCase + + def test_ex1 + assert_equal(0, brokenkeys('Perl Weekly Challenge', ['l', 'a'])) + end + + def test_ex2 + assert_equal(1, brokenkeys('Perl and Raku', ['a'])) + end + + def test_ex3 + assert_equal(2, brokenkeys('Well done Team PWC', ['l', 'o'])) + end + +end diff --git a/challenge-275/roger-bell-west/ruby/ch-2.rb b/challenge-275/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..8faa561bff --- /dev/null +++ b/challenge-275/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,38 @@ +#! /usr/bin/ruby + +def replacedigits(a) + out = "" + prev = 0 + a.chars.each do |c| + out += case c + when '0'..'9' + (prev + c.to_i).chr + else + prev = c.ord + c + end + end + out +end + +require 'test/unit' + +class TestReplacedigits < Test::Unit::TestCase + + def test_ex1 + assert_equal('abcdef', replacedigits('a1c1e1')) + end + + def test_ex2 + assert_equal('abbdcfdh', replacedigits('a1b2c3d4')) + end + + def test_ex3 + assert_equal('bdb', replacedigits('b2b')) + end + + def test_ex4 + assert_equal('abgz', replacedigits('a16z')) + end + +end diff --git a/challenge-275/roger-bell-west/rust/ch-1.rs b/challenge-275/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..8c8d634f50 --- /dev/null +++ b/challenge-275/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,38 @@ +#! /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!(brokenkeys("Perl Weekly Challenge", vec!['l', 'a']), 0); +} + +#[test] +fn test_ex2() { + assert_eq!(brokenkeys("Perl and Raku", vec!['a']), 1); +} + +#[test] +fn test_ex3() { + assert_eq!(brokenkeys("Well done Team PWC", vec!['l', 'o']), 2); +} + +#[test] +fn test_ex4() { + assert_eq!(brokenkeys("The joys of polyglottism", vec!['T']), 2); +} + +fn brokenkeys(a: &str, k: Vec<char>) -> u32 { + let mut out = 0; + let failset = + k.iter().map(|c| c.to_ascii_lowercase()).collect::<HashSet<char>>(); + for word in a.split(' ') { + let wordset = + word.to_ascii_lowercase().chars().collect::<HashSet<char>>(); + if wordset.intersection(&failset).count() == 0 { + out += 1; + } + } + out +} diff --git a/challenge-275/roger-bell-west/rust/ch-2.rs b/challenge-275/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..7c2ea4f33c --- /dev/null +++ b/challenge-275/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,39 @@ +#! /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!(replacedigits("a1c1e1"), "abcdef"); +} + +#[test] +fn test_ex2() { + assert_eq!(replacedigits("a1b2c3d4"), "abbdcfdh"); +} + +#[test] +fn test_ex3() { + assert_eq!(replacedigits("b2b"), "bdb"); +} + +#[test] +fn test_ex4() { + assert_eq!(replacedigits("a16z"), "abgz"); +} + +fn replacedigits(a: &str) -> String { + let mut out = String::new(); + let mut prev = 0; + for c in a.chars() { + out.push(match c { + '0'..='9' => { + char::from_u32(prev + c.to_digit(10).unwrap()).unwrap() + } + _ => { + prev = c as u32; + c + } + }); + } + out +} diff --git a/challenge-275/roger-bell-west/scala/ch-1.scala b/challenge-275/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..b70aaecb8c --- /dev/null +++ b/challenge-275/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,36 @@ + +object Brokenkeys { + def brokenkeys(a: String, k: List[Char]): Int = { + var out = 0 + val failset: Set[Char] = k.map(c => c.toLower).toSet + for (word <- a.split(" ")) { + val wordset: Set[Char] = word.toLowerCase().toSet + if ((failset & wordset).size == 0) { + out += 1 + } + } + return out + } + + def main(args: Array[String]) { + if (brokenkeys("Perl Weekly Challenge", List('l', 'a')) == 0) { + print("Pass") + } else { + print("Fail") + } |
