aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.freedom.nl>2022-01-05 14:25:47 +0100
committerAbigail <abigail@abigail.freedom.nl>2022-01-05 20:13:00 +0100
commit74d9830dc5dbf14456f9e4f6b15d82761224b11f (patch)
tree4caf22e6f5d0f058aced0c2912dd1c8abe1425df
parent76a305f969079f2534a8fe3ef6d88b6aa2839097 (diff)
downloadperlweeklychallenge-club-74d9830dc5dbf14456f9e4f6b15d82761224b11f.tar.gz
perlweeklychallenge-club-74d9830dc5dbf14456f9e4f6b15d82761224b11f.tar.bz2
perlweeklychallenge-club-74d9830dc5dbf14456f9e4f6b15d82761224b11f.zip
Week 2: Solutions in 6 more languages.
-rw-r--r--challenge-002/abigail/README.md12
-rw-r--r--challenge-002/abigail/go/ch-1.go24
-rw-r--r--challenge-002/abigail/go/ch-2.go46
-rw-r--r--challenge-002/abigail/java/ch-1.java18
-rw-r--r--challenge-002/abigail/java/ch-2.java38
-rw-r--r--challenge-002/abigail/pascal/ch-1.p19
-rw-r--r--challenge-002/abigail/pascal/ch-2.p38
-rw-r--r--challenge-002/abigail/r/ch-1.r19
-rw-r--r--challenge-002/abigail/r/ch-2.r47
-rw-r--r--challenge-002/abigail/scheme/ch-1.scm22
-rw-r--r--challenge-002/abigail/scheme/ch-2.scm56
-rw-r--r--challenge-002/abigail/tcl/ch-1.tcl17
-rw-r--r--challenge-002/abigail/tcl/ch-2.tcl56
13 files changed, 412 insertions, 0 deletions
diff --git a/challenge-002/abigail/README.md b/challenge-002/abigail/README.md
index 83153f6088..83ac952703 100644
--- a/challenge-002/abigail/README.md
+++ b/challenge-002/abigail/README.md
@@ -16,11 +16,17 @@ We cannot have a number with just 0's, as that would not be a positive number.
* [bc](bc/ch-1.bc)
* [Befunge-93](befunge-93/ch-1.bf93)
* [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)
## [Challenge #2](https://perlweeklychallenge.org/blog/perl-weekly-challenge-002/#challenge-2)
@@ -39,8 +45,14 @@ one number per line. Programs will use an option, -t (to base 35), or
* [AWK](awk/ch-2.awk)
* [Bash](bash/ch-2.sh)
* [C](c/ch-2.c)
+* [Go](go/ch-1.go)
+* [Java](java/ch-1.java)
* [Lua](lua/ch-2.lua)
* [Node](node/ch-2.js)
+* [Pascal](pascal/ch-1.p)
* [Perl](perl/ch-2.pl)
* [Python](python/ch-2.py)
+* [R](r/ch-2.r)
* [Ruby](ruby/ch-2.by)
+* [Scheme](scheme/ch-2.scm)
+* [Tcl](tcl/ch-2.tcl)
diff --git a/challenge-002/abigail/go/ch-1.go b/challenge-002/abigail/go/ch-1.go
new file mode 100644
index 0000000000..5c99f26012
--- /dev/null
+++ b/challenge-002/abigail/go/ch-1.go
@@ -0,0 +1,24 @@
+package main
+
+//
+// See https://theweeklychallenge.org/blog/perl-weekly-challenge-002
+//
+
+//
+// Run as: go run ch-1.go < input-file
+//
+
+import (
+ "fmt"
+)
+
+func main () {
+ var i int;
+ for {
+ var n, err = fmt . Scanf ("%d", &i)
+ if n != 1 || err != nil {
+ break
+ }
+ fmt . Println (i)
+ }
+}
diff --git a/challenge-002/abigail/go/ch-2.go b/challenge-002/abigail/go/ch-2.go
new file mode 100644
index 0000000000..328b13dbdc
--- /dev/null
+++ b/challenge-002/abigail/go/ch-2.go
@@ -0,0 +1,46 @@
+package main
+
+//
+// See https://theweeklychallenge.org/blog/perl-weekly-challenge-002
+//
+
+//
+// Run as: go run ch-2.go < input-file
+//
+
+import (
+ "fmt"
+ "flag"
+ "bufio"
+ "os"
+ "strconv"
+ "strings"
+)
+
+func main () {
+ to_base := flag . Bool ("t", false, "a bool")
+ from_base := flag . Bool ("f", false, "a bool")
+
+ flag . Parse ()
+
+ var reader = bufio . NewReader (os. Stdin)
+ for {
+ var line, err = reader . ReadString ('\n')
+ if (err != nil) {
+ break
+ }
+ line = strings . Trim (line, "\n")
+ if *from_base {
+ i, err := strconv . ParseInt (line, 35, 0)
+ if (err == nil) {
+ fmt . Println (i)
+ }
+ }
+ if *to_base {
+ i, err := strconv . ParseInt (line, 10, 0)
+ if (err == nil) {
+ fmt . Println (strings . ToUpper (strconv . FormatInt (i, 35)))
+ }
+ }
+ }
+}
diff --git a/challenge-002/abigail/java/ch-1.java b/challenge-002/abigail/java/ch-1.java
new file mode 100644
index 0000000000..d2b574e76d
--- /dev/null
+++ b/challenge-002/abigail/java/ch-1.java
@@ -0,0 +1,18 @@
+//
+// See https://theweeklychallenge.org/blog/perl-weekly-challenge-002
+//
+
+//
+// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 < input-file
+//
+
+import java.util.*;
+
+public class ch1 {
+ public static void main (String [] args) {
+ Scanner scanner = new Scanner (System . in);
+ while (scanner . hasNextInt ()) {
+ System . out . println (scanner . nextInt ());
+ }
+ }
+}
diff --git a/challenge-002/abigail/java/ch-2.java b/challenge-002/abigail/java/ch-2.java
new file mode 100644
index 0000000000..8c15c6ed49
--- /dev/null
+++ b/challenge-002/abigail/java/ch-2.java
@@ -0,0 +1,38 @@
+//
+// See https://theweeklychallenge.org/blog/perl-weekly-challenge-002
+//
+
+//
+// Run as: ln ch-2.java ch2.java; javac ch2.java; java ch2 < input-file
+//
+
+import java.util.*;
+
+public class ch2 {
+ public static void main (String [] args) {
+ Boolean from_base = false;
+ Boolean to_base = false;
+
+ if (args . length == 1) {
+ if (args [0] . equals ("-f")) {
+ from_base = true;
+ }
+ if (args [0] . equals ("-t")) {
+ to_base = true;
+ }
+ }
+
+ Scanner scanner = new Scanner (System . in);
+ while (scanner . hasNextLine ()) {
+ String line = scanner . nextLine () . trim ();
+ if (from_base) {
+ System . out . println (Integer . parseInt (line, 35));
+ }
+ if (to_base) {
+ System . out . println (Integer . toString (
+ Integer . parseInt (line), 35) .
+ toUpperCase ());
+ }
+ }
+ }
+}
diff --git a/challenge-002/abigail/pascal/ch-1.p b/challenge-002/abigail/pascal/ch-1.p
new file mode 100644
index 0000000000..85fee3525a
--- /dev/null
+++ b/challenge-002/abigail/pascal/ch-1.p
@@ -0,0 +1,19 @@
+Program XXX;
+
+(* *)
+(* See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 *)
+(* *)
+
+(* *)
+(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out < input-file *)
+(* *)
+
+var
+ i: LongInt;
+
+begin
+ while not eof do begin
+ readln (i);
+ writeln (i);
+ end
+end.
diff --git a/challenge-002/abigail/pascal/ch-2.p b/challenge-002/abigail/pascal/ch-2.p
new file mode 100644
index 0000000000..636bcb43c0
--- /dev/null
+++ b/challenge-002/abigail/pascal/ch-2.p
@@ -0,0 +1,38 @@
+Program ch2;
+
+(* *)
+(* See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 *)
+(* *)
+
+(* *)
+(* Run as: fpc -och-2.out ch-2.p; ./ch-2.out [-f | -t] < input-file *)
+(* *)
+
+uses
+ strutils;
+
+var
+ line: string;
+ num: longint;
+ to_base, from_base: boolean;
+
+begin
+ if paramCount () = 1 then begin
+ if paramStr (1) = '-f' then begin
+ from_base := true;
+ end;
+ if paramStr (1) = '-t' then begin
+ to_base := true;
+ end
+ end;
+ while not eof do begin
+ if to_base then begin
+ readln (num);
+ writeln (Dec2Numb (num, 1, 35));
+ end;
+ if from_base then begin
+ readln (line);
+ writeln (Numb2Dec (line, 35));
+ end
+ end
+end.
diff --git a/challenge-002/abigail/r/ch-1.r b/challenge-002/abigail/r/ch-1.r
new file mode 100644
index 0000000000..2dfc049aea
--- /dev/null
+++ b/challenge-002/abigail/r/ch-1.r
@@ -0,0 +1,19 @@
+#!/usr/local/bin/Rscript
+
+#
+# See https://theweeklychallenge.org/blog/perl-weekly-challenge-002
+#
+
+#
+# Run as: Rscript ch-1.r < input-file
+#
+
+stdin <- file ('stdin', 'r')
+repeat {
+ n <- readLines (stdin, n = 1)
+ if (length (n) == 0) {
+ break
+ }
+ n = as.integer (n)
+ cat (n, "\n")
+}
diff --git a/challenge-002/abigail/r/ch-2.r b/challenge-002/abigail/r/ch-2.r
new file mode 100644
index 0000000000..26e6796fd4
--- /dev/null
+++ b/challenge-002/abigail/r/ch-2.r
@@ -0,0 +1,47 @@
+#!/usr/local/bin/Rscript
+
+#
+# See https://theweeklychallenge.org/blog/perl-weekly-challenge-002
+#
+
+#
+# Run as: Rscript ch-2.r < input-file
+#
+
+from_base <- FALSE
+to_base <- FALSE
+
+args <- commandArgs ()
+for (i in 1 : length (args)) {
+ if (args [i] == "-f") {
+ from_base <- TRUE
+ }
+ if (args [i] == "-t") {
+ to_base <- TRUE
+ }
+}
+
+to_base_35 <- function (num) {
+ glyphs <- c (0 : 9, LETTERS)
+ out <- c ()
+ while (num > 0) {
+ rem <- num %% 35
+ num <- num %/% 35
+ out <- c (glyphs [rem + 1], out)
+ }
+ paste0 (out, collapse = "")
+}
+
+stdin <- file ('stdin', 'r')
+repeat {
+ line <- readLines (stdin, n = 1)
+ if (length (line) == 0) {
+ break
+ }
+ if (from_base) {
+ cat (strtoi (line, 35), "\n")
+ }
+ if (to_base) {
+ cat (to_base_35 (as.numeric (line)), "\n")
+ }
+}
diff --git a/challenge-002/abigail/scheme/ch-1.scm b/challenge-002/abigail/scheme/ch-1.scm
new file mode 100644
index 0000000000..62e68b8964
--- /dev/null
+++ b/challenge-002/abigail/scheme/ch-1.scm
@@ -0,0 +1,22 @@
+;;;
+;;; See https://theweeklychallenge.org/blog/perl-weekly-challenge-002
+;;;
+
+;;;
+;;; Run as: guile --no-auto-compile ch-1.scm
+;;;
+
+
+(use-modules (ice-9 rdelim))
+(define (main)
+ (define line (read-line))
+ (if (not (eof-object? line))
+ (begin
+ (display (string->number line))
+ (newline)
+ (main)
+ )
+ )
+)
+
+(main)
diff --git a/challenge-002/abigail/scheme/ch-2.scm b/challenge-002/abigail/scheme/ch-2.scm
new file mode 100644
index 0000000000..a78e77faa6
--- /dev/null
+++ b/challenge-002/abigail/scheme/ch-2.scm
@@ -0,0 +1,56 @@
+#!/usr/local/bin/guile
+!#
+
+;;;
+;;; See https://theweeklychallenge.org/blog/perl-weekly-challenge-002
+;;;
+
+;;;
+;;; Run as: guile --no-auto-compile ch-2.scm
+;;;
+
+
+(use-modules (ice-9 rdelim))
+
+(define frombase #f)
+(define tobase #f)
+(define base 35)
+
+(if (= (length (command-line)) 2)
+ (cond ((string=? (list-ref (command-line) 1) "-f") (set! frombase #t))
+ ((string=? (list-ref (command-line) 1) "-t") (set! tobase #t)))
+)
+
+(define glyphs "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
+
+(define (_to_base num base)
+ (cond ((= num 0) "")
+ (else (string-concatenate
+ (list (_to_base (floor-quotient num base) base)
+ (substring glyphs (modulo num base)
+ (+ 1 (modulo num base))))))))
+(define (to_base num base)
+ (cond ((= num 0) "0")
+ (else (_to_base num base))))
+
+
+(define (from_base num base)
+ (define len (string-length num))
+ (cond ((= len 0) 0)
+ (else (+ (string-contains glyphs (string-take-right num 1))
+ (* base (from_base (string-drop-right num 1) base))))))
+
+
+(define (main)
+ (define line (read-line))
+ (if (not (eof-object? line))
+ (begin
+ (if tobase (display ( to_base (string->number line) base)))
+ (if frombase (display (from_base line base)))
+ (newline)
+ (main)
+ )
+ )
+)
+
+(main)
diff --git a/challenge-002/abigail/tcl/ch-1.tcl b/challenge-002/abigail/tcl/ch-1.tcl
new file mode 100644
index 0000000000..856d91641e
--- /dev/null
+++ b/challenge-002/abigail/tcl/ch-1.tcl
@@ -0,0 +1,17 @@
+#!/usr/local/opt/tcl-tk/bin/tclsh
+
+#
+# See https://theweeklychallenge.org/blog/perl-weekly-challenge-002
+#
+
+#
+# Run as: tclsh ch-1.tcl < input-file
+#
+
+while {[gets stdin line] >= 0} {
+ set line [string trimleft $line 0]
+ if {[string length $line] == 0} {
+ set line 0
+ }
+ puts $line
+}
diff --git a/challenge-002/abigail/tcl/ch-2.tcl b/challenge-002/abigail/tcl/ch-2.tcl
new file mode 100644
index 0000000000..4feed6d2cb
--- /dev/null
+++ b/challenge-002/abigail/tcl/ch-2.tcl
@@ -0,0 +1,56 @@
+#!/usr/local/opt/tcl-tk/bin/tclsh
+
+#
+# See https://theweeklychallenge.org/blog/perl-weekly-challenge-002
+#
+
+#
+# Run as: tclsh ch-2.tcl < input-file
+#
+
+set frombase 0
+set tobase 0
+set glyphs "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+set base 35
+
+if {$argc == 1} {
+ if {[lindex $argv 0] == "-f"} {
+ set frombase 1
+ }
+ if {[lindex $argv 0] == "-t"} {
+ set tobase 1
+ }
+}
+
+proc from_base {num base} {
+ global glyphs
+ set out 0
+ foreach c [split $num {}] {
+ set out [expr $base * $out + [string first $c $glyphs]]
+ }
+ return $out
+}
+
+proc to_base {num base} {
+ global glyphs
+ if {$num == 0} {
+ return 0
+ }
+ set out ""
+ while {$num > 0} {
+ set out [string index $glyphs [expr {$num % $base}]]$out
+ set num [expr $num / $base]
+ }
+ return $out
+}
+
+
+
+while {[gets stdin line] >= 0} {
+ if {$frombase == 1} {
+ puts [from_base $line $base]
+ }
+ if { $tobase == 1} {
+ puts [ to_base $line $base]
+ }
+}