SHARE
TWEET

Untitled

a guest Jan 12th, 2017 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; read input
  2.  
  3. (defun read-input(p)
  4. (cond
  5.     ((null p) nil)
  6.     (T (cons p (read-input (read nil nil))))
  7. )
  8. )
  9.  
  10. (defun readinput(i)
  11.     (cdr (read-input 1))
  12. )  
  13. ;--don't change above this line
  14.  
  15.  
  16. ; write your function here
  17. (defvar start (readinput nil))
  18.  
  19. ;Flat list
  20.  
  21. (defun flatten (list)
  22.     (cond
  23.         ((null list) nil)
  24.         ((atom (car list)) (cons (car list) (flatten (cdr list))))
  25.         (t (append
  26.                 (flatten (car list))
  27.                 (flatten (cdr list))
  28.             ))
  29.         )
  30.    
  31.     )
  32.  
  33.  
  34. ;dolzina na vgnezdena lista
  35. (defun len (list)
  36.     (cond
  37.         ((null list) 0)
  38.         ((atom (car list)) (+ 1 (len (cdr list))))
  39.         (t (+ (len (car list)) (len (cdr list))))
  40.         )
  41.     )
  42.  
  43. ;proverka za parnost na indeks i nivo
  44. (defun paren (index)
  45.     (cond
  46.         ((eq(mod index 2) 0)t)
  47.         (t nil)
  48.         )
  49.     )
  50.  
  51. (defun neparen (index)
  52.     (cond
  53.         ((eq(mod index 2) 0) nil)
  54.         (t t)
  55.         )
  56.     )
  57.  
  58. ;broenje kolku pati se pojavuva element vo lista
  59. (defun countIt (list el)
  60.     (cond
  61.         ((null list) 0)
  62.         ((eq (car list) el) (+ 1 (countIt (cdr list) el)))
  63.         ((listp (car list)) (+ (countIt (car list) el) (countIt (cdr list) el)))
  64.         (t (countIt (cdr list) el))
  65.         )
  66.    
  67.     )
  68.  
  69. ;brisi element spored baranjeto
  70. (defun pomosna (el list level counter)
  71.         (cond
  72.             ((null list) nil)
  73.             ((and (atom (car list)) (eq (car list) el))
  74.                 (if (and (paren (+ counter 1)) (neparen level))
  75.                     (pomosna el (cdr list) level (+ counter 1))  
  76.                     (cons (car list) (pomosna el (cdr list) level (+ counter 1))) )
  77.             )
  78.             ((listp (car list))
  79.                 (cons
  80.                     (pomosna el (car list) (+ level 1) counter)
  81.                     (pomosna el (cdr list) level (+ counter (countIt (car list) el)))
  82.                  )
  83.             )
  84.             (t (cons (car list) (pomosna el (cdr list) level counter)))
  85.             )
  86. )
  87.  
  88. (defun trans (list flat rezult)
  89.     (setq after (pomosna (car flat) list 0 0))
  90.     (cond
  91.         ((null flat) rezult)
  92.         (t
  93.          (trans after (remove (car flat) flat) after)
  94.          )
  95.         )
  96.     )
  97.  
  98. ;glavna funkcija
  99. (defun transformiraj (list)
  100.     (cond
  101.         ((null list) nil)
  102.         (t (trans list (flatten list) '()))
  103.         )
  104.     )
  105.  
  106. ;--don't change below this line
  107. (print (transformiraj start))
RAW Paste Data
Top