Natijani to'playdigan satrlarni taqqoslash qismi

Sarlavha ostida aniqlik yo'qligi uchun afsusdaman, lekin bu mening jodugar tajribamning etishmasligini ko'rsatishi mumkin.

Men buyumlarning katta ro'yxatini olishga harakat qilaman va bu ro'yxatni boshqa qatorlar ro'yxatiga aylantirmoqchiman, akkumulyator bir oz uzunroq bo'lgunga qadar birlashtiraman.

Masalan, menda bo'lsa

[ "a" "bc" "def" "ghij" ]

and my max string length is 4, I would walk down the list, accumulating the concat, until my accumulation len > 4, and then start the accumulator from scratch. My result would look like:

[ "abc" "def" "ghij" ]

partition-by uchun kerakli hissiyotlarni keltirib chiqara olmayapman va bu meni biroz aqldan ozdiradi. Men akkumulyatorimni atom deb nomlashga harakat qildim (lekin qaerda reset! ni aniqlay olmaysiz), lekin bundan tashqari, men ko'rmayapman qaerda/qanday qilib mening to'plangan mag'lubiyatni kuzatish.

Menga rahm qiladigan har kimga oldindan rahmat.

1
Sizning aniq talablaringiz bo'yicha ba'zi noaniqliklar yuz berdi. Iltimos, mening javobimdagi birlik testlariga murojaat qiling va bu siz izlayotgan xatti-harakatingizni tasdiqlang. Rahmat.
qo'shib qo'ydi muallif Alan Thompson, manba
Sizning aniq talablaringiz bo'yicha ba'zi noaniqliklar yuz berdi. Iltimos, mening javobimdagi birlik testlariga murojaat qiling va bu siz izlayotgan xatti-harakatingizni tasdiqlang. Rahmat.
qo'shib qo'ydi muallif Alan Thompson, manba
Barcha ishtirokchilarga rahmat - men barcha takliflarni o'rganish tajribasi sifatida qo'llayapman va agar tushunganimda/javobni qabul qilsam. Yana bir bor rahmat!
qo'shib qo'ydi muallif Hoopes, manba

7 javoblar

Men buni qanday qilishim kerak:

(ns tst.demo.core
  (:use demo.core tupelo.core tupelo.test))

(def bound 4)

(defn catter [strings-in]
  (loop [merged-strs    []
         curr-merge     (first strings-in)
         remaining-strs (rest strings-in)]
   ;(newline) (spyx [merged-strs curr-merge remaining-strs])
    (if (empty? remaining-strs)
      (conj merged-strs curr-merge)
      (let          ; try using 'let-spy' instead
        [new-str   (first remaining-strs)
         new-merge (str curr-merge new-str)]
        (if (< (count new-merge) bound)
          (recur merged-strs new-merge (rest remaining-strs))
          (recur (conj merged-strs curr-merge) new-str (rest remaining-strs)))))))

(dotest
  (is=  ["abc" "def" "ghij"]     (catter ["a" "bc" "def" "ghij"]) )
  (is=  ["abc" "def" "ghij"]     (catter ["a" "b" "c" "def" "ghij"]) )
  (is=  ["abc" "def" "ghij"]     (catter ["a" "b" "c" "d" "ef" "ghij"]) )
  (is=  ["abc" "def" "ghij"]     (catter ["a" "bc" "d" "ef" "ghij"]) )
  (is=  ["abc" "def" "ghij"]     (catter ["a" "bc" "d" "e" "f" "ghij"]) )

  (is=  ["abc" "def" "gh" "ij"]  (catter ["abc" "d" "e" "f" "gh" "ij"]) )
  (is=  ["abc" "def" "ghi" "j"]  (catter ["abc" "d" "e" "f" "ghi" "j"]) )

  (is=  ["abcdef" "ghi" "j"]     (catter ["abcdef" "ghi" "j"]) )
  (is=  ["abcdef" "ghi" "j"]     (catter ["abcdef" "g" "h" "i" "j"]) )
)

Loyihani boshqarish uchun [tupelo "0.9.71"] ni qo'shishingiz kerak.


Yangilash:

Agar foydalanuvchi josus va let-spy foydalanayotgan bo'lsangiz, natijaga erishish uchun algoritmni ishlatish jarayonini ko'rishingiz mumkin. Misol uchun:

(catter ["a" "b" "c" "d" "ef" "ghij"]) )   =>     ["abc" "def" "ghij"]     

-----------------------------------------------------------------------------
strings-in => ["a" "b" "c" "d" "ef" "ghij"]

[merged-strs curr-merge remaining-strs] => [[] "a" ("b" "c" "d" "ef" "ghij")]
new-str => "b"
new-merge => "ab"

[merged-strs curr-merge remaining-strs] => [[] "ab" ("c" "d" "ef" "ghij")]
new-str => "c"
new-merge => "abc"

[merged-strs curr-merge remaining-strs] => [[] "abc" ("d" "ef" "ghij")]
new-str => "d"
new-merge => "abcd"

[merged-strs curr-merge remaining-strs] => [["abc"] "d" ("ef" "ghij")]
new-str => "ef"
new-merge => "def"

[merged-strs curr-merge remaining-strs] => [["abc"] "def" ("ghij")]
new-str => "ghij"
new-merge => "defghij"

[merged-strs curr-merge remaining-strs] => [["abc" "def"] "ghij" ()]

Ran 2 tests containing 10 assertions.
0 failures, 0 errors.
4
qo'shib qo'ydi

Men buni qanday qilishim kerak:

(ns tst.demo.core
  (:use demo.core tupelo.core tupelo.test))

(def bound 4)

(defn catter [strings-in]
  (loop [merged-strs    []
         curr-merge     (first strings-in)
         remaining-strs (rest strings-in)]
   ;(newline) (spyx [merged-strs curr-merge remaining-strs])
    (if (empty? remaining-strs)
      (conj merged-strs curr-merge)
      (let          ; try using 'let-spy' instead
        [new-str   (first remaining-strs)
         new-merge (str curr-merge new-str)]
        (if (< (count new-merge) bound)
          (recur merged-strs new-merge (rest remaining-strs))
          (recur (conj merged-strs curr-merge) new-str (rest remaining-strs)))))))

(dotest
  (is=  ["abc" "def" "ghij"]     (catter ["a" "bc" "def" "ghij"]) )
  (is=  ["abc" "def" "ghij"]     (catter ["a" "b" "c" "def" "ghij"]) )
  (is=  ["abc" "def" "ghij"]     (catter ["a" "b" "c" "d" "ef" "ghij"]) )
  (is=  ["abc" "def" "ghij"]     (catter ["a" "bc" "d" "ef" "ghij"]) )
  (is=  ["abc" "def" "ghij"]     (catter ["a" "bc" "d" "e" "f" "ghij"]) )

  (is=  ["abc" "def" "gh" "ij"]  (catter ["abc" "d" "e" "f" "gh" "ij"]) )
  (is=  ["abc" "def" "ghi" "j"]  (catter ["abc" "d" "e" "f" "ghi" "j"]) )

  (is=  ["abcdef" "ghi" "j"]     (catter ["abcdef" "ghi" "j"]) )
  (is=  ["abcdef" "ghi" "j"]     (catter ["abcdef" "g" "h" "i" "j"]) )
)

Loyihani boshqarish uchun [tupelo "0.9.71"] ni qo'shishingiz kerak.


Yangilash:

Agar foydalanuvchi josus va let-spy foydalanayotgan bo'lsangiz, natijaga erishish uchun algoritmni ishlatish jarayonini ko'rishingiz mumkin. Misol uchun:

(catter ["a" "b" "c" "d" "ef" "ghij"]) )   =>     ["abc" "def" "ghij"]     

-----------------------------------------------------------------------------
strings-in => ["a" "b" "c" "d" "ef" "ghij"]

[merged-strs curr-merge remaining-strs] => [[] "a" ("b" "c" "d" "ef" "ghij")]
new-str => "b"
new-merge => "ab"

[merged-strs curr-merge remaining-strs] => [[] "ab" ("c" "d" "ef" "ghij")]
new-str => "c"
new-merge => "abc"

[merged-strs curr-merge remaining-strs] => [[] "abc" ("d" "ef" "ghij")]
new-str => "d"
new-merge => "abcd"

[merged-strs curr-merge remaining-strs] => [["abc"] "d" ("ef" "ghij")]
new-str => "ef"
new-merge => "def"

[merged-strs curr-merge remaining-strs] => [["abc"] "def" ("ghij")]
new-str => "ghij"
new-merge => "defghij"

[merged-strs curr-merge remaining-strs] => [["abc" "def"] "ghij" ()]

Ran 2 tests containing 10 assertions.
0 failures, 0 errors.
4
qo'shib qo'ydi
(defn catsize [limit strs]
  (reduce (fn [res s]
              (let [base (peek res)]
                (if (> (+ (.length ^String base) (.length ^String s)) limit)
                  (conj res s)
                  (conj (pop res) (str base s)))))
          (if (seq strs) [(first strs)] [])
          (rest strs)))
2
qo'shib qo'ydi
(defn catsize [limit strs]
  (reduce (fn [res s]
              (let [base (peek res)]
                (if (> (+ (.length ^String base) (.length ^String s)) limit)
                  (conj res s)
                  (conj (pop res) (str base s)))))
          (if (seq strs) [(first strs)] [])
          (rest strs)))
2
qo'shib qo'ydi

Mana, mening bu borada:

(defn collapse [maxlen xs]
  (let [concats (take-while #(< (count %) maxlen) (reductions str xs))]
    (cons (last concats) (drop (count concats) xs))))
(collapse 4 ["a" "bc" "def" "ghij"])
;; => ("abc" "def" "ghij")
2
qo'shib qo'ydi
yondashuv to'g'ri, lekin u ishning boshlanishi boshlanganida ishlaydi. () => ("abc" () (4) ["asdf" "a" "bc" "def" "ghij"]) > (nil "asdf" "a" "bc" "def" "ghij") .. Biror narsa belgilanishi kerak)))
qo'shib qo'ydi muallif leetwinski, manba

Mana, mening bu borada:

(defn collapse [maxlen xs]
  (let [concats (take-while #(< (count %) maxlen) (reductions str xs))]
    (cons (last concats) (drop (count concats) xs))))
(collapse 4 ["a" "bc" "def" "ghij"])
;; => ("abc" "def" "ghij")
2
qo'shib qo'ydi
yondashuv to'g'ri, lekin u ishning boshlanishi boshlanganida ishlaydi. () => ("abc" () (4) ["asdf" "a" "bc" "def" "ghij"]) > (nil "asdf" "a" "bc" "def" "ghij") .. Biror narsa belgilanishi kerak)))
qo'shib qo'ydi muallif leetwinski, manba

Bu juda yaqin. Nimaga oxirgi satr oxirida j ga ega ekanligimdan amin emasman.

(sequence
 (comp
  (mapcat seq)
  (partition-all 3)
  (map clojure.string/join))
 ["a" "bc" "def" "ghij"]) => ("abc" "def" "ghi" "j")
1
qo'shib qo'ydi