Advertisement
Guest User

Untitled

a guest
May 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 6.05 KB | None | 0 0
  1. ;; contract: (list x) * (list y) --> (list x) or (list y)
  2. ;; purpose: to determine which list has more elements.
  3. ;; example: (bigger_list '(1 2 3) '(1 2 3 4)) should return '(1 2 3)
  4. ;; definition:
  5. (define (bigger_list list1 list2) (if (< (length list1) (length list2)) list2 list1))
  6.  
  7. ;; contract: (list x) *(list y) --> (length (list x))
  8. ;; purpose: to determine and use the list with a bigger length
  9. ;; example: (bigger_length '( a b c) '(a b) --> 3
  10. ;; definition:
  11. (define (bigger_length list1 list2)(if (> (length list1) (length list2)) (length list1) (length list2)))
  12.  
  13. ;; contract: (list x) --> (list x)
  14. ;; purpose: to reverse a list
  15. ;; example: (reverse_list '(a b c)) --> (c b a)
  16. ;; definition:
  17. (define (reverse_list list1)
  18.   (if (null? list1)
  19.       '()
  20.       (append (reverse (cdr list1)) (list (car list1)))))
  21.  
  22. ;; contract: (list x) * integer --> symbol
  23. ;; purpose: to create a list-ref function
  24. ;; example: (symbol_at_index '( a b c) 1 should return b
  25. ;; definition:
  26. (define (symbol_at_index list index)
  27.   (if (equal? list '())
  28.       '()
  29.       (if (= index 0)
  30.           (car list)
  31.           (symbol_at_index (cdr list) (- index 1)))))
  32.  
  33. ;; contract: integer x --> '( () () () () ... *x ())
  34. ;; purpose: init the 2d array for dynamic programming, the len of the list determine the number of coloumns
  35. ;; example: (base_case 3) should return '( () () () )
  36. ;; definition:
  37. (define (base_case len_list)
  38.   (if (= len_list 0)
  39.       '()
  40.       (cons '() (base_case (- len_list 1)))))
  41.  
  42. ;; contract: (integer x) (list y) (list z) ---> ( () () () () )
  43. ;; purpose: to compute column number x of the dynamic programming 2d list
  44. ;; example: (compute_column 2 '(a c b) '(a b c)) ---> ((c a) (a) (a) ())
  45. ;; definition:
  46.  
  47. (define (compute_column column_number list1 list2)
  48.   ;if we are at the 0th column return empty lists (base case)
  49.   (if (= column_number 0)
  50.        ;return base case
  51.       (base_case (+ (length list2) 1))
  52.       ;else recursively call 1,2,3...(length list) to get columns 1 2 3... etc
  53.       (get_current_column '(()) (compute_column (- column_number 1) list1 list2) column_number list1 list2)))
  54.        
  55. ;;contract: '((x) ()) '(() () ()) (int x) '(x) '(x) --> '( () () () ())
  56. ;;purpose: using the previous column compute the current column
  57. ;;example: (get_current_column '(()) (compute_column 3 '(a b c d) '(c d a b)) 4 '(a b c d) '(c d a b))
  58. ;;         returns ((d c) (d c) (d c) (c) ())
  59. ;;definition:
  60.  
  61. (define (get_current_column current_column prev_column column_number list1 list2)
  62.   ;If the column size is equal to the previous columns's size, were done
  63.   (if (= (length current_column) (length prev_column))
  64.       ;return the column
  65.       current_column
  66.       ;else recursively call compute_cell, which adds the next cell in the column.
  67.       (get_current_column (compute_cell current_column prev_column column_number list1 list2) prev_column column_number list1 list2)))
  68.  
  69. ;;contract: '( () ) '( () ) (int x) '(list) '(list) ---> '( () )
  70. ;;purpose: to compute the next cell for the current column based on the previous column
  71. ;;example: (compute_cell '((a) ()) '( () () () ) 1 '(a b c) '(a b d)) returns((a) (a) ())
  72. ;;definition
  73. (define (compute_cell current_column prev_column column_number list1 list2)
  74.  
  75.   (let*
  76.       (
  77.        ;to determine which row we look at how many things we've appened to current_column
  78.        (row_number (length current_column))
  79.        ;to find symbol one we subtract one because our column/row numbers contain the base case of ()
  80.        (symbol1 (symbol_at_index list1 (- column_number 1)))
  81.        (symbol2 (symbol_at_index list2 (- row_number 1)))
  82.        ;the cell below is the thing previously appened to current_column
  83.        (cell_below (car current_column))
  84.        ; the cell to the right in in the previous column and in order to index it we
  85.        ; must calculate the index backwards. This is because append puts items of the list
  86.        ; in the front. This also means each subtraction is actually + 1 not - 1.
  87.        (cell_right (symbol_at_index prev_column (- (- (length prev_column) row_number) 1)))
  88.        (diagonal_cell (symbol_at_index prev_column (- (length prev_column) row_number)))
  89.                )
  90.   (if (equal? symbol1 symbol2)
  91.       ;if the symbols are equal append the symbol to the diagonal cell solution
  92.       (append (list (cons symbol2 diagonal_cell)) current_column)
  93.       ;else append the best solution from cell below and cell right
  94.       (append (list (bigger_list cell_below cell_right)) current_column))))
  95.  
  96.      
  97.          
  98.  
  99. ;; contract: (list x) * (list y) --> (list z) where z is the largest common subset
  100. ;; purpose: to determine the largest common subset
  101. ;; example: '(a b c)
  102. ;; definition:
  103. (define (lcs_slow list1 list2)
  104.      ; If either list is an empty list, return an empty list
  105.     (if (or (equal? list1 '()) (equal? list2 '()))
  106.        
  107.         '()
  108.         ; else - if the head of both lists are equal
  109.         (if (equal? (car list1) (car list2))
  110.             ; #t then return the current element plus the recursively returned list ( until it returns an empty list )
  111.             (cons (car list1) (lcs_slow (cdr list1) (cdr list2)))
  112.             ; #f then return the best result from either list1 -1 or list2 -1
  113.             (bigger_list (lcs_slow list1 (cdr list2)) (lcs_slow (cdr list1) list2))
  114.         )
  115.     )
  116. )
  117.  
  118. ;;contract (list x) (list y) --> (list z)
  119. ;;purpose: a dynamic programming solution to find the largest common subsequence
  120. ;;example '(a b c) '(a b d) --> (a b)
  121. ;;definition
  122. (define (lcs_fast list1 list2)
  123.   (reverse_list (car (compute_column (bigger_length list1 list2) list1 list2))))
  124.  
  125. ;;Test Cases
  126. ;; (define list1 `(q q q a b d e))
  127. ;; (define list2 `(g g g g g a b c e))
  128. ;; (lcs_slow list1 list2)
  129. ;; (symbol_at_index '(a (a b) d c) 1 )
  130. ;; (compute_cell '((a) ()) '( () () () ) 1 '(a b c) '(a b d))
  131. ;; (get_current_column '(()) (compute_column 3 list1 list2) 4 list1 list2)
  132. ;; (lcs_fast list1 list2)
  133. ;; (reverse_list '(a b c))
  134. ;; (compute_column 2 '(a c b) '(a b c))
  135. ;; (get_current_column '(()) (compute_column 3 '(a b c d) '(c d a b)) 4 '(a b c d) '(c d a b))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement