fork(1) download
  1. (defun calculate-level-sums (lst)
  2. (prepare-result (calculate-level-sums-helper lst 1 '())))
  3.  
  4. (defun calculate-level-sums-helper (lst level acc)
  5. (cond
  6. ((null lst) acc)
  7. (t (let ((head (car lst))
  8. (tail (cdr lst)))
  9. (multiple-value-bind (new-acc remaining-tail)
  10. (process-item head level acc)
  11. (calculate-level-sums-helper tail level new-acc))))))
  12.  
  13. (defun process-item (item level acc)
  14. (cond
  15. ((numberp item)
  16. (values (update-level-sum acc level item) nil))
  17. ((listp item)
  18. (values (calculate-level-sums-helper item (1+ level) acc) nil))
  19. (t (values acc nil))))
  20.  
  21. (defun update-level-sum (acc level value)
  22. (let ((existing (assoc level acc)))
  23. (if existing
  24. (let ((new-acc (remove existing acc :test #'equal)))
  25. (cons (list level (+ value (cadr existing))) new-acc))
  26. (cons (list level value) acc))))
  27.  
  28. (defun prepare-result (acc)
  29. (if (assoc 1 acc)
  30. (sort acc #'< :key #'car)
  31. (sort (cons '(1 0) acc) #'< :key #'car)))
  32. ;; Тесты
  33. (format t "~a~%" (calculate-level-sums '(a (b (4 (2 e (3) k 15) e 5) 7)))) ; ((1 0) (2 7) (3 9) (4 17) (5 3))
  34. (format t "~a~%" (calculate-level-sums '(a b c))) ; ((1 0))
  35. (format t "~a~%" (calculate-level-sums '(1 (2 (3))))) ; ((1 1) (2 2) (3 3))
  36. (format t "~a~%" (calculate-level-sums'(1 (2 3 (4 (5 6)))))) ; ((1 1) (2 5) (3 4) (4 11))
  37.  
Success #stdin #stdout #stderr 0.01s 9536KB
stdin
Standard input is empty
stdout
((1 0) (2 7) (3 9) (4 17) (5 3))
((1 0))
((1 1) (2 2) (3 3))
((1 1) (2 5) (3 4) (4 11))
stderr
Warning: reserving address range 0x80000c0000...0x1fffffffffff that contains memory mappings. clisp might crash later!
Memory dump:
  0x8000000000 - 0x80000bffff
  0x1535e7800000 - 0x1535e7ae4fff
  0x1535e7c15000 - 0x1535e7c39fff
  0x1535e7c3a000 - 0x1535e7dacfff
  0x1535e7dad000 - 0x1535e7df5fff
  0x1535e7df6000 - 0x1535e7df8fff
  0x1535e7df9000 - 0x1535e7dfbfff
  0x1535e7dfc000 - 0x1535e7dfffff
  0x1535e7e00000 - 0x1535e7e02fff
  0x1535e7e03000 - 0x1535e8001fff
  0x1535e8002000 - 0x1535e8002fff
  0x1535e8003000 - 0x1535e8003fff
  0x1535e8080000 - 0x1535e808ffff
  0x1535e8090000 - 0x1535e80c3fff
  0x1535e80c4000 - 0x1535e81fafff
  0x1535e81fb000 - 0x1535e81fbfff
  0x1535e81fc000 - 0x1535e81fefff
  0x1535e81ff000 - 0x1535e81fffff
  0x1535e8200000 - 0x1535e8203fff
  0x1535e8204000 - 0x1535e8403fff
  0x1535e8404000 - 0x1535e8404fff
  0x1535e8405000 - 0x1535e8405fff
  0x1535e845f000 - 0x1535e8462fff
  0x1535e8463000 - 0x1535e8463fff
  0x1535e8464000 - 0x1535e8465fff
  0x1535e8466000 - 0x1535e8466fff
  0x1535e8467000 - 0x1535e8467fff
  0x1535e8468000 - 0x1535e8468fff
  0x1535e8469000 - 0x1535e8476fff
  0x1535e8477000 - 0x1535e8484fff
  0x1535e8485000 - 0x1535e8491fff
  0x1535e8492000 - 0x1535e8495fff
  0x1535e8496000 - 0x1535e8496fff
  0x1535e8497000 - 0x1535e8497fff
  0x1535e8498000 - 0x1535e849dfff
  0x1535e849e000 - 0x1535e849ffff
  0x1535e84a0000 - 0x1535e84a0fff
  0x1535e84a1000 - 0x1535e84a1fff
  0x1535e84a2000 - 0x1535e84a2fff
  0x1535e84a3000 - 0x1535e84d0fff
  0x1535e84d1000 - 0x1535e84dffff
  0x1535e84e0000 - 0x1535e8585fff
  0x1535e8586000 - 0x1535e861cfff
  0x1535e861d000 - 0x1535e861dfff
  0x1535e861e000 - 0x1535e861efff
  0x1535e861f000 - 0x1535e8632fff
  0x1535e8633000 - 0x1535e865afff
  0x1535e865b000 - 0x1535e8664fff
  0x1535e8665000 - 0x1535e8666fff
  0x1535e8667000 - 0x1535e866cfff
  0x1535e866d000 - 0x1535e866ffff
  0x1535e8672000 - 0x1535e8672fff
  0x1535e8673000 - 0x1535e8673fff
  0x1535e8674000 - 0x1535e8674fff
  0x1535e8675000 - 0x1535e8675fff
  0x1535e8676000 - 0x1535e8676fff
  0x1535e8677000 - 0x1535e867dfff
  0x1535e867e000 - 0x1535e8680fff
  0x1535e8681000 - 0x1535e8681fff
  0x1535e8682000 - 0x1535e86a2fff
  0x1535e86a3000 - 0x1535e86aafff
  0x1535e86ab000 - 0x1535e86abfff
  0x1535e86ac000 - 0x1535e86acfff
  0x1535e86ad000 - 0x1535e86adfff
  0x55eef0ed6000 - 0x55eef0fc6fff
  0x55eef0fc7000 - 0x55eef10d0fff
  0x55eef10d1000 - 0x55eef1130fff
  0x55eef1132000 - 0x55eef1160fff
  0x55eef1161000 - 0x55eef1191fff
  0x55eef1192000 - 0x55eef1195fff
  0x55eef18c6000 - 0x55eef18e6fff
  0x7ffe9a6d8000 - 0x7ffe9a6f8fff
  0x7ffe9a767000 - 0x7ffe9a76afff
  0x7ffe9a76b000 - 0x7ffe9a76cfff