aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-100/roger-bell-west/perl/ch-1.pl39
-rwxr-xr-xchallenge-100/roger-bell-west/perl/ch-2.pl38
-rwxr-xr-xchallenge-100/roger-bell-west/python/ch-1.py48
-rwxr-xr-xchallenge-100/roger-bell-west/python/ch-2.py33
-rwxr-xr-xchallenge-100/roger-bell-west/raku/ch-1.p637
-rwxr-xr-xchallenge-100/roger-bell-west/raku/ch-2.p634
-rwxr-xr-xchallenge-100/roger-bell-west/ruby/ch-1.rb59
-rwxr-xr-xchallenge-100/roger-bell-west/ruby/ch-2.rb41
-rwxr-xr-xchallenge-100/roger-bell-west/rust/ch-1.rs69
-rwxr-xr-xchallenge-100/roger-bell-west/rust/ch-2.rs49
10 files changed, 447 insertions, 0 deletions
diff --git a/challenge-100/roger-bell-west/perl/ch-1.pl b/challenge-100/roger-bell-west/perl/ch-1.pl
new file mode 100755
index 0000000000..7144a9c92e
--- /dev/null
+++ b/challenge-100/roger-bell-west/perl/ch-1.pl
@@ -0,0 +1,39 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 7;
+
+is(ft('05:15 pm'),'17:15','example 1');
+is(ft('05:15 pm'),'17:15','example 2');
+is(ft('19:15'),'07:15 pm','example 3');
+is(ft('00:00'),'12:00 am','example 4');
+is(ft('12:00'),'12:00 pm','example 5');
+is(ft('12:00 am'),'00:00','example 6');
+is(ft('12:00 pm'),'12:00','example 7');
+
+sub ft {
+ my $in=shift;
+ $in =~ /(\d+):(\d+)\s*([ap]m)?/;
+ my $h=$1;
+ my $t='';
+ if ($3) { # 12 to 24
+ if ($h==12) {
+ $h=0;
+ }
+ if ($3 eq 'pm') {
+ $h+=12;
+ }
+ } else { # 24 to 12
+ $t=' am';
+ if ($h > 11) {
+ $t=' pm';
+ $h-=12;
+ }
+ if ($h == 0) {
+ $h=12;
+ }
+ }
+ return sprintf('%02d:%02d%s',$h,$2,$t);
+}
diff --git a/challenge-100/roger-bell-west/perl/ch-2.pl b/challenge-100/roger-bell-west/perl/ch-2.pl
new file mode 100755
index 0000000000..c7ce5c41c2
--- /dev/null
+++ b/challenge-100/roger-bell-west/perl/ch-2.pl
@@ -0,0 +1,38 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 2;
+
+is(ts([[1],[2,4],[6,4,9],[5,1,7,2]]),8,'example 1');
+is(ts([[3],[3,1],[5,2,3],[4,3,1,3]]),7,'example 2');
+
+use List::Util qw(min);
+
+sub ts {
+ my $in=shift;
+ my @b;
+ my $n=0;
+ my $i=0;
+ my $s=$in->[0][0];
+ my @r;
+ while (1) {
+ if (@b) {
+ my $t=pop @b;
+ ($n,$i,$s)=@{$t};
+ }
+ if ($n < $#{$in}) {
+ $n++;
+ foreach my $ix ($i,$i+1) {
+ push @b,[$n,$ix,$s+$in->[$n][$ix]];
+ }
+ } else {
+ push @r,$s;
+ }
+ unless (@b) {
+ last;
+ }
+ }
+ return min(@r);
+}
diff --git a/challenge-100/roger-bell-west/python/ch-1.py b/challenge-100/roger-bell-west/python/ch-1.py
new file mode 100755
index 0000000000..66acebf6b3
--- /dev/null
+++ b/challenge-100/roger-bell-west/python/ch-1.py
@@ -0,0 +1,48 @@
+#! /usr/bin/python3
+
+import re
+
+def ft(i):
+ m=re.match('(\d+):(\d+)\s*([ap]m)?',i)
+ h=int(m.group(1))
+ t=""
+ if m.group(3) == None:
+ t=" am"
+ if h > 11:
+ t = " pm"
+ h -= 12
+ if h==0:
+ h=12
+ else:
+ if h==12:
+ h=0
+ if m.group(3) == "pm":
+ h += 12
+ return "{:02d}:{:02d}{:s}".format(h,int(m.group(2)),t)
+
+import unittest
+
+class TestFt(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(ft('05:15 pm'),'17:15','example 1')
+
+ def test_ex2(self):
+ self.assertEqual(ft('05:15 pm'),'17:15','example 2')
+
+ def test_ex3(self):
+ self.assertEqual(ft('19:15'),'07:15 pm','example 3')
+
+ def test_ex4(self):
+ self.assertEqual(ft('00:00'),'12:00 am','example 4')
+
+ def test_ex5(self):
+ self.assertEqual(ft('12:00'),'12:00 pm','example 5')
+
+ def test_ex6(self):
+ self.assertEqual(ft('12:00 am'),'00:00','example 6')
+
+ def test_ex7(self):
+ self.assertEqual(ft('12:00 pm'),'12:00','example 7')
+
+unittest.main()
diff --git a/challenge-100/roger-bell-west/python/ch-2.py b/challenge-100/roger-bell-west/python/ch-2.py
new file mode 100755
index 0000000000..0b47aa7237
--- /dev/null
+++ b/challenge-100/roger-bell-west/python/ch-2.py
@@ -0,0 +1,33 @@
+#! /usr/bin/python3
+
+def ts(inp):
+ b=list()
+ n=0
+ i=0
+ s=inp[0][0]
+ r=list()
+ while 1:
+ if len(b)>0:
+ t=b.pop()
+ (n,i,s)=t
+ if (n < len(inp)-1):
+ n += 1
+ for ix in range(i,i+2):
+ b.append([n,ix,s+inp[n][ix]])
+ else:
+ r.append(s)
+ if len(b)==0:
+ break;
+ return min(r)
+
+import unittest
+
+class TestFt(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(ts([[1],[2,4],[6,4,9],[5,1,7,2]]),8,'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(ts([[3],[3,1],[5,2,3],[4,3,1,3]]),7,'example 2')
+
+unittest.main()
diff --git a/challenge-100/roger-bell-west/raku/ch-1.p6 b/challenge-100/roger-bell-west/raku/ch-1.p6
new file mode 100755
index 0000000000..a88cde72cb
--- /dev/null
+++ b/challenge-100/roger-bell-west/raku/ch-1.p6
@@ -0,0 +1,37 @@
+#! /usr/bin/perl6
+
+use Test;
+
+plan 7;
+
+is(ft('05:15 pm'),'17:15','example 1');
+is(ft('05:15 pm'),'17:15','example 2');
+is(ft('19:15'),'07:15 pm','example 3');
+is(ft('00:00'),'12:00 am','example 4');
+is(ft('12:00'),'12:00 pm','example 5');
+is(ft('12:00 am'),'00:00','example 6');
+is(ft('12:00 pm'),'12:00','example 7');
+
+sub ft($in) {
+ $in ~~ /(\d+)\:(\d+)\s*(<[ap]>m)?/;
+ my $h=$0;
+ my $t='';
+ if ($2) { # 12 to 24
+ if ($h==12) {
+ $h=0;
+ }
+ if ($2 eq 'pm') {
+ $h+=12;
+ }
+ } else { # 24 to 12
+ $t=' am';
+ if ($h > 11) {
+ $t=' pm';
+ $h-=12;
+ }
+ if ($h == 0) {
+ $h=12;
+ }
+ }
+ return sprintf('%02d:%02d%s',$h,$1,$t);
+}
diff --git a/challenge-100/roger-bell-west/raku/ch-2.p6 b/challenge-100/roger-bell-west/raku/ch-2.p6
new file mode 100755
index 0000000000..cd21628ce0
--- /dev/null
+++ b/challenge-100/roger-bell-west/raku/ch-2.p6
@@ -0,0 +1,34 @@
+#! /usr/bin/perl6
+
+use Test;
+
+plan 2;
+
+is(ts([[1],[2,4],[6,4,9],[5,1,7,2]]),8,'example 1');
+is(ts([[3],[3,1],[5,2,3],[4,3,1,3]]),7,'example 2');
+
+sub ts($in) {
+ my @b;
+ my $n=0;
+ my $i=0;
+ my $s=$in[0][0];
+ my @r;
+ while (1) {
+ if (@b.elems > 0) {
+ my $t=@b.pop;
+ ($n,$i,$s)=$t;
+ }
+ if ($n < $in.elems-1) {
+ $n++;
+ for ($i,$i+1) -> $ix {
+ push @b,[$n,$ix,$s+$in[$n][$ix]];
+ }
+ } else {
+ push @r,$s;
+ }
+ unless (@b) {
+ last;
+ }
+ }
+ return min(@r);
+}
diff --git a/challenge-100/roger-bell-west/ruby/ch-1.rb b/challenge-100/roger-bell-west/ruby/ch-1.rb
new file mode 100755
index 0000000000..65a3509c17
--- /dev/null
+++ b/challenge-100/roger-bell-west/ruby/ch-1.rb
@@ -0,0 +1,59 @@
+#! /usr/bin/ruby
+
+def ft(inp)
+ md=/(\d+):(\d+)\s*([ap]m)?/.match(inp)
+ h=md[1].to_i
+ t=""
+ if md[3] then
+ if h==12 then
+ h=0
+ end
+ if md[3] == "pm" then
+ h += 12
+ end
+ else
+ t=" am"
+ if h > 11 then
+ t=" pm"
+ h -= 12
+ end
+ if h==0 then
+ h=12
+ end
+ end
+ return sprintf('%02d:%02d%s',h,md[2],t)
+end
+
+require 'test/unit'
+
+class TestFt < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal('17:15',ft('05:15 pm'))
+ end
+
+ def test_ex2
+ assert_equal('17:15',ft('05:15 pm'))
+ end
+
+ def test_ex3
+ assert_equal('07:15 pm',ft('19:15'))
+ end
+
+ def test_ex4
+ assert_equal('12:00 am',ft('00:00'))
+ end
+
+ def test_ex5
+ assert_equal('12:00 pm',ft('12:00'))
+ end
+
+ def test_ex6
+ assert_equal('00:00',ft('12:00 am'))
+ end
+
+ def test_ex7
+ assert_equal('12:00',ft('12:00 pm'))
+ end
+
+end
diff --git a/challenge-100/roger-bell-west/ruby/ch-2.rb b/challenge-100/roger-bell-west/ruby/ch-2.rb
new file mode 100755
index 0000000000..af3d690a76
--- /dev/null
+++ b/challenge-100/roger-bell-west/ruby/ch-2.rb
@@ -0,0 +1,41 @@
+#! /usr/bin/ruby
+
+def ts(inp)
+ b=Array.new
+ n=0
+ i=0
+ s=inp[0][0]
+ r=Array.new
+ while (1)
+ if b.length>0 then
+ t=b.pop;
+ (n,i,s)=t;
+ end
+ if n < inp.length-1 then
+ n += 1
+ i.upto(i+1) do |ix|
+ b.push([n,ix,s+inp[n][ix]])
+ end
+ else
+ r.push(s)
+ end
+ if b.length==0 then
+ break
+ end
+ end
+ return r.min
+end
+
+require 'test/unit'
+
+class TestPm < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal(8,ts([[1],[2,4],[6,4,9],[5,1,7,2]]))
+ end
+
+ def test_ex2
+ assert_equal(7,ts([[3],[3,1],[5,2,3],[4,3,1,3]]))
+ end
+
+end
diff --git a/challenge-100/roger-bell-west/rust/ch-1.rs b/challenge-100/roger-bell-west/rust/ch-1.rs
new file mode 100755
index 0000000000..a31338bfe6
--- /dev/null
+++ b/challenge-100/roger-bell-west/rust/ch-1.rs
@@ -0,0 +1,69 @@
+use regex::Regex;
+
+// Cargo.toml needs:
+// [dependencies]
+// regex = "1"
+
+#[test]
+fn test_ex1() {
+ assert_eq!(ft("05:15 pm"),"17:15");
+}
+
+#[test]
+fn test_ex2() {
+ assert_eq!(ft("05:15 pm"),"17:15");
+}
+
+#[test]
+fn test_ex3() {
+ assert_eq!(ft("19:15"),"07:15 pm");
+}
+
+#[test]
+fn test_ex4() {
+ assert_eq!(ft("00:00"),"12:00 am");
+}
+
+#[test]
+fn test_ex5() {
+ assert_eq!(ft("12:00"),"12:00 pm");
+}
+
+#[test]
+fn test_ex6() {
+ assert_eq!(ft("12:00 am"),"00:00");
+}
+
+#[test]
+fn test_ex7() {
+ assert_eq!(ft("12:00 pm"),"12:00");
+}
+
+fn ft(text: &str) -> String {
+ let re: Regex=Regex::new(r"(\d+):(\d+)\s*([ap]m)?").unwrap();
+ let caps=re.captures(text).unwrap();
+ let mut h: i8=caps.get(1).unwrap().as_str().parse::<i8>().unwrap();
+ let mut t: String="".to_string();
+ let ampm=match caps.get(3) {
+ None => "",
+ Some(i) => i.as_str(),
+ };
+ if ampm.len()>0 {
+ if h==12 {
+ h=0;
+ }
+ if ampm == "pm" {
+ h += 12;
+ }
+ } else {
+ t=" am".to_string();
+ if h>11 {
+ t=" pm".to_string();
+ h -= 12;
+ }
+ if h==0 {
+ h=12
+ }
+ }
+ return format!("{:02}:{:02}{}",h,caps.get(2).unwrap().as_str(),t);
+}
diff --git a/challenge-100/roger-bell-west/rust/ch-2.rs b/challenge-100/roger-bell-west/rust/ch-2.rs
new file mode 100755
index 0000000000..ba82bb7f30
--- /dev/null
+++ b/challenge-100/roger-bell-west/rust/ch-2.rs
@@ -0,0 +1,49 @@
+#! /bin/sh
+//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit
+
+#[test]
+fn test_ex1() {
+ assert_eq!(ts(vec![vec![1],vec![2,4],vec![6,4,9],vec![5,1,7,2]]),8);
+}
+
+#[test]
+fn test_ex2() {
+ assert_eq!(ts(vec![vec![3],vec![3,1],vec![5,2,3],vec![4,3,1,3]]),7);
+}
+
+pub struct Node {
+ n: usize,
+ i: usize,
+ s: i32
+}
+
+fn ts(inp: Vec<Vec<i32>>) -> i32 {
+ let mut b: Vec<Node>=vec![];
+ let mut n: usize=0;
+ let mut i: usize=0;
+ let mut s: i32=inp[0][0];
+ let mut r: Vec<i32>=vec![];
+ loop {
+ if b.len() > 0 {
+ let t=b.pop().unwrap();
+ n=t.n;
+ i=t.i;
+ s=t.s;
+ }
+ if n < inp.len()-1 {
+ n += 1;
+ for ix in i..i+2 {
+ b.push(Node {n: n,
+ i: ix,
+ s: s+inp[n][ix]
+ });
+ }
+ } else {
+ r.push(s);
+ }
+ if b.len()==0 {
+ break;
+ }
+ }
+ return *r.iter().min().unwrap();
+}