Help me to add a new function make-combinations-tail which will be make the same result…
- User
Help me to add a new function make-combinations-tail which will be make the same result as make-combinations, but the new function must be implemented using tail recursion. And it mustn't use such functions as "map", "reduce", "filter", "atom" and "apply". My code: (ns test0.core (:gen-class) ;;(:require [clojure.string :as s]) )
;; Генерация комбинаций символов длины n (defn make-combinations [chars n] ;; Базовый случай для – если n <= 0 (if (<= n 0) (list "") ;; Если chars пуст (if (empty? chars) '() (for [c chars rest (make-combinations chars (dec n))] ;; рекурсивно создаем комбинации длины n-1 (str c rest))))) ;; Соединяем символы
(defn -main [& args]
(println "make-combinations:\n")
(println (make-combinations '() 3)) ;; empty case. result: () (println (make-combinations '("a") 1)) ;; one-a case. result: (a) (println (make-combinations '("a") 3)) ;; three-a case. reult: (aaa) (println (make-combinations '("a" "b" "c") -3)) ;; negative-n case. result: () (println (make-combinations '("a" "b" "c") 2)) ;; three-chars-two-n case. result: (aa ab ac ba bb bc ca cb cc) (println (make-combinations '("a" "b" "c") 3)) ;; three-chars-three-n. result: (aaa aab aac aba abb abc aca acb acc baa bab bac bba bbb bbc bca bcb bcc caa cab cac cba cbb cbc cca ccb ccc) ) ;;(-main) I remind you, that it's prohibited to use "map", "reduce", "filter", "apply" and "atom"
- Assistant
- User
"reduce", "map", "filter", "apply" and "atom" are prohibited
- Assistant
- User
We have to change code, becasue there's an error somewhere. Look at the results. They are different but they have to be exactly the same: make-combinations:
() (a) (aaa) () (aa ab ac ba bb bc ca cb cc) (aaa aab aac aba abb abc aca acb acc baa bab bac bba bbb bbc bca bcb bcc caa cab cac cba cbb cbc cca ccb ccc) make-combinations-tail:
[] [a] [aaa] [] [abcabc] [abcabcabc]
- Assistant
- User
Nothing has changed: make-combinations:
() (a) (aaa) () (aa ab ac ba bb bc ca cb cc) (aaa aab aac aba abb abc aca acb acc baa bab bac bba bbb bbc bca bcb bcc caa cab cac cba cbb cbc cca ccb ccc) make-combinations-tail:
[] [a] [aaa] [] [abcabc] [abcabcabc]
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.