aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-26 13:15:25 +0000
committerGitHub <noreply@github.com>2021-11-26 13:15:25 +0000
commitc58aa139f267a08aee64b3129f854b6e637022b3 (patch)
tree8fba2eaf4ae6447217268b4fcda290fe782c1f4e
parent83a289ba2d08515dd1be9596ac461f9c100a2bc2 (diff)
parent42e47a01eea6770846b143077a263492cdc6e118 (diff)
downloadperlweeklychallenge-club-c58aa139f267a08aee64b3129f854b6e637022b3.tar.gz
perlweeklychallenge-club-c58aa139f267a08aee64b3129f854b6e637022b3.tar.bz2
perlweeklychallenge-club-c58aa139f267a08aee64b3129f854b6e637022b3.zip
Merge pull request #5282 from Abigail/abigail/week-135
Abigail/week 135
-rw-r--r--challenge-135/abigail/README.md5
-rw-r--r--challenge-135/abigail/bc/ch-1.bc35
-rw-r--r--challenge-135/abigail/pascal/ch-1.p43
-rw-r--r--challenge-135/abigail/pascal/ch-2.p56
-rw-r--r--challenge-135/abigail/r/ch-1.r30
-rw-r--r--challenge-135/abigail/r/ch-2.r36
-rw-r--r--challenge-135/abigail/t/ctest.ini7
-rw-r--r--challenge-135/abigail/t/input-1-21
-rw-r--r--challenge-135/abigail/t/input-1-31
-rw-r--r--challenge-135/abigail/t/output-1-2.exp1
-rw-r--r--challenge-135/abigail/t/output-1-3.exp1
11 files changed, 214 insertions, 2 deletions
diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md
index 6178e463fe..00e67bfcb0 100644
--- a/challenge-135/abigail/README.md
+++ b/challenge-135/abigail/README.md
@@ -4,13 +4,16 @@
* [AWK](awk/ch-1.awk)
* [Bash](bash/ch-1.sh)
+* [bc](bc/ch-1.bc)
* [C](c/ch-1.c)
* [Go](go/ch-1.go)
* [Java](java/ch-1.java)
* [Lua](lua/ch-1.lua)
* [Node.js](node/ch-1.js)
+* [Pascal](pascal/ch-1.p)
* [Perl](perl/ch-1.pl)
* [Python](python/ch-1.py)
+* [R](r/ch-1.r)
* [Ruby](ruby/ch-1.rb)
* [Scheme](scheme/ch-1.scm)
* [Tcl](tcl/ch-1.tcl)
@@ -25,7 +28,9 @@
* [Lua](lua/ch-2.lua)
* [Node.js](node/ch-2.js)
* [Perl](perl/ch-2.pl)
+* [Pascal](pascal/ch-2.p)
* [Python](python/ch-2.py)
+* [R](r/ch-2.r)
* [Ruby](ruby/ch-2.rb)
* [Scheme](scheme/ch-2.scm)
* [Tcl](tcl/ch-2.tcl)
diff --git a/challenge-135/abigail/bc/ch-1.bc b/challenge-135/abigail/bc/ch-1.bc
new file mode 100644
index 0000000000..14713dc7fe
--- /dev/null
+++ b/challenge-135/abigail/bc/ch-1.bc
@@ -0,0 +1,35 @@
+#
+# See ../README.md
+#
+
+#
+# Run as: bc ch-1.bc < input-file
+#
+
+while (1) {
+ n = read (); if (n == 0) {break;}
+ if (n < 0) {n = -n}
+ #
+ # Find the number of digits
+ #
+ d = 1
+ f = 10
+ while (f <= n) {
+ d = d + 1
+ f = f * 10
+ }
+ if (d % 2 == 0) {
+ "even number of digits"
+ } else {
+ if (d < 3) {
+ "too short"
+ } else {
+ for (q = (d + 3) / 2 - 1; q >= (d - 3) / 2; q --) {
+ r = (n / (10 ^ q)) % 10
+ print (r)
+ }
+ }
+ }
+ "
+"
+}
diff --git a/challenge-135/abigail/pascal/ch-1.p b/challenge-135/abigail/pascal/ch-1.p
new file mode 100644
index 0000000000..8f8bf9f507
--- /dev/null
+++ b/challenge-135/abigail/pascal/ch-1.p
@@ -0,0 +1,43 @@
+Program ch1;
+
+(* *)
+(* See ../README.md *)
+(* *)
+
+(* *)
+(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out < input-file *)
+(* *)
+
+uses
+ RegExpr;
+
+var
+ line: String;
+ re_valid: TRegExpr;
+ re_sign: TRegExpr;
+ len: Integer;
+
+begin
+ re_valid := TRegExpr . Create ('^[-+]?[0-9]*$');
+ re_sign := TRegExpr . Create ('^[-+]');
+ while not eof do begin
+ readln (line);
+ if not re_valid . Exec (line) then begin
+ writeln ('not an integer');
+ continue;
+ end;
+ if re_sign . Exec (line) then begin
+ Delete (line, 1, 1);
+ end;
+ len := length (line);
+ if len mod 2 = 0 then begin
+ writeln ('even number of digits');
+ continue;
+ end;
+ if len < 3 then begin
+ writeln ('too short');
+ continue;
+ end;
+ writeln (copy (line, 1 + ((len - 3) div 2), 3));
+ end
+end.
diff --git a/challenge-135/abigail/pascal/ch-2.p b/challenge-135/abigail/pascal/ch-2.p
new file mode 100644
index 0000000000..a6abd97b4b
--- /dev/null
+++ b/challenge-135/abigail/pascal/ch-2.p
@@ -0,0 +1,56 @@
+Program XXX;
+
+(* *)
+(* See ../README.md *)
+(* *)
+
+(* *)
+(* Run as: fpc -och-2.out ch-2.p; ./ch-2.out < input-file *)
+(* *)
+
+uses
+ RegExpr;
+
+const
+ ord_0 = Ord ('0');
+ ord_9 = Ord ('9');
+ ord_A = Ord ('A');
+ w : Array [0 .. 6] of Integer = (1, 3, 1, 7, 3, 9, 1);
+
+var
+ sedol: String;
+ valid: TRegExpr;
+ check: Integer;
+ c: Char;
+ val: Integer;
+ index: Integer;
+
+begin
+ valid := TRegExpr . Create ('^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}[0-9]$');
+ while not eof do begin
+ readln (sedol);
+ if not valid . Exec (sedol) then begin
+ writeln (0);
+ continue;
+ end;
+ check := 0;
+ index := 0;
+ for c in sedol do begin
+ val := Ord (c);
+ if val <= ord_9 then begin
+ val := val - ord_0;
+ end
+ else begin
+ val := val - ord_A;
+ end;
+ check := check + val * w [index];
+ inc (index);
+ end;
+ if check mod 10 = 0 then begin
+ writeln (1);
+ end
+ else begin
+ writeln (0);
+ end
+ end
+end.
diff --git a/challenge-135/abigail/r/ch-1.r b/challenge-135/abigail/r/ch-1.r
new file mode 100644
index 0000000000..e16d3210aa
--- /dev/null
+++ b/challenge-135/abigail/r/ch-1.r
@@ -0,0 +1,30 @@
+#
+# See ../README.md
+#
+
+#
+# Run as: Rscript ch-1.r < input-file
+#
+
+stdin <- file ('stdin', 'r')
+repeat {
+ n <- readLines (stdin, n = 1)
+ if (length (n) == 0) {
+ break
+ }
+ n <- sub ("^[-+]", "", n)
+ matches <- grep ("[^0-9]", n)
+ if (length (matches) != 0) {
+ cat ("not an integer\n")
+ next
+ }
+ if (nchar (n) %% 2 == 0) {
+ cat ("even number of digits\n")
+ next
+ }
+ if (nchar (n) < 3) {
+ cat ("too short\n")
+ next
+ }
+ cat (substr (n, 1 + (nchar (n) - 3) / 2, (nchar (n) + 3) / 2), "\n")
+}
diff --git a/challenge-135/abigail/r/ch-2.r b/challenge-135/abigail/r/ch-2.r
new file mode 100644
index 0000000000..611ca95bce
--- /dev/null
+++ b/challenge-135/abigail/r/ch-2.r
@@ -0,0 +1,36 @@
+#
+# See ../README.md
+#
+
+#
+# Run as: Rscript ch-2.r < input-file
+#
+
+ord.0 <- as.integer (charToRaw ("0"))
+ord.9 <- as.integer (charToRaw ("9"))
+ord.A <- as.integer (charToRaw ("A"))
+w <- c (1, 3, 1, 7, 3, 9, 1)
+
+stdin <- file ('stdin', 'r')
+repeat {
+ n <- readLines (stdin, n = 1)
+ if (length (n) == 0) {
+ break
+ }
+ valid <- 1
+ c <- grep ("^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}[0-9]$", n)
+ if (length (c) == 0) {
+ cat ("0\n")
+ next
+ }
+
+ check <- 0
+ index <- 0
+ for (char in strsplit (n, split = "") [[1]]) {
+ a <- as.integer (charToRaw (char))
+ a <- a - (if (a <= ord.9) ord.0 else ord.A)
+ index <- index + 1
+ check <- check + a * w [index]
+ }
+ cat (if (check %% 10 == 0) 1 else 0, "\n")
+}
diff --git a/challenge-135/abigail/t/ctest.ini b/challenge-135/abigail/t/ctest.ini
index e3c9965d46..a2c8d47f9b 100644
--- a/challenge-135/abigail/t/ctest.ini
+++ b/challenge-135/abigail/t/ctest.ini
@@ -6,5 +6,12 @@
[names]
1-1 = Given Examples
1-2 = More tests
+1-3 = Not an integer
2-1 = Given Examples
2-2 = Additional tests
+
+[1-1,1-2/bc]
+add_to_input = 0
+
+[1-3/bc]
+skip = "bc can only read numbers"
diff --git a/challenge-135/abigail/t/input-1-2 b/challenge-135/abigail/t/input-1-2
index f10923ad73..bdef652f0a 100644
--- a/challenge-135/abigail/t/input-1-2
+++ b/challenge-135/abigail/t/input-1-2
@@ -1,3 +1,2 @@
12121212
1212121
-1,000,000
diff --git a/challenge-135/abigail/t/input-1-3 b/challenge-135/abigail/t/input-1-3
new file mode 100644
index 0000000000..597c879e5c
--- /dev/null
+++ b/challenge-135/abigail/t/input-1-3
@@ -0,0 +1 @@
+1,000,000
diff --git a/challenge-135/abigail/t/output-1-2.exp b/challenge-135/abigail/t/output-1-2.exp
index f9502fcb06..f67abe56b1 100644
--- a/challenge-135/abigail/t/output-1-2.exp
+++ b/challenge-135/abigail/t/output-1-2.exp
@@ -1,3 +1,2 @@
even number of digits
121
-not an integer
diff --git a/challenge-135/abigail/t/output-1-3.exp b/challenge-135/abigail/t/output-1-3.exp
new file mode 100644
index 0000000000..c7bc91e121
--- /dev/null
+++ b/challenge-135/abigail/t/output-1-3.exp
@@ -0,0 +1 @@
+not an integer