fork 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.02s 9552KB
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
  0x1522f1600000 - 0x1522f18e4fff
  0x1522f1a15000 - 0x1522f1a39fff
  0x1522f1a3a000 - 0x1522f1bacfff
  0x1522f1bad000 - 0x1522f1bf5fff
  0x1522f1bf6000 - 0x1522f1bf8fff
  0x1522f1bf9000 - 0x1522f1bfbfff
  0x1522f1bfc000 - 0x1522f1bfffff
  0x1522f1c00000 - 0x1522f1c02fff
  0x1522f1c03000 - 0x1522f1e01fff
  0x1522f1e02000 - 0x1522f1e02fff
  0x1522f1e03000 - 0x1522f1e03fff
  0x1522f1e80000 - 0x1522f1e8ffff
  0x1522f1e90000 - 0x1522f1ec3fff
  0x1522f1ec4000 - 0x1522f1ffafff
  0x1522f1ffb000 - 0x1522f1ffbfff
  0x1522f1ffc000 - 0x1522f1ffefff
  0x1522f1fff000 - 0x1522f1ffffff
  0x1522f2000000 - 0x1522f2003fff
  0x1522f2004000 - 0x1522f2203fff
  0x1522f2204000 - 0x1522f2204fff
  0x1522f2205000 - 0x1522f2205fff
  0x1522f22c1000 - 0x1522f22c4fff
  0x1522f22c5000 - 0x1522f22c5fff
  0x1522f22c6000 - 0x1522f22c7fff
  0x1522f22c8000 - 0x1522f22c8fff
  0x1522f22c9000 - 0x1522f22c9fff
  0x1522f22ca000 - 0x1522f22cafff
  0x1522f22cb000 - 0x1522f22d8fff
  0x1522f22d9000 - 0x1522f22e6fff
  0x1522f22e7000 - 0x1522f22f3fff
  0x1522f22f4000 - 0x1522f22f7fff
  0x1522f22f8000 - 0x1522f22f8fff
  0x1522f22f9000 - 0x1522f22f9fff
  0x1522f22fa000 - 0x1522f22fffff
  0x1522f2300000 - 0x1522f2301fff
  0x1522f2302000 - 0x1522f2302fff
  0x1522f2303000 - 0x1522f2303fff
  0x1522f2304000 - 0x1522f2304fff
  0x1522f2305000 - 0x1522f2332fff
  0x1522f2333000 - 0x1522f2341fff
  0x1522f2342000 - 0x1522f23e7fff
  0x1522f23e8000 - 0x1522f247efff
  0x1522f247f000 - 0x1522f247ffff
  0x1522f2480000 - 0x1522f2480fff
  0x1522f2481000 - 0x1522f2494fff
  0x1522f2495000 - 0x1522f24bcfff
  0x1522f24bd000 - 0x1522f24c6fff
  0x1522f24c7000 - 0x1522f24c8fff
  0x1522f24c9000 - 0x1522f24cefff
  0x1522f24cf000 - 0x1522f24d1fff
  0x1522f24d4000 - 0x1522f24d4fff
  0x1522f24d5000 - 0x1522f24d5fff
  0x1522f24d6000 - 0x1522f24d6fff
  0x1522f24d7000 - 0x1522f24d7fff
  0x1522f24d8000 - 0x1522f24d8fff
  0x1522f24d9000 - 0x1522f24dffff
  0x1522f24e0000 - 0x1522f24e2fff
  0x1522f24e3000 - 0x1522f24e3fff
  0x1522f24e4000 - 0x1522f2504fff
  0x1522f2505000 - 0x1522f250cfff
  0x1522f250d000 - 0x1522f250dfff
  0x1522f250e000 - 0x1522f250efff
  0x1522f250f000 - 0x1522f250ffff
  0x55e15e660000 - 0x55e15e750fff
  0x55e15e751000 - 0x55e15e85afff
  0x55e15e85b000 - 0x55e15e8bafff
  0x55e15e8bc000 - 0x55e15e8eafff
  0x55e15e8eb000 - 0x55e15e91bfff
  0x55e15e91c000 - 0x55e15e91ffff
  0x55e160281000 - 0x55e1602a1fff
  0x7ffc8ff2b000 - 0x7ffc8ff4bfff
  0x7ffc8ff8f000 - 0x7ffc8ff92fff
  0x7ffc8ff93000 - 0x7ffc8ff94fff