From 8777ff071f7bb37631baa7b6717ad29961e50911 Mon Sep 17 00:00:00 2001 From: "H.J" Date: Wed, 9 Oct 2024 11:36:55 -0400 Subject: sorting by language --- leetcode/lc-1018-binary-prefix.rkt | 11 ------ leetcode/lc-1037-boomerang.rkt | 21 ----------- leetcode/lc-1185-day-of-week.rkt | 18 --------- leetcode/lc-1207-unique-occurences.rkt | 10 ----- leetcode/lc-1221-split-a-string-balanced.rkt | 19 ---------- leetcode/lc-125-valid-palindrome.rkt | 12 ------ leetcode/lc-1295-even-number-of-digits.rkt | 4 -- .../lc-1299-replace-with-greatest-to-right.rkt | 8 ---- leetcode/lc-1304-find-n-unique-integers.rkt | 5 --- leetcode/lc-1436-destination-city.rkt | 14 ------- leetcode/lc-1450-students-doing-homework.rkt | 12 ------ leetcode/lc-1460-make-two-arrays-equal.rkt | 4 -- leetcode/lc-1496-path-crossing.rkt | 25 ------------ leetcode/lc-1700-students-unable-to-eat.rkt | 36 ------------------ leetcode/lc-1812-chessboard-square.rkt | 7 ---- .../lc-1844-replace-all-digits-with-characters.rkt | 18 --------- leetcode/lc-1854-max-pop-year.rkt | 17 --------- leetcode/lc-2-add-two-numbers.rkt | 35 ----------------- leetcode/lc-217-contains-duplicate.rkt | 12 ------ leetcode/lc-228-summary-ranges.rkt | 27 ------------- leetcode/lc-290-word-pattern.rkt | 23 ----------- leetcode/lc-345-reverse-vowels.rkt | 9 ----- leetcode/lc-349-intersection-of-2-arrays.rkt | 5 --- leetcode/lc-36-valid-sudoku.rkt | 33 ---------------- leetcode/lc-415-add-strings.rkt | 28 -------------- leetcode/lc-43-multiply-strings.rkt | 28 -------------- leetcode/lc-476-number-complement.rkt | 11 ------ leetcode/lc-500-keyboard-row.rkt | 23 ----------- leetcode/lc-504-base7.rkt | 16 -------- leetcode/lc-520-detect-capital.rkt | 12 ------ leetcode/lc-551-student-attendance-record-1.rkt | 11 ------ leetcode/lc-58-length-of-last-word.rkt | 7 ---- leetcode/lc-645-set-mismatch.rkt | 24 ------------ leetcode/lc-657-robot-return.rkt | 17 --------- leetcode/lc-68-justification.rkt | 44 ---------------------- leetcode/lc-690-employee-importance.rkt | 14 ------- leetcode/lc-717-1bit-and-2bit.rkt | 12 ------ leetcode/lc-745-prefix-suffix.rkt | 27 ------------- leetcode/lc-747-largest-number-twice.rkt | 15 -------- leetcode/lc-766-toeplitz-matrix.rkt | 9 ----- leetcode/lc-771-jewels-and-stones.rkt | 6 --- leetcode/lc-788-rotated-digits.rkt | 17 --------- leetcode/lc-796-rotate-string.rkt | 6 --- leetcode/lc-819-most-common-word.rkt | 14 ------- leetcode/lc-836-rectangle-overlap.rkt | 14 ------- leetcode/lc-844-backspace-string-compare.rkt | 17 --------- leetcode/lc-896-monotonic-array.rkt | 16 -------- leetcode/lc-9-palindromic-number.rkt | 38 ------------------- leetcode/lc-905-sort-by-parity.rkt | 7 ---- leetcode/lc-944-delete-columns.rkt | 7 ---- leetcode/lc-953-alien-dictionary.rkt | 26 ------------- leetcode/lc-989-add-to-array-form.rkt | 24 ------------ leetcode/lc-999-available-captures.rkt | 28 -------------- 53 files changed, 903 deletions(-) delete mode 100644 leetcode/lc-1018-binary-prefix.rkt delete mode 100644 leetcode/lc-1037-boomerang.rkt delete mode 100644 leetcode/lc-1185-day-of-week.rkt delete mode 100644 leetcode/lc-1207-unique-occurences.rkt delete mode 100644 leetcode/lc-1221-split-a-string-balanced.rkt delete mode 100644 leetcode/lc-125-valid-palindrome.rkt delete mode 100644 leetcode/lc-1295-even-number-of-digits.rkt delete mode 100644 leetcode/lc-1299-replace-with-greatest-to-right.rkt delete mode 100644 leetcode/lc-1304-find-n-unique-integers.rkt delete mode 100644 leetcode/lc-1436-destination-city.rkt delete mode 100644 leetcode/lc-1450-students-doing-homework.rkt delete mode 100644 leetcode/lc-1460-make-two-arrays-equal.rkt delete mode 100644 leetcode/lc-1496-path-crossing.rkt delete mode 100644 leetcode/lc-1700-students-unable-to-eat.rkt delete mode 100644 leetcode/lc-1812-chessboard-square.rkt delete mode 100644 leetcode/lc-1844-replace-all-digits-with-characters.rkt delete mode 100644 leetcode/lc-1854-max-pop-year.rkt delete mode 100644 leetcode/lc-2-add-two-numbers.rkt delete mode 100644 leetcode/lc-217-contains-duplicate.rkt delete mode 100644 leetcode/lc-228-summary-ranges.rkt delete mode 100644 leetcode/lc-290-word-pattern.rkt delete mode 100644 leetcode/lc-345-reverse-vowels.rkt delete mode 100644 leetcode/lc-349-intersection-of-2-arrays.rkt delete mode 100644 leetcode/lc-36-valid-sudoku.rkt delete mode 100644 leetcode/lc-415-add-strings.rkt delete mode 100644 leetcode/lc-43-multiply-strings.rkt delete mode 100644 leetcode/lc-476-number-complement.rkt delete mode 100644 leetcode/lc-500-keyboard-row.rkt delete mode 100644 leetcode/lc-504-base7.rkt delete mode 100644 leetcode/lc-520-detect-capital.rkt delete mode 100644 leetcode/lc-551-student-attendance-record-1.rkt delete mode 100644 leetcode/lc-58-length-of-last-word.rkt delete mode 100644 leetcode/lc-645-set-mismatch.rkt delete mode 100644 leetcode/lc-657-robot-return.rkt delete mode 100644 leetcode/lc-68-justification.rkt delete mode 100644 leetcode/lc-690-employee-importance.rkt delete mode 100644 leetcode/lc-717-1bit-and-2bit.rkt delete mode 100644 leetcode/lc-745-prefix-suffix.rkt delete mode 100644 leetcode/lc-747-largest-number-twice.rkt delete mode 100644 leetcode/lc-766-toeplitz-matrix.rkt delete mode 100644 leetcode/lc-771-jewels-and-stones.rkt delete mode 100644 leetcode/lc-788-rotated-digits.rkt delete mode 100644 leetcode/lc-796-rotate-string.rkt delete mode 100644 leetcode/lc-819-most-common-word.rkt delete mode 100644 leetcode/lc-836-rectangle-overlap.rkt delete mode 100644 leetcode/lc-844-backspace-string-compare.rkt delete mode 100644 leetcode/lc-896-monotonic-array.rkt delete mode 100644 leetcode/lc-9-palindromic-number.rkt delete mode 100644 leetcode/lc-905-sort-by-parity.rkt delete mode 100644 leetcode/lc-944-delete-columns.rkt delete mode 100644 leetcode/lc-953-alien-dictionary.rkt delete mode 100644 leetcode/lc-989-add-to-array-form.rkt delete mode 100644 leetcode/lc-999-available-captures.rkt (limited to 'leetcode') diff --git a/leetcode/lc-1018-binary-prefix.rkt b/leetcode/lc-1018-binary-prefix.rkt deleted file mode 100644 index fa82681..0000000 --- a/leetcode/lc-1018-binary-prefix.rkt +++ /dev/null @@ -1,11 +0,0 @@ -#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/leetcode/lc-1037-boomerang.rkt b/leetcode/lc-1037-boomerang.rkt deleted file mode 100644 index fd95695..0000000 --- a/leetcode/lc-1037-boomerang.rkt +++ /dev/null @@ -1,21 +0,0 @@ -#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/leetcode/lc-1185-day-of-week.rkt b/leetcode/lc-1185-day-of-week.rkt deleted file mode 100644 index c90a626..0000000 --- a/leetcode/lc-1185-day-of-week.rkt +++ /dev/null @@ -1,18 +0,0 @@ -#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/leetcode/lc-1207-unique-occurences.rkt b/leetcode/lc-1207-unique-occurences.rkt deleted file mode 100644 index 1b4d107..0000000 --- a/leetcode/lc-1207-unique-occurences.rkt +++ /dev/null @@ -1,10 +0,0 @@ -#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/leetcode/lc-1221-split-a-string-balanced.rkt b/leetcode/lc-1221-split-a-string-balanced.rkt deleted file mode 100644 index 4c75770..0000000 --- a/leetcode/lc-1221-split-a-string-balanced.rkt +++ /dev/null @@ -1,19 +0,0 @@ -#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/leetcode/lc-125-valid-palindrome.rkt b/leetcode/lc-125-valid-palindrome.rkt deleted file mode 100644 index ed91d08..0000000 --- a/leetcode/lc-125-valid-palindrome.rkt +++ /dev/null @@ -1,12 +0,0 @@ -#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/leetcode/lc-1295-even-number-of-digits.rkt b/leetcode/lc-1295-even-number-of-digits.rkt deleted file mode 100644 index 9e88454..0000000 --- a/leetcode/lc-1295-even-number-of-digits.rkt +++ /dev/null @@ -1,4 +0,0 @@ -#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/leetcode/lc-1299-replace-with-greatest-to-right.rkt b/leetcode/lc-1299-replace-with-greatest-to-right.rkt deleted file mode 100644 index 34d3eae..0000000 --- a/leetcode/lc-1299-replace-with-greatest-to-right.rkt +++ /dev/null @@ -1,8 +0,0 @@ -#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/leetcode/lc-1304-find-n-unique-integers.rkt b/leetcode/lc-1304-find-n-unique-integers.rkt deleted file mode 100644 index 9b810a0..0000000 --- a/leetcode/lc-1304-find-n-unique-integers.rkt +++ /dev/null @@ -1,5 +0,0 @@ -#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/leetcode/lc-1436-destination-city.rkt b/leetcode/lc-1436-destination-city.rkt deleted file mode 100644 index ce82f08..0000000 --- a/leetcode/lc-1436-destination-city.rkt +++ /dev/null @@ -1,14 +0,0 @@ -#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/leetcode/lc-1450-students-doing-homework.rkt b/leetcode/lc-1450-students-doing-homework.rkt deleted file mode 100644 index 14ff079..0000000 --- a/leetcode/lc-1450-students-doing-homework.rkt +++ /dev/null @@ -1,12 +0,0 @@ -#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/leetcode/lc-1460-make-two-arrays-equal.rkt b/leetcode/lc-1460-make-two-arrays-equal.rkt deleted file mode 100644 index 584ac97..0000000 --- a/leetcode/lc-1460-make-two-arrays-equal.rkt +++ /dev/null @@ -1,4 +0,0 @@ -#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/leetcode/lc-1496-path-crossing.rkt b/leetcode/lc-1496-path-crossing.rkt deleted file mode 100644 index 9c1941d..0000000 --- a/leetcode/lc-1496-path-crossing.rkt +++ /dev/null @@ -1,25 +0,0 @@ -#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/leetcode/lc-1700-students-unable-to-eat.rkt b/leetcode/lc-1700-students-unable-to-eat.rkt deleted file mode 100644 index 75cc243..0000000 --- a/leetcode/lc-1700-students-unable-to-eat.rkt +++ /dev/null @@ -1,36 +0,0 @@ -#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/leetcode/lc-1812-chessboard-square.rkt b/leetcode/lc-1812-chessboard-square.rkt deleted file mode 100644 index 206392c..0000000 --- a/leetcode/lc-1812-chessboard-square.rkt +++ /dev/null @@ -1,7 +0,0 @@ -#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/leetcode/lc-1844-replace-all-digits-with-characters.rkt b/leetcode/lc-1844-replace-all-digits-with-characters.rkt deleted file mode 100644 index 96aba6e..0000000 --- a/leetcode/lc-1844-replace-all-digits-with-characters.rkt +++ /dev/null @@ -1,18 +0,0 @@ -#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/leetcode/lc-1854-max-pop-year.rkt b/leetcode/lc-1854-max-pop-year.rkt deleted file mode 100644 index 75104f1..0000000 --- a/leetcode/lc-1854-max-pop-year.rkt +++ /dev/null @@ -1,17 +0,0 @@ -#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/leetcode/lc-2-add-two-numbers.rkt b/leetcode/lc-2-add-two-numbers.rkt deleted file mode 100644 index 8062817..0000000 --- a/leetcode/lc-2-add-two-numbers.rkt +++ /dev/null @@ -1,35 +0,0 @@ -#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/leetcode/lc-217-contains-duplicate.rkt b/leetcode/lc-217-contains-duplicate.rkt deleted file mode 100644 index ca8d193..0000000 --- a/leetcode/lc-217-contains-duplicate.rkt +++ /dev/null @@ -1,12 +0,0 @@ -#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/leetcode/lc-228-summary-ranges.rkt b/leetcode/lc-228-summary-ranges.rkt deleted file mode 100644 index 9140895..0000000 --- a/leetcode/lc-228-summary-ranges.rkt +++ /dev/null @@ -1,27 +0,0 @@ -#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/leetcode/lc-290-word-pattern.rkt b/leetcode/lc-290-word-pattern.rkt deleted file mode 100644 index 77cdba0..0000000 --- a/leetcode/lc-290-word-pattern.rkt +++ /dev/null @@ -1,23 +0,0 @@ -#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/leetcode/lc-345-reverse-vowels.rkt b/leetcode/lc-345-reverse-vowels.rkt deleted file mode 100644 index c05bf2d..0000000 --- a/leetcode/lc-345-reverse-vowels.rkt +++ /dev/null @@ -1,9 +0,0 @@ -#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/leetcode/lc-349-intersection-of-2-arrays.rkt b/leetcode/lc-349-intersection-of-2-arrays.rkt deleted file mode 100644 index 14d56ca..0000000 --- a/leetcode/lc-349-intersection-of-2-arrays.rkt +++ /dev/null @@ -1,5 +0,0 @@ -#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/leetcode/lc-36-valid-sudoku.rkt b/leetcode/lc-36-valid-sudoku.rkt deleted file mode 100644 index 915b533..0000000 --- a/leetcode/lc-36-valid-sudoku.rkt +++ /dev/null @@ -1,33 +0,0 @@ -#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/leetcode/lc-415-add-strings.rkt b/leetcode/lc-415-add-strings.rkt deleted file mode 100644 index e140155..0000000 --- a/leetcode/lc-415-add-strings.rkt +++ /dev/null @@ -1,28 +0,0 @@ -#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/leetcode/lc-43-multiply-strings.rkt b/leetcode/lc-43-multiply-strings.rkt deleted file mode 100644 index dac8c31..0000000 --- a/leetcode/lc-43-multiply-strings.rkt +++ /dev/null @@ -1,28 +0,0 @@ -#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/leetcode/lc-476-number-complement.rkt b/leetcode/lc-476-number-complement.rkt deleted file mode 100644 index 724bb47..0000000 --- a/leetcode/lc-476-number-complement.rkt +++ /dev/null @@ -1,11 +0,0 @@ -#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/leetcode/lc-500-keyboard-row.rkt b/leetcode/lc-500-keyboard-row.rkt deleted file mode 100644 index 5f13143..0000000 --- a/leetcode/lc-500-keyboard-row.rkt +++ /dev/null @@ -1,23 +0,0 @@ -#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/leetcode/lc-504-base7.rkt b/leetcode/lc-504-base7.rkt deleted file mode 100644 index 3e75052..0000000 --- a/leetcode/lc-504-base7.rkt +++ /dev/null @@ -1,16 +0,0 @@ -#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/leetcode/lc-520-detect-capital.rkt b/leetcode/lc-520-detect-capital.rkt deleted file mode 100644 index 80b5f7e..0000000 --- a/leetcode/lc-520-detect-capital.rkt +++ /dev/null @@ -1,12 +0,0 @@ -#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/leetcode/lc-551-student-attendance-record-1.rkt b/leetcode/lc-551-student-attendance-record-1.rkt deleted file mode 100644 index c5f1456..0000000 --- a/leetcode/lc-551-student-attendance-record-1.rkt +++ /dev/null @@ -1,11 +0,0 @@ -#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/leetcode/lc-58-length-of-last-word.rkt b/leetcode/lc-58-length-of-last-word.rkt deleted file mode 100644 index 716df90..0000000 --- a/leetcode/lc-58-length-of-last-word.rkt +++ /dev/null @@ -1,7 +0,0 @@ -#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/leetcode/lc-645-set-mismatch.rkt b/leetcode/lc-645-set-mismatch.rkt deleted file mode 100644 index a9d9a61..0000000 --- a/leetcode/lc-645-set-mismatch.rkt +++ /dev/null @@ -1,24 +0,0 @@ -#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/leetcode/lc-657-robot-return.rkt b/leetcode/lc-657-robot-return.rkt deleted file mode 100644 index 908605a..0000000 --- a/leetcode/lc-657-robot-return.rkt +++ /dev/null @@ -1,17 +0,0 @@ -#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/leetcode/lc-68-justification.rkt b/leetcode/lc-68-justification.rkt deleted file mode 100644 index 537e2c5..0000000 --- a/leetcode/lc-68-justification.rkt +++ /dev/null @@ -1,44 +0,0 @@ -#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/leetcode/lc-690-employee-importance.rkt b/leetcode/lc-690-employee-importance.rkt deleted file mode 100644 index 1fb3fcc..0000000 --- a/leetcode/lc-690-employee-importance.rkt +++ /dev/null @@ -1,14 +0,0 @@ -#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/leetcode/lc-717-1bit-and-2bit.rkt b/leetcode/lc-717-1bit-and-2bit.rkt deleted file mode 100644 index d9988ec..0000000 --- a/leetcode/lc-717-1bit-and-2bit.rkt +++ /dev/null @@ -1,12 +0,0 @@ -#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/leetcode/lc-745-prefix-suffix.rkt b/leetcode/lc-745-prefix-suffix.rkt deleted file mode 100644 index b01e3bb..0000000 --- a/leetcode/lc-745-prefix-suffix.rkt +++ /dev/null @@ -1,27 +0,0 @@ -#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/leetcode/lc-747-largest-number-twice.rkt b/leetcode/lc-747-largest-number-twice.rkt deleted file mode 100644 index cea931b..0000000 --- a/leetcode/lc-747-largest-number-twice.rkt +++ /dev/null @@ -1,15 +0,0 @@ -#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/leetcode/lc-766-toeplitz-matrix.rkt b/leetcode/lc-766-toeplitz-matrix.rkt deleted file mode 100644 index 5606d2a..0000000 --- a/leetcode/lc-766-toeplitz-matrix.rkt +++ /dev/null @@ -1,9 +0,0 @@ -#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/leetcode/lc-771-jewels-and-stones.rkt b/leetcode/lc-771-jewels-and-stones.rkt deleted file mode 100644 index 40ccf14..0000000 --- a/leetcode/lc-771-jewels-and-stones.rkt +++ /dev/null @@ -1,6 +0,0 @@ -#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/leetcode/lc-788-rotated-digits.rkt b/leetcode/lc-788-rotated-digits.rkt deleted file mode 100644 index 79400b8..0000000 --- a/leetcode/lc-788-rotated-digits.rkt +++ /dev/null @@ -1,17 +0,0 @@ -#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/leetcode/lc-796-rotate-string.rkt b/leetcode/lc-796-rotate-string.rkt deleted file mode 100644 index b51d9e3..0000000 --- a/leetcode/lc-796-rotate-string.rkt +++ /dev/null @@ -1,6 +0,0 @@ -#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/leetcode/lc-819-most-common-word.rkt b/leetcode/lc-819-most-common-word.rkt deleted file mode 100644 index 68a89c3..0000000 --- a/leetcode/lc-819-most-common-word.rkt +++ /dev/null @@ -1,14 +0,0 @@ -#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/leetcode/lc-836-rectangle-overlap.rkt b/leetcode/lc-836-rectangle-overlap.rkt deleted file mode 100644 index ecdfb56..0000000 --- a/leetcode/lc-836-rectangle-overlap.rkt +++ /dev/null @@ -1,14 +0,0 @@ -#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/leetcode/lc-844-backspace-string-compare.rkt b/leetcode/lc-844-backspace-string-compare.rkt deleted file mode 100644 index a07ec3c..0000000 --- a/leetcode/lc-844-backspace-string-compare.rkt +++ /dev/null @@ -1,17 +0,0 @@ -#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/leetcode/lc-896-monotonic-array.rkt b/leetcode/lc-896-monotonic-array.rkt deleted file mode 100644 index fa43244..0000000 --- a/leetcode/lc-896-monotonic-array.rkt +++ /dev/null @@ -1,16 +0,0 @@ -#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/leetcode/lc-9-palindromic-number.rkt b/leetcode/lc-9-palindromic-number.rkt deleted file mode 100644 index 1fccbef..0000000 --- a/leetcode/lc-9-palindromic-number.rkt +++ /dev/null @@ -1,38 +0,0 @@ -#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/leetcode/lc-905-sort-by-parity.rkt b/leetcode/lc-905-sort-by-parity.rkt deleted file mode 100644 index 7c736f3..0000000 --- a/leetcode/lc-905-sort-by-parity.rkt +++ /dev/null @@ -1,7 +0,0 @@ -#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/leetcode/lc-944-delete-columns.rkt b/leetcode/lc-944-delete-columns.rkt deleted file mode 100644 index f9714b7..0000000 --- a/leetcode/lc-944-delete-columns.rkt +++ /dev/null @@ -1,7 +0,0 @@ -#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 (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 (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/leetcode/lc-999-available-captures.rkt b/leetcode/lc-999-available-captures.rkt deleted file mode 100644 index 1b1a3a9..0000000 --- a/leetcode/lc-999-available-captures.rkt +++ /dev/null @@ -1,28 +0,0 @@ -#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 -- cgit v1.2.3