diff options
author | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
---|---|---|
committer | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
commit | 8777ff071f7bb37631baa7b6717ad29961e50911 (patch) | |
tree | 6d59c4ed58e454b960339c3d1151f0a879e8d7cb /racket/leetcode | |
parent | 6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff) | |
download | gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip |
sorting by language
Diffstat (limited to 'racket/leetcode')
53 files changed, 903 insertions, 0 deletions
diff --git a/racket/leetcode/lc-1018-binary-prefix.rkt b/racket/leetcode/lc-1018-binary-prefix.rkt new file mode 100644 index 0000000..fa82681 --- /dev/null +++ b/racket/leetcode/lc-1018-binary-prefix.rkt @@ -0,0 +1,11 @@ +#lang racket +(define/contract (prefixes-div-by5 A) + (-> (listof exact-integer?) (listof boolean?)) + (define ns (make-vector (length A) #false)) + (for/fold ([acc 0]) + ([b (in-list A)] + [i (in-naturals)]) + (let ([test-val (remainder (+ (* 2. acc) b) 5)]) + (when (= 0 test-val) (vector-set! ns i #true)) + test-val)) + (vector->list ns))
\ No newline at end of file diff --git a/racket/leetcode/lc-1037-boomerang.rkt b/racket/leetcode/lc-1037-boomerang.rkt new file mode 100644 index 0000000..fd95695 --- /dev/null +++ b/racket/leetcode/lc-1037-boomerang.rkt @@ -0,0 +1,21 @@ +#lang racket +(define/contract (is-boomerang points) + (-> (listof (listof exact-integer?)) boolean?) + (match points + [(list-no-order a b c) #:when (equal? a b) #false] ; Are any two points the same? + [(list (list x _) (list x _) (list x _)) #false] ; Are they on a horizontal line? + [(list (list _ y) (list _ y) (list _ y)) #false] ; Are they on a vertical line? + [(list-no-order (list x1 _) (list x2 _) (list x3 _)) ; Are two points on a horizontal line, + #:when (and (= x1 x2) ; but the third point isn't? + (not (= x1 x3))) #true] + [(list-no-order (list _ y1) (list _ y2) (list _ y3)) ; Are two points on a vertical line, + #:when (and (= y1 y2) ; but the third point isn't? + (not (= y1 y3))) #true] + [(list (list x1 y1) (list x2 y2) (list x3 y3)) ; If none of the special cases apply, + (let ([m (/ (- y2 y1) (- x2 x1))]) ; calculate the slope between two points + (not (= y3 (+ y1 (* m (- x3 x1))))))])) ; and see if the line passes through the third + +(is-boomerang '((1 1) (2 3) (3 2))) +(is-boomerang '((1 1) (2 2) (3 3))) +(is-boomerang '((0 0) (0 2) (2 1))) +(is-boomerang '((0 0) (1 1) (1 1)))
\ No newline at end of file diff --git a/racket/leetcode/lc-1185-day-of-week.rkt b/racket/leetcode/lc-1185-day-of-week.rkt new file mode 100644 index 0000000..c90a626 --- /dev/null +++ b/racket/leetcode/lc-1185-day-of-week.rkt @@ -0,0 +1,18 @@ +#lang racket +(require racket/date) + +(define day-names + (for/hash ([day-number (in-range 0 7)] + [day-name (in-list '("Sunday" + "Monday" + "Tuesday" + "Thursday" + "Friday" + "Saturday"))]) + (values day-number day-name))) + +(define/contract (day-of-the-week day month year) + (-> exact-integer? exact-integer? exact-integer? string?) + (hash-ref day-names (date-week-day + (seconds->date + (find-seconds 0 0 0 day month year))))) diff --git a/racket/leetcode/lc-1207-unique-occurences.rkt b/racket/leetcode/lc-1207-unique-occurences.rkt new file mode 100644 index 0000000..1b4d107 --- /dev/null +++ b/racket/leetcode/lc-1207-unique-occurences.rkt @@ -0,0 +1,10 @@ +#lang racket +(define/contract (unique-occurrences arr) + (-> (listof exact-integer?) boolean?) + (define occurrences (make-hash)) + (for ([n (in-list arr)]) + (hash-update! occurrences n add1 1)) + (equal? (hash-values occurrences) + (remove-duplicates (hash-values occurrences)))) + +(unique-occurrences '(1 2 2 1 1 3))
\ No newline at end of file diff --git a/racket/leetcode/lc-1221-split-a-string-balanced.rkt b/racket/leetcode/lc-1221-split-a-string-balanced.rkt new file mode 100644 index 0000000..4c75770 --- /dev/null +++ b/racket/leetcode/lc-1221-split-a-string-balanced.rkt @@ -0,0 +1,19 @@ +#lang racket +(require rackunit) + +(define/contract (balanced-string-split s) + (-> string? exact-integer?) + (for/fold ([acc 0] + [count 0] + #:result count) + ([c (string->list s)]) + (let* ([increment (case c + [(#\R) 1] + [(#\L) -1])] + [new-acc (+ increment acc)] + [new-count (case new-acc + [(0) (add1 count)] + [else count])]) + (values new-acc new-count)))) + +(check-eq? (balanced-string-split "RLRRLLRLRL") 4)
\ No newline at end of file diff --git a/racket/leetcode/lc-125-valid-palindrome.rkt b/racket/leetcode/lc-125-valid-palindrome.rkt new file mode 100644 index 0000000..ed91d08 --- /dev/null +++ b/racket/leetcode/lc-125-valid-palindrome.rkt @@ -0,0 +1,12 @@ +#lang racket + +(define/contract (is-palindrome s) + (-> string? boolean?) + (define clean-string + (string-downcase (string-replace s #rx"[^A-Za-z0-9]" ""))) + (string-prefix? (apply string-append (map string (reverse (string->list clean-string)))) + (substring clean-string + 0 + (ceiling (/ (string-length clean-string) 2))))) + +(is-palindrome "A man, a plan, a canal: Panama")
\ No newline at end of file diff --git a/racket/leetcode/lc-1295-even-number-of-digits.rkt b/racket/leetcode/lc-1295-even-number-of-digits.rkt new file mode 100644 index 0000000..9e88454 --- /dev/null +++ b/racket/leetcode/lc-1295-even-number-of-digits.rkt @@ -0,0 +1,4 @@ +#lang racket +(define/contract (find-numbers nums) + (-> (listof exact-integer?) exact-integer?) + (count (λ (n) (odd? (order-of-magnitude n))) nums))
\ No newline at end of file diff --git a/racket/leetcode/lc-1299-replace-with-greatest-to-right.rkt b/racket/leetcode/lc-1299-replace-with-greatest-to-right.rkt new file mode 100644 index 0000000..34d3eae --- /dev/null +++ b/racket/leetcode/lc-1299-replace-with-greatest-to-right.rkt @@ -0,0 +1,8 @@ +#lang racket +(define/contract (replace-elements arr) + (-> (listof exact-integer?) (listof exact-integer?)) + (cond [(= 1 (length arr)) '(-1)] + [else (cons (apply max (cdr arr)) + (replace-elements (cdr arr)))])) + +(replace-elements '(17 18 5 4 6 1))
\ No newline at end of file diff --git a/racket/leetcode/lc-1304-find-n-unique-integers.rkt b/racket/leetcode/lc-1304-find-n-unique-integers.rkt new file mode 100644 index 0000000..9b810a0 --- /dev/null +++ b/racket/leetcode/lc-1304-find-n-unique-integers.rkt @@ -0,0 +1,5 @@ +#lang racket +(define/contract (sum-zero n) + (-> exact-integer? (listof exact-integer?)) + (cond [(even? n) (remove 0 (range (/ n -2) (add1 (/ n 2))))] + [(odd? n) (range (/ (- n 1) -2) (add1 (/ (- n 1) 2)))]))
\ No newline at end of file diff --git a/racket/leetcode/lc-1436-destination-city.rkt b/racket/leetcode/lc-1436-destination-city.rkt new file mode 100644 index 0000000..ce82f08 --- /dev/null +++ b/racket/leetcode/lc-1436-destination-city.rkt @@ -0,0 +1,14 @@ +#lang racket +(define/contract (dest-city paths) + (-> (listof (listof string?)) string?) + (define city-pairs (make-hash paths)) + (define (go-to-next-city origin) + (let ([destination (hash-ref city-pairs origin #false)]) + (if destination + (go-to-next-city (car destination)) + origin))) + (go-to-next-city (caar paths))) + +(dest-city '(("London" "New York") + ("New York" "Lima") + ("Lima" "Sao Paolo")))
\ No newline at end of file diff --git a/racket/leetcode/lc-1450-students-doing-homework.rkt b/racket/leetcode/lc-1450-students-doing-homework.rkt new file mode 100644 index 0000000..14ff079 --- /dev/null +++ b/racket/leetcode/lc-1450-students-doing-homework.rkt @@ -0,0 +1,12 @@ +#lang racket +(define/contract (busy-student start-time end-time query-time) + (-> (listof exact-integer?) (listof exact-integer?) exact-integer? exact-integer?) + (count (λ (start end) + (and (start . <= . query-time) + (query-time . <= . end))) start-time end-time)) + +(busy-student '(1 2 3) '(3 2 7) 4) +(busy-student '(4) '(4) 4) +(busy-student '(9 8 7 6 5 4 3 2 1) + '(10 10 10 10 10 10 10 10 10) + 5)
\ No newline at end of file diff --git a/racket/leetcode/lc-1460-make-two-arrays-equal.rkt b/racket/leetcode/lc-1460-make-two-arrays-equal.rkt new file mode 100644 index 0000000..584ac97 --- /dev/null +++ b/racket/leetcode/lc-1460-make-two-arrays-equal.rkt @@ -0,0 +1,4 @@ +#lang racket +(define/contract (can-be-equal target arr) + (-> (listof exact-integer?) (listof exact-integer?) boolean?) + (equal? (sort target <) (sort arr <)))
\ No newline at end of file diff --git a/racket/leetcode/lc-1496-path-crossing.rkt b/racket/leetcode/lc-1496-path-crossing.rkt new file mode 100644 index 0000000..9c1941d --- /dev/null +++ b/racket/leetcode/lc-1496-path-crossing.rkt @@ -0,0 +1,25 @@ +#lang racket +(define/contract (is-path-crossing path) + (-> string? boolean?) + (for/fold ([current-x 0] + [current-y 0] + [trail (set '(0 0))] + [check #false] + #:result check) + ([step (in-list (string->list path))] + #:break check) + (let*-values + ([(new-x new-y) + (case step + [(#\N) (values current-x (add1 current-y))] + [(#\S) (values current-x (sub1 current-y))] + [(#\E) (values (add1 current-x) current-y)] + [(#\W) (values (sub1 current-x) current-y)])] + [(new-trail-point) (list new-x new-y)]) + (cond [(set-member? trail new-trail-point) + (values void void void #true)] + [else + (values new-x + new-y + (set-add trail new-trail-point) + #false)]))))
\ No newline at end of file diff --git a/racket/leetcode/lc-1700-students-unable-to-eat.rkt b/racket/leetcode/lc-1700-students-unable-to-eat.rkt new file mode 100644 index 0000000..75cc243 --- /dev/null +++ b/racket/leetcode/lc-1700-students-unable-to-eat.rkt @@ -0,0 +1,36 @@ +#lang racket +(define/contract (count-students students sandwiches) + (-> (listof exact-integer?) (listof exact-integer?) exact-integer?) + (for/fold ([sandwich-pile sandwiches] + [student-line students] + [remaining-students (length students)] + [break? #false] + #:result remaining-students) + ([i (in-naturals)] + #:break break?) + (cond [(and (empty? sandwich-pile) + (empty? student-line)) + (values void + void + remaining-students + #true)] + [(equal? (car sandwich-pile) + (car student-line)) + (values (cdr sandwich-pile) + (cdr student-line) + (sub1 remaining-students) + #false)] + [(and (not (equal? (list (car sandwich-pile)) + (remove-duplicates student-line))) + (= 1 (length (remove-duplicates student-line)))) + (values void + void + remaining-students + #true)] + [else + (values sandwich-pile + (append (cdr student-line) (list (car student-line))) + remaining-students + #false)]))) + +(count-students '(1 1 0 0) '(0 1 0 1)) diff --git a/racket/leetcode/lc-1812-chessboard-square.rkt b/racket/leetcode/lc-1812-chessboard-square.rkt new file mode 100644 index 0000000..206392c --- /dev/null +++ b/racket/leetcode/lc-1812-chessboard-square.rkt @@ -0,0 +1,7 @@ +#lang racket +(define/contract (square-is-white coordinates) + (-> string? boolean?) + (define file (first (string->list coordinates))) + (define rank (second (string->list coordinates))) + (or (and (odd? (char->integer file)) (even? (char->integer rank))) + (and (even? (char->integer file)) (odd? (char->integer rank)))))
\ No newline at end of file diff --git a/racket/leetcode/lc-1844-replace-all-digits-with-characters.rkt b/racket/leetcode/lc-1844-replace-all-digits-with-characters.rkt new file mode 100644 index 0000000..96aba6e --- /dev/null +++ b/racket/leetcode/lc-1844-replace-all-digits-with-characters.rkt @@ -0,0 +1,18 @@ +#lang racket +(define/contract (replace-digits s) + (-> string? string?) + (define/contract (shift-letter c x) + (-> char? char? char?) + (integer->char (+ (string->number (string x)) (char->integer c)))) + (define letters (string->list (string-replace s #rx"[0-9]" ""))) + (define digits (string->list (string-replace s #rx"[a-z]" ""))) + (foldl (λ (c x acc) + (if (equal? x #\X) + (string-append acc (string c)) + (string-append acc (string c) (string (shift-letter c x))))) + "" + letters + (if (= (length digits) (length letters)) + digits + (append digits '(#\X))) + ))
\ No newline at end of file diff --git a/racket/leetcode/lc-1854-max-pop-year.rkt b/racket/leetcode/lc-1854-max-pop-year.rkt new file mode 100644 index 0000000..75104f1 --- /dev/null +++ b/racket/leetcode/lc-1854-max-pop-year.rkt @@ -0,0 +1,17 @@ +#lang racket +(define/contract (maximum-population logs) + (-> (listof (listof exact-integer?)) exact-integer?) + ; make a hash table of every year encountered between the birth and death years + (define population (make-hash)) + ; for each person in the logs, + (for/list ([person (in-list logs)]) + ; for every year from birth to the year before death, + (for/list ([year (in-range (first person) (second person))]) + ; look up the year in the hash table and add 1 to its key, + ; or add the key and set its value to 1 if it doesn't exist yet + (hash-update! population year add1 1))) + ; convert the hash table to a list, + ; sort the list by year, + ; find the first element that maximizes the count, + ; and return the associated year + (car (argmax cdr (sort (hash->list population) < #:key car))))
\ No newline at end of file diff --git a/racket/leetcode/lc-2-add-two-numbers.rkt b/racket/leetcode/lc-2-add-two-numbers.rkt new file mode 100644 index 0000000..8062817 --- /dev/null +++ b/racket/leetcode/lc-2-add-two-numbers.rkt @@ -0,0 +1,35 @@ +#lang racket +; Definition for singly-linked list: + + +; val : integer? +; next : (or/c list-node? #f) +(struct list-node + (val next) #:mutable #:transparent) + +; constructor +(define (make-list-node val [next-node #f]) + (list-node val next-node)) + + +(define/contract (add-two-numbers l1 l2) + (-> (or/c list-node? #f) (or/c list-node? #f) (or/c list-node? #f)) + (define (process-list node [acc '()]) + (if (list-node-next node) + (process-list (list-node-next node) (cons (list-node-val node) acc)) + (cons (list-node-val node) acc))) + (define sum-of-lists (+ (string->number (apply ~a (process-list l1))) + (string->number (apply ~a (process-list l2))))) + (define sum-list-digits + (reverse + (map (λ (x) (string->number (string x))) + (string->list (number->string sum-of-lists))))) + (define (build-list l) + (if (empty? l) + #f + (make-list-node (car l) (build-list (cdr l))))) + (build-list sum-list-digits)) + +(define list1 (make-list-node 2 (make-list-node 4 (make-list-node 3)))) +(define list2 (make-list-node 5 (make-list-node 6 (make-list-node 4)))) +(add-two-numbers list1 list2)
\ No newline at end of file diff --git a/racket/leetcode/lc-217-contains-duplicate.rkt b/racket/leetcode/lc-217-contains-duplicate.rkt new file mode 100644 index 0000000..ca8d193 --- /dev/null +++ b/racket/leetcode/lc-217-contains-duplicate.rkt @@ -0,0 +1,12 @@ +#lang racket +(define/contract (contains-duplicate nums) + (-> (listof exact-integer?) boolean?) + (define nums-hash (make-hash)) + (define (check-next-number nums) + (cond [(empty? nums) #false] + [(hash-ref nums-hash (car nums) #false) #true] + [else (hash-set! nums-hash (car nums) #true) + (check-next-number (cdr nums))])) + (check-next-number nums)) + +(contains-duplicate '(1 2 3))
\ No newline at end of file diff --git a/racket/leetcode/lc-228-summary-ranges.rkt b/racket/leetcode/lc-228-summary-ranges.rkt new file mode 100644 index 0000000..9140895 --- /dev/null +++ b/racket/leetcode/lc-228-summary-ranges.rkt @@ -0,0 +1,27 @@ +#lang racket +(define (summary-ranges nums) + (define range-pairs + (cond + [(empty? nums) '()] + [(empty? (cdr nums)) (list (cons (car nums) (car nums)))] + [else (for/fold ([ranges '()] + [open-pair (first nums)] + [prev-num (first nums)] + #:result (append ranges (list (cons open-pair prev-num)))) + ([i (cdr nums)]) + (cond [(= (add1 prev-num) i) + (values ranges + open-pair + i)] + [else + (values (append ranges (list (cons open-pair prev-num))) + i + i)]))])) + (for/list ([p (in-list range-pairs)]) + (cond [(= (car p) (cdr p)) (format "~a" (car p))] + [else (format "~a->~a" (car p) (cdr p))]))) + +(summary-ranges '(0 1 2 4 5 7)) +(summary-ranges '(0 2 3 4 6 8 9)) +(summary-ranges '()) +(summary-ranges '(0))
\ No newline at end of file diff --git a/racket/leetcode/lc-290-word-pattern.rkt b/racket/leetcode/lc-290-word-pattern.rkt new file mode 100644 index 0000000..77cdba0 --- /dev/null +++ b/racket/leetcode/lc-290-word-pattern.rkt @@ -0,0 +1,23 @@ +#lang racket +(define match-string "abba") +(define a "dog") +(define b "cat") +(define Σ "dog cat cat dog") + +(define/contract (word-pattern pattern s) + (-> string? string? boolean?) + (define pattern-list (map string (string->list pattern))) + (define s-list (string-split s)) + (define match-hash (make-hash)) + (if (= (length pattern-list) (length s-list)) + (for/and ([pattern-part pattern-list] + [s-part s-list]) + (cond [(and (not (hash-has-key? match-hash pattern-part)) + (member s-part (hash-values match-hash))) #f] + [(not (hash-has-key? match-hash pattern-part)) + (hash-set! match-hash pattern-part s-part) #t] + [(string=? (hash-ref match-hash pattern-part) s-part) #t] + [else #f])) + #f)) + +(word-pattern match-string Σ)
\ No newline at end of file diff --git a/racket/leetcode/lc-345-reverse-vowels.rkt b/racket/leetcode/lc-345-reverse-vowels.rkt new file mode 100644 index 0000000..c05bf2d --- /dev/null +++ b/racket/leetcode/lc-345-reverse-vowels.rkt @@ -0,0 +1,9 @@ +#lang racket + +(define/contract (reverse-vowels s) + (-> string? string?) + (define vowels-only + (string-replace s #rx"[^aeiouAEIOU]" "")) + (define consonants-with-placeholders + (string-replace s #rx"[aeiouAEIOU]" "~a")) + (apply format consonants-with-placeholders (reverse (string->list vowels-only))))
\ No newline at end of file diff --git a/racket/leetcode/lc-349-intersection-of-2-arrays.rkt b/racket/leetcode/lc-349-intersection-of-2-arrays.rkt new file mode 100644 index 0000000..14d56ca --- /dev/null +++ b/racket/leetcode/lc-349-intersection-of-2-arrays.rkt @@ -0,0 +1,5 @@ +#lang racket + +(define/contract (intersection nums1 nums2) + (-> (listof exact-integer?) (listof exact-integer?) (listof exact-integer?)) + (set-intersect nums1 nums2))
\ No newline at end of file diff --git a/racket/leetcode/lc-36-valid-sudoku.rkt b/racket/leetcode/lc-36-valid-sudoku.rkt new file mode 100644 index 0000000..915b533 --- /dev/null +++ b/racket/leetcode/lc-36-valid-sudoku.rkt @@ -0,0 +1,33 @@ +#lang racket + +(define (pos board r c) + (list-ref (list-ref board r) c)) + +(define (scan-for-duplicates array) + (andmap (λ (row) (not (check-duplicates row))) + (map (curry filter-not (curry equal? ".")) array))) + +(define (check-rows board) + (scan-for-duplicates board)) + +(define (check-cols board) + (scan-for-duplicates (apply map list board))) + +(define (check-boxes board) + (define boxes-to-lists + (for*/list ([r (in-list '(0 3 6))] + [c (in-list '(0 3 6))]) + (for*/list ([box-r (in-range r (+ r 3))] + [box-c (in-range c (+ c 3))] + #:unless (equal? "." (pos board box-r box-c))) + (pos board box-r box-c)))) + (scan-for-duplicates boxes-to-lists)) + +(define/contract (is-valid-sudoku board) + (-> (listof (listof string?)) boolean?) + (and (check-rows board) + (check-cols board) + (check-boxes board))) + +(define valid-sudoku '[["5" "3" "." "." "7" "." "." "." "."] ["6" "." "." "1" "9" "5" "." "." "."] ["." "9" "8" "." "." "." "." "6" "."] ["8" "." "." "." "6" "." "." "." "3"] ["4" "." "." "8" "." "3" "." "." "1"] ["7" "." "." "." "2" "." "." "." "6"] ["." "6" "." "." "." "." "2" "8" "."] ["." "." "." "4" "1" "9" "." "." "5"] ["." "." "." "." "8" "." "." "7" "9"]]) +(define invalid-sudoku '[["8" "3" "." "." "7" "." "." "." "."] ["6" "." "." "1" "9" "5" "." "." "."] ["." "9" "8" "." "." "." "." "6" "."] ["8" "." "." "." "6" "." "." "." "3"] ["4" "." "." "8" "." "3" "." "." "1"] ["7" "." "." "." "2" "." "." "." "6"] ["." "6" "." "." "." "." "2" "8" "."] ["." "." "." "4" "1" "9" "." "." "5"] ["." "." "." "." "8" "." "." "7" "9"]])
\ No newline at end of file diff --git a/racket/leetcode/lc-415-add-strings.rkt b/racket/leetcode/lc-415-add-strings.rkt new file mode 100644 index 0000000..e140155 --- /dev/null +++ b/racket/leetcode/lc-415-add-strings.rkt @@ -0,0 +1,28 @@ +#lang racket + +(define/contract (add-strings num1 num2) + (-> string? string? string?) + (define (char->integer c) + ((compose string->number string) c)) + (define pad-length + (add1 (apply max (map string-length (list num1 num2))))) + (define (pad-with-zeroes n) + (~a n + #:align 'right + #:min-width pad-length + #:pad-string "0")) + (define (string-reverse s) + ((compose list->string reverse string->list) s)) + (define raw-sum + (for/fold ([sum-string ""] + [carry 0] + #:result sum-string) + ([n1 (string-reverse (pad-with-zeroes num1))] + [n2 (string-reverse (pad-with-zeroes num2))]) + (let* ([digit-sum (+ carry (char->integer n1) (char->integer n2))] + [sum-place (number->string (modulo digit-sum 10))] + [sum-carry (quotient digit-sum 10)]) + (values (string-append sum-place sum-string) + sum-carry)))) + (cond [(equal? raw-sum "00") "0"] + [else (string-trim raw-sum "0" #:repeat? #t #:right? #f)]))
\ No newline at end of file diff --git a/racket/leetcode/lc-43-multiply-strings.rkt b/racket/leetcode/lc-43-multiply-strings.rkt new file mode 100644 index 0000000..dac8c31 --- /dev/null +++ b/racket/leetcode/lc-43-multiply-strings.rkt @@ -0,0 +1,28 @@ +#lang racket + +(define/contract (char-digit->integer c) + (-> char? integer?) + (- (char->integer c) 48)) + +(define/contract (integer->string-digit n) + (-> integer? string?) + (string (integer->char (+ n 48)))) + +(define/contract (number->string1 n [acc ""]) + (->* (integer?) (string?) string?) + (cond [(and (= n 0) (equal? acc "")) "0"] + [(= n 0) acc] + [else (number->string1 + (quotient n 10) + (string-append (integer->string-digit (remainder n 10)) acc))])) + +(define/contract (multiply num1 num2) + (-> string? string? string?) + (define multiplication-steps + (for/list ([n1 (in-string num1)] + [place1 (in-range (sub1 (string-length num1)) -1 -1)]) + (for/list ([n2 (in-string num2)] + [place2 (in-range (sub1 (string-length num2)) -1 -1)]) + (apply * (append (map char-digit->integer (list n1 n2)) + (list (expt 10 place1) (expt 10 place2))))))) + (number->string1 (apply + (flatten multiplication-steps))))
\ No newline at end of file diff --git a/racket/leetcode/lc-476-number-complement.rkt b/racket/leetcode/lc-476-number-complement.rkt new file mode 100644 index 0000000..724bb47 --- /dev/null +++ b/racket/leetcode/lc-476-number-complement.rkt @@ -0,0 +1,11 @@ +#lang racket + +(define (flip-bit bit) + (cond [(char=? bit #\1) #\0] + [(char=? bit #\0) #\1])) + +(define/contract (find-complement num) + (-> exact-integer? exact-integer?) + (define num-binary-list + (string->list (number->string num 2))) + (string->number (apply ~a (map flip-bit num-binary-list)) 2))
\ No newline at end of file diff --git a/racket/leetcode/lc-500-keyboard-row.rkt b/racket/leetcode/lc-500-keyboard-row.rkt new file mode 100644 index 0000000..5f13143 --- /dev/null +++ b/racket/leetcode/lc-500-keyboard-row.rkt @@ -0,0 +1,23 @@ +#lang racket + +(define keyboard-rows (list "qwertyuiop" + "asdfghjkl" + "zxcvbnm")) + +(define keyboard-row-sets + (for/list ([row keyboard-rows]) + (list->set (map string (string->list row))))) + +(define/contract (find-words words) + (-> (listof string?) (listof string?)) + (define word-checks + (for/list ([w words]) + (define word-set + (list->set (map string (string->list (string-downcase w))))) + (if (for/or ([row keyboard-row-sets]) + (subset? word-set row)) + w + '()))) + (filter-not empty? word-checks)) + +(find-words '("Hello" "Alaska" "Dad" "Peace"))
\ No newline at end of file diff --git a/racket/leetcode/lc-504-base7.rkt b/racket/leetcode/lc-504-base7.rkt new file mode 100644 index 0000000..3e75052 --- /dev/null +++ b/racket/leetcode/lc-504-base7.rkt @@ -0,0 +1,16 @@ +#lang racket + +(define/contract (convert-to-base7 num) + (-> exact-integer? string?) + (define (max-base-power n base [pow 1]) + (cond [(n . = . (expt base pow)) pow] + [(n . < . (expt base pow)) (sub1 pow)] + [else (max-base-power n base (add1 pow))])) + (define (add-next-digit n pow acc) + (cond [(= pow 0) (string-append acc (number->string n))] + [else (add-next-digit (remainder n (expt 7 pow)) + (sub1 pow) + (string-append acc + (number->string (quotient n (expt 7 pow)))))])) + (string-append (if (negative? num) "-" "") + (add-next-digit (abs num) (max-base-power (abs num) 7) "")))
\ No newline at end of file diff --git a/racket/leetcode/lc-520-detect-capital.rkt b/racket/leetcode/lc-520-detect-capital.rkt new file mode 100644 index 0000000..80b5f7e --- /dev/null +++ b/racket/leetcode/lc-520-detect-capital.rkt @@ -0,0 +1,12 @@ +#lang racket + +(define/contract (detect-capital-use word) + (-> string? boolean?) + (if + (member word (list (string-upcase word) + (string-downcase word) + (string-titlecase word))) + #true + #false)) + +(detect-capital-use "Google")
\ No newline at end of file diff --git a/racket/leetcode/lc-551-student-attendance-record-1.rkt b/racket/leetcode/lc-551-student-attendance-record-1.rkt new file mode 100644 index 0000000..c5f1456 --- /dev/null +++ b/racket/leetcode/lc-551-student-attendance-record-1.rkt @@ -0,0 +1,11 @@ +#lang racket + +(define/contract (check-record s) + (-> string? boolean?) + (define s-list (map string (string->list s))) + (cond [(<= 2 (count (curry string=? "A") s-list)) #false] + [(string-contains? s "LLL") #false] + [else #true])) + +(check-record "PPALLP") +(check-record "PPALLL")
\ No newline at end of file diff --git a/racket/leetcode/lc-58-length-of-last-word.rkt b/racket/leetcode/lc-58-length-of-last-word.rkt new file mode 100644 index 0000000..716df90 --- /dev/null +++ b/racket/leetcode/lc-58-length-of-last-word.rkt @@ -0,0 +1,7 @@ +#lang racket + +(define/contract (length-of-last-word s) + (-> string? exact-integer?) + (if (empty? (string-split s)) + 0 + (string-length (last (string-split s)))))
\ No newline at end of file diff --git a/racket/leetcode/lc-645-set-mismatch.rkt b/racket/leetcode/lc-645-set-mismatch.rkt new file mode 100644 index 0000000..a9d9a61 --- /dev/null +++ b/racket/leetcode/lc-645-set-mismatch.rkt @@ -0,0 +1,24 @@ +#lang racket + +(define/contract (find-error-nums nums) + (-> (listof exact-integer?) (listof exact-integer?)) + (define nums-set (list->set nums)) + (define range-set (apply set (range 1 (+ 2 (set-count nums-set))))) + (define missing-num (first (set->list (set-subtract range-set nums-set)))) + (define necessary-num + (if (set-member? nums-set (- missing-num 1)) + (+ missing-num 1) + (- missing-num 1))) + (list missing-num necessary-num)) + +(find-error-nums '(1 2 2 4)) + +(define fact-stream + (letrec ([f (lambda (x y) + (cond + [(zero? (modulo (- y 1) 3)) (cons (* 3 x) (lambda() (f (* x + y) (+ y 1))))] + [else (cons x (lambda() (f (* x y) (+ y 1))))]) + [else (cons x (lambda() (f (* x y) (+ y 1))))] + (lambda () (f 1 2)) + )])))
\ No newline at end of file diff --git a/racket/leetcode/lc-657-robot-return.rkt b/racket/leetcode/lc-657-robot-return.rkt new file mode 100644 index 0000000..908605a --- /dev/null +++ b/racket/leetcode/lc-657-robot-return.rkt @@ -0,0 +1,17 @@ +#lang racket + +(define/contract (judge-circle moves) + (-> string? boolean?) + (equal? '(0 0) + (for/fold ([y-pos 0] + [x-pos 0] + #:result (list y-pos x-pos)) + ([move (map string (string->list moves))]) + (values (case move + [("U") (add1 y-pos)] + [("D") (sub1 y-pos)] + [else y-pos]) + (case move + [("L") (add1 x-pos)] + [("R") (sub1 x-pos)] + [else x-pos])))))
\ No newline at end of file diff --git a/racket/leetcode/lc-68-justification.rkt b/racket/leetcode/lc-68-justification.rkt new file mode 100644 index 0000000..537e2c5 --- /dev/null +++ b/racket/leetcode/lc-68-justification.rkt @@ -0,0 +1,44 @@ +#lang racket +(define/contract (full-justify words max-width) + (-> (listof string?) exact-integer? (listof string?)) + + (define/contract (justify-line line [last-line #f]) + (->* ((listof string?)) (boolean?) string?) + (define gaps (sub1 (length line))) + (cond [last-line + (~a (string-join line " ") #:min-width max-width)] ; Right-pad the last line + [(= 1 (length line)) + (~a (first line) #:min-width max-width)] ; Right-pad single-word lines + [else + (let* ([words-length (apply + (map string-length line))] + [spacing-length (- max-width words-length)] ; How many spaces do we need? + [spacing-list (make-list gaps 1)] ; Every gap needs to be at least + [distribute (- spacing-length gaps)] ; 1 space long, so we need to + [distributed-spaces ; distribute the excess + (for/list ([space (in-list spacing-list)] + [i (in-naturals 1)]) + (+ space + (quotient distribute gaps) ; Add an equal number of spaces + (if (<= i ; to each gap, then add the + (modulo distribute gaps)) 1 0)))]) ; remainder at the front + (apply string-append + (append (map (λ (w s) + (string-append ; Knit together the first (n-1) + w (make-string s #\space))) ; words and gaps, then append + (drop-right line 1) ; the final word at the end + distributed-spaces) + (take-right line 1))))])) + + (for/fold ([lines '()] ; List of justified lines + [line-acc '()] ; Words to fit into the next line + #:result (append lines ; Only return the list of lines + (list (justify-line line-acc #t)))) ; and append the final line + ([word (in-list words)]) + (let* ([candidate-acc (append line-acc (list word))] + [candidate-length + (string-length (string-join candidate-acc " "))]) + (if (candidate-length . <= . max-width) ; If the word fits into the line, + (values lines ; keep the current line list + candidate-acc) ; and add it to the accumulator + (values (append lines (list (justify-line line-acc))) ; Otherwise, wrap up this line + (list word)))))) ; and start a new accumulator
\ No newline at end of file diff --git a/racket/leetcode/lc-690-employee-importance.rkt b/racket/leetcode/lc-690-employee-importance.rkt new file mode 100644 index 0000000..1fb3fcc --- /dev/null +++ b/racket/leetcode/lc-690-employee-importance.rkt @@ -0,0 +1,14 @@ +#lang racket + +(define/contract (sum-even-after-queries A queries) + (-> (listof exact-integer?) + (listof (listof exact-integer?)) + (listof exact-integer?)) + (define array (list->vector A)) + (for/list ([query (in-list queries)]) + (vector-set! array + (second query) + (+ (first query) (vector-ref array (second query)))) + (for/sum ([element (vector-filter even? array)]) element))) + +(sum-even-after-queries '[1 2 3 4] '[[1 0] [-3 1] [-4 0] [2 3]])
\ No newline at end of file diff --git a/racket/leetcode/lc-717-1bit-and-2bit.rkt b/racket/leetcode/lc-717-1bit-and-2bit.rkt new file mode 100644 index 0000000..d9988ec --- /dev/null +++ b/racket/leetcode/lc-717-1bit-and-2bit.rkt @@ -0,0 +1,12 @@ +#lang racket + +(define/contract (is-one-bit-character bits) + (-> (listof exact-integer?) boolean?) + (define/match (check-next-character x . xs) + [(0 '()) #true] + [(1 (list _ ..2)) (apply check-next-character (cdr xs))] + [(0 (list _ ..1)) (apply check-next-character xs)] + [(_ _) #false]) + (apply check-next-character bits)) + +(is-one-bit-character (list 1 1 0 1 0))
\ No newline at end of file diff --git a/racket/leetcode/lc-745-prefix-suffix.rkt b/racket/leetcode/lc-745-prefix-suffix.rkt new file mode 100644 index 0000000..b01e3bb --- /dev/null +++ b/racket/leetcode/lc-745-prefix-suffix.rkt @@ -0,0 +1,27 @@ +#lang racket +(define word-filter% + (class object% + (super-new) + + ; words : (listof string?) + (init-field + words) ; Take in the provided dictionary. + (define word-ends-hash (make-hash)) ; Make an empty hash table. + + (for/list ([w (in-list words)] ; For each word in the dictionary, + [index (in-naturals)]) ; and its corresponding index, + (define len (string-length w)) ; calculate its length, + (for*/list ([head (in-range 1 (min 11 (add1 len)))] ; and for every combination of head length + [tail (in-range 1 (min 11 (add1 len)))]) ; and tail length + (hash-set! word-ends-hash ; from 1 to the max. affix length, + (list (substring w 0 head) ; set the key to the list containing + (substring w (- len tail) len)) ; the prefix and suffix + index))) ; and map it to the word's index. + + ; f : string? string? -> exact-integer? + (define/public (f prefix suffix) ; Return the mapped value for the affixes + (hash-ref word-ends-hash (list prefix suffix) -1)))) ; or -1 if it doesn't exist. + +;; Your word-filter% object will be instantiated and called as such: +;; (define obj (new word-filter% [words words])) +;; (define param_1 (send obj f prefix suffix))
\ No newline at end of file diff --git a/racket/leetcode/lc-747-largest-number-twice.rkt b/racket/leetcode/lc-747-largest-number-twice.rkt new file mode 100644 index 0000000..cea931b --- /dev/null +++ b/racket/leetcode/lc-747-largest-number-twice.rkt @@ -0,0 +1,15 @@ +#lang racket +(define/contract (dominant-index nums) + (-> (listof exact-integer?) exact-integer?) + (if (empty? (cdr nums)) + 0 + (let* ([indexed-list + (map cons nums (range (length nums)))] + [sorted-indexed-list + (sort indexed-list > #:key car)]) + (if ((car (first sorted-indexed-list)) . >= . (* 2 (car (second sorted-indexed-list)))) + (cdr (first sorted-indexed-list)) + -1)))) + +(dominant-index '(3 6 1 0)) +(dominant-index '(0))
\ No newline at end of file diff --git a/racket/leetcode/lc-766-toeplitz-matrix.rkt b/racket/leetcode/lc-766-toeplitz-matrix.rkt new file mode 100644 index 0000000..5606d2a --- /dev/null +++ b/racket/leetcode/lc-766-toeplitz-matrix.rkt @@ -0,0 +1,9 @@ +#lang racket + +(define/contract (is-toeplitz-matrix matrix) + (-> (listof (listof exact-integer?)) boolean?) + (cond [(empty? (cdr matrix)) #true] + [(equal? (drop-right (car matrix) 1) + (drop (cadr matrix) 1)) + (is-toeplitz-matrix (cdr matrix))] + [else #false]))
\ No newline at end of file diff --git a/racket/leetcode/lc-771-jewels-and-stones.rkt b/racket/leetcode/lc-771-jewels-and-stones.rkt new file mode 100644 index 0000000..40ccf14 --- /dev/null +++ b/racket/leetcode/lc-771-jewels-and-stones.rkt @@ -0,0 +1,6 @@ +#lang racket + +(define/contract (num-jewels-in-stones jewels stones) + (-> string? string? exact-integer?) + (for/sum ([jewel (in-string jewels)]) + (count (curry char=? jewel) (string->list stones))))
\ No newline at end of file diff --git a/racket/leetcode/lc-788-rotated-digits.rkt b/racket/leetcode/lc-788-rotated-digits.rkt new file mode 100644 index 0000000..79400b8 --- /dev/null +++ b/racket/leetcode/lc-788-rotated-digits.rkt @@ -0,0 +1,17 @@ +#lang racket + +(define/contract (rotated-digits max-n) + (-> exact-integer? exact-integer?) + (for/fold ([good-number-count 0]) + ([n (in-range 1 (add1 max-n))]) + (if (is-good? n) + (add1 good-number-count) + good-number-count))) + +(define/contract (is-good? test-number) + (-> exact-integer? boolean?) + (define test-string (number->string test-number)) + (match test-string + [(regexp #rx"^[018]*$") #false] + [(regexp #rx"^[0125689]*$") #true] + [_ #false]))
\ No newline at end of file diff --git a/racket/leetcode/lc-796-rotate-string.rkt b/racket/leetcode/lc-796-rotate-string.rkt new file mode 100644 index 0000000..b51d9e3 --- /dev/null +++ b/racket/leetcode/lc-796-rotate-string.rkt @@ -0,0 +1,6 @@ +#lang racket/base +(define/contract (rotate-string A B) + (-> string? string? boolean?) + (define doubled-A (string-append A A)) + (and (= (string-length A) (string-length B)) + (string-contains? doubled-A B)))
\ No newline at end of file diff --git a/racket/leetcode/lc-819-most-common-word.rkt b/racket/leetcode/lc-819-most-common-word.rkt new file mode 100644 index 0000000..68a89c3 --- /dev/null +++ b/racket/leetcode/lc-819-most-common-word.rkt @@ -0,0 +1,14 @@ +#lang racket +(define/contract (most-common-word paragraph banned) + (-> string? (listof string?) string?) + (define word-count-hash (make-hash)) + (define banned-word-hash + (apply hash (flatten (map (λ (w) (cons w 'banned)) banned)))) + (define word-list + ((compose string-split string-downcase) + (string-replace paragraph #px"[^A-Za-z[:space:]]" " "))) + (for/list ([word (in-list word-list)]) + (cond [(hash-has-key? banned-word-hash word) void] + [else (hash-update! word-count-hash word add1 0)])) + (car (argmax cdr (hash->list word-count-hash)))) + diff --git a/racket/leetcode/lc-836-rectangle-overlap.rkt b/racket/leetcode/lc-836-rectangle-overlap.rkt new file mode 100644 index 0000000..ecdfb56 --- /dev/null +++ b/racket/leetcode/lc-836-rectangle-overlap.rkt @@ -0,0 +1,14 @@ +#lang racket +(define (rectangle-area lst) + (match-define (list x1 y1 x2 y2) lst) + (* (- x2 x1) (- y2 y1))) + +(define/contract (is-rectangle-overlap rec1 rec2) + (-> (listof exact-integer?) (listof exact-integer?) boolean?) + (cond [(or (= 0 (rectangle-area rec1)) + (= 0 (rectangle-area rec2))) #false] + [(not (or ((list-ref rec1 2) . <= . (list-ref rec2 0)) + ((list-ref rec1 3) . <= . (list-ref rec2 1)) + ((list-ref rec1 0) . >= . (list-ref rec2 2)) + ((list-ref rec1 1) . >= . (list-ref rec2 3)))) #true] + [else #false]))
\ No newline at end of file diff --git a/racket/leetcode/lc-844-backspace-string-compare.rkt b/racket/leetcode/lc-844-backspace-string-compare.rkt new file mode 100644 index 0000000..a07ec3c --- /dev/null +++ b/racket/leetcode/lc-844-backspace-string-compare.rkt @@ -0,0 +1,17 @@ +#lang racket + +(define/contract (process-backspace-string strng) + (-> string? string?) + (apply ~a (for/fold ([str-out '()] + #:result (reverse str-out)) + ([character (in-string strng)]) + (case character + [(#\#) (if (empty? str-out) + str-out + (cdr str-out))] + [else (cons character str-out)])))) + +(define/contract (backspace-compare s t) + (-> string? string? boolean?) + (equal? (process-backspace-string s) + (process-backspace-string t)))
\ No newline at end of file diff --git a/racket/leetcode/lc-896-monotonic-array.rkt b/racket/leetcode/lc-896-monotonic-array.rkt new file mode 100644 index 0000000..fa43244 --- /dev/null +++ b/racket/leetcode/lc-896-monotonic-array.rkt @@ -0,0 +1,16 @@ +#lang racket +(define/contract (is-monotonic test-list) + (-> (listof exact-integer?) boolean?) + (cond [(empty? (cdr test-list)) #true] + [((car test-list) . > . (cadr test-list)) + (is-monotonic-direction test-list >=)] + [((car test-list) . < . (cadr test-list)) + (is-monotonic-direction test-list <=)] + [else (is-monotonic (cdr test-list))])) + +(define/contract (is-monotonic-direction test-list dir) + (-> (listof exact-integer?) procedure? boolean?) + (cond [(empty? (cdr test-list)) #true] + [((car test-list) . dir . (cadr test-list)) + (is-monotonic-direction (cdr test-list) dir)] + [else #false]))
\ No newline at end of file diff --git a/racket/leetcode/lc-9-palindromic-number.rkt b/racket/leetcode/lc-9-palindromic-number.rkt new file mode 100644 index 0000000..1fccbef --- /dev/null +++ b/racket/leetcode/lc-9-palindromic-number.rkt @@ -0,0 +1,38 @@ +#lang racket +(define/contract (is-palindrome x) + (-> exact-integer? boolean?) + ; get the easy cases out of the way first + ; negative numbers are not palindromes, single-digit numbers are + (cond [(x . < . 0) #false] + [(x . < . 10) #true] + [else + ; order-of-magnitude returns the scientific notation exponent + ; so add 1 to get the number of digits + (define digits + (add1 (order-of-magnitude x))) + ; figure out how many digits we need to trim to find the mirrored halves + ; if there are an even number of digits 2n, we will remove n of them from the right + ; if there are an odd number of digits 2n+1, we will remove n+1 of them from the right + (define half-digits + (cond [(even? digits) (/ digits 2)] + [else (/ (add1 digits) 2)])) + ; divide x by a power of 10 to get the digits to match + (define front-half + (quotient x (expt 10 half-digits))) + ; reverse the back half with repeated divisions by 10 + (define back-half-reversed + (for/fold ([reversed 0] + [remaining (remainder x (expt 10 half-digits))] + ; build up the sum of the digits in reversed and return it at the end + #:result reversed) + ; if we have an odd number of digits, we don't need to match the middle one + ([n (in-range (if (even? digits) + half-digits + (sub1 half-digits)))]) + ; shift all the accumulated digits in the mirror to the left one place + ; and add the next one to the right, + ; then chop the right-most digit off the original + (values (+ (* 10 reversed) (remainder remaining 10)) + (quotient remaining 10)))) + ; finally, check to see if the mirrored right is equal to the original left + (= front-half back-half-reversed)])) diff --git a/racket/leetcode/lc-905-sort-by-parity.rkt b/racket/leetcode/lc-905-sort-by-parity.rkt new file mode 100644 index 0000000..7c736f3 --- /dev/null +++ b/racket/leetcode/lc-905-sort-by-parity.rkt @@ -0,0 +1,7 @@ +#lang racket +(define/contract (sort-array-by-parity A) + (-> (listof exact-integer?) (listof exact-integer?)) + ((compose flatten (if (odd? (car A)) reverse identity)) + (group-by (λ (n) (modulo n 2)) A))) + +(sort-array-by-parity '(1 1 3 4))
\ No newline at end of file diff --git a/racket/leetcode/lc-944-delete-columns.rkt b/racket/leetcode/lc-944-delete-columns.rkt new file mode 100644 index 0000000..f9714b7 --- /dev/null +++ b/racket/leetcode/lc-944-delete-columns.rkt @@ -0,0 +1,7 @@ +#lang racket +(define/contract (min-deletion-size strs) + (-> (listof string?) exact-integer?) + (define transposed-strs (apply map list (map string->list strs))) + (count (λ (s) (not (equal? s (sort s char<?)))) transposed-strs)) + +(min-deletion-size '["cba" "daf" "ghi"]) diff --git a/racket/leetcode/lc-953-alien-dictionary.rkt b/racket/leetcode/lc-953-alien-dictionary.rkt new file mode 100644 index 0000000..f72a895 --- /dev/null +++ b/racket/leetcode/lc-953-alien-dictionary.rkt @@ -0,0 +1,26 @@ +#lang racket + +(define/contract (is-alien-sorted words order) + (-> (listof string?) string? boolean?) + (define alpha-order + (make-hash (map (λ (a b) (cons a b)) + (map string (string->list order)) + (build-list (string-length order) identity)))) + (hash-set! alpha-order #\* -1) + (define (alien<? s1 s2) + (cond [(equal? s1 "") #true] + [(equal? s2 "") #false] + [(equal? (hash-ref alpha-order (substring s1 0 1)) + (hash-ref alpha-order (substring s2 0 1))) + (alien<? (substring s1 1 (string-length s1)) + (substring s2 1 (string-length s2)))] + [(< (hash-ref alpha-order (substring s1 0 1)) + (hash-ref alpha-order (substring s2 0 1))) #true] + [else false])) + (equal? words + (sort words alien<?))) + +(is-alien-sorted '["hello" "leetcode"] "hlabcdefgijkmnopqrstuvwxyz") +(is-alien-sorted '["word" "world" "row"] "worldabcefghijkmnpqstuvxyz") +(is-alien-sorted '["apple" "app"] "abcdefghijklmnopqrstuvwxyz") +(is-alien-sorted '["iekm" "tpnhnbe"] "loxbzapnmstkhijfcuqdewyvrg")
\ No newline at end of file diff --git a/racket/leetcode/lc-989-add-to-array-form.rkt b/racket/leetcode/lc-989-add-to-array-form.rkt new file mode 100644 index 0000000..c88bdc9 --- /dev/null +++ b/racket/leetcode/lc-989-add-to-array-form.rkt @@ -0,0 +1,24 @@ +#lang racket +(define/contract (add-to-array-form num k) + (-> (listof exact-integer?) exact-integer? (listof exact-integer?)) + (define rev-num (reverse num)) + (define raw-array-form + (cond [(= k 0) num] + [else + (for/fold ([sum-list '()] + [addend k] + [carry 0] + #:result sum-list) + ([place (append rev-num + (make-list (add1 (order-of-magnitude k)) 0))]) + (let* ([place-sum (+ place carry (modulo addend 10))] + [to-carry (quotient place-sum 10)] + [new-place (modulo place-sum 10)]) + (values (cons new-place sum-list) + (quotient addend 10) + to-carry)))])) + (match-define-values + (result _) (drop-common-prefix raw-array-form + (make-list (length raw-array-form) 0))) + (cond [(empty? result) '(0)] + [else result]))
\ No newline at end of file diff --git a/racket/leetcode/lc-999-available-captures.rkt b/racket/leetcode/lc-999-available-captures.rkt new file mode 100644 index 0000000..1b1a3a9 --- /dev/null +++ b/racket/leetcode/lc-999-available-captures.rkt @@ -0,0 +1,28 @@ +#lang racket +(define/contract (num-rook-captures board) + (-> (listof (listof string?)) exact-integer?) + + (define (get-rook-space [board-state board]) + (for/or ([board-rank (in-list board-state)] + [rank (in-range 0 (length board-state))] + #:when (index-of board-rank "R")) + (list rank (index-of board-rank "R")))) + + (define (check-for-capturable-pawns spaces) + (match spaces + [(list _ ... "p" "." ... "R" "." ... "p" _ ...) 2] + [(list _ ... "R" "." ... "p" _ ...) 1] + [(list _ ... "p" "." ... "R" _ ...) 1] + [_ 0])) + + (define (check-rank rank [board-state board]) + (let ([spaces (list-ref board-state rank)]) + (check-for-capturable-pawns spaces))) + + (define (check-file file [board-state board]) + (let ([spaces (map (curryr list-ref file) board)]) + (check-for-capturable-pawns spaces))) + + (match (get-rook-space board) + [(list rank file) (+ (check-rank rank) + (check-file file))]))
\ No newline at end of file |