ตีกอล์ฟปัญหาย่อย


15

งาน

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


กรณีทดสอบ

อินพุต: 8 −7 5 −3 −2
เอาต์พุต:-3 -2 5


เกณฑ์การชนะ

นี่คือดังนั้นโค้ดที่สั้นที่สุดในหน่วยไบต์ชนะ!


1
เราต้องกังวลเกี่ยวกับความเป็นเอกลักษณ์หรือไม่ถ้าข้อมูลมีตัวเลขที่ไม่ซ้ำกันหรือไม่? กล่าวอีกนัยหนึ่งฉันต้องพิมพ์ผลลัพธ์จำนวนมากสำหรับอินพุต3 3 -3 -3อย่างไร
Keith Randall

@Keith ตามแบบแผนชุดประกอบด้วยองค์ประกอบที่แตกต่างที่ปรากฏมากที่สุดครั้งเดียว Multisets สามารถมีองค์ประกอบที่ปรากฏมากกว่าหนึ่งครั้ง en.wikipedia.org/wiki/Multiset
DavidC

4
@DavidCarraher, OP ผสมคำศัพท์โดยพูดถึงส่วนย่อยของรายการ
ปีเตอร์เทย์เลอร์

@ PeterTaylor ขอบคุณ จุดดี.
DavidC

คำตอบ:


4

GolfScript, 41 ตัวอักษร

~][[]]\{`{1$+$}+%}%;(;.&{{+}*!},{" "*}%n*

หากคุณไม่สนใจรูปแบบผลลัพธ์เฉพาะคุณสามารถย่อรหัสให้เหลือ 33 อักขระ

~][[]]\{`{1$+$}+%}%;(;.&{{+}*!},`

ตัวอย่าง (ดูออนไลน์ ):

> 8 -7 5 -3 -2 4
-3 -2 5
-7 -2 4 5
-7 -3 -2 4 8


4

Python 119 ตัวอักษร

def S(C,L):
 if L:S(C,L[1:]);S(C+[L[0]],L[1:])
 elif sum(map(int,C))==0and C:print' '.join(C)
S([],raw_input().split())

ระบุชุดย่อย 2 ^ n ทั้งหมดซ้ำและตรวจสอบแต่ละชุดย่อย


ไชโย! ฉันเข้ามาภายในตัวละคร ...
บูธโดย

3

Python 120

ฉันเป็นตัวละครที่แย่กว่าทางออกของคี ธ แต่ ... นี่ใกล้เกินไปที่จะไม่โพสต์ หนึ่งในคุณสมบัติที่ฉันโปรดปรานของ code-golf คือวิธีการแก้ปัญหาความยาวคล้ายกันที่แตกต่างกันได้อย่างไร

l=raw_input().split()
print[c for c in[[int(j)for t,j in enumerate(l)if 2**t&i]for i in range(1,2**len(l))]if sum(c)==0]

2

Python ( 128 137 136)

ประณามคุณitertools.permutationsด้วยชื่ออันยาวเหยียด!

วิธีการแก้ปัญหากำลังดุร้าย ฉันประหลาดใจที่มันไม่สั้นที่สุด: แต่ฉันคิดว่าitertoolsซากปรักหักพังทางออก

Ungolfed:

import itertools
initial_set=map(int, input().split())
ans=[]
for length in range(1, len(x)+1):
    for subset in itertools.permutations(initial_set, length):
        if sum(subset)==0:
            ans+=str(sorted(subset))
print set(ans)

Golfed (ออกน่าเกลียด):

from itertools import*
x=map(int,input().split())
print set(`sorted(j)`for a in range(1,len(x)+1)for j in permutations(x,a)if sum(j)==0)

Golfed (เอาท์พุทสวย) (183):

from itertools import*
x=map(int,input().split())
print `set(`sorted(j)`[1:-1]for a in range(1,len(x)+1)for j in permutations(x,a)if sum(j)==0)`[5:-2].replace("'","\n").replace(",","")

import itertools as i: นำเข้าโมดูล itertools และเรียกมัน i

x=map(int,input().split()): แยกการป้อนข้อมูลด้วยช่องว่างจากนั้นเปลี่ยนรายการของผลลัพธ์ให้เป็นจำนวนเต็ม ( 2 3 -5-> [2, 3, -5])

set ( sorted(j)สำหรับ a in range (1, len (x) +1) สำหรับ j ใน i.permutations (x, a) ถ้า sum (j) == 0):
ส่งคืนรายการของชุดย่อยทั้งหมดในxเรียงลำดับโดยที่ sum เป็น 0 จากนั้นรับเฉพาะรายการที่ไม่ซ้ำกัน
( set(...))

หลุมฝังศพ ( `) รอบเป็นงูหลามชวเลขsorted(j) repr(sorted(j))สาเหตุที่นี่เป็นเพราะชุดใน Python ไม่สามารถจัดการรายการดังนั้นสิ่งที่ดีที่สุดถัดไปคือการใช้สตริงที่มีรายการเป็นข้อความ


ฉันสับสนเกี่ยวกับวิธีที่คุณรับจำนวนเต็มแทนที่จะเป็นสตริง split()สร้างรายการของสตริง แต่หลังจากนั้นคุณจะโทรหาsumชุดย่อยของการแยกนั้น
Keith Randall

@ KeithRandall: facepalmฉันรีบเร่งดังนั้นฉันจึงไม่ได้ทดสอบโค้ดของฉัน ขอบคุณสำหรับการชี้ให้เห็นว่า
beary605

คุณอาจจะสามารถบันทึกตัวอักษรด้วยการทำfrom itertools import*
แมตต์

จริงๆแล้วหลุมฝังศพนั้นจดชวเลขrepr()
gnibbler

@gnibbler: นั่นจะทำให้รู้สึกได้มากขึ้นเมื่อใช้งาน '' hello'` ขอบคุณ!
beary605

2

C # - 384 ตัวอักษร

ตกลงการเขียนโปรแกรมสไตล์การทำงานใน C # ไม่สั้นแต่ฉันรักมัน! (การใช้การแจงนับที่โหดร้ายไม่มีอะไรดีไปกว่า)

using System;using System.Linq;class C{static void Main(){var d=Console.ReadLine().Split(' ').Select(s=>Int32.Parse(s)).ToArray();foreach(var s in Enumerable.Range(1,(1<<d.Length)-1).Select(p=>Enumerable.Range(0,d.Length).Where(i=>(p&1<<i)!=0)).Where(p=>d.Where((x,i)=>p.Contains(i)).Sum()==0).Select(p=>String.Join(" ",p.Select(i=>d[i].ToString()).ToArray())))Console.WriteLine(s);}}

จัดรูปแบบและแสดงความคิดเห็นเพื่อให้อ่านง่ายขึ้น:

using System;
using System.Linq;

class C
{
    static void Main()
    {
        // read the data from stdin, split by spaces, and convert to integers, nothing fancy
        var d = Console.ReadLine().Split(' ').Select(s => Int32.Parse(s)).ToArray();
        // loop through all solutions generated by the following LINQ expression
        foreach (var s in
            // first, generate all possible subsets; well, first just their numbers
            Enumerable.Range(1, (1 << d.Length) - 1)
            // convert the numbers to the real subsets of the indices in the original data (using the number as a bit mask)
            .Select(p => Enumerable.Range(0, d.Length).Where(i => (p & 1 << i) != 0))
            // and now filter those subsets only to those which sum to zero
            .Where(p => d.Where((x, i) => p.Contains(i)).Sum() == 0)
            // we have the list of solutions here! just convert them to space-delimited strings
            .Select(p => String.Join(" ", p.Select(i => d[i].ToString()).ToArray()))
        )
            // and print them!
            Console.WriteLine(s);
    }
}

2

SWI-Prolog 84

รุ่นนี้พิมพ์รายการแทนที่จะพยายามค้นหาการเชื่อมโยงที่เหมาะสมสำหรับคำในภาคแสดง

s([],O):-O=[_|_],sum_list(O,0),print(O).
s([H|T],P):-s(T,[H|P]).
s([_|T],O):-s(T,O).

วิธีการป้อนข้อมูล

s([8,-7,5,-3,-2,4],[]).

สำหรับเร็กคอร์ดนี่เป็นเวอร์ชันที่พบการเชื่อมโยงเพื่อทำให้เป็นจริง:

s(L,O):-s(L,0,O),O=[_|_].
s([],0,[]).
s([H|T],S,[H|P]):-R is H+S,s(T,R,P).
s([_|T],S,O):-s(T,S,O).

วิธีการป้อนข้อมูล

s([8,-7,5,-3,-2,4],O).

การแก้ไขก่อนหน้ามีโซลูชันที่ไม่สมบูรณ์ซึ่งไม่ได้จัดการเพื่อลบชุดว่าง


2

Mathematica 62 57 38

รหัส

อินพุตที่ป้อนเป็นจำนวนเต็มในอาร์เรย์, x.

x

input

Grid@Select[Subsets@x[[1, 1]], Tr@# == 0 &]

เอาท์พุต

output


คำอธิบาย

x[[1, 1]] แปลงอินพุตเป็นรายการจำนวนเต็ม

Subsets สร้างชุดย่อยทั้งหมดจากจำนวนเต็ม

Select....Tr@# == 0 ให้เซตย่อยทั้งหมดที่มีผลรวมเท่ากับ 0

Grid จัดรูปแบบชุดย่อยที่เลือกเป็นจำนวนเต็มคั่นด้วยช่องว่าง


2

เยลลี่ขนาด 6 ไบต์

ŒPḊSÐḟ

ลองออนไลน์!

เพียงเพื่อความสมบูรณ์ คล้ายกับ Brachylog, Jelly ก็โพสต์ความท้าทายด้วย แต่ตอนนี้ภาษาใหม่จะแข่งขันกันตามปกติ

ŒP       Power set.
  Ḋ      Dequeue, remove the first element (empty set).
    Ðḟ   Filter out the subsets with truthy (nonzero)...
   S       sum.


1

J, 57 53 51 49 ตัวอักษร

>a:-.~(#:@i.@(2&^)@#<@":@(#~0=+/)@#"1 _])".1!:1[1

การใช้งาน:

   >a:-.~(#:@i.@(2&^)@#<@":@(#~0=+/)@#"1 _])".1!:1[1
8 _7 5 _3 _2 4
5 _3 _2
_7 5 _2 4
8 _7 _3 _2 4

เขียนใหม่รถไฟเช่นเดียวกับ(<@":@(#~0=+/)@#"1 _~2#:@i.@^#)ประหยัด 4 ตัวละคร
algorithmshark

1

Stax , 8 ไบต์CP437

â±3╒yΣ╓à

เรียกใช้และแก้ไขข้อบกพร่องออนไลน์!

คำอธิบาย

ใช้เวอร์ชันที่ไม่มีการแพ็ก (9 ไบต์) เพื่ออธิบาย

LS{|+!fmJ
L            Convert to list
 S           Powerset
  {   f      Filter with block
   |+!       Sum is zero
       mJ    Print each subset, joined by spaces

Given a list of space-delimited integers as input; คุณ - แต่ - ใช้รายการเป็นอินพุต
Jonathan Frech

จะแก้ไขที่ราคาหนึ่งไบต์
Weijun Zhou

1

J , 34 ไบต์

(a:-.~](<@#~0=+/)@#~[:#:@i.2^#)@".

ลองออนไลน์!

อย่างไร

".แปลงอินพุตให้เป็นรายการ แล้ว:

a: -.~ ] (<@#~ (0 = +/))@#~ [: #:@i. 2 ^ #
                                  i.       NB. ints from 0 to...
                                     2 ^ # NB. 2 to the input len
                            [: #:@         NB. converted to binary masks
       ] (             ) #~                NB. filter the input using
                                           NB. using those masks, giving
                                           NB. us all subsets
         (             )@                  NB. and to each one...
         (  #~ (0 = +/))                   NB. return it if its sum is
                                           NB. 0, otherwise make it 
                                           NB. the empty list.
         (<@           )                   NB. and box the result.
                                           NB. now we have our answers
                                           NB. and a bunch of empty 
                                           NB. boxes, or aces (a:).
a: -.~                                     NB. remove the aces.

1

Perl 6 , 51 ไบต์

*.words.combinations.skip.grep(!*.sum)>>.Bag.unique

ลองออนไลน์!

ส่งคืนรายการกระเป๋าเฉพาะที่รวมเป็น 0 กระเป๋าเป็นชุดที่มีน้ำหนัก

คำอธิบาย:

*.words                 # Split by whitespace
 .combinations          # Get the powerset
 .skip                  # Skip the empty list
 .grep(!*.sum)          # Select the ones that sum to 0
 >>.Bag                 # Map each to a weighted Set
 .unique                # And get the unique sets

0

Ruby, 110 ไบต์

a=gets.split.map &:to_i;b=[];(1...a.length).each{|c|a.permutation(c){|d|d.sum==0?b<<d:0}};p b.map(&:sort).uniq

จะเพิ่มลิงค์ TIO ในภายหลัง

รับอินพุตจาก stdin เป็นรายการตัวเลขเช่น 8 −7 5 −3 −2

มันทำงานอย่างไร: มันแปลงอินพุตเป็นอาร์เรย์ของตัวเลข รับพีชคณิตทั้งหมดของความยาวจาก 1 ถึงความยาวของอาร์เรย์ มันเพิ่มพวกเขาไปยังอาร์เรย์ผลลัพธ์ถ้าพวกเขารวมถึง 0 มันส่งออกอาร์เรย์โดยไม่ซ้ำกัน

เอาต์พุตสำหรับอินพุตตัวอย่าง: [[-3, -2, 5]]

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.