Advertisement
kachamaka

Untitled

Nov 18th, 2021
1,335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 3.10 KB | None | 0 0
  1.  
  2. (define (itinerary flights)
  3.   ;function to get the best matching pair according to string conditions
  4.   (define (getBestPair start lst [pair '()])
  5.     (if (empty? lst)
  6.         pair
  7.         ;if element we are expecting is a match
  8.         (if (equal? start (car (car lst)))
  9.             (cond
  10.               ;case for getting first pair
  11.               [(empty? pair) (getBestPair start (cdr lst) (car lst))]
  12.               ;best pair condition
  13.               [(string<? (cdr (car lst)) (cdr pair)) (getBestPair start (cdr lst) (car lst))]
  14.               ;else skip pair
  15.               [else (getBestPair start (cdr lst) pair)])
  16.             ;skip pair
  17.             (getBestPair start (cdr lst) pair))
  18.         )
  19.     )
  20.   ;get list of all possible best pairs
  21.   (define (getBestPairs start lst [res '()])
  22.     (define pair (getBestPair start lst))
  23.     (if (empty? pair)
  24.         res
  25.         (getBestPairs start (remove pair lst) (append res (list pair)))
  26.         )
  27.     )
  28.   ;inspect all possible routes according to best pairs
  29.   (define (getBestRoute start inspectList pairs [res '()])
  30.     ;if no elements to inspect
  31.     (if (empty? inspectList)
  32.         ;add start because path gets appended before calling function for next pair to examine
  33.         (append res (list start))
  34.         (let ()
  35.           ;if there are no pairs to inspect
  36.           (if (empty? pairs)
  37.               ;no such path
  38.               #f
  39.               (let ()
  40.                 (define pair (car pairs))
  41.                 (define newList (remove pair inspectList))
  42.                 (define newStart (cdr pair))
  43.                 ;get rest of the path (new arguments)
  44.                 (define newRes (getBestRoute newStart newList (getBestPairs newStart newList) (append res (list (car pair)))))
  45.                 ;catch the false state
  46.                 (if (eq? newRes #f)
  47.                     ;revert old arguments and remove inspected pair
  48.                     (getBestRoute start inspectList (remove pair pairs) res)
  49.                     ;return result
  50.                     newRes
  51.                     )
  52.                 )
  53.               )
  54.           )
  55.         )
  56.     )
  57.        
  58.   (λ (start)
  59.     (if (empty? (getBestPair start flights))        
  60.         ;if there is no best pair        
  61.         (error "No such itinerary!")
  62.         (let ()
  63.           (define pairs (getBestPairs start flights))
  64.           (define route (getBestRoute start flights pairs))
  65.           (if (eq? route #f)
  66.               (error "No such itinerary!")
  67.               route
  68.               )
  69.           )
  70.         )
  71.     )
  72.   )
  73. ;((itinerary '(("A" . "C") ("C" . "A") ("A" . "B"))) "A")
  74. (equal? ((itinerary '(("A" . "C") ("C" . "A") ("A" . "B"))) "A") '("A" "C" "A" "B"))
  75. (equal? ((itinerary '(("G" . "E") ("E" . "F") ("A" . "G") ("D" . "E") ("B" . "C") ("E" . "C") ("A" . "B") ("C" . "D") ("F" . "A") ("C" . "A"))) "A")
  76.         '("A" "B" "C" "A" "G" "E" "C" "D" "E" "F" "A"))
  77. (equal? ((itinerary '(("SFO" . "HKO") ("YYZ" . "SFO") ("YUL" . "YYZ") ("HKO" . "ORD"))) "YUL") '("YUL" "YYZ" "SFO" "HKO" "ORD"))
  78.  
  79. ;this line has to throw error
  80. ;((itinerary '(("SFO" . "COM") ("COM" . "YYZ"))) "COM")
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement