text:
Do you know your 'KVZ's?
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])

Shouldn't Z be "locked in place", it doesn't rhyme with the others?

Depends 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 :)

Can you please clarify if we should just output one scrambled possibility or all valid possibilities?

@pixma140 Clarified in the main post! Just one scrambled string is fine

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.)

Random! Hardcoding would suck out all the fun @RobinRyder

Uniformly random or every possibility having a non-zero probability, or something else?

In what variants of English does W not rhyme with U?

@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.

@PeterTaylor All.

@Adám, in my native en-GB, U is a phonetic suffix of W and therefore rhymes with it.

@Adám U = "You", W = "Double You".

@PeterTaylor If you try using the same exact final syllable in a poem, it will sound very jarring and off.

@mbomb007 Because I like you so much / I will explain it such: / I wrote this poem for you / read! — it is for you.

It still rhymes (trivially), though.

I'll get around to a solution eventually, need to head to the nearest kindergarten first

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

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".

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!

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!

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!

You could at least replace ['B','C','D','E','G','P','T','V','Z'] by 'BCDEGPTVZ'.split`` to save some bytes

@tsh thank you! didn't came up with this idea.

Try 'BCDEGPTVZ'.split\`instead of.split('')` for -2.

I missed what you pointed out in the end of the split operation tsh, thanks @Geza Kerecsenyi

Also, you can define y=q=>q.split\`at the top of your code, and make all of the arrays strings that you pass intoy()- e.ga=['A','J','K']becomesa=y("AJK")`

And replace 'BCDEGPTVZ'.split('') with y('BCDEGPTVZ')

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

Let us continue this discussion in chat.

'abcdef'.includes(s)?r(eval(s)):l[i]

-18 bytes

-9 bytes

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?

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

@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...

-12 bytes

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

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

R, 93 91 bytes
`^`=strsplit
L=LETTERS
for(s in el('AJK7BCDEGPTVZ7IY7QU7FSX7MN'^7)^'')L[L%in%s]=sample(s)
L

Try it online!

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.

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.

@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

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.

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

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

Try it online!

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).

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

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

Charcoal, 43 bytes
ＦαＦ⪪”＆↖(vＪf#Ｓ»↖ιηa↷Ｎ↖⪪νＰ´↑x‖υ” Ｆ№κι‽Φκ¬№ＫＡμ

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:
Ｆα

Loop over each letter of the alphabet.
Ｆ⪪”＆↖(vＪf#Ｓ»↖ιηa↷Ｎ↖⪪νＰ´↑x‖υ”

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

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.)
‽Φκ¬№ＫＡμ

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

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.


text:
Tips for golfing in Grass
What general tips do you have for golfing in Grass? I'm looking for ideas which can be applied to code-golf problems and which are also at least somewhat specific to Grass (e.g. "remove no-op characters" is not an answer).
Please post one tip per answer.

Define λx.f x instead of copying f
In Grass you could usually copy something to the top of the stack to reduce the length of code referring to them.
But instead of defining the identity function and applying it:
wvWwwwww

You could define a new function that calls the original function instead:
wWWWWWw

Depending on the surrounding code and the number of items to be copied, it could be sometimes longer than the copying code. But it would likely still save you characters because it has one less item in the stack, and makes all references across this point shorter.
It doesn't work for characters that you would apply Out or Succ on them. It also cannot be used in a function. But it's likely you'll find other workarounds and make it still shorter.

Hide intermediate values in a function
For the intermediate values in a chain of statements that only one final result is useful afterwards, you could hide all the intermediate values in a function, to not use spaces in the main stack.
If the function is pure (doesn't involve I/O), and the argument isn't used, instead of defining the function and immediately calling it, you could save one item in the stack by defining the function to apply the argument as a function to the supposed return value and delaying the call to where it is used.
That is, for such a function, its application and further uses of its return value:
w WWwwww WWWw WWWWw v
Ww
WWWw WWWWww

Apply the argument to the supposed return value in the end of the function, remove the application, and swap the function and the argument when you need to use the supposed return value later:
w WWwwww WWWw WWWWw WWWWw v

Www WWwww

If you try them as full programs, they give different results, because the function calls Out which isn't pure, and they ran twice for the two calls.

Fluent interface for printing
It's often more useful to make the output function return itself, instead of the outputted character, when you are writing programs to print a block of hardcoded text. You could do this by quining:
ww WWWw WWWwww v     a = λf.λx.(Out(x);f(f))
Ww                   Out2 = a(a)

source: Stack Exchange

license: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/4.0/

sort: votes