-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0816-ambiguous-coordinates.rkt
94 lines (73 loc) · 2.4 KB
/
0816-ambiguous-coordinates.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#lang racket
(define (x-str str)
(map (curry substring str 0) (range 1 (string-length str))))
(define (y-str str)
(map (curry substring str) (range 1 (string-length str))))
(define (all-xy-pairs str)
(if (= (string-length str) 1)
str
(map list (x-str str) (y-str str))))
(define (whole-str str)
(map (curry substring str 0) (range 1 (add1 (string-length str)))))
(define (decimal-str str)
(map (curry substring str) (range 1 (add1 (string-length str)))))
(define (all-wd-pairs str)
(map list (whole-str str) (decimal-str str)))
; (define (whole-valid? str)
; (cond [(= (string-length str) 1) true]
; [(eq? (first (string->list str)) #\0) false]
; [else true]))
(define (whole-valid? str)
(or (= (string-length str) 1)
(not (string-prefix? str "0"))))
(define (decimal-valid? str)
(or (zero? (string-length str))
(not (string-suffix? str "0"))))
(define (wd-pair-valid? wd-pair)
(let ([whole (car wd-pair)]
[decimal (cadr wd-pair)])
(and (whole-valid? whole)
(decimal-valid? decimal))))
(define (wd-pair->str wd-pair)
(let ([whole (car wd-pair)]
[decimal (cadr wd-pair)])
(if (zero? (string-length decimal))
whole
(string-append whole "." decimal))))
(define (valid-wd-pairs str)
(filter wd-pair-valid?
(all-wd-pairs str)))
(define (valid-nums str)
(map wd-pair->str
(valid-wd-pairs str)))
(define (valid-xy-pairs str)
(map (curry map valid-nums) (all-xy-pairs str)))
; (define (all-cons xy)
; (let ([x-list (car xy)]
; [y-list (cadr xy)])
; (append-map (lambda (x)
; (map (lambda (y)
; (string-append "(" x ", " y ")"))
; y-list))
; x-list)))
(define (all-cons xy-pair)
(let ([x-list (car xy-pair)]
[y-list (cadr xy-pair)])
(map (curryr string-join ", "
#:before-first "("
#:after-last ")")
(cartesian-product x-list y-list))))
(define/contract (ambiguous-coordinates s)
(-> string? (listof string?))
(append-map all-cons
(valid-xy-pairs (substring s 1 (sub1 (string-length s))))))
; (define xy (caddr (valid-xy "123456")))
; (define x-list (car xy))
; (define y-list (cadr xy))
(all-wd-pairs "123")
(valid-wd-pairs "123")
(valid-nums "123")
(all-xy-pairs "123")
(valid-xy-pairs "123")
(define str "123")
(ambiguous-coordinates "(123)")