Diberikan seperangkat string yang tidak kosong dan daftar string, cari tahu berapa kali set terjadi dalam daftar, yaitu berapa kali Anda bisa membuat set dengan item dari daftar. Setiap elemen dari daftar hanya dapat digunakan satu kali.
Petunjuk: satu set adalah daftar item unik yang tidak disusun.
Default Aturan input / output berlaku.
Tidak ada perpustakaan eksternal yang diizinkan. Compiler / Interpreter libs standar tidak masalah. Ini kode golf, jadi solusi terpendek diperhitungkan.
Kasus uji:
["apple", "banana"], ["apple", "pear", "apple", "banana", "banana"] => 2
["apple", "banana"], ["apple", "pear", "apple", "banana", "apple"] => 1
["apple", "banana", "pear"], ["apple", "banana", "kiwi", "apple"] => 0
["coconut"], [] => 0
EDIT: menghapus kalimat yang menyatakan bahwa params input didefinisikan dalam lingkup lokal. Ini bertentangan dengan aturan IO default yang ditautkan di atas.
code-golf
set-partitions
Hubert Grzeskowiak
sumber
sumber
Jawaban:
Python, 30 byte
Cobalah online!
sumber
lambda
ke aprint
membawa jumlah byte hingga 37 karena keduanyainput()
diperlukan.Jelly , 4 byte
Cobalah online!
Bagaimana?
sumber
Jelly ,
654 byteCobalah online!
Argumen pertama dari program adalah himpunan, dan argumen kedua adalah daftar.
Penjelasan
-1 byte berkat @ETHproductions
-1 byte lagi berkat @ETHproductions
sumber
⁹ċ$€Ṃ
Saya punya perasaan yang dapat dibuat lebih pendek dengan menggunakan argumen benar implisit sebagai pengganti⁹
...ċ@€Ṃ
berfungsi untuk menyimpan byte lain ... (@
membalikkan argumen keċ
)Ɱ
sebagai gantinya@€
(dengan argumen terbalik ke program) menyimpan byte lain: Coba online!JavaScript (ES6), 56 byte
Cobalah online
sumber
n=>h=>Math.min(...n.map(c=>h.filter($=>$==c).length))
untuk 53 byteJavaScript (ES6), 64 byte
Mengasumsikan keduanya
s
danl
merupakan array objek. Menggunakan kesetaraan ketat JavaScript untuk perbandingan, jadi misalnya[] === []
salah.sumber
Haskell,
3734 bytesThanks to @Laikoni for shaving off three bytes.
Call with
(set::[a]) # (list::[a])
wherea
is any type derivingEq
.sumber
length[y|y<-l,y==x]
you can usesum[1|y<-l,y==x]
.sum[1|y<-l,y==x,_<-y]
, which comes out to two bytes longer—I could definitely be missing something there, thoughCJam, 11 bytes
Try it online!
Explanation
sumber
Mathematica, 24 bytes
Pure function taking two lists as arguments in the suggested order and returning a nonnegative integer.
Tally
counts how many occurrences of every symbol occur in the input list, and#/.Rule@@@
converts each element of the input set into the corresponding number of occurrences.sumber
T-SQL, 62
59bytesPrevious version didn't work for sets with no matches
With s and l as tables and columns named the same as the table
sumber
Swift, 39 bytes
explanation:
s.map{}
goes through each word in s and will produce an array of countsw in
names the mapped word for use in the next filterl.filter{}
aplies a filter to the l array$0==w
is the filter condition matching word w.count
gives the number of elements of l that met the condition.min()
returns the lowest count in the mapped resultsumber
APL (Dyalog), 9 bytes
Try it online!
⎕
get evaluated input (list of strings)⎕∘.≡
get evaluated input (non-empty set of strings) and create equivalency table+/
add across⌊/
minimum acrosssumber
Perl 6,
3718 bytes37
Try it
Expanded:
See Sets, Bags, and Mixes for more information.
18
Try it
Explanation:
@^b.Bag
create a Bag from the values{@^a}
key into that Bag (returns a list of counts).min
get the minimum value of the resulting listsumber
Axiom, 42 bytes
test code and results
sumber
C++,
203201 bytesThanks to @Quentin for saving two bytes!
Try it online!
sumber
L.begin()
->begin(L)
saves one byte :)using T=std::vector<std::string>;
saves another! Who knew modern pretty syntax could also help golfing.PHP, 74 Bytes
Testcases
PHP, 108 Bytes
Testcases
sumber
Pyth, 5 bytes
Takes the list first and the set second. Test suite.
Explanation:
sumber
C#, 36 bytes
n
andh
arestring[]
and the output is anint
.Try it online!
This answer is inspire by @ovs and @Alberto Rivera's logic. Thank you!
sumber
Java, 135 bytes
This is my first code golf challenge and answer, so not sure about the format. Does it need to be a full compiling program? Do I need to define the parameters? Suggestions appreciated.
EDIT: wrapped code in a function. Thanks @Steadybox
sumber
05AB1E, 7 bytes
Try it online!
sumber
Java, 114 Bytes
Tio coming soon
Explanation
creates local variable m.
maps the set to a stream.
for each element, if the number of occurances of the element in the list is less than m, m is set to that value.
returns m, which is the number of complete versions of the set
sumber
R 54 Bytes
Explanation: creating a table of the counts of only the values in the list that also appear in the sublist.
I then turn the variable into a factor in order to generate zeros if a value that appears in the sublist does not appear in the list. Finally, I take the minimum of the counts.
sumber
R,
615744 bytesAnonymous function.Apparently you don't have to define a function for this challenge. Saved 13 bytes thanks to count.Explanation:
sum(l%in%x))
returns the number of times a string ins
is found inl
.lapply(s,function(x))
applies that to each string ins
separately and returns a list of sums.min()
returns the smallest from that list.sumber
z=c();for(i in s)z[i]=sum(l%in%i);min(z)
min(sapply(s,function(x)sum(l%in%x)))
JavaScript (ES6), 59 bytes
Try it
sumber