Do you know your 'KVZ's?

23

4

Concept

In what ways can you scramble the English alphabet so that it can still be sung to the tune Twinkle Twinkle Little Star without ruining the tune?

Rules

Swapping

Let's just assume the letters contained in each of the following sets can be swapped freely by default without ruining the tune:

  • { A, J, K }
  • { B, C, D, E, G, P, T, V, Z }
  • { I, Y }
  • { Q, U }
  • { S, X, F }
  • { M, N }
  • Therefore H, L, O, R, and W are locked in place

Output

The program needs to output a single RANDOM string (or list of characters) containing the complete English alphabet in any order provided that order satisfies the conditions above. There should be no way for you to predict which string your program will output(if we ignore seeding), meaning you can't just hardcode it.

Your program must have some positive probability (not necessarily uniform) of generating each of the \$ 9! \cdot 3! \cdot 3! \cdot 2! \cdot 2! \cdot 2! = 104509440 \$ outputs.

There are no particular formatting restrictions regarding spacing, delimiters or case, just be consistent.

Goal

Fewest bytes wins!

Examples:

  • KCDBPSVHIAJLMNOZQRXGUEWFYT
  • A,G,Z,V,P,X,C,H,Y,K,J,L,N,M,O,T,U,R,S,D,Q,B,W,F,I,E
  • KVTDCFBHIJALNMOPURSZQGWXYE
  • j c d e b x t h i k a l n m o g u r s v q p w f y z
  • ABCDEFGHIJKLMNOPQRSTUVWXYZ

Nonexample:

  • HLWROABCDEFZXYGIJKMNPQTSVU

Proof of Concept: (Python3, 529 bytes)

import random
g1 = ['A', 'J', 'K']
g2 = ['B', 'C', 'D', 'E', 'G', 'P', 'T', 'V', 'Z']
g3 = ['I', 'Y']
g4 = ['Q', 'U']
g5 = ['S', 'X', 'F']
g6 = ['M', 'N']
random.shuffle(g1)
random.shuffle(g2)
random.shuffle(g3)
random.shuffle(g4)
random.shuffle(g5)
random.shuffle(g6)
print(g1[0] + g2[0] + g2[1] + g2[2] + g2[3] + g5[0] + g2[4] + 'H' + g3[0] + g1[1] + g1[2] + 'L' + g6[0] + g6[1] + 'O' + g2[5] + g4[0] + 'R' + g5[1] + g2[6] + g4[1] + g2[7] + 'W' + g5[2] + g3[1] + g2[8])

breadlord

Posted 2019-07-10T07:19:25.320

Reputation: 341

5Shouldn't Z be "locked in place", it doesn't rhyme with the others? – Shaggy – 2019-07-10T07:37:15.597

3Depends on where you are from I suppose. If you say 'zed' then it would make sense to take it out but otherwise, if you say 'zee' then leave it in. Ultimately it's up to you, as are the rest of the sets. They're supposed to be guidelines and starting points opposed to strict rules :) – breadlord – 2019-07-10T07:46:30.970

1Can you please clarify if we should just output one scrambled possibility or all valid possibilities? – pixma140 – 2019-07-10T07:52:02.097

@pixma140 Clarified in the main post! Just one scrambled string is fine – breadlord – 2019-07-10T08:21:52.023

Should the output be random? Or would it be acceptable to systematically output the alphabet in order? (In which case this will be hard to beat.) – Robin Ryder – 2019-07-10T08:47:13.820

Random! Hardcoding would suck out all the fun @RobinRyder – breadlord – 2019-07-10T08:54:57.497

3Uniformly random or every possibility having a non-zero probability, or something else? – jimmy23013 – 2019-07-10T09:32:03.603

In what variants of English does W not rhyme with U? – Peter Taylor – 2019-07-10T10:11:32.647

8@PeterTaylor I think the intention is that the members of the groups can be easily swapped in the middle of the song while maintaining the tune and rhythm of the original - so while they do rhyme, W is 3 syllables long while U is only 1, which would change the rhythm of the song. – Sok – 2019-07-10T10:18:23.283

@PeterTaylor All. – Adám – 2019-07-10T11:42:46.530

@Adám, in my native en-GB, U is a phonetic suffix of W and therefore rhymes with it. – Peter Taylor – 2019-07-10T13:58:32.603

@Adám U = "You", W = "Double You". – mbomb007 – 2019-07-10T15:30:28.593

@PeterTaylor If you try using the same exact final syllable in a poem, it will sound very jarring and off. – Adám – 2019-07-10T15:53:53.583

@mbomb007 Because I like you so much / I will explain it such: / I wrote this poem for you / read! — it is for you. – Adám – 2019-07-10T15:59:33.110

1It still rhymes (trivially), though. – mbomb007 – 2019-07-10T16:22:20.457

I'll get around to a solution eventually, need to head to the nearest kindergarten first – Punintended – 2019-07-10T20:01:27.830

2

For those who (like me) had no idea what the question was talking about: https://en.wikipedia.org/wiki/Alphabet_song

– anatolyg – 2019-07-11T10:36:45.807

Answers

6

05AB1E, 28 bytes

A.•¬=©ƶÓÄûkTVã”ØζÞ•Dás#€.rJ‡

Outputs as a single lowercase string.

Try it online or verify \$n\$ random outputs at once.

Explanation:

A                    # (a) Push the lowercase alphabet
 .•¬=©ƶÓÄûkTVã”ØζÞ•  # Push compressed string "ajk bcdegptvz iy qu sxf mn"
  Dá                 # (b) Duplicate it, and only keep the letters (removing the spaces)
    s#               # Swap to get the string again, and split it by spaces
      €.r            # Shuffle each substring randomly
         J           # (c) Join it back together to a single string
          ‡          # Transliterate all characters from (b) to (c) in string (a)
                     # (and output the result implicitly)

See this 05AB1E tip of mine (section How to compress strings not part of the dictionary?) to understand why .•¬=©ƶÓÄûkTVã”ØζÞ• is "ajk bcdegptvz iy qu sxf mn".

Kevin Cruijssen

Posted 2019-07-10T07:19:25.320

Reputation: 67 575

7

Python 3, 140 133 124 123 bytes

d=*map(set,'AJK BCDEGPTVZ IY QU SXF MN H L O R W'.split()),
print([d[int(c,16)].pop()for c in'0111141620075581394131a421'])

Try it online!

-1 byte, thanks to Jo King


Python 2, 174 170 158 bytes

from random import*
s=''
for c in'abbbbebHcaaLffObdRebdbWecb':s+=choice(list(set(('AJK BCDEGPTVZ IY QU SXF MN '+c).split()['abcdef'.find(c)])-set(s)))
print s

Try it online!

TFeld

Posted 2019-07-10T07:19:25.320

Reputation: 19 246

4

Ruby, 102 bytes

s=[*?A..?Z]*''
%w(AJK BCDEGPTVZ IY QU SXF MN).map{|e|a=e.chars.shuffle;s.gsub!(/[#{e}]/){a.pop}}
$><<s

Try it online!

Value Ink

Posted 2019-07-10T07:19:25.320

Reputation: 10 608

3

JavaScript - 421 344 328 320 306 280 277 276 ... 176 Bytes

-77 Bytes - on my own

-18 Byte - thanks to @tsh and @Geza Kerecsenyi who made me see what @tsh initially pointed out too

-8 Bytes - thanks to @Geza Kerecsenyi

-14 Bytes - with help of @Geza Kerecsenyi

-28 Bytes - on my own

-3 Bytes - again with with help of @Geza Kerecsenyi

-1 Bytes - how could this happen...

...

-100 Bytes - @Kaiido killed it and via some steps before this whole thing got down to 176 Bytes

Golfed:

c=[,'AJK','BCDEGPTVZ','IY','QU','SXF','MN'].map(s=>[...s]);alert([...'1222252H311L66O24R5242W532'].reduce((o,v)=>o+(+v?(p=>p.splice((Math.random()*p.length)|0,1))(c[v]):v),""))

or try it online!

pixma140

Posted 2019-07-10T07:19:25.320

Reputation: 135

1You could at least replace ['B','C','D','E','G','P','T','V','Z'] by 'BCDEGPTVZ'.split`` to save some bytes – tsh – 2019-07-10T09:32:05.623

@tsh thank you! didn't came up with this idea. – pixma140 – 2019-07-10T10:49:37.080

1Try 'BCDEGPTVZ'.split\`` instead of .split('') for -2. – Geza Kerecsenyi – 2019-07-10T10:54:00.747

I missed what you pointed out in the end of the split operation tsh, thanks @Geza Kerecsenyi – pixma140 – 2019-07-10T11:01:38.517

1Also, you can define y=q=>q.split\`` at the top of your code, and make all of the arrays strings that you pass into y() - e.g a=['A','J','K'] becomes a=y("AJK") – Geza Kerecsenyi – 2019-07-10T11:02:27.023

1And replace 'BCDEGPTVZ'.split('') with y('BCDEGPTVZ') – Geza Kerecsenyi – 2019-07-10T11:08:37.360

Is there maybe a way how I can make o+=s=='a'|s=='b'|s=='c'|s=='d'|s=='e'|s=='f'?r(l[i]):l[i]; work? Passing a char to decide which array to call. Like converting the char to the variable name? @Geza Kerecsenyi – pixma140 – 2019-07-10T11:18:22.453

Let us continue this discussion in chat.

– pixma140 – 2019-07-10T11:33:05.930

1'abcdef'.includes(s)?r(eval(s)):l[i] – Geza Kerecsenyi – 2019-07-10T11:54:12.873

1-18 bytes – Geza Kerecsenyi – 2019-07-10T18:19:04.623

1-9 bytes – Neil – 2019-07-10T20:21:01.623

Just as an CGCC newbie, do I have to update my post with the newest version of these which you all here have provided or are the comments from you guys with the TIO links enough? – pixma140 – 2019-07-11T05:43:54.623

1

Newbie myself but here is a 176 bytes version (-100 from your current)

– Kaiido – 2019-07-11T05:44:03.617

1@Kaiido - I updated the post with your solution. Don't know the procedure here but now the shortest version is faster to find :) good job with this.. I would not have come up with all these golfing steps... – pixma140 – 2019-07-11T05:55:50.987

1-12 bytes – Kaiido – 2019-07-11T07:05:11.150

3

Pyth, 59 57 56 bytes

hMeD,Vs.SMJc"ajk bcdegptvz iy qu fsx mn h l o r w"dxLGsJ

Try it online!

Output is an array of lower case letters.

hMeD,Vs.SMJc"ajk bcdegptvz iy qu fsx mn h l o r w"dxLGsJ   Implicit: d=" ", G=<lowercase alphabet>
          Jc"ajk bcdegptvz iy qu fsx mn h l o r w"d        Chop the grouping string on spaces, store in J
                                                      sJ   Concatenate J into single string
                                                   xLG     Find the index of each letter in grouping string in the unaltered alphabet
       .SMJ                                                Shuffle each group in J
      s                                                    Concatenate into a single string
    ,V                                                     Pair the shuffled string with their 'correct' positions in the alphabet
  eD                                                       Order the pairs by the derived positions (last element of each pair)
hM                                                         Keep the letter from each pair (the first element)
                                                           Implicit print

Sok

Posted 2019-07-10T07:19:25.320

Reputation: 5 592

3

Perl 6, 76 bytes

{my@a='A'..'Z';<AJK BCDEGPTVZ IY QU SXF MN>>>.&{@a[.ords X-65].=pick(*)};@a}

Try it online!

Anonymous code block taking no arguments and returning a list of characters.

Explanation:

{                                                                          } # Anonymous code block
 my@a='A'..'Z';      # Initialise @a as a list of the alphabet
               <AJK BCDEGPTVZ IY QU SXF MN>   # For each of the sets of letters
                                           >>.&{@a[.ords X-65].=       }  # Set those indexes
                                                                pick(*)   # To a random order
                                                                        ;@a  # And return

Jo King

Posted 2019-07-10T07:19:25.320

Reputation: 38 234

3

R, 93 91 bytes

`^`=strsplit
L=LETTERS
for(s in el('AJK7BCDEGPTVZ7IY7QU7FSX7MN'^7)^'')L[L%in%s]=sample(s)
L

Try it online!

digEmAll

Posted 2019-07-10T07:19:25.320

Reputation: 4 599

2

Perl 5, 103 91 85 bytes

map{my%l;@l{/./g}++;@k{/./g}=keys%l}AJK,BCDEGPTVZ,SXF,IY,QU,MN;say map$k{$_}||$_,A..Z

Try it online!

This code (ab)uses the fact that Perl's output of hash keys (%l) is random to create a mapping (%k) of all the modifiable letters to one of their possible counterparts. At output time, any key that doesn't exist is assumed to be unchanged.

Xcali

Posted 2019-07-10T07:19:25.320

Reputation: 7 671

Except Perl's output of hash keys isn't random at all. It's completely deterministic, just influenced by the keys themselves in a way that makes it difficult to predict. That's why this code produces the same output on every run. Dunno whether that disqualifies this approach or not. – John Bollinger – 2019-07-11T20:49:36.467

@JohnBollinger That is only true within a run of a program. Within a single run, the hash order will be the same if the hash is unmodified. Across two runs or with a modification, there is a random seed created during each invocation of perl. Reference

– Xcali – 2019-07-11T21:23:25.273

Ok, @Xcali, I stand corrected. I have been at least partially misled, however, by your "Try it online!" link, which repeatedly generates the same output for me. It must be caching, or something. – John Bollinger – 2019-07-11T21:27:49.133

Using keys is definitely a good approach, but you can save 6 bytes using sort rand 2,... instead :( Try it online!

– Dom Hastings – 2019-07-18T08:10:34.973

2

Jelly, 34 bytes

ØA“wẸXWỵḲ⁻ȦƙṄṇ’œ?µ“ĠQ’ḃ3ÄœṖ⁸Ẋ€Ẏị@Ụ

Try it online!

Erik the Outgolfer

Posted 2019-07-10T07:19:25.320

Reputation: 38 134

2

Runic Enchantments, 210 bytes

>yy `AJK`06B$̤$@
>`BCDEGPTVZ`06B$$$$ $̤$y $ $y @
>̤`IY`06Byyy$yyy̤ @
> ̤`QU`06Byy̤ $y @
> ̤`FSX`06B $yy̤$yy@
>y̤ `MN`06Byyy $@
}}f}l3-[r\
3-[2'RA?rR1Kl'RAs]{1-:0)?\}l
> ̤`HLORW`06Bo$y $y$y$yy@ \~{{B͍

Try it online!

The randomization is not uniform as there isn't a good way to do that in Runic. Instead it randomly rotates each collection of letters (e.g. [BCDEGPTVZ] is one grouping) by some amount (e.g. rotating the above set by 4, where the top of the stack is at the right, the result would be [BCDEGZPTV]) and then randomly decides whether or not to reverse the stack. It performs these operations 15 times. As a result, all possible orderings are possible but not equally likely. (In the event that this is not enough, increasing it further costs zero bytes, up to 15000 shuffle loops).

This is the section of the code that handles the shuffling:

  v              vvvv           Loop counter

}}f}l3-[r\                      < Loop entry
[2'RA?r1KRl'RAs]{1-:0)?\}l3-
                       \~{{B͍    < loop exit

 ^^^^^^                         Randomly reverse
          ^^^^^                 Rotate by a random an amount

The rest of the code unrolls into this:

                     ABCDEFGHIJKLMNOPQRSTUVWXYZ
>      `AJK`08B      $        $$;
> `BCDEGPTVZ`08B      $$$$ $        $   $ $   $;
>         `IY`08B            $               $;
>          `QU`08B                   $   $;
>          `FSX`08B       $            $    $;
>            `MN`08B             $$;
>          `HLORW`08Bo      $   $  $  $    $;

^                                                   Create IPs
 ^^^^^^^^^^^^^^^^                                   Set letter groupings
                  ^^^                               Call shuffle function
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^    Print letters
                     ^                              Last letter group needs to be sorted

If the letters are left unshuffled (but once-reversed) by changing two bytes the alphabet is printed normally, which can be used to verify that all letter groupings print in the correct places. The whitespace shifting the B commands out of phase is so that all the IPs can use the function loop at the same time without colliding, and then getting them back into phase again.

To golf, first any space that could be removed on all lines was trimmed, then each two spaces were converted to a y, and every sequence of yyyy was converted to ̤ because ̤ and yyyy are the same amount of delay, but 2 bytes cheaper. The loop exit was also combined with the HLORW main program segment in order to save on the spacing bytes (12 bytes).

Draco18s no longer trusts SE

Posted 2019-07-10T07:19:25.320

Reputation: 3 053

1

Python 3, 149 bytes

a,b,i,q,s,m,h,l,o,r,w=(set(s)for s in["AJK","BCDEGPTVZ","IY","QU","SXF","MN",*"HLORW"])
print([eval(c).pop()for c in[*"abbbbsbhiaalmmobqrsbqbwsib"]])

Try it online!

Randomization by using pop() for letter set

Jitse

Posted 2019-07-10T07:19:25.320

Reputation: 3 566

1

APL (Dyalog Extended), 55 bytes

Full program. Prints uppercase with a leading and trailing space, but no intermediary spaces.

{(?⍨∘≢⊇⊢)@(∊∘⍺)⊢⍵}/⌈'AjkBcdegptvzIyQuSxfMn'(⊂⍤⊢,⍨∊⊂⊣)⎕A

Try it online!

⎕A the uppercase alphabet

'AjkBcdegptvzIyQuSxfMn'() apply the following anonymous tacit function with that as right argument and the indicated string as left argument:

 for the left argument,

 partition it, beginning a new segment where

 the left arguments characters are members of the right argument (i.e. on uppercase letters)

,⍨ append

 enclose (to treat it as a single element)
 the
 right argument

 uppercase everything

{}/ reduce by the following anonymous lambda, giving …"QU"λ("SXF"λ("MN"λ"A-Z")):

⊢⍵ on the right argument (the scrambling-in-progress alphabet)

()@(∊∘⍺) apply the following anonymous tacit function to the subset that is a member of the left argument (a rhyme group)

   on that subset

   reorder it to be

  ?⍨ a random permutation
   of length
   tally of letters in the subset

Adám

Posted 2019-07-10T07:19:25.320

Reputation: 37 779

1

Charcoal, 43 bytes

FαF⪪”&↖(vJf#S»↖ιηa↷N↖⪪νP´↑x‖υ” F№κι‽Φκ¬№KAμ

Try it online! Link is to verbose version of code. Charcoal has no shuffling operators, but I came up with a method of sampling without replacement. Explanation:

Fα

Loop over each letter of the alphabet.

F⪪”&↖(vJf#S»↖ιηa↷N↖⪪νP´↑x‖υ” 

Split the string AJK BCDEGPTVZ IY QU SXF MN H L O R W on spaces and loop over the substrings.

F№κι

Loop over the number of times the current letter appears in the substring. (I use a loop because a conditional would need an else caluse. Alternatively I could have filtered on the substring containing the current letter for the same byte count.)

‽Φκ¬№KAμ

Print a random character but exclude ones that have already been printed.

Neil

Posted 2019-07-10T07:19:25.320

Reputation: 95 035

0

Retina, 80 bytes

K`1A2B2C2D2E5F2GH3I1J1KL6M6NO2P4QR5S2T4U2VW5X3Y2Z
~(K`123456
.
?O`$&.¶
)`¶$
[blank line]
\d
[blank line]

Try it online!

Probably not the most golfed method, but I'll submit it anyway.

Explanation:

K`1A2B2C2D2E5F2GH3I1J1KL6M6NO2P4QR5S2T4U2VW5X3Y2Z

Set the working string to 1A2B2C2D2E5F2GH3I1J1KL6M6NO2P4QR5S2T4U2VW5X3Y2Z. There is a number before each letter in a group, for example A, J and K all have 1 before them.

~(

Mark a section of code that will produce some retina code, then run it afterwards.

K`123456

Set the working string to 123456

.
?O`$&.¶

Replace each character with ?O`{character}.¶

)`¶$
[blank line]

Remove the trailing newline and finish the group to generate the code. The group will generate the code:

?O`1.
?O`2.
?O`3.
?O`4.
?O`5.
?O`6.

{n}. matches all instances of number n followed by a character. ?O sorts each instance randomly, and this is done for all character sets.

\d
[blank line]

Finally, remove all numbers and implicitly output the generated string.

lolad

Posted 2019-07-10T07:19:25.320

Reputation: 754