สร้างชุดค่าผสมที่รวมกันเป็นค่าเป้าหมาย


14

ท้าทาย

สมมติว่าคุณมีรายการตัวเลขและค่าเป้าหมาย ค้นหาชุดของการรวมกันทั้งหมดของตัวเลขของคุณซึ่งรวมถึงค่าเป้าหมายและส่งกลับเป็นดัชนีรายการ

อินพุตและเอาต์พุต

อินพุตจะใช้รายการของตัวเลข (ไม่จำเป็นต้องซ้ำกัน) และหมายเลขการสรุปเป้าหมาย เอาท์พุทจะเป็นชุดของรายการที่ไม่ว่างเปล่าแต่ละรายการมีค่าจำนวนเต็มที่สอดคล้องกับตำแหน่งของค่าในรายการอินพุตดั้งเดิม

ตัวอย่าง

Input: values = [1, 2, 1, 5], target = 8
Output: [ [0,1,3], [1,2,3] ]

Input: values = [4.8, 9.5, 2.7, 11.12, 10], target = 14.8
Output: [ [0,4] ]

Input: values = [7, 8, 9, -10, 20, 27], target = 17
Output: [ [1,2], [0,3,4], [3,5] ]

Input: values = [1, 2, 3], target = 7
Output: [ ]

เกณฑ์การให้คะแนน

นี่คือดังนั้นรหัสที่สั้นที่สุดชนะ!


6
เกี่ยวข้องอาจล่อลวง
Giuseppe

ฉันคิดว่านี่เป็นล่อลวง แต่ฉันอยากจะปิดรุ่นเก่าเพราะมันล้าสมัย
โพสต์ร็อค Garf Hunter

4
ตัวเลขจุดลอยตัวเพิ่มสิ่งที่ท้าทายหรือไม่? ไม่แน่ใจว่าฉันทามติคืออะไร แต่พวกเขาอาจนำไปสู่ข้อผิดพลาดที่แม่นยำในหลายภาษา
Arnauld

ฉันตั้งใจจะให้คะแนนลอยตัวใช่
soapergem

14
Bleh, ดัชนี? ฉันคิดว่านี่จะเป็นความท้าทายที่ดีกว่าในการส่งคืนรายการค่าแม้ว่าฉันเดาว่าจะทำให้เกิดคำถามขึ้นว่าการจัดการค่าซ้ำ ๆ ในเซตย่อยเป็นอย่างไร
xnor

คำตอบ:


3

Huskขนาด 10 ไบต์

ηλfo=¹ṁ⁰tṖ

1 การจัดทำดัชนี ลองออนไลน์!

คำอธิบาย

ηλfo=¹ṁ⁰tṖ  Inputs are a number n (explicit, accessed with ¹) and a list x (implicit).
η           Act on the incides of x
 λ          using this function:
         Ṗ   Take all subsets,
        t    remove the first one (the empty subset),
  f          and keep those that satisfy this:
      ṁ⁰      The sum of the corresponding elements of x
   o=¹        equals n.

สิ่งนี้ใช้การเพิ่มล่าสุดกับ Husk, η(ดำเนินการกับดัชนี) แนวคิดก็คือว่าηจะใช้เวลาการทำงานที่สูงขึ้นเพื่อα(ที่นี่ฟังก์ชั่นแลมบ์ดาอินไลน์) และรายการxและบริการโทรαในฟังก์ชั่นการจัดทำดัชนีx(ซึ่งก็คือในโปรแกรมข้างต้น) xและดัชนีของ ตัวอย่างเช่นṁ⁰ใช้ส่วนย่อยของดัชนีแผนที่การทำดัชนีไปที่xพวกเขาและผลรวม


9

JavaScript (ES6), 96 ไบต์

(list)(target)จะเข้าในไวยากรณ์ currying

a=>s=>a.reduce((b,_,x)=>[...b,...b.map(y=>[...y,x])],[[]]).filter(b=>!b.reduce((p,i)=>p-a[i],s))

กรณีทดสอบ

นี้จะล้มเหลวในการทดสอบกรณีที่ 2 ถ้า 4.8 และ 10 ถูกสลับเนื่องจากข้อผิดพลาด IEEE 754 ความแม่นยำ - คือแต่14.8 - 4.8 - 10 == 0 14.8 - 10 - 4.8 != 0ฉันคิดว่านี่ใช้ได้แต่อาจมีการอ้างอิงที่เกี่ยวข้องมากกว่าหนึ่งแห่งในเมตาดาต้า

แสดงความคิดเห็น

a => s =>                 // given an array a[] of length N and an integer s
  a.reduce((b, _, x) =>   // step #1: build the powerset of [0, 1, ..., N-1]
    [ ...b,               //   by repeatedly adding to the previous list b[]
      ...b                //   new arrays made of:
      .map(y =>           //     all previous entries stored in y[]
        [...y, x]         //     followed by the new index x
      )                   //   leading to:
    ],                    //   [[]] -> [[],[0]] -> [[],[0],[1],[0,1]] -> ...
    [[]]                  //   we start with a list containing an empty array
  )                       // end of reduce()
  .filter(b =>            // step #2: filter the powerset
    !b.reduce((p, i) =>   //   keeping only entries b
      p - a[i],           //     for which sum(a[i] for i in b)
      s                   //     is equal to s
    )                     //   end of reduce()
  )                       // end of filter()

7
ไม่ใช่หนึ่งเดียว แต่สองตัวreduce? ฉันต้องลงคะแนนให้
Neil

1
@Neil รู้จักกันน้อยลงว่า "lessMapReduce"
ลอร์ด Farquaad


7

R , 85 84 ไบต์

function(l,k){N=combn
o={}
for(i in I<-1:sum(l|1))o=c(o,N(I,i,,F)[N(l,i,sum)==k])
o}

ลองออนไลน์!

1 การจัดทำดัชนี

combnโดยปกติแล้วจะคืนค่า a matrixแต่การตั้งค่าsimplify=Fจะคืนค่าa listแทนทำให้เราสามารถcรวบรวมผลลัพธ์ทั้งหมดเข้าด้วยกันได้ combn(I,i,,F)ผลตอบแทนที่ได้รวมกันทั้งหมดของดัชนีและเราจะเป็นดัชนีลงในรายการที่จะตรวจสอบผู้ที่เท่ากับN(l,i,sum)==kk


7

J , 32 31 ไบต์

(=1#.t#])<@I.@#t=.1-[:i.&.#.1"0

ลองออนไลน์!

                  1-[:i.&.#.1"0         Make a list of all masks
                                        for the input list. We flip the bits
                                        to turn the unnecessary (0...0)         
                                        into (1...1) that would be missing.
                                        Define it as t.

(=1#.t#])                               Apply the masks, sum and
                                        compare with the target

         <@I.@#                         Turn the matching masks into 
                                        lists of indices

ฉันรู้สึกเหมือนคำนิยามที่ชัดเจนจะช่วยให้ทุกองค์ประกอบ 4 :'<@I.t#~x=1#.y#~t=.#:}.i.2^#y'แต่โชคร้ายที่ฉันมีเพียงระยะเวลาเดียวกัน: ลองออนไลน์!
โคล

5

Japt , 14 ไบต์

m, à f_x!gU ¥V

ทดสอบออนไลน์!

มันทำงานอย่างไร

m, à f_x!gU ¥V   Implicit: U = input array, V = target sum
m,               Turn U into a range [0, 1, ..., U.length - 1].
   à             Generate all combinations of this range.
     f_          Filter to only the combinations where
       x           the sum of
        !gU        the items at these indices in U
            ¥V     equals the target sum.
                 Implicit: output result of last expression

m,เคล็ดลับที่ดีกับ ฉันมีÊo à k@VnXx@gXจำนวนไบต์เดียวกัน
Shaggy



4

เยลลี่ 11 ไบต์

ị³S=
JŒPçÐf

ลองออนไลน์!

1 การจัดทำดัชนี 4 ไบต์ใช้กับดัชนีที่ส่งคืนแทนที่จะเป็นองค์ประกอบเอง

-1 ไบต์ขอบคุณ user202729
-1 ไบต์ขอบคุณ Jonathan Allan



@ user202729 ยอดเยี่ยมขอบคุณ!
HyperNeutrino

1
-1 ไบต์ที่: ไม่จำเป็นถ้าคุณใช้มากกว่าç Ç
Jonathan Allan

@Janathan ทุกคนขอบคุณมาก
HyperNeutrino


2

Python 3 , 144 ไบต์

lambda a,t:[[e for e,_ in x]for r in range(len(a))for x in combinations(list(enumerate(a)),r+1)if sum(y for _,y in x)==t]
from itertools import*

ลองออนไลน์!

0 การจัดทำดัชนี 44 ไบต์ใช้กับดัชนีที่ส่งคืนแทนที่จะเป็นองค์ประกอบเอง


2

Brachylogขนาด18 15 ไบต์

hiᶠ⊇Shᵐ+~t?∧Stᵐ

ลองออนไลน์!

-3 ไบต์เพราะตอนนี้ทำงานเป็นเครื่องกำเนิดไฟฟ้า (อาจเป็นไปได้ที่จะเล่นกอล์ฟได้มากขึ้น แต่การแก้ไขดัชนีจำเป็นต้องใช้นั้นค่อนข้างอึดอัด)

    S              The variable S
   ⊇               is a sublist of
  ᶠ                the list of all
 i                 pairs [element, index] from
h                  the first element of
                   the input;
     hᵐ            the first elements of each pair
       +           add up to
        ~t         the last element of
          ?        the input
           ∧       which isn't necessarily
            S      S,
             tᵐ    from which the last elements of each pair
                   are output.

hiᶠ⊇z+ʰXh~t?∧Xtออกมายาวเท่ากัน
สตริงที่ไม่เกี่ยวข้อง


1

APL (NARS), 49 ตัวอักษร, 98 ไบต์

{∨/b←⍺=+/¨{∊⍵⊂w}¨n←{⍵⊤⍨k⍴2}¨⍳¯1+2*k←≢w←⍵:⍸¨b/n⋄⍬}

1 การจัดทำดัชนี; ทดสอบ:

  f←{∨/b←⍺=+/¨{∊⍵⊂w}¨n←{⍵⊤⍨k⍴2}¨⍳¯1+2*k←≢w←⍵:⍸¨b/n⋄⍬}
  ⎕fmt 8 f 1 2 1 5
┌2──────────────┐
│┌3────┐ ┌3────┐│
││2 3 4│ │1 2 4││
│└~────┘ └~────┘2
└∊──────────────┘
  ⎕fmt   14.8  f  4.8 9.5 2.7 11.12 10
┌1────┐
│┌2──┐│
││1 5││
│└~──┘2
└∊────┘
  ⎕fmt 17 f 7, 8, 9, ¯10, 20, 27
┌3──────────────────┐
│┌2──┐ ┌2──┐ ┌3────┐│
││4 6│ │2 3│ │1 4 5││
│└~──┘ └~──┘ └~────┘2
└∊──────────────────┘
  ⎕fmt 7 f 1 2 3
┌0┐
│0│
└~┘

แสดงความคิดเห็น:

{∨/b←⍺=+/¨{∊⍵⊂w}¨n←{⍵⊤⍨k⍴2}¨⍳¯1+2*k←≢w←⍵:⍸¨b/n⋄⍬}
                             ⍳¯1+2*k←≢w←⍵         copy ⍵ in w, len(⍵) in k, return 1..2^(k-1) 
                 n←{⍵⊤⍨k⍴2}¨                     traslate in binary each element of  1..2^(k-1) and assign the result to n
          {∊⍵⊂w}¨                                for each binary element of n return the elemets of ⍵ in the place where there are the 1s
    b←⍺=+/¨                                       sum them and see if the sum is ⍺, that binary array saved in b
  ∨/                                     :⍸¨b/n   if or/b, get all the elements of n that are 1s for array b, and calculate each all indexs
                                               ⋄⍬ else return Zilde as void set

0

Pyth, 11 ไบต์

fqvzs@LQTyU

ลองออนไลน์ได้ที่นี่หรือตรวจสอบทุกกรณีการทดสอบในครั้งเดียวที่นี่

fqvzs@LQTyUQ   Implicit: Q=input 1 (list of numbers), z=input 2 (target value, as string)
               Trailing Q inferred
          UQ   Generate range [0-<length of Q>)
         y     Powerset of the above
f              Keep elements of the above, as T, when the following is truthy:
      L T        Map elements of T...
     @ Q         ... to the indicies in Q
    s            Take the sum
 q               Is the above equal to...
  vz             z as an integer
               Implicit print of the remaining results
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.