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 ไม่สามารถจัดการรายการดังนั้นสิ่งที่ดีที่สุดถัดไปคือการใช้สตริงที่มีรายการเป็นข้อความ
3 3 -3 -3อย่างไร