Add most of lab 6 content

This commit is contained in:
Isaac Shoebottom 2022-09-29 16:37:45 -03:00
parent 7d96f68a5e
commit 8addf5798a
3 changed files with 56 additions and 0 deletions

View File

@ -5,3 +5,9 @@
In this lab I learned about using hash sets and json parsing in racket. In this lab I learned about using hash sets and json parsing in racket.
<!-- more --> <!-- more -->
## Hash Sets
I learned about how to create, access and how to perform operations around hash sets, such as retrieving a color when accessing the hash set with the key of a fruit. You can use them to keep track of counts of a given string, such as in the test module in the lab.
## JSON Parsing
To be filled

30
labs/L06/Lab6.rkt Normal file
View File

@ -0,0 +1,30 @@
#lang racket
(define ht (hash "apple" 'red "banana" 'yellow))
(module+ test
(require rackunit)
(check-equal? (hash-ref ht "apple") 'red))
(define ht2 (hash-set ht "coconut" 'brown))
(module+ test
(check-equal? (hash-ref ht2 "coconut") 'brown)
(check-exn exn:fail? (lambda () (hash-ref ht "coconut"))))
(define (census . lst)
(define (helper lst acc-hash)
(cond
[(empty? lst) (hash->list acc-hash)]
[else
(let* ([key (first lst)]
[current (hash-ref acc-hash key 0)])
(helper (rest lst) (hash-set acc-hash key (add1 current))))]))
(helper lst (hash)))
(module+ test
(check-equal?
(sort (census 1 2 3 1 2) #:key car <) '((1 . 2) (2 . 2) (3 . 1)))
(check-equal?
(sort (census "aardvark" "dingo" "buffalo" "buffalo" "bear") #:key car string<?)
'(("aardvark" . 1) ("bear" . 1) ("buffalo" . 2) ("dingo" . 1))))

20
labs/L06/errors.json Normal file
View File

@ -0,0 +1,20 @@
{
"errors": [
{
"status": "403",
"source": { "pointer": "/data/attributes/secret-powers" },
"detail": "Editing secret powers is not authorized on Sundays."
},
{
"status": "422",
"source": { "pointer": "/data/attributes/volume" },
"detail": "Volume does not, in fact, go to 11."
},
{
"status": "500",
"source": { "pointer": "/data/attributes/reputation" },
"title": "The backend responded with an error",
"detail": "Reputation service not responding after three requests."
}
]
}