6
The set of necklaces is the set of strings, where two strings are considered to be the same necklace if you can rotate one into the other. Your program will take nonnegative integers k
and n
, and generate a list of the k
-ary (fixed) necklaces of length n
.
Necklaces will be represented by any representative string. So the necklace corresponding to the strings {ABC, BCA, CAB} can represented as ABC
, BCA
, or CAB
.
The program will output a list of strings, such that each necklace is represented by exactly one string in the list. So for instance, outputting ABC
and BCA
would not be valid, since the same necklace was represented twice.
Some other details:
- Your program may choose which
k
characters to use for the alphabet. If you prefer, you can instead choosek
distinct values of any type, and have your program output lists (or some other sequence type) of thosek
values. (This might in fact be necessary ifk
is greater than the number of characters avaible in your language.) For example, ifk=3
, you could use {A,B,C}, {&, H, (}, or even {10, 11, 12} as your alphabet. The only restriction is that elements of your alphabet may not contain whitespace. - You may output any representative for each necklace. So for the necklace {ABC, BCA, CAB}, you may output
ABC
,BCA
, orCAB
. There is no "preferred" representative.
This is code-golf, so the shortest program wins!
Also, here is a useful test to see if your program is working. Given k
and n
, the list your program outputs have the length listed here. Here is an OEIS sequence corresponding to k
=2.
Also, here are some examples and counterexamples. Note that these are not test cases, because any input has both an infinite number of correct and incorrect outputs. I will give inputs in the form (k,n)
.
Examples:
(2,2)
:[AA, BB, AB]
(2,2)
:[AA, BA, BB]
(2,2)
:[[1,0], [0,0], [1,1]]
(3,2)
:[AA, BB, CC, AB, BC, AC]
(2,3)
:[AAA, BBB, AAB, ABB]
(3,3)
:[AAA, BBB, CCC, AAB, AAC, ABB, ABC, ACB, ACC, BBC, BCC]
(0,n)
:[]
(for positive integers n)(k,0)
:[[]]
(for nonnegative integers k)(k,n)
: Search "necklaces with k colors and n beads" on wolfram alpha (for positive integers k and n)
Counterexamples:
(2,2)
:[AA, BB, AB, BA]
(2,2)
:[AA, BB]
(3,3)
:[AAA, BBB, CCC, AAB, AAC, ABB, ABC, ACC, BBC, BCC]
(3,3)
:[AAA, BBB, CCC, AAB, BAA, AAC, ABB, ABC, ACB, ACC, BBC, BCC]
(k,n)
: Any list whose length is different from this (for positive integers n)(3,3)
:[AAA, BBB, CCC, AAB, BAA, AAC, ABB, ABC, ACC, BBC, BCC]
You could have a program that verifies outputs. I think examples would also be helpful. – lirtosiast – 2019-02-01T19:27:44.020
@lirtosiast Well, that would also depend on the output format. Different languages have different notations for lists. I suppose I could make one for python and let people adapt it to different languages. Think that would help? – PyRulez – 2019-02-01T19:39:52.420
Related – PyRulez – 2019-02-01T19:44:11.317
1Very closely related – Peter Taylor – 2019-02-01T22:54:37.200
@PeterTaylor Indeed. One "gotcha" to look out for though is that the number of necklaces is not the same as the number of lyndon words, because a lyndon word also needs to be distinct from all its rotations. – PyRulez – 2019-02-01T23:56:31.733
That's a slightly opaque way of phrasing it. Was that intentional? – Peter Taylor – 2019-02-02T08:28:02.813
@PeterTaylor What do you mean? Like I said, lyndon words are related but not exactly the same as necklaces. – PyRulez – 2019-02-02T08:30:50.897
The transparent way of phrasing it is that the necklaces of length n correspond to the Lyndon words of length d where d is a factor of n. I wondered whether you were deliberately avoiding that level of detail to not give people hints. – Peter Taylor – 2019-02-02T09:11:09.110
@PeterTaylor oh, I actually did not know that. That would be a pretty big hint by my standards, though. – PyRulez – 2019-02-02T09:12:33.260