36
\$\begingroup\$

Here's an array representing a standard deck of cards, including two Jokers.

[
  "AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", 
  "AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD", 
  "AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH", 
  "AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC", 
  "J", "J"
]

It's composed in this manner:

  • There are four suits; hearts, spades, diamonds, and clubs (H, S, D, C).
  • Each suit has one card for the numbers 2 to 10, plus 4 'picture' cards, Ace, Jack, Queen, and King (A, J, Q, K).
  • For each combination of suit and value, there should be one item in the array, which is a string, and is made up of the value followed by the suit (Whitespace between these is permitted).
  • On top of that, there are two Joker cards ('J').
  • Write in any language you please.
  • Golf it up! Try to produce this output in the smallest number of bytes.
  • It does not matter what order the output is in.
\$\endgroup\$
28
  • 16
    \$\begingroup\$ I was just wondering if you could get 23456789 by multiplying 2 smaller numbers... only to discover it's prime! \$\endgroup\$
    – match
    Commented Jun 11, 2018 at 11:57
  • 4
    \$\begingroup\$ @match Since the order of the output doesn't matter, maybe you can still create it with smaller numbers by ending it with 2 or 4 or anything else so it isn't a prime anymore. \$\endgroup\$ Commented Jun 11, 2018 at 13:19
  • 3
    \$\begingroup\$ The playing field isn't very level when you've excluded a majority of languages \$\endgroup\$
    – Jo King
    Commented Jun 12, 2018 at 9:55
  • 6
    \$\begingroup\$ Rules in comments don't count. If we cannot print the result to STDOUT (and that's a big if, as it overrides our defaults for I/O and prevents languages without functions and string arrays from participating), that rule has to be stated explicitly in the challenge spec. \$\endgroup\$
    – Dennis
    Commented Jun 12, 2018 at 15:05
  • 7
    \$\begingroup\$ That doesn't rule out a string representation of such an array, which could be printed to STDOUT. If not for the comment section, I never would have guessed this wasn't allowed. \$\endgroup\$
    – Dennis
    Commented Jun 12, 2018 at 23:50

66 Answers 66

28
\$\begingroup\$

JavaScript (ES6), 62 60 bytes

f=n=>n>51?'JJ':[('JQKA'[k=n>>2]||k-2)+'CDHS'[n&3],...f(-~n)]

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Smooth moves!!! \$\endgroup\$ Commented Jun 11, 2018 at 14:46
  • \$\begingroup\$ I didn't know spread syntax existed until I read this -- very cool \$\endgroup\$
    – logic8
    Commented Jun 15, 2018 at 17:29
24
\$\begingroup\$

Bash 43 34 bytes

d=({A,J,Q,K,{2..10}}{H,S,D,C} J J)

Try it online!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ 9 bytes shorter \$\endgroup\$ Commented Jun 11, 2018 at 17:34
  • \$\begingroup\$ I did not think braces were nestable. Thanks. \$\endgroup\$ Commented Jun 11, 2018 at 17:53
  • \$\begingroup\$ And here I was thinking "what language do I know that conveniently does direct products on lists of strings?" I didn't even think about good old glob! \$\endgroup\$
    – aschepler
    Commented Jun 13, 2018 at 1:21
  • 1
    \$\begingroup\$ Technically brace expansion is not a glob pattern, it is its own special syntax \$\endgroup\$ Commented Jun 14, 2018 at 3:19
14
\$\begingroup\$

brainfuck, 200 197 bytes

+[[<+>->++<]>]+<++++<<+++[->+++>+++++>+>+<<<<]>+.<<.>>.--<<<[->>+++<<]>>+>[<<.>.-.+>.<<.<<[>>>+.>.<<.<+<-]>[-<+>>>+<<]>>.>.<<.>+++++++++.>.<<.>+.>.<<.>++++++.>.,++++[-<-------->]>[[-<+>]>]<<[<]>>>]

Try it online!

Uses one negative cell (+1 bytes to fix), wrapping cells (lots o' bytes to fix) and 0 on EOF (+2 bytes to remove dependancy).

Outputs

J J 10H 2H 3H 4H 5H 6H 7H 8H 9H AH JH KH QH 10S 2S 3S 4S 5S 6S 7S 8S 9S AS JS KS QS 10D 2D 3D 4D 5D 6D 7D 8D 9D AD JD KD QD 10C 2C 3C 4C 5C 6C 7C 8C 9C AC JC KC QC

Explanation:

+[[<+>->++<]>]  Sets the tape to powers of 2 
          TAPE: 1 2 4 8 16 32 64 128 0 0'
+<++++<<+++[->+++>+++++>+>+<<<<]   Uses the 64 and the 128 to generate the suit letters
          TAPE: 1 2 4 8 16 32 0' 73 83 68 67
                                 I  S  D  C
>+.<<.>>.--<<     Prints the Jokers
<[->>+++<<]>>+>   Uses the 16 to create 49
          TAPE: 1 2 4 8 0 32 49 72' 83 68 67
                                 H   S  D  C
[  Loop over the suits
   <<.>.-.+>.<<.<<      Print the 10 card with a leading space
          TAPE: 1 2 4 8' 0 32 49 Suit
   [>>>+.>.<<.<+<-]  Use the 8 to print the other 8 number cards
          TAPE: 1 2 4 0' 8 32 57 Suit
   >[-<+>>>+<<]      Move the 8 back into place while also adding it to the 57
          TAPE: 1 2 4 8 0' 32 65 Suit

   >>.>.<<.         Print the Ace
   >+++++++++.>.<<. Print the Jack
   >+.>.<<.         Print the King
   >++++++.>.<<.    Print the Queen
          TAPE: 1 2 4 8 0 32 81' Suit
   >>,    Clear the current suit
   ++++[-<-------->]    Subtract 32 to get back to 49 (reusing the space is longer than just subtracting 32)
          TAPE: 1 2 4 8 0 32 49 0' MoreSuits?
   >[[-<+>]>]<<[<]>>>   Shift all the remaining suits over one
          TAPE: 1 2 4 8 0 32 49 Suit?'
] End on the character of the next suit and repeat
\$\endgroup\$
1
  • \$\begingroup\$ That more than 2.5 time shorter than my solution. Well done. \$\endgroup\$ Commented Jun 12, 2018 at 14:52
11
\$\begingroup\$

05AB1E, 28 27 25 24 23 bytes

2TŸ.•-Ÿ•S«.•ôì•âJ„jjS«u

Try it online.

-1 byte thanks to @Emigna removing the S after "HSDC", because â does this implicitly.

Explanation:

2TŸ           # Push list in the range [2,10]: [2,3,4,5,6,7,8,9,10]
   .•-Ÿ•      # Push compressed string "ajqk"
        S     # Convert it to a list of characters: ["a","j","q","k"]
         «    # Merge the two lists together: [2,3,4,5,6,7,8,9,10,"a","j","q","k"]
.•ôì•         # Push compressed string "cdhs"
â             # Cartesian product of each (pair each character of both lists):
              #  [[2,"a"],[2,"d"],[2,"h"],...,["k","d"],["k","h"],["k","s"]]
J             # Join each pair together to a single string:
              #  ["2a","2d","2h",...,"kd","kh","ks"]
„jjS          # Push string "jj", and convert it to a list of characters: ["j","j"]
    «         # Merge both lists together:
              #  ["2a","2d","2h",...,"kd","kh","ks","j","j"]
     u        # Convert everything to uppercase:
              #  ["2A","2D","2H",...,"KD","KH","KS","J","J"]
              # (and output the result implicitly)

See this 05AB1E tip of mine (section How to compress strings not part of the dictionary?) to understand why .•-Ÿ• is "ajqk" and .•ôì• is "cdhs".

\$\endgroup\$
0
9
\$\begingroup\$

brainfuck, 550 504 bytes

++++++++++[>+++>+++++>+++++++<<<-]>++>->[>+>+>+>+<<<<-]>----->++++.<<<<.>>>>.<<<<.>>>>>--->+++++++++++++<<<<<.-.+>>>>.<<<<<.>.-.+>>>>+.<<<<<.>.-.+>>>>++++.-----<<<<<.>.-.+>>>>>.<<<<<<.>>++++++++[-<+.>>>>.<<<<<.>.>>>>+.<<<<<.>.>>>>++++.-----<<<<<.>.>>>>>.<<<<<<.>>]>.>>.<<<<<.>>>>.>.<<<<<.>>>>+.>.<<<<<.>>>>++++++.>.<<<<<.>>>.>>+.<<<<<.>>>>.>.<<<<<.>>>>------.>.<<<<<.>>>>-.>.<<<<<.>>>.>>++++.<<<<<.>>>>.>.<<<<<.>>>>+.>.<<<<<.>>>>++++++.>.<<<<<.>>>.>>>.<<<<<<.>>>>.>>.<<<<<<.>>>>------.>>.<<<<<<.>>>>-.>>.

Try it online!

Old answer, also online!

\$\endgroup\$
0
8
\$\begingroup\$

Java 10, 153 151 125 77 75 bytes

v->("AJQK2345678910".replaceAll("1?.","$0H,$0S,$0D,$0C,")+"J,J").split(",")

-28 bytes thanks to @OlivierGrégoire.
-50 bytes thanks to @mazzy.

Try it online.

Explanation:

v->                      // Method with empty unused parameter and String-array return-type
  "AJQK2345678910"
   .replaceAll("1?.",    //  Replace every loose character (or "10" as single char)
     "$0H,$0S,$0D,$0C,") //  with "cH,cS,cD,cC,", where c is the character
   +"J,J")               //  Append "J,J"
  .split(",")            //  And split everything by commas
\$\endgroup\$
6
7
\$\begingroup\$

APL (Dyalog Unicode), 29 bytes

1 byte saved thanks to Probie by using 1+⍳9 instead of 1↓⍳10

'JJ',,'HCDS'∘.,⍨'AKJQ',⍕¨1+⍳9

Try it online!

This is a full program. In the TIO link, I have enabled boxing so that the individual elements of the array can be distinguished.

Here is the boxed output.

┌─┬─┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬───┬───┬───┬───┐
│J│J│AH│AC│AD│AS│KH│KC│KD│KS│JH│JC│JD│JS│QH│QC│QD│QS│2H│2C│2D│2S│3H│3C│3D│3S│4H│4C│4D│4S│5H│5C│5D│5S│6H│6C│6D│6S│7H│7C│7D│7S│8H│8C│8D│8S│9H│9C│9D│9S│10H│10C│10D│10S│
└─┴─┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴───┴───┴───┴───┘

'JJ', 2 jokers concatenated to

, the raveled form of

∘.,⍨ the matrix created by concatenating every combination of

  • 'HCDS' this string against

  • 'AKJQ', this string with

    • ⍕¨ the stringified forms of each of

      • 1+⍳9 the numbers 2..10 (1 plus the range 1..9)
\$\endgroup\$
3
  • \$\begingroup\$ I've changed the indentation of your list in the explanation. It looked a bit weird with the two and three dots.. If that was intended for some reason, feel free to rollback. \$\endgroup\$ Commented Jun 11, 2018 at 11:31
  • \$\begingroup\$ @KevinCruijssen Thanks, I couldn't figure out how to do that, so I used multiple bullet points hah \$\endgroup\$
    – user41805
    Commented Jun 11, 2018 at 11:39
  • \$\begingroup\$ Yeah, I thought so, that's why I changed it. :) You need multiples of four leading spaces before the - to indent it. \$\endgroup\$ Commented Jun 11, 2018 at 11:51
7
\$\begingroup\$

Befunge-98 (FBBI), 75 bytes

j4d*1-2k:d%4+1g:'0-!#v_>,d/1g,' ,!#v_3
CHSDA234567890JQK@;1'< ^,;,k2"J J" <

Try it online!

Program structure

enter image description here

At first, the stack is filled with 0's, and j does not jump over the initialisation. The initialisation pushes 4 * 13 = 52, which is the program counter. In the following iterations the trailing 3 causes the pointer to jump over this part.

1-2k:d%4+1g,d/1g,' ,!#v_  Main loop

1-                        decrement counter
  2k:                     duplicate counter three times
     d%                   counter mod 13 ...
       4+                 ... + 4 is the position of the card in the Data section
         1g,              get the card from row 1 and print it
            d/            counter / 13 is the position of the suit in the Data section
              1g,         get the suit from row 1 and print it
                 ' ,      print a space as seperator
                    !     negate the counter (0 for non-zero, 1 for 0)
                       _  If the counter is 0, move West ...
                     #v   ... and move to the termination
                          otherwise, continue with the next iteration

Code that prints 10:

'0-!#v_>    Checks if Card is '0'

'0-         subtract the '0' from the card
   !        negate (1 for '0', 0 for all other cards)
      _     If card was '0', move West
    #v        and go South to print '10'
            Else continue to go East

  ;1'< ^,;  Prints '10'

     <      Go West
   1'       Push '1'
  ;         Jump to ...
         ;  ... the next semicolon
        ,   Print '1'
       ^    Go back to the main loop
            The '0' will be printed by the main loop

Termination:

@; ... ;,k2"J J" <

                 <  Go West
           "J J"    Push "J J"
        ,k2         Print it
       ;            Jump to ...
 ;                  ... the next semicolon
@                   terminate the program
\$\endgroup\$
7
\$\begingroup\$

R, 65 bytes

c(outer(c('A',2:10,J<-'J','Q','K'),c('S','D','H','C'),paste),J,J)

Try it online!

-2 Bytes thanks to @Giuseppe and @JayCe suggestions

\$\endgroup\$
6
  • 1
    \$\begingroup\$ This was my exact answer, although I think I used double quotes instead of single quotes. \$\endgroup\$
    – Giuseppe
    Commented Jun 11, 2018 at 12:44
  • \$\begingroup\$ Sorry, have I double-posted your answer ? I checked but I couldn't find any R answer... \$\endgroup\$
    – digEmAll
    Commented Jun 11, 2018 at 13:14
  • \$\begingroup\$ Oh no I had just typed it all up myself in TIO before seeing yours :-) \$\endgroup\$
    – Giuseppe
    Commented Jun 11, 2018 at 13:26
  • 1
    \$\begingroup\$ Yes outer is definitely the way to go here! Although whitespace is premitted as per spec so you can save one byte by using paste. @Giuseppe too. \$\endgroup\$
    – JayCe
    Commented Jun 11, 2018 at 15:00
  • \$\begingroup\$ I pointed this out on JayCe's answer, but you can set x<-'J' inside the outer and then re-use x as a variable in the outermost c() statement to shave off a byte: Try it online! \$\endgroup\$
    – Giuseppe
    Commented Jun 11, 2018 at 18:03
6
\$\begingroup\$

Powershell, 63 61 59 56 bytes

-3 bytes: thanks ConnorLSW

2..10+'AJQK'[0..3]|%{"$_`H";"$_`S";"$_`D";"$_`C"};,'J'*2
\$\endgroup\$
3
  • 1
    \$\begingroup\$ -3 using string to array for the first part : 2..10+'AJQK'[0..4]|%{"$_`H";"$_`S";"$_`D";"$_`C"};,'J'*2 \$\endgroup\$
    – colsw
    Commented Jun 11, 2018 at 14:43
  • 1
    \$\begingroup\$ cool! any idea how to remove ````? \$\endgroup\$
    – mazzy
    Commented Jun 11, 2018 at 14:57
  • 1
    \$\begingroup\$ as far as I know that's the cheapest way to break up a variable in a string, I took a look at a loop but it's more expensive by 2 chars to do it that way. \$\endgroup\$
    – colsw
    Commented Jun 11, 2018 at 14:59
6
\$\begingroup\$

Perl 5, 41 bytes

sub{J,J,map<"$_"{S,D,H,C}>,A,2..10,J,Q,K}

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This has taught me something really interesting about globbing. Nice. \$\endgroup\$ Commented Jun 12, 2018 at 23:33
5
\$\begingroup\$

Python 3, 67 64 bytes

print(*[a+b for a in['10',*'A23456789JQK']for b in'CHSD'],*'JJ')

Try it online!


Python 2, 78 76 74 68 bytes

print['1'*(a<'1')+a+b for a in'A234567890JQK'for b in'CHSD']+['J']*2

Try it online!

Alt:

Python 2, 68 bytes

print[a+b for a in['10']+list('A23456789JQK')for b in'CHSD']+['J']*2

print['1'[a>'0':]+a+b for a in'A234567890JQK'for b in'CHSD']+['J']*2

saved

  • -3 bytes, thanks to ovs
\$\endgroup\$
3
  • \$\begingroup\$ That's a pretty thorough answer... \$\endgroup\$
    – AJFaraday
    Commented Jun 11, 2018 at 9:15
  • \$\begingroup\$ 64 bytes \$\endgroup\$
    – ovs
    Commented Jun 11, 2018 at 11:07
  • \$\begingroup\$ If we don't have to use print, can't we do [*[a+b for a in['10',*'A23456789JQK']for b in'CHSD'],*'JJ'] in 59 bytes. The Haskell solution doesn't need to output to stdout, why would Python? \$\endgroup\$ Commented Jul 4, 2018 at 2:38
5
\$\begingroup\$

K (ngn/k), 30 bytes

"JJ",/("AKQJ",$2+!9),'/:"SHDC"

Try it online!

!9 is the list 0 1 ... 8

2+!9 is the list 2 3 ... 10

$ to string

, concatenate

,'/: concatenate each with each right, i.e. Cartesian product; normally it's ,/:\: or ,\:/: but on the right we have only scalars ("SHDC"), so we can use ' instead of \:

"JJ",/ join (concat reduce) the list on the right using "JJ" as initial value for the reduction

\$\endgroup\$
5
\$\begingroup\$

MS-SQL, 137 bytes

SELECT v.value+s.value
FROM STRING_SPLIT('A-2-3-4-5-6-7-8-9-10-J-Q-K','-')v,STRING_SPLIT('H-S-D-C--','-')s
WHERE s.value>''OR v.value='J'

An array of values in SQL is returned as separate query rows. Uses the STRING_SPLIT function introduced in SQL 2016.

It includes the jokers by adding two "blank string" suits to take advantage of the existing "J" for Jack, then filtering out rows we don't want. Shorter than using UNION ALL statements to add the jokers.

\$\endgroup\$
2
  • \$\begingroup\$ What happens if you make the second argument for STRING_SPLIT an empty string? I don’t use MS-SQL, but in many languages it would give you all the characters in an array. \$\endgroup\$
    – AJFaraday
    Commented Jun 12, 2018 at 7:15
  • \$\begingroup\$ @AJFaraday That would be handy, but it doesn't work, you get Procedure expects parameter 'separator' of type 'nchar(1)/nvarchar(1)'. It would also be handy for golfing if it defaulted to a comma if you left out the second parameter, but it doesn't support that either. Another item for the GolfSQL language I'll probably never get around to writing :) \$\endgroup\$
    – BradC
    Commented Jun 12, 2018 at 13:29
4
\$\begingroup\$

Ruby, 61 bytes

->{[*0..52,52].map{|x|['JAKQ'[w=x%13]||w-2,'SDHC'[x/13]]*''}}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ That's some skill to use the J for both Jack and Joker :-) \$\endgroup\$
    – Marty Neal
    Commented Jun 14, 2018 at 15:37
  • \$\begingroup\$ Can't you save 4 bytes by leaving out the ->{...}? It runs on its own and doesn't need parameters, so it shouldn't need to be a lambda \$\endgroup\$
    – Piccolo
    Commented Aug 10, 2018 at 1:27
4
\$\begingroup\$

C# .NET, 114 bytes

o=>(new System.Text.RegularExpressions.Regex("1?.").Replace("AJQK2345678910","$0H,$0S,$0D,$0C,")+"J,J").Split(',')

Port of my Java answer (credit to @mazzy).

Try it online.


Interesting alternative of 119 bytes by @Corak.

using System.Linq;o=>new[]{"J","J"}.Concat(from s in"SDCH"from n in"A234567890JQK"select(n=='0'?"10":n+"")+s).ToArray()

Try it online.

If an System.Collections.Generic.IEnumerable<string> instead of string[] is an acceptable output, the trailing .ToArray() can be dropped so it becomes 109 bytes.

Explanation:

using System.Linq;       // Required import for the `from .. in ..` and `select` parts
o=>                      // Method with empty unused parameter and string-array return-type
  new[]{"J","J"}         //  Return a string-array containing two times "J"
   .Concat(              //  And add:
     from s in"SDCH"     //   Loop over the suits
       from n in"A234567890JQK"
                         //    Inner loop over the cards
         select(n=='0'?  //     If the current card item is '0'
                 "10"    //      Use 10 instead
                :        //     Else:
                 n+"")   //      Simply use the card item as is
                      +s)//     And append the suit
   .ToArray()            //  Convert the IEnumerable to an array
\$\endgroup\$
4
\$\begingroup\$

PHP, 108 99 97 Bytes

Try it online!

Try it online! (Edit 1)

Code

<?php $r=[J,J];foreach([A,J,Q,K,2,3,4,5,6,7,8,9,10]as$t)
$r=array_merge($r,[H.$t,S.$t,D.$t,C.$t]);

Tried to use purely php funcions, but bytecount was lower with a loop :(

Output (using print_r)

Array
(
[0] => J
[1] => J
[2] => HA
[3] => SA
[4] => DA
[5] => CA
[6] => HJ
[7] => SJ
[8] => DJ
[9] => CJ
[10] => HQ
[11] => SQ
[12] => DQ
[13] => CQ
[14] => HK
[15] => SK
[16] => DK
[17] => CK
[18] => H2
[19] => S2
[20] => D2
[21] => C2
[22] => H3
[23] => S3
[24] => D3
[25] => C3
[26] => H4
[27] => S4
[28] => D4
[29] => C4
[30] => H5
[31] => S5
[32] => D5
[33] => C5
[34] => H6
[35] => S6
[36] => D6
[37] => C6
[38] => H7
[39] => S7
[40] => D7
[41] => C7
[42] => H8
[43] => S8
[44] => D8
[45] => C8
[46] => H9
[47] => S9
[48] => D9
[49] => C9
[50] => H10
[51] => S10
[52] => D10
[53] => C10
)

Edit

Thanks to @JoKing by suggesting change explode(" ","H$t S$t D$t C$t") for [H.$t,S.$t,D.$t,C.$t]

\$\endgroup\$
6
  • \$\begingroup\$ Wouldn't [H.$t,S.$t,D.$t,C.$t] be shorter than explode? \$\endgroup\$
    – Jo King
    Commented Jun 12, 2018 at 11:00
  • \$\begingroup\$ You are absolutely right, gonna take your advise, thank you. \$\endgroup\$ Commented Jun 12, 2018 at 13:49
  • 1
    \$\begingroup\$ 73 bytes: Try it online! \$\endgroup\$
    – Night2
    Commented Jul 22, 2018 at 9:56
  • 2
    \$\begingroup\$ @FranciscoHahn, sorry, I didn't notice that! Here is a 87 bytes version then: Try it online! \$\endgroup\$
    – Night2
    Commented Jul 23, 2018 at 17:09
  • 1
    \$\begingroup\$ @Nigth2 i had no idea you can use array_push($arr, $item1,$item2,$item3...$itemN) like that, nice one \$\endgroup\$ Commented Jul 23, 2018 at 17:13
4
\$\begingroup\$

SMBF, 169 bytes

represents a literal NUL byte \x00.

<[.<]␀J J HA HK HQ HJ H01 H9 H8 H7 H6 H5 H4 H3 H2 SA SK SQ SJ S01 S9 S8 S7 S6 S5 S4 S3 S2 DA DK DQ DJ D01 D9 D8 D7 D6 D5 D4 D3 D2 CA CK CQ CJ C01 C9 C8 C7 C6 C5 C4 C3 C2

This is the naive solution.

Because this program contains a NUL byte, there's no easy way to use TIO. Run this in the Python interpreter using

data = bytearray(b'<[.<]\x00J J HA HK HQ HJ H01 H9 H8 H7 H6 H5 H4 H3 H2 SA SK SQ SJ S01 S9 S8 S7 S6 S5 S4 S3 S2 DA DK DQ DJ D01 D9 D8 D7 D6 D5 D4 D3 D2 CA CK CQ CJ C01 C9 C8 C7 C6 C5 C4 C3 C2')
\$\endgroup\$
3
  • \$\begingroup\$ TIO \$\endgroup\$ Commented Aug 7, 2018 at 15:24
  • \$\begingroup\$ @EriktheOutgolfer How did you generate that? \$\endgroup\$
    – mbomb007
    Commented Aug 7, 2018 at 15:43
  • 1
    \$\begingroup\$ I opened the F12 developer tools, selected the text box by hovering, went to the console and, after I copied and pasted the part before the null byte, typed $0.value+='\0'. \$\endgroup\$ Commented Aug 7, 2018 at 16:07
4
\$\begingroup\$

Japt, 32 30 26 bytes

'J²¬c"JQKA"¬c9õÄ)ï+"CHSD"q

Test it

'J²¬c"JQKA"¬c9õÄ)ï+"CHSD"q
'J                             :Literal "J"
  ²                            :Repeat twice
   ¬                           :Split
    c                          :Concatenate
     "JQKA"¬                   :  Split "JQKA"
            c                  :  Concatenate
             9õ                :    Range [1,9]
               Ä               :    Add 1 to each
                )              :  End concatenation
                 ï             :  Cartesian Product
                   "CHSD"q     :    Split "CHSD"
                  +            :  Join each pair to a string
\$\endgroup\$
3
\$\begingroup\$

Batch, 118 bytes

@for %%s in (C D H S)do @(for /l %%r in (2,1,10)do @echo %%r%%s)&for %%r in (J Q K A)do @echo %%r%%s
@echo J
@echo J
\$\endgroup\$
3
\$\begingroup\$

J, 41 bytes

'J';^:2,'HCDS',&.>/~(":&.>2+i.9),;/'AJQK'

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 'J';^:2,;&.>{_13<\'10';;/'23456789AJKQHCDS' slightly simpler but for 43. I feel like there's more to shave here... but I can't see how. \$\endgroup\$
    – Jonah
    Commented Jun 12, 2018 at 5:26
  • \$\begingroup\$ @Jonah Yes, I also tried something similar and I'm sure my solution can be golfed further, but there are always new problems to be solved :) \$\endgroup\$ Commented Jun 12, 2018 at 6:27
3
\$\begingroup\$

R, 67 66 bytes

c(paste(rep(c('A',2:10,J<-'J','Q','K'),4),c('S','D','H','C')),J,J)

Try it online!

Only one more byte than digEmAll's golfier solution. Inspired byGiuseppe's solution to this related challenge - the same Giuseppe who golfed one byte off by answer!

I'm posting separately since it's a slightly different approach taking advantage of the fact that 4 is not a divisor of 13 and that the output does not need to be in any particular order.

\$\endgroup\$
4
  • \$\begingroup\$ oh haha I didn't notice this, I commented this solution on the other one \$\endgroup\$
    – Giuseppe
    Commented Jun 11, 2018 at 16:48
  • \$\begingroup\$ @Giuseppe I was very close to comment instead of posting myself. I guess I just couldn't resist the urge to answer :) \$\endgroup\$
    – JayCe
    Commented Jun 11, 2018 at 17:35
  • \$\begingroup\$ iiiit's just as golfy as digemall's answer :-) try it online \$\endgroup\$
    – Giuseppe
    Commented Jun 11, 2018 at 17:44
  • \$\begingroup\$ @I was just experimenting with that :) so both digemall and I can golf one byte thanks to you :) \$\endgroup\$
    – JayCe
    Commented Jun 11, 2018 at 17:47
3
\$\begingroup\$

C (gcc), 126 137 133 bytes

#define S(X)"A"X,"2"X,"3"X,"4"X,"5"X,"6"X,"7"X,"8"X,"9"X,"10"X,"J"X,"Q"X,"K"X,
#define c (char*[54]){S("S")S("D")S("H")S("C")"J","J"}

Try it online!

+11 bytes to be more complete thanks to Jakob.

-4 bytes thanks to Zachary

Mostly abusing of preprocessor to compress the suits. Could probably be out-golfed, but is pretty efficient all things considered.

\$\endgroup\$
14
  • 1
    \$\begingroup\$ I would argue that c is not a "[...] weird 'function' [...]" but rather an array declaration stored in a macro. Such a form of output is to my knowledge not permitted by default. \$\endgroup\$ Commented Jun 11, 2018 at 15:19
  • 1
    \$\begingroup\$ Except c is not an array, it would be an in-place literal. Each 'invokation' of c in the source will create a new copy of the array in the resulting executable (barring optimizations). Thus, the code char *a[] = c; char *b[] = c; create two deep copies of c. This behaviour is what you'd expect of a function as well. \$\endgroup\$
    – LambdaBeta
    Commented Jun 11, 2018 at 15:21
  • 1
    \$\begingroup\$ If there was a challenge to output the integer zero, would you consider the C code c=0; a valid submission, viewing c as a function? \$\endgroup\$ Commented Jun 11, 2018 at 15:34
  • 1
    \$\begingroup\$ no, but I would consider #define z 0 valid. The reasoning is that: c=0; c++; x=c; results in x == 1 so c doesn't act much like a function. Meanwhile #define z 0 c=z; c++; x=z; does result in x == 0 so z acts like a function. \$\endgroup\$
    – LambdaBeta
    Commented Jun 11, 2018 at 15:36
  • 1
    \$\begingroup\$ I do not think that is a fair comparision as you first change the supposed function, then only change the supposed function's return value. z=0;/**/c=z;c++;x=z; results in x==0, thus z acts like a function. \$\endgroup\$ Commented Jun 11, 2018 at 15:41
3
\$\begingroup\$

Javascript (ES6) 77 74 72 bytes

This is a complete program.

`J,J,${[..."A23456789JQK",10].map(c=>[..."SDHC"].map(s=>c+s))}`.split`,`
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Nice. You can save a few bytes by splitting on commas instead of concatenating: BTJ,J,${[..."A23456789JQK","10"].map(c=>[..."SDHC"].map(s=>c+s))}BT.splitBT,BT (where BT is a backtick). \$\endgroup\$ Commented Jun 11, 2018 at 17:19
  • \$\begingroup\$ Awesome suggestion! Thanks. \$\endgroup\$
    – MattH
    Commented Jun 11, 2018 at 17:20
3
\$\begingroup\$

C (gcc, clang), 138 bytes

#define p(c)putchar(c)
int main(){for(char*q,*r="CDHS";*r;r++)for(q="A234567890JQK";*q;q++)(48==*q)&&p(49),p(*q),p(*r),p(32);puts("J J");}

Approach is to encode the sequences in character arrays.

Sample Output

AC AD AH AS 2C 2D 2H 2S 3C 3D 3H 3S 4C 4D 4H 4S 5C 5D 5H 5S 6C 6D 6H 6S 7C 7D 7H 7S 8C 8D 8H 8S 9C 9D 9H 9S 10C 10D 10H 10S JC JD JH JS QC QD QH QS KC KD KH KS J J

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Hint: you can use a single printf to get rid of all putchars. \$\endgroup\$
    – Dennis
    Commented Jun 11, 2018 at 21:57
  • 1
    \$\begingroup\$ 127 bytes. \$\endgroup\$ Commented Jun 11, 2018 at 22:00
  • 1
    \$\begingroup\$ Regarding your method of output, the OP does not allow printing. \$\endgroup\$ Commented Jun 11, 2018 at 22:03
  • \$\begingroup\$ Building on Jonathan Frech and Dennis 113 bytes \$\endgroup\$
    – ceilingcat
    Commented Aug 5, 2019 at 5:04
3
\$\begingroup\$

Oracle SQL, 164 bytes

Not a golfing language but...

SELECT CASE WHEN LEVEL>52THEN'J'ELSE DECODE(MOD(LEVEL,13),1,'A',11,'J',12,'Q',0,'K',MOD(LEVEL,13))||SUBSTR('HDSC',CEIL(LEVEL/13),1)END FROM DUAL CONNECT BY LEVEL<55

Try it online - SQL Fiddle

\$\endgroup\$
4
  • 2
    \$\begingroup\$ I love that someone answered this with Oracle SQL \$\endgroup\$
    – AJFaraday
    Commented Jun 13, 2018 at 12:38
  • \$\begingroup\$ postgres nearly beats this with just a string 172 bytes, 'select 'AS,2S,3S,4S,5S,6S,7S,8S,9S,10S,JS,QS,KS,AD,2D,3D,4D,5D,6D,7D,8D,9D,10D,JD,QD,KD,AH,2H,3H,4H,5H,6H,7H,8H,9H,10H,JH,QH,KH,AC,2C,3C,4C,5C,6C,7C,8C,9C,10C,JC,QC,KC,J,J'' \$\endgroup\$
    – dwana
    Commented Jun 13, 2018 at 13:43
  • \$\begingroup\$ @dwana That is not an array of strings (or, since this is SQL, a set of rows). You would need to split the string into rows (or columns) to meet the question's specifications. If you want to do that as a separate PostgreSQL solution then I would like to see that. \$\endgroup\$
    – MT0
    Commented Jun 13, 2018 at 13:47
  • \$\begingroup\$ sorry, didn't see the array requirement \$\endgroup\$
    – dwana
    Commented Jun 13, 2018 at 13:53
3
\$\begingroup\$

Lua, 156 127 138 129 bytes

loadstring'r={"J","J"}for x=1,52 do p=x%4+1r[#r+1]=({"A",2,3,4,5,6,7,8,9,10,"J","Q","K"})[x%13+1]..("SDHC"):sub(p,p)end return r'

Try it online!

Based on Jo King's code. As he suggested in the comments, my original answer wasn't valid (I'm still learning how code golf works 😬), and linked a better and valid answer. Then I made it smaller.


Original solution (156 bytes):

r={}s="SDHC"c={"J","Q","K"}t=table.insert for x in s:gmatch"."do for y=1,13 do t(r,(y==1 and"A"or y>10 and c[y-10]or y)..x)end end for _=1,2 do t(r, "J")end
r={} -- initializes the result table
s="SDHC" -- initializes the suits' string, it's better to be a string because we're just looping through it
c={"J","Q","K"} -- initializes some of the special cards

t=table.insert -- stores the table.insert call inside the 't' variable

for x in s:gmatch"."do -- loops through the suits, calling each one 'x'
  for y=1,13 do -- 'y' is the current card

    t(r,(y==1 and"A"or y>10 and c[y-10]or y)..x) -- adds the card accompanied by the suit to the result ('r') table
      -- y==1 and"A"or y>10 and c[y-10]or y  means if we're at the first card, it's an "A", else if we're past the 10th card, it's a special card, else, it's the number itself

  end
end
for _=1,2 do t(r, "J")end -- loop 2 times, adding a "J" to the result table

I just want to say that I'm new to this Code Golf stuff, so if I'm doing something wrong, feel free to tell me. I know this answer isn't one of the smallest, I just wanted to challenge myself.

If you have suggestions for reducing my code you can say too. :)

\$\endgroup\$
6
  • 2
    \$\begingroup\$ Welcome to the site! \$\endgroup\$
    – Wheat Wizard
    Commented Jul 21, 2018 at 20:23
  • \$\begingroup\$ @WW thanks! Hahah :D \$\endgroup\$
    – Visckmart
    Commented Jul 21, 2018 at 21:34
  • \$\begingroup\$ What happens with the table r? Either you need to print it to STDOUT, or change this to a function and return r \$\endgroup\$
    – Jo King
    Commented Jul 22, 2018 at 9:03
  • \$\begingroup\$ I'm no expert with Lua, but here's a valid 142 byte anonymous function \$\endgroup\$
    – Jo King
    Commented Jul 22, 2018 at 9:37
  • \$\begingroup\$ 138 bytes \$\endgroup\$
    – Jo King
    Commented Jul 22, 2018 at 9:54
3
\$\begingroup\$

Perl 6,  43  42 bytes

{|(|(2..10),|<A J Q K>X~ <S D H C>),|<J J>}

Try it

{|(|(^9+2),|<A J Q K>X~ <S D H C>),|<J J>}

Try it from Jo King

Expanded:

{  # bare block lambda

    |(  # flatten into outer list

        |(  # flatten into left-side list for X~
          ^9 + 2
          # 0+2 ..^ 9+2
          #   2 ..^ 11
          #   2 ..  10
        ),
        |   # flatten into left-side list for X~

          < A J Q K >

      X~    # cross using &infix:« ~ » (string concatenation)

        <S D H C>

    ),
    |< J J > # flatten two `J`s into outer list
}
\$\endgroup\$
1
  • \$\begingroup\$ 41 bytes \$\endgroup\$
    – Jo King
    Commented Jan 10, 2019 at 20:09
3
\$\begingroup\$

QBasic 4.5, 114 142 127 bytes

dim r$(54)
FOR i=1TO 52
m=i MOD 13
r$(i)=MID$("JQKA2345678910",1+m,1-(m=12))+MID$("CDHS",(i-1)\13+1,1)
NEXT
r$(53)="J
r$(54)="J

Release Notes:

  • V1.0 Initial deploy
  • V1.1 Misread the challenge requirements, so switched to a more expensive array r$. All the rest of the code is pretty much the same.
  • V1.2 Suggestions from @TaylorScott lead to a complete rewrite saving 15 bytes!

Sample output

If we add this snippet to our code, we can see what is put into r$:

for i = 1 to ubound(r$)
?r$(i)
next

QC
KC
AC
2C
3C
4C
5C
6C
7C
8C
9C
10C
JC
QD
KD
AD
[... snip ...]
6S
7S
8S
9S
10S
JS
J
J

But how? Well, let me tell you:

dim r$(54)          ' create a 54-slot array for our results
FOR i=1TO 52        ' Loop through the numbers 1-52 in var i
m=i MOD 13          ' Take i mod 13 to select a value (saved as m)
                    ' , and (not saved) i intdiv 13 to get a suit
r$(i)=              ' assigns to slot i in the result array
  MID$("JQKA2345678910"  ' a substring from the string with all the values
  ,1+m                   ' from position 1-13 (13 % 13 = 0, but QBasic strings are 1-based.
                         ' Hence the + 1)
  ,1-(m=12))             ' taking 1 char by default, and 2 for the Ten
                         ' Note that m=12 evaluates to 0 for 0-11, and to -1 for 12
  + MID$("CDHS",(i-1)\13+1,1)  ' and take 1 char from the suits-string
NEXT
r$(53)="J           ' Then append 2 jokers
r$(54)="J           ' These strings are auto-closed by the QBasic compiler.
\$\endgroup\$
5
  • \$\begingroup\$ This does not meet the Array of strings requirement, and printing to STDOUT is explicitly forbidden \$\endgroup\$ Commented Jun 13, 2018 at 15:58
  • \$\begingroup\$ @TaylorScott Thanks for noticing. Fixed now, @ 30 bytes extra. \$\endgroup\$
    – steenbergh
    Commented Jun 13, 2018 at 16:29
  • 1
    \$\begingroup\$ I think that you should be able to get the bytecount down by dropping the a$ and b$ vars and using a line something like r$(i)=MID$("JQKA2345678910",1+m,1-(m=12))+MID$("CDHS",(i-1)\13+1,1) \$\endgroup\$ Commented Jun 13, 2018 at 18:31
  • 1
    \$\begingroup\$ @TaylorScott Thanks! That reshuffling in the values-string is particularly clever. Nice! \$\endgroup\$
    – steenbergh
    Commented Jun 13, 2018 at 18:49
  • \$\begingroup\$ This is longer than just PRINT "<output>" \$\endgroup\$
    – Tornado547
    Commented Jun 29, 2018 at 21:03
3
\$\begingroup\$

Pyth, 26 25 bytes

+ B\JsM*+tSTc"JQKA"1"CDHS

Saved a byte thanks to hakr14.
Try it here

Explanation

+ B\JsM*+tSTc"JQKA"1"CDHS
         tST                Get the range from 2 to 10...
        +   c"JQKA"1        ... plus the list ['J', 'Q', 'K', 'A'].
       *            "CDHS   Take the Cartesian product with the suits.
     sM                     Stick the ranks and suits together.
+ B\J                       Add the jokers.
\$\endgroup\$
3
  • \$\begingroup\$ +\J+\J and +*2]\J are equivalent and both 6 bytes, but the latter is considered better form, as it allows for up to 10 copies to be added before any additional bytes are needed. \$\endgroup\$
    – hakr14
    Commented Jun 13, 2018 at 2:05
  • \$\begingroup\$ @hakr14 True, but I'm not into code golf for the sake of writing the cleanest or most generalizable code. \$\endgroup\$
    – user48543
    Commented Jun 13, 2018 at 2:23
  • \$\begingroup\$ +\J+\J can be replaced with + B\J to save a byte. \$\endgroup\$
    – hakr14
    Commented Nov 8, 2018 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.