Clojure Even ro'yxat ham sanoqsiz

Ayni paytda mening ro'yxatlarimni bir-biridan ajratib turadigan teng qiymatlarga aylantirishga harakat qilaman. Buni tushuntirish.

menda bor

(def a '(1 2 3 4 5 6 7 8 9 10))

Men nima qilishni xohlasam, uni ajratish funktsiyasi va ro'yxatdagi hatto ro'yxatlarni ham yaratish mumkin. Agar ro'yxatimni ikkiga bo'ladigan bo'lsa, men uni o'xshashligini istardim

((1 2 3 4 5 6) (7 8 9 10)) or 
((1 2 3 4)(5 6 7 8 9 10)) producing 2 lists with even counts.

yoki 4-alohida ro'yxatga bo'linishni istasam, u bir narsaga o'xshaydi

((1 2)(3 4)(5 6)(7 8 9 10)) 

Men har doim ham (ex. 2,4,8,16,32) bilan bo'linib ketaman. Lekin men har bir qaytish ro'yxatining qoldiqsiz hisoblashini istayman.

Men yo'qolganman va ko'p ishlamay qoladigan ko'plab parash funksiyalarini yaratdim. Har qanday yordamni qadrlaymiz.

1

5 javoblar

Men buni qanday qilishim kerak:

(def a '(1 2 3 4 5 6 7 8 9 10))

(defn trunc-to-even
  "Truncates a value the largest even int less than or equal to value"
  [val]
  (* 2 (Math/floor (/ val 2))))

(defn even-lists [some-list]
  (let [half-even (trunc-to-even (/ (count some-list) 2))]
    (split-at half-even some-list )))

(println (even-lists a))

;=>  [(1 2 3 4) (5 6 7 8 9 10)]
0
qo'shib qo'ydi
Men faqat ikkiga bo'linib qolsam yaxshi ishlaydi. Bo'limni sozlashni xohlayman. har bir ro'yxat uchun hatto hisoblash qiymatlari bilan ro'yxatning 2,4,6,8 qismi bilan ham ishlash usuli.
qo'shib qo'ydi muallif Jeffrey Morgan, manba

Men buni qanday qilishim kerak:

(def a '(1 2 3 4 5 6 7 8 9 10))

(defn trunc-to-even
  "Truncates a value the largest even int less than or equal to value"
  [val]
  (* 2 (Math/floor (/ val 2))))

(defn even-lists [some-list]
  (let [half-even (trunc-to-even (/ (count some-list) 2))]
    (split-at half-even some-list )))

(println (even-lists a))

;=>  [(1 2 3 4) (5 6 7 8 9 10)]
0
qo'shib qo'ydi
Men faqat ikkiga bo'linib qolsam yaxshi ishlaydi. Bo'limni sozlashni xohlayman. har bir ro'yxat uchun hatto hisoblash qiymatlari bilan ro'yxatning 2,4,6,8 qismi bilan ham ishlash usuli.
qo'shib qo'ydi muallif Jeffrey Morgan, manba

Men buni qanday qilishim kerak:

(def a '(1 2 3 4 5 6 7 8 9 10))

(defn trunc-to-even
  "Truncates a value the largest even int less than or equal to value"
  [val]
  (* 2 (Math/floor (/ val 2))))

(defn even-lists [some-list]
  (let [half-even (trunc-to-even (/ (count some-list) 2))]
    (split-at half-even some-list )))

(println (even-lists a))

;=>  [(1 2 3 4) (5 6 7 8 9 10)]
0
qo'shib qo'ydi
Men faqat ikkiga bo'linib qolsam yaxshi ishlaydi. Bo'limni sozlashni xohlayman. har bir ro'yxat uchun hatto hisoblash qiymatlari bilan ro'yxatning 2,4,6,8 qismi bilan ham ishlash usuli.
qo'shib qo'ydi muallif Jeffrey Morgan, manba

Yuqoridagi bandni to'g'ri javob sifatida belgilash. Men bu usulni undan ham alohida ro'yxatlarga ajratish uchun ajratib olish uchun foydalanishga muvaffaq bo'ldim.

(defn trunc-to-even
"Truncates a value the largest int less than or equal to value"
  [val]
  (* 2 (Math/floor (/ val 2))))

(defn split-2 [some-list]
  (let [half-even (trunc-to-even (/ (count some-list) 2))]
    (split-at half-even some-list )))

(defn split-4 [x]
  (apply concat (pmap split-2(concat (split-2 x)))))

Buni qilsam, 8-16-32 orasida bo'linishni davom ettiraman.

0
qo'shib qo'ydi

Yuqoridagi bandni to'g'ri javob sifatida belgilash. Men bu usulni undan ham alohida ro'yxatlarga ajratish uchun ajratib olish uchun foydalanishga muvaffaq bo'ldim.

(defn trunc-to-even
"Truncates a value the largest int less than or equal to value"
  [val]
  (* 2 (Math/floor (/ val 2))))

(defn split-2 [some-list]
  (let [half-even (trunc-to-even (/ (count some-list) 2))]
    (split-at half-even some-list )))

(defn split-4 [x]
  (apply concat (pmap split-2(concat (split-2 x)))))

Buni qilsam, 8-16-32 orasida bo'linishni davom ettiraman.

0
qo'shib qo'ydi