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.
67 Answers 67
JavaScript (ES6), (削除) 62 (削除ここまで) 60 bytes
f=n=>n>51?'JJ':[('JQKA'[k=n>>2]||k-2)+'CDHS'[n&3],...f(-~n)]
-
\$\begingroup\$ Smooth moves!!! \$\endgroup\$Code Whisperer– Code Whisperer2018年06月11日 14:46:47 +00:00Commented Jun 11, 2018 at 14:46
-
\$\begingroup\$ I didn't know spread syntax existed until I read this -- very cool \$\endgroup\$logic8– logic82018年06月15日 17:29:20 +00:00Commented Jun 15, 2018 at 17:29
-
1\$\begingroup\$ 9 bytes shorter \$\endgroup\$Digital Trauma– Digital Trauma2018年06月11日 17:34:52 +00:00Commented Jun 11, 2018 at 17:34
-
\$\begingroup\$ I did not think braces were nestable. Thanks. \$\endgroup\$glenn jackman– glenn jackman2018年06月11日 17:53:39 +00:00Commented 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– aschepler2018年06月13日 01:21:57 +00:00Commented Jun 13, 2018 at 1:21
-
1\$\begingroup\$ Technically brace expansion is not a glob pattern, it is its own special syntax \$\endgroup\$glenn jackman– glenn jackman2018年06月14日 03:19:30 +00:00Commented Jun 14, 2018 at 3:19
brainfuck, (削除) 200 (削除ここまで) 197 bytes
+[[<+>->++<]>]+<++++<<+++[->+++>+++++>+>+<<<<]>+.<<.>>.--<<<[->>+++<<]>>+>[<<.>.-.+>.<<.<<[>>>+.>.<<.<+<-]>[-<+>>>+<<]>>.>.<<.>+++++++++.>.<<.>+.>.<<.>++++++.>.,++++[-<-------->]>[[-<+>]>]<<[<]>>>]
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
-
\$\begingroup\$ That more than 2.5 time shorter than my solution. Well done. \$\endgroup\$The random guy– The random guy2018年06月12日 14:52:30 +00:00Commented Jun 12, 2018 at 14:52
05AB1E, (削除) 28 (削除ここまで) (削除) 27 (削除ここまで) (削除) 25 (削除ここまで) (削除) 24 (削除ここまで) 23 bytes
2TŸ.•-Ÿ•S«.•ôì•âJ„jjS«u
-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".
brainfuck, (削除) 550 (削除ここまで) 504 bytes
++++++++++[>+++>+++++>+++++++<<<-]>++>->[>+>+>+>+<<<<-]>----->++++.<<<<.>>>>.<<<<.>>>>>--->+++++++++++++<<<<<.-.+>>>>.<<<<<.>.-.+>>>>+.<<<<<.>.-.+>>>>++++.-----<<<<<.>.-.+>>>>>.<<<<<<.>>++++++++[-<+.>>>>.<<<<<.>.>>>>+.<<<<<.>.>>>>++++.-----<<<<<.>.>>>>>.<<<<<<.>>]>.>>.<<<<<.>>>>.>.<<<<<.>>>>+.>.<<<<<.>>>>++++++.>.<<<<<.>>>.>>+.<<<<<.>>>>.>.<<<<<.>>>>------.>.<<<<<.>>>>-.>.<<<<<.>>>.>>++++.<<<<<.>>>>.>.<<<<<.>>>>+.>.<<<<<.>>>>++++++.>.<<<<<.>>>.>>>.<<<<<<.>>>>.>>.<<<<<<.>>>>------.>>.<<<<<<.>>>>-.>>.
Java 10, (削除) 153 (削除ここまで) (削除) 151 (削除ここまで) (削除) 125 (削除ここまで) (削除) 77 (削除ここまで) 75 bytes
v->("AJQK2345678910".replaceAll("1?.","0ドルH,0ドルS,0ドルD,0ドルC,")+"J,J").split(",")
-28 bytes thanks to @OlivierGrégoire.
-50 bytes thanks to @mazzy.
Explanation:
v-> // Method with empty unused parameter and String-array return-type
"AJQK2345678910"
.replaceAll("1?.", // Replace every loose character (or "10" as single char)
"0ドルH,0ドルS,0ドルD,0ドルC,") // with "cH,cS,cD,cC,", where c is the character
+"J,J") // Append "J,J"
.split(",") // And split everything by commas
-
2\$\begingroup\$
v->{var r="J J";for(var s:"HSDC".split(""))for(var c:"A,2,3,4,5,6,7,8,9,10,J,Q,K".split(","))r+=" "+c+s;return r.split(" ");}(125 bytes) \$\endgroup\$Olivier Grégoire– Olivier Grégoire2018年06月11日 10:13:26 +00:00Commented Jun 11, 2018 at 10:13 -
3\$\begingroup\$ try
"AJQK2345678910".replace("1?.","0ドルH,0ドルS,0ドルD,0ドルC,")+"J,J"and split it. \$\endgroup\$mazzy– mazzy2018年06月11日 11:51:07 +00:00Commented Jun 11, 2018 at 11:51 -
1\$\begingroup\$ Smart @mazzy Thanks. And was already editing it, Olivier. :) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年06月11日 12:02:16 +00:00Commented Jun 11, 2018 at 12:02
-
2\$\begingroup\$ cool! I believe it is possible to remove 2 bytes more - brackets in the pattern. see regex101.com/r/aDbz9C/1 \$\endgroup\$mazzy– mazzy2018年06月11日 12:35:23 +00:00Commented Jun 11, 2018 at 12:35
-
1\$\begingroup\$ @mazzy Oh, didn't even knew
0ドルwas possible in Java. TIL, thanks. :) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年06月11日 12:54:12 +00:00Commented Jun 11, 2018 at 12:54
APL (Dyalog Unicode), 29 bytes
1 byte saved thanks to Probie by using 1+⍳9 instead of 1↓⍳10
'JJ',,'HCDS'∘.,⍨'AKJQ',⍕ ̈1+⍳9
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 of1+⍳9the numbers 2..10 (1 plus the range 1..9)
-
\$\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\$Kevin Cruijssen– Kevin Cruijssen2018年06月11日 11:31:05 +00:00Commented 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– user418052018年06月11日 11:39:25 +00:00Commented 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\$Kevin Cruijssen– Kevin Cruijssen2018年06月11日 11:51:01 +00:00Commented Jun 11, 2018 at 11:51
Befunge-98 (FBBI), 75 bytes
j4d*1-2k:d%4+1g:'0-!#v_>,d/1g,' ,!#v_3
CHSDA234567890JQK@;1'< ^,;,k2"J J" <
Program structure
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
R, 65 bytes
c(outer(c('A',2:10,J<-'J','Q','K'),c('S','D','H','C'),paste),J,J)
-2 Bytes thanks to @Giuseppe and @JayCe suggestions
-
1\$\begingroup\$ This was my exact answer, although I think I used double quotes instead of single quotes. \$\endgroup\$Giuseppe– Giuseppe2018年06月11日 12:44:37 +00:00Commented 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– digEmAll2018年06月11日 13:14:11 +00:00Commented Jun 11, 2018 at 13:14
-
\$\begingroup\$ Oh no I had just typed it all up myself in TIO before seeing yours :-) \$\endgroup\$Giuseppe– Giuseppe2018年06月11日 13:26:52 +00:00Commented 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– JayCe2018年06月11日 15:00:03 +00:00Commented Jun 11, 2018 at 15:00 -
\$\begingroup\$ I pointed this out on JayCe's answer, but you can set
x<-'J'inside theouterand then re-usexas a variable in the outermostc()statement to shave off a byte: Try it online! \$\endgroup\$Giuseppe– Giuseppe2018年06月11日 18:03:40 +00:00Commented Jun 11, 2018 at 18:03
Powershell, (削除) 63 (削除ここまで) (削除) 61 (削除ここまで) (削除) 59 (削除ここまで) 56 bytes
-3 bytes: thanks ConnorLSW
2..10+'AJQK'[0..3]|%{"$_`H";"$_`S";"$_`D";"$_`C"};,'J'*2
-
1\$\begingroup\$ -3 using string to array for the first part :
2..10+'AJQK'[0..4]|%{"$_`H";"$_`S";"$_`D";"$_`C"};,'J'*2\$\endgroup\$colsw– colsw2018年06月11日 14:43:25 +00:00Commented Jun 11, 2018 at 14:43 -
1\$\begingroup\$ cool! any idea how to remove ````? \$\endgroup\$mazzy– mazzy2018年06月11日 14:57:49 +00:00Commented 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– colsw2018年06月11日 14:59:30 +00:00Commented Jun 11, 2018 at 14:59
-
1\$\begingroup\$ This has taught me something really interesting about globbing. Nice. \$\endgroup\$AmbroseChapel– AmbroseChapel2018年06月12日 23:33:57 +00:00Commented Jun 12, 2018 at 23:33
Python 3, (削除) 67 (削除ここまで) 64 bytes
print(*[a+b for a in['10',*'A23456789JQK']for b in'CHSD'],*'JJ')
Python 2, (削除) 78 (削除ここまで) (削除) 76 (削除ここまで) (削除) 74 (削除ここまで) 68 bytes
print['1'*(a<'1')+a+b for a in'A234567890JQK'for b in'CHSD']+['J']*2
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
-
\$\begingroup\$ That's a pretty thorough answer... \$\endgroup\$AJFaraday– AJFaraday2018年06月11日 09:15:15 +00:00Commented Jun 11, 2018 at 9:15
-
-
\$\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\$Enrico Borba– Enrico Borba2018年07月04日 02:38:53 +00:00Commented Jul 4, 2018 at 2:38
K (ngn/k), 30 bytes
"JJ",/("AKQJ",2ドル+!9),'/:"SHDC"
!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
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.
-
\$\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– AJFaraday2018年06月12日 07:15:01 +00:00Commented 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 theGolfSQLlanguage I'll probably never get around to writing :) \$\endgroup\$BradC– BradC2018年06月12日 13:29:57 +00:00Commented Jun 12, 2018 at 13:29
-
\$\begingroup\$ That's some skill to use the J for both Jack and Joker :-) \$\endgroup\$Marty Neal– Marty Neal2018年06月14日 15:37:34 +00:00Commented 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– Piccolo2018年08月10日 01:27:58 +00:00Commented Aug 10, 2018 at 1:27
C# .NET, 114 bytes
o=>(new System.Text.RegularExpressions.Regex("1?.").Replace("AJQK2345678910","0ドルH,0ドルS,0ドルD,0ドルC,")+"J,J").Split(',')
Port of my Java answer (credit to @mazzy).
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()
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
PHP, (削除) 108 (削除ここまで) (削除) 99 (削除ここまで) 97 Bytes
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]
-
\$\begingroup\$ Wouldn't
[H.$t,S.$t,D.$t,C.$t]be shorter than explode? \$\endgroup\$Jo King– Jo King2018年06月12日 11:00:46 +00:00Commented Jun 12, 2018 at 11:00 -
\$\begingroup\$ You are absolutely right, gonna take your advise, thank you. \$\endgroup\$Francisco Hahn– Francisco Hahn2018年06月12日 13:49:06 +00:00Commented Jun 12, 2018 at 13:49
-
1\$\begingroup\$ 73 bytes: Try it online! \$\endgroup\$Night2– Night22018年07月22日 09:56:59 +00:00Commented 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– Night22018年07月23日 17:09:32 +00:00Commented 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\$Francisco Hahn– Francisco Hahn2018年07月23日 17:13:35 +00:00Commented Jul 23, 2018 at 17:13
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')
-
\$\begingroup\$ TIO \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2018年08月07日 15:24:13 +00:00Commented Aug 7, 2018 at 15:24
-
\$\begingroup\$ @EriktheOutgolfer How did you generate that? \$\endgroup\$mbomb007– mbomb0072018年08月07日 15:43:40 +00:00Commented 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\$Erik the Outgolfer– Erik the Outgolfer2018年08月07日 16:07:33 +00:00Commented Aug 7, 2018 at 16:07
Japt, (削除) 32 (削除ここまで) (削除) 30 (削除ここまで) 26 bytes
'J2¬c"JQKA"¬c9õÄ)ï+"CHSD"q
'J2¬c"JQKA"¬c9õÄ)ï+"CHSD"q
'J :Literal "J"
2 :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
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
-
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– Jonah2018年06月12日 05:26:00 +00:00Commented 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\$Galen Ivanov– Galen Ivanov2018年06月12日 06:27:16 +00:00Commented Jun 12, 2018 at 6:27
R, (削除) 67 (削除ここまで) 66 bytes
c(paste(rep(c('A',2:10,J<-'J','Q','K'),4),c('S','D','H','C')),J,J)
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.
-
\$\begingroup\$ oh haha I didn't notice this, I commented this solution on the other one \$\endgroup\$Giuseppe– Giuseppe2018年06月11日 16:48:28 +00:00Commented 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– JayCe2018年06月11日 17:35:31 +00:00Commented Jun 11, 2018 at 17:35
-
\$\begingroup\$ iiiit's just as golfy as digemall's answer :-) try it online \$\endgroup\$Giuseppe– Giuseppe2018年06月11日 17:44:27 +00:00Commented 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– JayCe2018年06月11日 17:47:39 +00:00Commented Jun 11, 2018 at 17:47
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"}
+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.
-
1\$\begingroup\$ I would argue that
cis 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\$Jonathan Frech– Jonathan Frech2018年06月11日 15:19:57 +00:00Commented Jun 11, 2018 at 15:19 -
1\$\begingroup\$ Except c is not an array, it would be an in-place literal. Each 'invokation' of
cin the source will create a new copy of the array in the resulting executable (barring optimizations). Thus, the codechar *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– LambdaBeta2018年06月11日 15:21:41 +00:00Commented 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, viewingcas a function? \$\endgroup\$Jonathan Frech– Jonathan Frech2018年06月11日 15:34:33 +00:00Commented Jun 11, 2018 at 15:34 -
1\$\begingroup\$ no, but I would consider
#define z 0valid. The reasoning is that:c=0; c++; x=c;results inx == 1socdoesn't act much like a function. Meanwhile#define z 0 c=z; c++; x=z;does result inx == 0sozacts like a function. \$\endgroup\$LambdaBeta– LambdaBeta2018年06月11日 15:36:15 +00:00Commented 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 inx==0, thuszacts like a function. \$\endgroup\$Jonathan Frech– Jonathan Frech2018年06月11日 15:41:30 +00:00Commented Jun 11, 2018 at 15:41
Javascript (ES6) (削除) 77 (削除ここまで) (削除) 74 (削除ここまで) 72 bytes
This is a complete program.
`J,J,${[..."A23456789JQK",10].map(c=>[..."SDHC"].map(s=>c+s))}`.split`,`
-
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\$Rick Hitchcock– Rick Hitchcock2018年06月11日 17:19:07 +00:00Commented Jun 11, 2018 at 17:19 -
\$\begingroup\$ Awesome suggestion! Thanks. \$\endgroup\$MattH– MattH2018年06月11日 17:20:44 +00:00Commented Jun 11, 2018 at 17:20
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
-
\$\begingroup\$ Hint: you can use a single
printfto get rid of allputchars. \$\endgroup\$Dennis– Dennis2018年06月11日 21:57:56 +00:00Commented Jun 11, 2018 at 21:57 -
1\$\begingroup\$ 127 bytes. \$\endgroup\$Jonathan Frech– Jonathan Frech2018年06月11日 22:00:05 +00:00Commented Jun 11, 2018 at 22:00
-
1\$\begingroup\$ Regarding your method of output, the OP does not allow printing. \$\endgroup\$Jonathan Frech– Jonathan Frech2018年06月11日 22:03:45 +00:00Commented Jun 11, 2018 at 22:03
-
\$\begingroup\$ Building on Jonathan Frech and Dennis 113 bytes \$\endgroup\$ceilingcat– ceilingcat2019年08月05日 05:04:12 +00:00Commented Aug 5, 2019 at 5:04
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
-
2\$\begingroup\$ I love that someone answered this with Oracle SQL \$\endgroup\$AJFaraday– AJFaraday2018年06月13日 12:38:22 +00:00Commented 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– dwana2018年06月13日 13:43:17 +00:00Commented 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– MT02018年06月13日 13:47:50 +00:00Commented Jun 13, 2018 at 13:47
-
\$\begingroup\$ sorry, didn't see the array requirement \$\endgroup\$dwana– dwana2018年06月13日 13:53:32 +00:00Commented Jun 13, 2018 at 13:53
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'
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. :)
-
2\$\begingroup\$ Welcome to the site! \$\endgroup\$2018年07月21日 20:23:02 +00:00Commented Jul 21, 2018 at 20:23
-
\$\begingroup\$ @WW thanks! Hahah :D \$\endgroup\$Visckmart– Visckmart2018年07月21日 21:34:15 +00:00Commented 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 returnr\$\endgroup\$Jo King– Jo King2018年07月22日 09:03:40 +00:00Commented 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– Jo King2018年07月22日 09:37:26 +00:00Commented Jul 22, 2018 at 9:37
-
Perl 6, (削除) 43 (削除ここまで) 42 bytes
{|(|(2..10),|<A J Q K>X~ <S D H C>),|<J J>}
{|(|(^9+2),|<A J Q K>X~ <S D H C>),|<J J>}
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
}
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.
-
\$\begingroup\$ This does not meet the Array of strings requirement, and printing to
STDOUTis explicitly forbidden \$\endgroup\$Taylor Raine– Taylor Raine2018年06月13日 15:58:40 +00:00Commented Jun 13, 2018 at 15:58 -
\$\begingroup\$ @TaylorScott Thanks for noticing. Fixed now, @ 30 bytes extra. \$\endgroup\$steenbergh– steenbergh2018年06月13日 16:29:19 +00:00Commented Jun 13, 2018 at 16:29
-
1\$\begingroup\$ I think that you should be able to get the bytecount down by dropping the
a$andb$vars and using a line something liker$(i)=MID$("JQKA2345678910",1+m,1-(m=12))+MID$("CDHS",(i-1)13円+1,1)\$\endgroup\$Taylor Raine– Taylor Raine2018年06月13日 18:31:04 +00:00Commented Jun 13, 2018 at 18:31 -
1\$\begingroup\$ @TaylorScott Thanks! That reshuffling in the values-string is particularly clever. Nice! \$\endgroup\$steenbergh– steenbergh2018年06月13日 18:49:04 +00:00Commented Jun 13, 2018 at 18:49
-
\$\begingroup\$ This is longer than just
PRINT "<output>"\$\endgroup\$Tornado547– Tornado5472018年06月29日 21:03:12 +00:00Commented Jun 29, 2018 at 21:03
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.
-
\$\begingroup\$
+\J+\Jand+*2]\Jare 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– hakr142018年06月13日 02:05:39 +00:00Commented 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– user485432018年06月13日 02:23:03 +00:00Commented Jun 13, 2018 at 2:23
-
\$\begingroup\$
+\J+\Jcan be replaced with+ B\Jto save a byte. \$\endgroup\$hakr14– hakr142018年11月08日 16:45:01 +00:00Commented Nov 8, 2018 at 16:45
2or4or anything else so it isn't a prime anymore. \$\endgroup\$