diff options
30 files changed, 513 insertions, 20 deletions
diff --git a/challenge-002/abigail/README.md b/challenge-002/abigail/README.md index bb5cb18c8f..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) @@ -37,9 +43,16 @@ one number per line. Programs will use an option, -t (to base 35), or ### Solutions * [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/awk/ch-1.awk b/challenge-002/abigail/awk/ch-1.awk index 6f2ef2064c..b40b9b0616 100644 --- a/challenge-002/abigail/awk/ch-1.awk +++ b/challenge-002/abigail/awk/ch-1.awk @@ -1 +1,11 @@ +#!/usr/bin/awk + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 +# + +# +# Run as: awk -f ch-1.awk < input-file +# + {print 0 + $1} diff --git a/challenge-002/abigail/awk/ch-2.awk b/challenge-002/abigail/awk/ch-2.awk index b171fcd08f..77148f99c7 100644 --- a/challenge-002/abigail/awk/ch-2.awk +++ b/challenge-002/abigail/awk/ch-2.awk @@ -1,6 +1,7 @@ #!/usr/bin/awk + # -# See ../README.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 # # diff --git a/challenge-002/abigail/bash/ch-1.sh b/challenge-002/abigail/bash/ch-1.sh index b860cc4aa6..593a174ff3 100644 --- a/challenge-002/abigail/bash/ch-1.sh +++ b/challenge-002/abigail/bash/ch-1.sh @@ -1,7 +1,7 @@ #!/bin/sh # -# See ../README.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 # # diff --git a/challenge-002/abigail/bash/ch-2.sh b/challenge-002/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..006c46a8d6 --- /dev/null +++ b/challenge-002/abigail/bash/ch-2.sh @@ -0,0 +1,60 @@ +#!/bin/sh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +printf -v ord_0 %d "'0" +printf -v ord_A %d "'A" + +while getopts "ft" opt +do case "${opt}" in + f) action=from ;; + t) action=to ;; + esac +done + +function from_base_35 () { + local in=$1 + local ord + local char + base_10=0 + + for ((i = 0; i < ${#in}; i ++)) + do ((base_10 = 35 * base_10)) + char=${in:$i:1} + printf -v ord %d "'$char" + if [[ $char =~ [0-9] ]] + then ((base_10 = base_10 + ord - ord_0)) + else ((base_10 = base_10 + ord - ord_A + 10)) + fi + done +} + +function to_base_35 () { + local in=$1 + base_35="" + + while ((in > 0)) + do ((rem = in % 35)) + ((in = in / 35)) + if ((rem > 9)) + then printf -v char "\x$(printf %x $((ord_A + rem - 10)))" + else char=$rem + fi + base_35=${char}${base_35} + done +} + +while read line +do if [[ "$action" = from ]] + then from_base_35 $line; echo $base_10 + else to_base_35 $line; echo $base_35 + fi +done diff --git a/challenge-002/abigail/bc/ch-1.bc b/challenge-002/abigail/bc/ch-1.bc index e69de29bb2..942d7b709f 100644 --- a/challenge-002/abigail/bc/ch-1.bc +++ b/challenge-002/abigail/bc/ch-1.bc @@ -0,0 +1,9 @@ +#!/usr/bin/bc + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 +# + +# +# Run as: bc ch-1.bc < input-file +# diff --git a/challenge-002/abigail/c/ch-1.c b/challenge-002/abigail/c/ch-1.c index 09eb2ebf82..5e8f704b81 100644 --- a/challenge-002/abigail/c/ch-1.c +++ b/challenge-002/abigail/c/ch-1.c @@ -3,7 +3,7 @@ # include <string.h> /* - * See ../README.md + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 */ /* diff --git a/challenge-002/abigail/c/ch-2.c b/challenge-002/abigail/c/ch-2.c index 20cdd146d6..91ff62dcfd 100644 --- a/challenge-002/abigail/c/ch-2.c +++ b/challenge-002/abigail/c/ch-2.c @@ -4,7 +4,7 @@ # include <unistd.h> /* - * See ../README.md + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 */ /* 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/lua/ch-1.lua b/challenge-002/abigail/lua/ch-1.lua index a60dee614f..a9cdc35ba3 100644 --- a/challenge-002/abigail/lua/ch-1.lua +++ b/challenge-002/abigail/lua/ch-1.lua @@ -1,7 +1,7 @@ #!/opt/local/bin/lua -- --- See ../README.md +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 -- -- diff --git a/challenge-002/abigail/lua/ch-2.lua b/challenge-002/abigail/lua/ch-2.lua index 789e2a0173..355d10d133 100644 --- a/challenge-002/abigail/lua/ch-2.lua +++ b/challenge-002/abigail/lua/ch-2.lua @@ -1,7 +1,7 @@ #!/opt/local/bin/lua -- --- See ../README.md +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 -- -- diff --git a/challenge-002/abigail/node/ch-1.js b/challenge-002/abigail/node/ch-1.js index 5146e85366..cae6d1fde8 100644 --- a/challenge-002/abigail/node/ch-1.js +++ b/challenge-002/abigail/node/ch-1.js @@ -1,7 +1,7 @@ #!/usr/local/bin/node // -// See ../README.md +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 // // diff --git a/challenge-002/abigail/node/ch-2.js b/challenge-002/abigail/node/ch-2.js index 84d0f6976e..d7086b3b8f 100644 --- a/challenge-002/abigail/node/ch-2.js +++ b/challenge-002/abigail/node/ch-2.js @@ -1,7 +1,7 @@ #!/usr/local/bin/node // -// See ../README.md +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 // // 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/perl/ch-1.pl b/challenge-002/abigail/perl/ch-1.pl index 08572c9fa6..800784d54b 100644 --- a/challenge-002/abigail/perl/ch-1.pl +++ b/challenge-002/abigail/perl/ch-1.pl @@ -10,7 +10,7 @@ use experimental 'signatures'; use experimental 'lexical_subs'; # -# See ../README.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 # # diff --git a/challenge-002/abigail/perl/ch-2.pl b/challenge-002/abigail/perl/ch-2.pl index a9b9c006ba..983f52c9f6 100644 --- a/challenge-002/abigail/perl/ch-2.pl +++ b/challenge-002/abigail/perl/ch-2.pl @@ -10,7 +10,7 @@ use experimental 'signatures'; use experimental 'lexical_subs'; # -# See ../README.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 # # diff --git a/challenge-002/abigail/python/ch-1.py b/challenge-002/abigail/python/ch-1.py index de6839af0a..0c7d4ebe5d 100644 --- a/challenge-002/abigail/python/ch-1.py +++ b/challenge-002/abigail/python/ch-1.py @@ -1,14 +1,14 @@ #!/opt/local/bin/python # -# See ../READ.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 # # -# Run as python ch-1.py < input-file +# Run as: python ch-1.py < input-file # import fileinput for line in fileinput . input (): - print int (line) + print (int (line)) diff --git a/challenge-002/abigail/python/ch-2.py b/challenge-002/abigail/python/ch-2.py index 22fe4fcaf0..b00e0dd541 100644 --- a/challenge-002/abigail/python/ch-2.py +++ b/challenge-002/abigail/python/ch-2.py @@ -1,11 +1,11 @@ #!/opt/local/bin/python # -# See ../READ.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 # # -# Run as python ch-2.py {-f | -t} < input-file +# Run as: python ch-2.py {-f | -t} < input-file # import fileinput @@ -28,7 +28,7 @@ for opt, val in opts: do_to_base = 1 if do_to_base + do_from_base != 1: - print "Need exactly one of -f or -t" + print ("Need exactly one of -f or -t") sys . exit (1) @@ -52,7 +52,7 @@ def to_base (number): # Translate a number from base BASE to base 10 # def from_base (number): - return int (number, BASE) + return int (number . strip (), BASE) # # Need to clean argv, else fileinput will try to open a file @@ -60,4 +60,4 @@ def from_base (number): sys . argv [1:] = [] for line in fileinput . input (): - print from_base (line) if do_from_base else to_base (int (line)) + print (from_base (line) if do_from_base else to_base (int (line))) 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/ruby/ch-1.rb b/challenge-002/abigail/ruby/ch-1.rb index 0ef00293d6..b052cf842f 100644 --- a/challenge-002/abigail/ruby/ch-1.rb +++ b/challenge-002/abigail/ruby/ch-1.rb @@ -1,7 +1,7 @@ #!/usr/bin/ruby # -# See ../README.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 # # diff --git a/challenge-002/abigail/ruby/ch-2.rb b/challenge-002/abigail/ruby/ch-2.rb index 95efc096a7..2a55410b96 100644 --- a/challenge-002/abigail/ruby/ch-2.rb +++ b/challenge-002/abigail/ruby/ch-2.rb @@ -1,7 +1,7 @@ #!/usr/bin/ruby # -# See ../README.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-002 # # 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] + } +} |
