Last active
February 5, 2020 01:12
-
-
Save kmicinski/9bfe1761418c9d729f85b5c00de90f04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(define x (hash 'n0 1/2 | |
'n1 1/4 | |
'n2 1/4)) | |
;; input is some hash, h | |
(define (max-key h) | |
(define keys (hash-keys h)) | |
(define values (hash-values h)) | |
(define max-value (apply max values)) | |
(define key-value-pairs (hash->list h)) | |
(pretty-print key-value-pairs) | |
;; kvs is a list of key/value pairs | |
(define (return-max-key kvs max-val) | |
(match kvs | |
['() (error 'impossible)] | |
[`(,hd . ,tl) #:when (equal? max-val (cdr hd)) | |
(car hd)] | |
[`(,hd . ,tl) | |
(return-max-key tl max-val)])) | |
(return-max-key key-value-pairs max-value)) | |
;; | |
(define (last-element l) | |
(match l | |
[`() (error 'empty-list)] | |
[`(,hd) hd] | |
[`(,hd . ,tl) (last-element tl)])) | |
(define (reverse l) | |
(match l | |
[`() '()] | |
[`(,hd) `(,hd)] | |
[`(,hd . ,tl) (append (reverse tl) `(,hd))])) | |
;; going to set each of the keys in h-acc to 1 | |
(define (set-1 keys h-acc) | |
(match keys | |
[`() h-acc] | |
[`(,hd . ,tl) (set-1 tl (hash-set h-acc hd 1))])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment