aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-10-22 15:49:40 +0200
committerAbigail <abigail@abigail.be>2021-10-22 15:49:40 +0200
commitf99e71c8bc4fa2a05cacbd35b1871cff3f81fe72 (patch)
tree38ff1ffad4c3dc4a1600a0912d765d7ffb712d1e
parenta761e5f5c11dee3dc7f95a9bd1b3b47b1b60a23a (diff)
downloadperlweeklychallenge-club-f99e71c8bc4fa2a05cacbd35b1871cff3f81fe72.tar.gz
perlweeklychallenge-club-f99e71c8bc4fa2a05cacbd35b1871cff3f81fe72.tar.bz2
perlweeklychallenge-club-f99e71c8bc4fa2a05cacbd35b1871cff3f81fe72.zip
Scheme solutions for week 135
-rw-r--r--challenge-135/abigail/README.md2
-rw-r--r--challenge-135/abigail/scheme/ch-1.scm41
-rw-r--r--challenge-135/abigail/scheme/ch-2.scm67
3 files changed, 110 insertions, 0 deletions
diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md
index e10d8fbeda..6178e463fe 100644
--- a/challenge-135/abigail/README.md
+++ b/challenge-135/abigail/README.md
@@ -12,6 +12,7 @@
* [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)
## Part 2
@@ -26,4 +27,5 @@
* [Perl](perl/ch-2.pl)
* [Python](python/ch-2.py)
* [Ruby](ruby/ch-2.rb)
+* [Scheme](scheme/ch-2.scm)
* [Tcl](tcl/ch-2.tcl)
diff --git a/challenge-135/abigail/scheme/ch-1.scm b/challenge-135/abigail/scheme/ch-1.scm
new file mode 100644
index 0000000000..1b391cd159
--- /dev/null
+++ b/challenge-135/abigail/scheme/ch-1.scm
@@ -0,0 +1,41 @@
+;;;
+;;; See ../README.md
+;;;
+
+;;;
+;;; Run as: guile --no-auto-compile ch-1.scm < input-file
+;;;
+
+(use-modules (ice-9 regex))
+(use-modules (ice-9 rdelim))
+
+(define (main)
+ (define line (read-line))
+ (define is-number)
+ (define number)
+ (define ll)
+ (if (not (eof-object? line))
+ (begin
+ (set! is-number (string-match "^[-+]?([0-9]+)$" line))
+ (if (not is-number)
+ (display "not an integer")
+ (begin
+ (set! number (match:substring is-number 1))
+ (set! ll (string-length number))
+ (if (= (modulo ll 2) 0)
+ (display "even number of digits")
+ (if (< ll 3)
+ (display "too short")
+ (display (substring number (/ (- ll 3) 2)
+ (/ (+ ll 3) 2)))
+ )
+ )
+ )
+ )
+ (newline)
+ (main)
+ )
+ )
+)
+
+(main)
diff --git a/challenge-135/abigail/scheme/ch-2.scm b/challenge-135/abigail/scheme/ch-2.scm
new file mode 100644
index 0000000000..f5c29b5dac
--- /dev/null
+++ b/challenge-135/abigail/scheme/ch-2.scm
@@ -0,0 +1,67 @@
+;;;
+;;; See ../README.md
+;;;
+
+;;;
+;;; Run as: guile --no-auto-compile ch-2.scm < input-file
+;;;
+
+(use-modules (ice-9 regex))
+(use-modules (ice-9 rdelim))
+(use-modules (ice-9 iconv))
+(use-modules (rnrs bytevectors))
+(use-modules (srfi srfi-1))
+
+(define pat "^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}[0-9]$")
+(define w (list 1 3 1 7 3 9 1))
+
+
+;;
+;; Create a procedure 'byte->val'. It takes the ASCII value of a
+;; character, and returns the corresponding number for the SEDOL
+;; checksum (-10 for "A" .. "Z").
+;;
+(define ords (bytevector->u8-list (string->bytevector "09A" "UTF-8")))
+(define ord-0 (list-ref ords 0))
+(define ord-9 (list-ref ords 1))
+(define ord-A (list-ref ords 2))
+(define (byte->val b)
+ (if (<= b ord-9)
+ (- b ord-0)
+ (- b ord-A)))
+
+
+(define (main)
+ (define sedol (read-line))
+ (define is-sedol)
+ (define valid 0)
+ (define check 0)
+ (define values)
+ (if (not (eof-object? sedol))
+ (begin
+ (set! valid 0)
+ (set! check 0)
+ (set! is-sedol (string-match pat sedol))
+ (if (regexp-match? is-sedol)
+ (begin
+ (set! values
+ (map-in-order byte->val
+ (bytevector->u8-list
+ (string->bytevector
+ (match:substring is-sedol 0) "UTF-8"))))
+ (set! check
+ (fold (lambda (weight val sum) (+ sum (* weight val)))
+ 0 w values))
+ ;;
+ ;; A SEDOL is valid iff the checksum is a multiple of 10
+ ;;
+ (if (= 0 (modulo check 10)) (set! valid 1))
+ )
+ )
+ (display valid)(newline)
+ (main)
+ )
+ )
+)
+
+(main)