diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-11-28 16:30:14 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-28 16:30:14 +0000 |
| commit | 6f5b251acfdda5af6d18822caccbc1f8bff7a20e (patch) | |
| tree | 6c4e32d2942cef92ed933f0bebfd4781be557331 | |
| parent | 1c24e34ffe53335eb33b081ec36cf0c70c66a1ac (diff) | |
| parent | e35c89e1756f1092172d34494193acb9795d8c29 (diff) | |
| download | perlweeklychallenge-club-6f5b251acfdda5af6d18822caccbc1f8bff7a20e.tar.gz perlweeklychallenge-club-6f5b251acfdda5af6d18822caccbc1f8bff7a20e.tar.bz2 perlweeklychallenge-club-6f5b251acfdda5af6d18822caccbc1f8bff7a20e.zip | |
Merge pull request #5290 from Abigail/abigail/week-126
Two more solutions for week 126, part 1
| -rw-r--r-- | challenge-126/abigail/README.md | 4 | ||||
| -rw-r--r-- | challenge-126/abigail/bc/ch-1.bc | 51 | ||||
| -rw-r--r-- | challenge-126/abigail/go/ch-1.go | 41 | ||||
| -rw-r--r-- | challenge-126/abigail/scheme/ch-1.scm | 44 | ||||
| -rw-r--r-- | challenge-126/abigail/t/ctest.ini | 3 | ||||
| -rw-r--r-- | challenge-126/abigail/tcl/ch-1.tcl | 23 |
6 files changed, 166 insertions, 0 deletions
diff --git a/challenge-126/abigail/README.md b/challenge-126/abigail/README.md index 07235caf33..ee8f84f1bb 100644 --- a/challenge-126/abigail/README.md +++ b/challenge-126/abigail/README.md @@ -28,12 +28,16 @@ There are 13 numbers between `1` and `25` that don't contain digit `1`: ### Solutions * [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) * [Lua](lua/ch-1.lua) * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) * [Ruby](ruby/ch-1.rb) +* [Scheme](scheme/ch-1.scm) +* [Tcl](tcl/ch-1.tcl) ### Blog [Perl Weekly Challenge 126: Count Numbers][blog1] diff --git a/challenge-126/abigail/bc/ch-1.bc b/challenge-126/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..a3204ccc31 --- /dev/null +++ b/challenge-126/abigail/bc/ch-1.bc @@ -0,0 +1,51 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-1.bc < input-file +# + +# +# Kind of reverses a number. Trailing zero's will be dropped though +# +define reverse (n) { + result = 0 + while (n > 0) { + result = 10 * result + (n % 10) + n = n / 10 + } + return result +} + + +while (1) { + n = read (); if (n == 0) {break} + # + # Reverse the number -- after adding a 1 + # + n = reverse (10 * n + 1) + + result = 0 + seen_one = 0 + while (n > 9) { + digit = n % 10 + n = n / 10 + result = result * 9 + if (seen_one == 1) { + result = result + 8 + } else { + if (digit == 1) { + seen_one = 1 + } else { + if (digit > 0) { + result = result + digit - 1 + } + } + } + } + result +} + + +halt diff --git a/challenge-126/abigail/go/ch-1.go b/challenge-126/abigail/go/ch-1.go new file mode 100644 index 0000000000..a769bbf4af --- /dev/null +++ b/challenge-126/abigail/go/ch-1.go @@ -0,0 +1,41 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import ( + "fmt" + "bufio" + "os" +) + +func main () { + var reader = bufio . NewReader (os. Stdin) + for { + var text, err = reader . ReadString ('\n') + if (err != nil) { + break + } + result := 0 + seen_one := false + for _, digit := range text { + if digit < '0' || digit > '9' { + continue + } + result = 9 * result + if seen_one { + result += 8 + } else if digit == '1' { + seen_one = true + } else if digit != '0' { + result = result + int (digit - '0') - 1 + } + } + fmt . Println (result) + } +} diff --git a/challenge-126/abigail/scheme/ch-1.scm b/challenge-126/abigail/scheme/ch-1.scm new file mode 100644 index 0000000000..7ae444e59d --- /dev/null +++ b/challenge-126/abigail/scheme/ch-1.scm @@ -0,0 +1,44 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-1.scm < input-file +;;; + +(use-modules (ice-9 rdelim)) + +;; +;; Return the first character of a string, *as a number*. +;; We won't call this with an empty or non-numeric string. +;; +(define (first str)(string->number (string-take str 1))) + + +(define (_nr-of-ones input result seen-one) + (if (string-null? input) result + (_nr-of-ones (string-drop input 1) + (+ (* result 9) + (cond (seen-one 8) + ((> (first input) 0) (- (first input) 1)) + (else 0))) + (or seen-one (= (first input) 1)))) +) + +;; +;; Wrapper around _nr-of-ones, supplying additional parameters. +;; +(define (nr-of-ones input)(_nr-of-ones input 0 #f)) + + +(define (main) + (define line (read-line)) + (if (not (eof-object? line)) + (begin + (display (nr-of-ones line))(newline) + (main) + ) + ) +) + +(main) diff --git a/challenge-126/abigail/t/ctest.ini b/challenge-126/abigail/t/ctest.ini index 84c6227ab4..dd16bcecfe 100644 --- a/challenge-126/abigail/t/ctest.ini +++ b/challenge-126/abigail/t/ctest.ini @@ -7,3 +7,6 @@ 1-1 = Given Examples
1-2 = Other Examples
2-1 = Given Example
+
+[1-1,1-2/bc]
+add_to_input = 0
diff --git a/challenge-126/abigail/tcl/ch-1.tcl b/challenge-126/abigail/tcl/ch-1.tcl new file mode 100644 index 0000000000..664cf75b90 --- /dev/null +++ b/challenge-126/abigail/tcl/ch-1.tcl @@ -0,0 +1,23 @@ +# +# See ../README.md +# + +# +# Run as: tclsh ch-1.tcl < input-file +# + +while {[gets stdin line] >= 0} { + set result 0 + set seen_one 0 + foreach char [split $line ""] { + set result [expr 9 * $result] + if {$seen_one == 1} { + set result [expr $result + 8] + } elseif {$char == 1} { + set seen_one 1 + } elseif {$char > 0} { + set result [expr $result + $char - 1] + } + } + puts $result +} |
