Skip to content

Implement etl #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 8, 2015
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"hamming",
"anagram",
"rna-transcription",
"etl",
"word-count",
"phone-number",
"nucleotide-count",
Expand Down
52 changes: 52 additions & 0 deletions etl/etl-test.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#lang racket

(require "etl.rkt")

(module+ test
(require rackunit rackunit/text-ui)

(define mixed-case-input (hash 1 '("a" "E" "I" "o" "U" "L" "N" "r" "s" "T")
2 '("D" "G")
3 '("B" "c" "M" "P")
4 '("f" "h" "V" "W" "y")
5 '("K")
8 '("J" "x")
10 '("q" "z")))

(define expected (hash "a" 1 "b" 3 "c" 3 "d" 2 "e" 1 "f" 4 "g" 2
"h" 4 "i" 1 "j" 8 "k" 5 "l" 1 "m" 3 "n" 1
"o" 1 "p" 3 "q" 10 "r" 1 "s" 1 "t" 1
"u" 1 "v" 4 "x" 8 "w" 4 "y" 4 "z" 10))

(define negative-keys-input (hash 1 '("L" "N")
-3 '("B" "c" "P")))

(define listof-chars-input (hash 1 '(#\N #\r #\q)))

(define suite
(test-suite
"etl tests"

(test-true "empty hash"
(zero? (hash-count (etl (make-hash)))))

(test-case
"mixed case input"
(define actual (etl mixed-case-input))
(check-equal? (hash-count actual) (hash-count expected))
(for ([k (hash-keys actual)])
#;(printf "actual: ~a -> ~a, expected: ~a -> ~a\n"
k (hash-ref actual k)
k (hash-ref expected k))
(check-equal? (hash-ref actual k)
(hash-ref expected k))))

(test-exn
"test bad input: negative keys"
exn:fail:contract? (lambda () (etl negative-keys-input)))

(test-exn
"test bad input: list of non-string values"
exn:fail:contract? (lambda () (etl listof-chars-input)))))

(run-tests suite))
3 changes: 3 additions & 0 deletions etl/etl.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#lang racket

(provide etl)
21 changes: 21 additions & 0 deletions etl/example.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#lang racket

(provide (contract-out
[etl (-> valid-input valid-output)]))

(define (lower-case? s) (not (regexp-match? #rx"[A-Z]" s)))

;; Accept upper- or lower-case-letters strings as values
(define valid-input
(hash/c exact-nonnegative-integer? (listof string?)))

;; Keys will always be lower-case
(define valid-output
(hash/c lower-case? exact-nonnegative-integer?))

(define (etl h)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but you can use for*/hash to build the result and in-hash to walk the old table faster.

(define (etl h)
    (for*/hash ([(score letter*) (in-hash h)]
                [letter (in-list letter*)])
      ...))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those I didn't know: duly noted, thanks.

(for*/hash ([(score letter*) (in-hash h)]
[letter (in-list letter*)])
;(printf "~a: ~a\n" letter score)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might suggest deleting this line.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved!

(values (string-downcase letter) score)))