diff options
| author | Roger Bell_West <roger@firedrake.org> | 2024-07-17 12:45:16 +0100 |
|---|---|---|
| committer | Roger Bell_West <roger@firedrake.org> | 2024-07-17 12:45:16 +0100 |
| commit | 96abfd6c0b42e00d4f986e1f7a1114b13a406136 (patch) | |
| tree | 95163ca0d718306b8216d0b980f248820a1f5782 /challenge-278 | |
| parent | 4fd983bd163dfa634e5ae8b5d07255f14e33ad24 (diff) | |
| download | perlweeklychallenge-club-96abfd6c0b42e00d4f986e1f7a1114b13a406136.tar.gz perlweeklychallenge-club-96abfd6c0b42e00d4f986e1f7a1114b13a406136.tar.bz2 perlweeklychallenge-club-96abfd6c0b42e00d4f986e1f7a1114b13a406136.zip | |
RogerBW solutions for challenge no. 278
Diffstat (limited to 'challenge-278')
23 files changed, 998 insertions, 0 deletions
diff --git a/challenge-278/roger-bell-west/crystal/ch-1.cr b/challenge-278/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..702e2de4a1 --- /dev/null +++ b/challenge-278/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,25 @@ +#! /usr/bin/crystal +require "spec" +describe "sortstring" do + it "test_ex1" do + sortstring("and2 Raku3 cousins5 Perl1 are4").should eq "Perl and Raku are cousins" + end + it "test_ex2" do + sortstring("guest6 Python1 most4 the3 popular5 is2 language7").should eq "Python is the most popular guest language" + end + it "test_ex3" do + sortstring("Challenge3 The1 Weekly2").should eq "The Weekly Challenge" + end +end + +def sortstring(a) + words = a.split(" ") + out = words.clone + words.each do |w| + if md = /^(.*?)([0-9]+)$/.match(w) + ix = md[2].to_i - 1 + out[ix] = md[1] + end + end + out.join(" ") +end diff --git a/challenge-278/roger-bell-west/crystal/ch-2.cr b/challenge-278/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..5b4260b6e0 --- /dev/null +++ b/challenge-278/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,24 @@ +#! /usr/bin/crystal +require "spec" +describe "reverseword" do + it "test_ex1" do + reverseword("challenge", "e").should eq "acehllnge" + end + it "test_ex2" do + reverseword("programming", "a").should eq "agoprrmming" + end + it "test_ex3" do + reverseword("champion", "b").should eq "champion" + end +end + +def reverseword(a, c) + mm = a.index(c) + if mm.nil? + return a + end + b = a[0, mm + 1].split("") + b.sort!() + b.concat(a[mm + 1, a.size - mm].split("") ) + b.join("") +end diff --git a/challenge-278/roger-bell-west/javascript/ch-1.js b/challenge-278/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..28aaf1df4e --- /dev/null +++ b/challenge-278/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,34 @@ +#! /usr/bin/node + +"use strict" + +function sortstring(a) { + let words = a.split(" "); + let out = Array(words.length); + const re = /^(.*?)([0-9]+)$/; + for (let w of words) { + let match = w.match(re); + let index = match[2] - 1; + out[index] = match[1]; + } + return out.join(" "); +} + +if (sortstring('and2 Raku3 cousins5 Perl1 are4') == 'Perl and Raku are cousins') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (sortstring('guest6 Python1 most4 the3 popular5 is2 language7') == 'Python is the most popular guest language') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (sortstring('Challenge3 The1 Weekly2') == 'The Weekly Challenge') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-278/roger-bell-west/javascript/ch-2.js b/challenge-278/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..b0911c99fa --- /dev/null +++ b/challenge-278/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,33 @@ +#! /usr/bin/node + +"use strict" + +function reverseword(a, c) { + const m = a.indexOf(c); + if (m == -1) { + return a; + } + let b = a.slice(0, m + 1).split(""); + b.sort(); + b.push(...a.slice(m + 1).split("")); + return b.join(""); +} + +if (reverseword('challenge', 'e') == 'acehllnge') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (reverseword('programming', 'a') == 'agoprrmming') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (reverseword('champion', 'b') == 'champion') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-278/roger-bell-west/kotlin/ch-1.kt b/challenge-278/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..d1eefad642 --- /dev/null +++ b/challenge-278/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,34 @@ +fun sortstring(a: String): String { + var words = a.split(" ") + var out = ArrayList<String>(words) + val re = "^(.*?)([0-9]+)$".toRegex() + for (w in words) { + val mr = re.find(w) + var index = mr!!.groupValues[2].toInt() - 1 + out[index] = mr.groupValues[1] + } + return out.joinToString(" ") +} + +fun main() { + + if (sortstring("and2 Raku3 cousins5 Perl1 are4") == "Perl and Raku are cousins") { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (sortstring("guest6 Python1 most4 the3 popular5 is2 language7") == "Python is the most popular guest language") { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (sortstring("Challenge3 The1 Weekly2") == "The Weekly Challenge") { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-278/roger-bell-west/kotlin/ch-2.kt b/challenge-278/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..edf4b3633f --- /dev/null +++ b/challenge-278/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,33 @@ +fun reverseword(a: String, c: Char): String { + val m = a.indexOf(c) + if (m == -1) { + return a + } + var b = ArrayList(a.slice(0 .. m).split("")) + b.sort() + b.addAll(a.slice(m + 1 .. a.length - 1).split("")) + return b.joinToString("") +} + +fun main() { + + if (reverseword("challenge", 'e') == "acehllnge") { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (reverseword("programming", 'a') == "agoprrmming") { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (reverseword("champion", 'b') == "champion") { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-278/roger-bell-west/lua/ch-1.lua b/challenge-278/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..2cba1c8d35 --- /dev/null +++ b/challenge-278/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,61 @@ +#! /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 joins(t,pad) + local out="" + local later = false + for k,v in pairs(t) do + if later then + out = out .. pad + end + out = out .. v + later = true + end + return out +end + +function sortstring(a) + local words =splits(a, " ") + local out = {} + for _a, _b in ipairs(words) do + table.insert(out, "") + end + for _, w in ipairs(words) do + local _a, _b, aa, ab = string.find(w, "^(.-)(%d+)$") + out[0 + ab] = aa + end + return joins(out, " ") +end + +if sortstring("and2 Raku3 cousins5 Perl1 are4") == "Perl and Raku are cousins" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if sortstring("guest6 Python1 most4 the3 popular5 is2 language7") == "Python is the most popular guest language" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if sortstring("Challenge3 The1 Weekly2") == "The Weekly Challenge" then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-278/roger-bell-west/lua/ch-2.lua b/challenge-278/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..23f45fdeae --- /dev/null +++ b/challenge-278/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,55 @@ +#! /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 reverseword(a, c) + local s, e = string.find(a, c, 1, true) + if s == nil then + return a + end + local b = split(string.sub(a, 1, s)) + table.sort(b) + for _, ch in ipairs(split(string.sub(a, s+1))) do + table.insert(b, ch) + end + return join(b) +end + +if reverseword("challenge", "e") == "acehllnge" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if reverseword("programming", "a") == "agoprrmming" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if reverseword("champion", "b") == "champion" then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-278/roger-bell-west/perl/ch-1.pl b/challenge-278/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..3194a0f6b7 --- /dev/null +++ b/challenge-278/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,21 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(sortstring('and2 Raku3 cousins5 Perl1 are4'), 'Perl and Raku are cousins', 'example 1'); +is(sortstring('guest6 Python1 most4 the3 popular5 is2 language7'), 'Python is the most popular guest language', 'example 2'); +is(sortstring('Challenge3 The1 Weekly2'), 'The Weekly Challenge', 'example 3'); + +sub sortstring($a) { + my @words = split ' ', $a; + my @out = ("") x scalar @words; + foreach my $w (@words) { + $w =~ /^(.*?)([0-9]+)$/; + @out[$2 - 1] = $1; + } + return join(' ', @out); +} diff --git a/challenge-278/roger-bell-west/perl/ch-2.pl b/challenge-278/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..fd1b08e384 --- /dev/null +++ b/challenge-278/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,22 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(reverseword('challenge', 'e'), 'acehllnge', 'example 1'); +is(reverseword('programming', 'a'), 'agoprrmming', 'example 2'); +is(reverseword('champion', 'b'), 'champion', 'example 3'); + +sub reverseword($a, $c) { + my $m = index($a, $c); + if ($m < 0) { + return $a; + } + my @aa = split('', substr($a, 0, $m+1)); + @aa = sort @aa; + push @aa, split('', substr($a, $m+1)); + return join('', @aa); +} diff --git a/challenge-278/roger-bell-west/postscript/ch-1.ps b/challenge-278/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..bb96d80d6e --- /dev/null +++ b/challenge-278/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,155 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/c.isdigit { + dup 48 ge exch 57 le and +} bind def + +/strconcat % (a) (b) -> (ab) +{ + [ + 3 -1 roll + s2a aload length + 2 add -1 roll + s2a aload pop + ] a2s +} bind def + +/reverse { + 1 dict begin + dup length /l exch def + [ exch + aload pop + 2 1 l { + -1 roll + } for + ] + end +} 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 + +/s2a { + [ exch { } forall ] +} 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 + +/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 + + +/strjoin % [(a) (b) (c)] (j) -> (ajbjc) +{ + 3 dict begin + /j exch def + dup 0 get /out exch def + /first true def + { + first { + pop + /first false def + } { + out j strconcat + exch strconcat + /out exch def + } ifelse + } forall + out + 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 + +/sortstring { + 0 dict begin + ( ) strsplit /words exch def + /out words length array def + words { + s2a /ws exch def + /m -1 def + /n -1 def + ws reverse { + /c exch def + /n n 1 add def + c c.isdigit not { + /m ws length 1 sub n sub def + exit + } if + } forall + ws m 1 add ws length m sub 1 sub getinterval a2s cvi 1 sub /ix exch def + out ix ws 0 m 1 add getinterval a2s put + } forall + out ( ) strjoin + end +} bind def + +(sortstring) test.start +(and2 Raku3 cousins5 Perl1 are4) sortstring (Perl and Raku are cousins) eq test +(guest6 Python1 most4 the3 popular5 is2 language7) sortstring (Python is the most popular guest language) eq test +(Challenge3 The1 Weekly2) sortstring (The Weekly Challenge) eq test +test.end diff --git a/challenge-278/roger-bell-west/postscript/ch-2.ps b/challenge-278/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..3cfb87ab46 --- /dev/null +++ b/challenge-278/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,162 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + +/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 + +/quicksort { + { quicksort.cmp } quicksort.with_comparator +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} 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 + +/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 + +/s2a { + [ exch { } forall ] +} 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.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } 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 { + /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 + +/reverseword { + 0 dict begin + /c exch def + /a exch def + /s a s2a def + a c search { + length /m exch def + pop + pop + s 0 m 1 add getinterval quicksort aload pop + s m 1 add a length m sub 1 sub getinterval aload pop + a length array astore a2s + } if + end +} bind def + +(reverseword) test.start +(challenge) (e) reverseword (acehllnge) eq test +(programming) (a) reverseword (agoprrmming) eq test +(champion) (b) reverseword (champion) eq test +test.end diff --git a/challenge-278/roger-bell-west/python/ch-1.py b/challenge-278/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..fbed9dc486 --- /dev/null +++ b/challenge-278/roger-bell-west/python/ch-1.py @@ -0,0 +1,28 @@ +#! /usr/bin/python3 + +import re + +def sortstring(a): + words = a.split(" ") + out = words.copy() + pr = re.compile(r'^(.*?)([0-9]+)$') + for w in words: + c = pr.search(w) + index = int(c.group(2)) - 1 + out[index] = c.group(1) + return " ".join(out) + +import unittest + +class TestSortstring(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(sortstring("and2 Raku3 cousins5 Perl1 are4"), "Perl and Raku are cousins", 'example 1') + + def test_ex2(self): + self.assertEqual(sortstring("guest6 Python1 most4 the3 popular5 is2 language7"), "Python is the most popular guest language", 'example 2') + + def test_ex3(self): + self.assertEqual(sortstring("Challenge3 The1 Weekly2"), "The Weekly Challenge", 'example 3') + +unittest.main() diff --git a/challenge-278/roger-bell-west/python/ch-2.py b/challenge-278/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..aa6af5663a --- /dev/null +++ b/challenge-278/roger-bell-west/python/ch-2.py @@ -0,0 +1,25 @@ +#! /usr/bin/python3 + +def reverseword(a, c): + s = a.find(c) + if s == -1: + return a + b = list(a[0:s+1]) + b.sort() + b.extend(list(a[s+1:])) + return "".join(b) + +import unittest + +class TestReverseword(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(reverseword("challenge", "e"), "acehllnge", 'example 1') + + def test_ex2(self): + self.assertEqual(reverseword("programming", "a"), "agoprrmming", 'example 2') + + def test_ex3(self): + self.assertEqual(reverseword("champion", "b"), "champion", 'example 3') + +unittest.main() diff --git a/challenge-278/roger-bell-west/raku/ch-1.p6 b/challenge-278/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..e081b800f6 --- /dev/null +++ b/challenge-278/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,19 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(sortstring('and2 Raku3 cousins5 Perl1 are4'), 'Perl and Raku are cousins', 'example 1'); +is(sortstring('guest6 Python1 most4 the3 popular5 is2 language7'), 'Python is the most popular guest language', 'example 2'); +is(sortstring('Challenge3 The1 Weekly2'), 'The Weekly Challenge', 'example 3'); + +sub sortstring($a) { + my @words = $a.split(' '); + my @out = ["" xx @words.elems]; + for @words -> $w { + $w ~~ /^(.*?)(<[0..9]>+)$/; + @out[$1 - 1] = $0 ~ ''; + } + return @out.join(' '); +} diff --git a/challenge-278/roger-bell-west/raku/ch-2.p6 b/challenge-278/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..36e74203fc --- /dev/null +++ b/challenge-278/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,20 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(reverseword('challenge', 'e'), 'acehllnge', 'example 1'); +is(reverseword('programming', 'a'), 'agoprrmming', 'example 2'); +is(reverseword('champion', 'b'), 'champion', 'example 3'); + +sub reverseword($a, $c) { + with $a.index($c) -> $m { + my @aa = $a.substr(0, $m+1).comb; + @aa = sort @aa; + @aa.push($a.substr($m+1).comb.Slip); + return @aa.join(''); + } else { + return $a; + } +} diff --git a/challenge-278/roger-bell-west/ruby/ch-1.rb b/challenge-278/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..e87b3d74fb --- /dev/null +++ b/challenge-278/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,31 @@ +#! /usr/bin/ruby + +def sortstring(a) + words = a.split(" ") + out = words.clone + words.each do |w| + if md = /^(.*?)([0-9]+)$/.match(w) + ix = md[2].to_i - 1 + out[ix] = md[1] + end + end + out.join(" ") +end + +require 'test/unit' + +class TestSortstring < Test::Unit::TestCase + + def test_ex1 + assert_equal('Perl and Raku are cousins', sortstring('and2 Raku3 cousins5 Perl1 are4')) + end + + def test_ex2 + assert_equal('Python is the most popular guest language', sortstring('guest6 Python1 most4 the3 popular5 is2 language7')) + end + + def test_ex3 + assert_equal('The Weekly Challenge', sortstring('Challenge3 The1 Weekly2')) + end + +end diff --git a/challenge-278/roger-bell-west/ruby/ch-2.rb b/challenge-278/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..e63f122b49 --- /dev/null +++ b/challenge-278/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,30 @@ +#! /usr/bin/ruby + +def reverseword(a, c) + mm = a.index(c) + if mm.nil? + return a + end + b = a[0, mm + 1].split("") + b.sort!() + b.concat(a[mm + 1, a.size - mm].split("") ) + b.join("") +end + +require 'test/unit' + +class TestReverseword < Test::Unit::TestCase + + def test_ex1 + assert_equal('acehllnge', reverseword('challenge', 'e')) + end + + def test_ex2 + assert_equal('agoprrmming', reverseword('programming', 'a')) + end + + def test_ex3 + assert_equal('champion', reverseword('champion', 'b')) + end + +end diff --git a/challenge-278/roger-bell-west/rust/ch-1.rs b/challenge-278/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..3cca2ed314 --- /dev/null +++ b/challenge-278/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,34 @@ +use regex::Regex; + +#[test] +fn test_ex1() { + assert_eq!( + sortstring("and2 Raku3 cousins5 Perl1 are4"), + "Perl and Raku are cousins" + ); +} + +#[test] +fn test_ex2() { + assert_eq!( + sortstring("guest6 Python1 most4 the3 popular5 is2 language7"), + "Python is the most popular guest language" + ); +} + +#[test] +fn test_ex3() { + assert_eq!(sortstring("Challenge3 The1 Weekly2"), "The Weekly Challenge"); +} + +fn sortstring(a: &str) -> String { + let words = a.split(' ').collect::<Vec<&str>>(); + let mut out = vec![String::new(); words.len()]; + let re = Regex::new(r"^(.*?)([0-9]+)$").unwrap(); + for w in words { + let c = re.captures(&w).unwrap(); + let index = c.get(2).unwrap().as_str().parse::<usize>().unwrap() - 1; + out[index] = c.get(1).unwrap().as_str().to_string(); + } + out.join(" ") +} diff --git a/challenge-278/roger-bell-west/rust/ch-2.rs b/challenge-278/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..2d342cec8e --- /dev/null +++ b/challenge-278/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,32 @@ +#! /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!(reverseword("challenge", 'e'), "acehllnge"); +} + +#[test] +fn test_ex2() { + assert_eq!(reverseword("programming", 'a'), "agoprrmming"); +} + +#[test] +fn test_ex3() { + assert_eq!(reverseword("champion", 'b'), "champion"); +} + +fn reverseword(a: &str, c: char) -> String { + match a.find(c) { + Some(s) => { + let b = a.bytes().collect::<Vec<_>>(); + let mut c: Vec<u8> = b[0..=s].to_vec(); + c.sort(); + c.append(&mut b[s + 1..b.len()].to_vec()); + return std::str::from_utf8(&c).unwrap().to_string(); + } + _ => { |
