diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-09-23 10:31:35 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-23 10:31:35 +0100 |
| commit | 34939d1acd073f0c864a299c58680b930e250945 (patch) | |
| tree | e8ec48fd002e5575572948d51feeca0305c37d22 | |
| parent | a51975ed93b84c5ed5b30f6cb950f62cb805a2b5 (diff) | |
| parent | 2f7f7621eff28b12d42eb73914c9d7ca43c92fad (diff) | |
| download | perlweeklychallenge-club-34939d1acd073f0c864a299c58680b930e250945.tar.gz perlweeklychallenge-club-34939d1acd073f0c864a299c58680b930e250945.tar.bz2 perlweeklychallenge-club-34939d1acd073f0c864a299c58680b930e250945.zip | |
Merge pull request #12730 from Firedrake/rogerbw-challenge-340
RogerBW solutions for challenge no. 340
25 files changed, 1138 insertions, 0 deletions
diff --git a/challenge-340/roger-bell-west/crystal/ch-1.cr b/challenge-340/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..59e3857a66 --- /dev/null +++ b/challenge-340/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,32 @@ +#! /usr/bin/crystal + +def duplicateremovals(a) + b = Array(Char).new + a.chars.each do |c| + if b.size == 0 || c != b[-1] + b.push(c) + else + b.pop + end + end + b.join("") +end + +require "spec" +describe "duplicateremovals" do + it "test_ex1" do + duplicateremovals("abbaca").should eq "ca" + end + it "test_ex2" do + duplicateremovals("azxxzy").should eq "ay" + end + it "test_ex3" do + duplicateremovals("aaaaaaaa").should eq "" + end + it "test_ex4" do + duplicateremovals("aabccba").should eq "a" + end + it "test_ex5" do + duplicateremovals("abcddcba").should eq "" + end +end diff --git a/challenge-340/roger-bell-west/crystal/ch-2.cr b/challenge-340/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..ca5ee60de4 --- /dev/null +++ b/challenge-340/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,34 @@ +#! /usr/bin/crystal + +def ascendingnumbers(a) + prev = nil + a.split(" ").each do |c| + n = c.to_i? + if !n.nil? + if !prev.nil? && prev >= n + return false + end + prev = n + end + end + true +end + +require "spec" +describe "ascendingnumbers" do + it "test_ex1" do + ascendingnumbers("The cat has 3 kittens 7 toys 10 beds").should eq true + end + it "test_ex2" do + ascendingnumbers("Alice bought 5 apples 2 oranges 9 bananas").should eq false + end + it "test_ex3" do + ascendingnumbers("I ran 1 mile 2 days 3 weeks 4 months").should eq true + end + it "test_ex4" do + ascendingnumbers("Bob has 10 cars 10 bikes").should eq false + end + it "test_ex5" do + ascendingnumbers("Zero is 0 one is 1 two is 2").should eq true + end +end diff --git a/challenge-340/roger-bell-west/javascript/ch-1.js b/challenge-340/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..7ee4dd80e9 --- /dev/null +++ b/challenge-340/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,46 @@ +#! /usr/bin/node + +"use strict" + +function duplicateremovals(a) { + var b = []; + for (let c of a.split("")) { + if (b.length == 0 || c != b[b.length - 1]) { + b.push(c); + } else { + b.pop(); + } + } + return b.join(""); +} + +if (duplicateremovals('abbaca') == 'ca') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (duplicateremovals('azxxzy') == 'ay') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (duplicateremovals('aaaaaaaa') == '') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (duplicateremovals('aabccba') == 'a') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (duplicateremovals('abcddcba') == '') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-340/roger-bell-west/javascript/ch-2.js b/challenge-340/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..b61f6b9748 --- /dev/null +++ b/challenge-340/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,48 @@ +#! /usr/bin/node + +"use strict" + +function ascendingnumbers(a) { + let prev = null; + for (let c of a.split(" ")) { + let n = parseInt(c, 10); + if (!isNaN(n)) { + if (prev !== null && prev >= n) { + return false; + } + prev = n; + } + } + return true; +} + +if (ascendingnumbers('The cat has 3 kittens 7 toys 10 beds')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!ascendingnumbers('Alice bought 5 apples 2 oranges 9 bananas')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (ascendingnumbers('I ran 1 mile 2 days 3 weeks 4 months')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!ascendingnumbers('Bob has 10 cars 10 bikes')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (ascendingnumbers('Zero is 0 one is 1 two is 2')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-340/roger-bell-west/kotlin/ch-1.kt b/challenge-340/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..ed18b5d961 --- /dev/null +++ b/challenge-340/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,46 @@ +fun duplicateremovals(a: String): String { + var b = ArrayList<Char>() + for (c in a.toCharArray()) { + if (b.size == 0 || c != b[b.size - 1]) { + b.add(c) + } else { + b = ArrayList(b.dropLast(1)) + } + } + return b.joinToString("") +} + +fun main() { + + if (duplicateremovals("abbaca") == "ca") { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicateremovals("azxxzy") == "ay") { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicateremovals("aaaaaaaa") == "") { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicateremovals("aabccba") == "a") { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicateremovals("abcddcba") == "") { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-340/roger-bell-west/kotlin/ch-2.kt b/challenge-340/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..5069f93319 --- /dev/null +++ b/challenge-340/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,48 @@ +fun ascendingnumbers(a: String): Boolean { + var prev: Int? = null + for (c in a.split(" ")) { + val n = c.toIntOrNull() + if (n != null) { + if (prev != null && prev >= n) { + return false + } + prev = n + } + } + return true +} + +fun main() { + + if (ascendingnumbers("The cat has 3 kittens 7 toys 10 beds")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!ascendingnumbers("Alice bought 5 apples 2 oranges 9 bananas")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (ascendingnumbers("I ran 1 mile 2 days 3 weeks 4 months")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!ascendingnumbers("Bob has 10 cars 10 bikes")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (ascendingnumbers("Zero is 0 one is 1 two is 2")) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-340/roger-bell-west/lua/ch-1.lua b/challenge-340/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..360f4fa318 --- /dev/null +++ b/challenge-340/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,68 @@ +#! /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 duplicateremovals(a) + local b = {} + for _, c in ipairs(split(a)) do + if #b == 0 or c ~= b[#b] then + table.insert(b, c) + else + table.remove(b, #b) + end + end + return join(b) +end + +if duplicateremovals("abbaca") == "ca" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if duplicateremovals("azxxzy") == "ay" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if duplicateremovals("aaaaaaaa") == "" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if duplicateremovals("aabccba") == "a" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if duplicateremovals("abcddcba") == "" then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-340/roger-bell-west/lua/ch-2.lua b/challenge-340/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..0f34e7a0aa --- /dev/null +++ b/challenge-340/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,79 @@ +#! /usr/bin/lua + +function splits(inputstr, sep) + sep = sep or '%s' + local starts = {1} + local ends = {} + local n = 1 + while true do + local fs, fe = string.find(inputstr, sep, n) + if fs == nil then + break + end + table.insert(ends, fs - 1) + table.insert(starts, fe + 1) + n = fe + 1 + end + table.insert(ends, #inputstr) + local t = {} + for i = 1, #starts do + local s = starts[i] + local e = ends[i] + if e >= s then + table.insert(t, string.sub(inputstr, s, e)) + end + end + return t +end + +function ascendingnumbers(a) + local prev = nil + for _, c in ipairs(splits(a, " ")) do + local n = tonumber(c) + if type(n) ~= "nil" then + if type(prev) ~= "nil" then + if prev >= n then + return false + end + end + prev = n + end + end + return true +end + +if ascendingnumbers("The cat has 3 kittens 7 toys 10 beds") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not ascendingnumbers("Alice bought 5 apples 2 oranges 9 bananas") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if ascendingnumbers("I ran 1 mile 2 days 3 weeks 4 months") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not ascendingnumbers("Bob has 10 cars 10 bikes") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if ascendingnumbers("Zero is 0 one is 1 two is 2") then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-340/roger-bell-west/perl/ch-1.pl b/challenge-340/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..e258669dd1 --- /dev/null +++ b/challenge-340/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,25 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is(duplicateremovals('abbaca'), 'ca', 'example 1'); +is(duplicateremovals('azxxzy'), 'ay', 'example 2'); +is(duplicateremovals('aaaaaaaa'), '', 'example 3'); +is(duplicateremovals('aabccba'), 'a', 'example 4'); +is(duplicateremovals('abcddcba'), '', 'example 5'); + +sub duplicateremovals($a) { + my @b; + foreach my $c (split '', $a) { + if (scalar @b == 0 || $c ne $b[-1]) { + push @b, $c; + } else { + pop @b; + } + } + join('', @b); +} diff --git a/challenge-340/roger-bell-west/perl/ch-2.pl b/challenge-340/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..bafca33fc3 --- /dev/null +++ b/challenge-340/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 => 5; + +is(ascendingnumbers('The cat has 3 kittens 7 toys 10 beds'), 1, 'example 1'); +is(ascendingnumbers('Alice bought 5 apples 2 oranges 9 bananas'), 0, 'example 2'); +is(ascendingnumbers('I ran 1 mile 2 days 3 weeks 4 months'), 1, 'example 3'); +is(ascendingnumbers('Bob has 10 cars 10 bikes'), 0, 'example 4'); +is(ascendingnumbers('Zero is 0 one is 1 two is 2'), 1, 'example 5'); + +sub ascendingnumbers($a) { + my $prev = undef; + foreach my $c (split ' ', $a) { + if ($c =~ /^[0-9]+$/) { + if (defined $prev && $prev >= $c) { + return 0; + } + $prev = $c; + } + } + 1; +} diff --git a/challenge-340/roger-bell-west/postscript/ch-1.ps b/challenge-340/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..f13e8344c1 --- /dev/null +++ b/challenge-340/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,73 @@ +%!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.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} 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 + +/s2a { + [ exch { } forall ] +} 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 + + +% end included library code + +/duplicateremovals { + s2a + [ exch + { + counttomark 0 gt + 3 copy pop eq and { + pop pop + } if + } forall + ] + a2s +} bind def + +(duplicateremovals) test.start +(abbaca) duplicateremovals (ca) eq test +(azxxzy) duplicateremovals (ay) eq test +(aaaaaaaa) duplicateremovals () eq test +(aabccba) duplicateremovals (a) eq test +(abcddcba) duplicateremovals () eq test +test.end diff --git a/challenge-340/roger-bell-west/postscript/ch-2.ps b/challenge-340/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..b158ca91d8 --- /dev/null +++ b/challenge-340/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,96 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} 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 + +/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 + + +% end included library code + +/ascendingnumbers { + 0 dict begin + /first true def + /prev 0 def + true exch + ( ) strsplit { + { + cvi + } stopped { + pop + } { + /n exch def + first not { + prev n ge { + pop false + exit + } if + } if + /first false def + /prev n def + } ifelse + } forall + end +} bind def + +(ascendingnumbers) test.start +(The cat has 3 kittens 7 toys 10 beds) ascendingnumbers test +(Alice bought 5 apples 2 oranges 9 bananas) ascendingnumbers not test +(I ran 1 mile 2 days 3 weeks 4 months) ascendingnumbers test +(Bob has 10 cars 10 bikes) ascendingnumbers not test +(Zero is 0 one is 1 two is 2) ascendingnumbers test +test.end diff --git a/challenge-340/roger-bell-west/python/ch-1.py b/challenge-340/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..6d8b329a40 --- /dev/null +++ b/challenge-340/roger-bell-west/python/ch-1.py @@ -0,0 +1,31 @@ +#! /usr/bin/python3 + +def duplicateremovals(a): + b = [] + for c in a: + if len(b) == 0 or c != b[-1]: + b.append(c) + else: + b.pop() + return ''.join(b) + +import unittest + +class TestDuplicateremovals(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(duplicateremovals("abbaca"), "ca", 'example 1') + + def test_ex2(self): + self.assertEqual(duplicateremovals("azxxzy"), "ay", 'example 2') + + def test_ex3(self): + self.assertEqual(duplicateremovals("aaaaaaaa"), "", 'example 3') + + def test_ex4(self): + self.assertEqual(duplicateremovals("aabccba"), "a", 'example 4') + + def test_ex5(self): + self.assertEqual(duplicateremovals("abcddcba"), "", 'example 5') + +unittest.main() diff --git a/challenge-340/roger-bell-west/python/ch-2.py b/challenge-340/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..ddf9aa6280 --- /dev/null +++ b/challenge-340/roger-bell-west/python/ch-2.py @@ -0,0 +1,34 @@ +#! /usr/bin/python3 + +def ascendingnumbers(a): + prev = None + for c in a.split(" "): + try: + n = int(c) + if prev is not None and prev >= n: + return False + prev = n + except ValueError: + None + return True + +import unittest + +class TestAscendingnumbers(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(ascendingnumbers("The cat has 3 kittens 7 toys 10 beds"), True, 'example 1') + + def test_ex2(self): + self.assertEqual(ascendingnumbers("Alice bought 5 apples 2 oranges 9 bananas"), False, 'example 2') + + def test_ex3(self): + self.assertEqual(ascendingnumbers("I ran 1 mile 2 days 3 weeks 4 months"), True, 'example 3') + + def test_ex4(self): + self.assertEqual(ascendingnumbers("Bob has 10 cars 10 bikes"), False, 'example 4') + + def test_ex5(self): + self.assertEqual(ascendingnumbers("Zero is 0 one is 1 two is 2"), True, 'example 5') + +unittest.main() diff --git a/challenge-340/roger-bell-west/raku/ch-1.p6 b/challenge-340/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..1e42ce3ecd --- /dev/null +++ b/challenge-340/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,23 @@ +#! /usr/bin/raku + +use Test; + +plan 5; + +is(duplicateremovals('abbaca'), 'ca', 'example 1'); +is(duplicateremovals('azxxzy'), 'ay', 'example 2'); +is(duplicateremovals('aaaaaaaa'), '', 'example 3'); +is(duplicateremovals('aabccba'), 'a', 'example 4'); +is(duplicateremovals('abcddcba'), '', 'example 5'); + +sub duplicateremovals($a) { + my @b; + for $a.comb -> $c { + if (@b.elems == 0 || $c ne @b[*-1]) { + @b.push($c); + } else { + @b.pop; + } + } + join('', @b); +} diff --git a/challenge-340/roger-bell-west/raku/ch-2.p6 b/challenge-340/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..aae213c679 --- /dev/null +++ b/challenge-340/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,26 @@ +#! /usr/bin/raku + +use Test; + +plan 5; + +is(ascendingnumbers('The cat has 3 kittens 7 toys 10 beds'), True, 'example 1'); +is(ascendingnumbers('Alice bought 5 apples 2 oranges 9 bananas'), False, 'example 2'); +is(ascendingnumbers('I ran 1 mile 2 days 3 weeks 4 months'), True, 'example 3'); +is(ascendingnumbers('Bob has 10 cars 10 bikes'), False, 'example 4'); +is(ascendingnumbers('Zero is 0 one is 1 two is 2'), True, 'example 5'); + +sub ascendingnumbers($a) { + my $prev = Nil; + for $a.split(' ') -> $c { + if ($c ~~ /^<[0..9]>+$/) { + with $prev { + if ($prev >= $c) { + return False; + } + } + $prev = $c; + } + } + True; +} diff --git a/challenge-340/roger-bell-west/ruby/ch-1.rb b/challenge-340/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..3ac24450e3 --- /dev/null +++ b/challenge-340/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,39 @@ +#! /usr/bin/ruby + +def duplicateremovals(a) + b = Array.new + a.chars.each do |c| + if b.size == 0 || c != b[-1] + b.push(c) + else + b.pop + end + end + b.join("") +end + +require 'test/unit' + +class TestDuplicateremovals < Test::Unit::TestCase + + def test_ex1 + assert_equal('ca', duplicateremovals('abbaca')) + end + + def test_ex2 + assert_equal('ay', duplicateremovals('azxxzy')) + end + + def test_ex3 + assert_equal('', duplicateremovals('aaaaaaaa')) + end + + def test_ex4 + assert_equal('a', duplicateremovals('aabccba')) + end + + def test_ex5 + assert_equal('', duplicateremovals('abcddcba')) + end + +end diff --git a/challenge-340/roger-bell-west/ruby/ch-2.rb b/challenge-340/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..9b511533d3 --- /dev/null +++ b/challenge-340/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,42 @@ +#! /usr/bin/ruby + +def ascendingnumbers(a) + np = Regexp.new("^[0-9]+$") + prev = nil + a.split(" ").each do |c| + if c =~ np + n = c.to_i + if !prev.nil? && prev >= n + return false + end + prev = n + end + end + true +end + +require 'test/unit' + +class TestAscendingnumbers < Test::Unit::TestCase + + def test_ex1 + assert_equal(true, ascendingnumbers('The cat has 3 kittens 7 toys 10 beds')) + end + + def test_ex2 + assert_equal(false, ascendingnumbers('Alice bought 5 apples 2 oranges 9 bananas')) + end + + def test_ex3 + assert_equal(true, ascendingnumbers('I ran 1 mile 2 days 3 weeks 4 months')) + end + + def test_ex4 + assert_equal(false, ascendingnumbers('Bob has 10 cars 10 bikes')) + end + + def test_ex5 + assert_equal(true, ascendingnumbers('Zero is 0 one is 1 two is 2')) + end + +end diff --git a/challenge-340/roger-bell-west/rust/ch-1.rs b/challenge-340/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..cad2bf2a69 --- /dev/null +++ b/challenge-340/roger-bell-west/rust/ch-1.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!(duplicateremovals("abbaca"), "ca"); +} + +#[test] +fn test_ex2() { + assert_eq!(duplicateremovals("azxxzy"), "ay"); +} + +#[test] +fn test_ex3() { + assert_eq!(duplicateremovals("aaaaaaaa"), ""); +} + +#[test] +fn test_ex4() { + assert_eq!(duplicateremovals("aabccba"), "a"); +} + +#[test] +fn test_ex5() { + assert_eq!(duplicateremovals("abcddcba"), ""); +} + +fn duplicateremovals(a: &str) -> String { + let mut b = Vec::new(); + for c in a.chars() { + if b.len() == 0 || c != b[b.len() - 1] { + b.push(c); + } else { + b.pop(); + } + } + b.into_iter().collect() +} diff --git a/challenge-340/roger-bell-west/rust/ch-2.rs b/challenge-340/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..cf5fd6d4fe --- /dev/null +++ b/challenge-340/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,43 @@ +#! /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!(ascendingnumbers("The cat has 3 kittens 7 toys 10 beds"), true); +} + +#[test] +fn test_ex2() { + assert_eq!( + ascendingnumbers("Alice bought 5 apples 2 oranges 9 bananas"), |
