Which Card is it?

30

2

Introduction

Long ago, when I used to code card games with usual playing cards, I used to specify a number for each card and call a function with some number to get a card. This somewhat inspired me to make this challenge.

So for the people unaware of the playing cards, a deck of cards consist of 52 cards (13 in each of the four suits, i.e, Hearts, Diamonds, Spades, Clubs). In each suit, there are 13 cards - firstly the cards numbered from 2-10, then the Jack(J), Queen(Q), King(K) and the Ace(A). This is the order

Challenge

The challenge is to take an integer between 1-52 as input and display the card at that position. But, your output must be in words. Also, order must be maintained, i.e, first 13 cards will be of Hearts, then Diamonds, then Spades and finally Clubs.

For example, if someone chooses the number 30.The card would then belong to the third suit, i.e, the Spades. Also, it would be the fourth card in the suit, which means the number 5. Hence your output in words must be: five of spades and it should always follow this format, i.e, first the card, followed by an of and the name of the suit at the end, with required spaces in between.

Input And Output

The input will be an integer between 1-52 (both inclusive). Note that here counting starts from 1. You may choose to start from 0. However, you must maintain the order of the cards which is mentioned above. Your output should be the card at that position written in words. You do not need to handle invalid inputs. Also, your output may be in lower-case or in upper-case.

Given below is the list of all the possible inputs and their outputs:

1 -> two of hearts
2 -> three of hearts
3 -> four of hearts
4 -> five of hearts
5 -> six of hearts
6 -> seven of hearts
7 -> eight of hearts
8 -> nine of hearts
9 -> ten of hearts
10 -> jack of hearts
11 -> queen of hearts
12 -> king of hearts
13 -> ace of hearts
14 -> two of diamonds
15 -> three of diamonds
16 -> four of diamonds
17 -> five of diamonds
18 -> six of diamonds
19 -> seven of diamonds
20 -> eight of diamonds
21 -> nine of diamonds
22 -> ten of diamonds
23 -> jack of diamonds
24 -> queen of diamonds
25 -> king of diamonds
26 -> ace of diamonds
27 -> two of spades
28 -> three of spades
29 -> four of spades
30 -> five of spades
31 -> six of spades
32 -> seven of spades
33 -> eight of spades
34 -> nine of spades
35 -> ten of spades
36 -> jack of spades
37 -> queen of spades
38 -> king of spades
39 -> ace of spades
40 -> two of clubs
41 -> three of clubs
42 -> four of clubs
43 -> five of clubs
44 -> six of clubs
45 -> seven of clubs
46 -> eight of clubs
47 -> nine of clubs
48 -> ten of clubs
49 -> jack of clubs
50 -> queen of clubs
51 -> king of clubs
52 -> ace of clubs

Scoring

This is , so the shortest code wins.

Manish Kundu

Posted 2018-03-14T06:00:51.810

Reputation: 1 947

1Isn't the default order of suits usually hearts, spaces, diamonds, clubs (red, black, red, black). Not that it matters for the challenge, was just wondering why it's in this order. – Kevin Cruijssen – 2018-03-14T07:53:06.757

3It varies from game to game. Different games follow different orders. Also talking about cards, some games even have ace as the lowest card in the suit. – Manish Kundu – 2018-03-14T07:58:31.450

Can I output two\s\s\sof\shearts where \s stands for a space? (Note the two extra space.) – totallyhuman – 2018-03-14T10:03:44.747

2@totallyhuman sorry but there must be exactly 1 space in between – Manish Kundu – 2018-03-14T10:19:24.800

Answers

31

Python 3,  115  90 bytes

from unicodedata import*
lambda n:name(chr(n%13+n%13//11+[6,0,4,2][-n//13]*8+127137))[13:]

An unnamed function returning the string in uppercase.

Try it online!

How?

Unicode characters have names. The names of some of these are like "PLAYING CARD TWO OF SPADES", hence we can get the characters of the Unicode character representing the required card and strip off the first 13 characters to get our output.

The Unicode characters of interest are within a block like so:

            0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
U+1F0Ax     x   As  2s  3s  4s  5s  6s  7s  8s  9s  Ts  Js  x   Qs  Ks  x
U+1F0Bx     x   Ah  2h  3h  4h  5h  6h  7h  8h  9h  Th  Jh  x   Qh  Kh  x
U+1F0Cx     x   Ad  2d  3d  4d  5d  6d  7d  8d  9d  Td  Jd  x   Qd  Kd  x
U+1F0Dx     x   Ac  2c  3c  4c  5c  6c  7c  8c  9c  Tc  Jc  x   Qc  Kc  x                           

Where the x are not characters we are after (the four in the C column are "knights"; three in F are "jokers"; one in 0 is generic; the rest are reserved characters).

As such we can add some value to 0x1F0A1 = 127137 (As) to find the card we want.

The value to add is only complicated by three things:

  1. We need to reorder the suits (from s,h,d,c to h,d,s,c)
  2. We need to reorder the ranks from (A,2,...,K to 2,...,K,A)
  3. We need to avoid the columns without cards of interest.

Using the one-indexing option allows the use of negative integer division to index into an array of row-wise offsets for the suit re-ordering with [6,0,4,2][-n//13]*8+ (effectively [48,0,32,16][-n//13]), we can then also place the aces into the correct locations with n%13+ and then avoid the knights in column C with n%13//11+ (effectively (n%13>10)+).

Jonathan Allan

Posted 2018-03-14T06:00:51.810

Reputation: 67 804

Just as I was starting an answer like this (I'm sure mine would have been longer) I glanced over and saw your answer. Nice one. – mbomb007 – 2018-03-14T19:38:38.037

...and yet another byte was there to be golfed :) – Jonathan Allan – 2018-03-14T19:51:24.597

13

Perl6/Rakudo 70 bytes

Index 0

Using perl6 -pe, and with no dictionary compression:

chr(''.ords[$_/13]+($_+1)%13*1.091).uniname.substr(13)

It just looks up the card in Unicode (starting from the Ace), asks for the name and uses that. This is a similar route (though I didn't know it at the time!) to Jonathan Aitken's Python answer - only I index from all 4 aces rather than 4 offsets from the Ace of Spades, and I multiply by 1.091 to make the index round away from the Knight entry in Unicode.

See all the output (for input values 0 to 51) https://glot.io/snippets/ez5v2gkx83

Edited to cope with Knights in the Unicode deck, because Unicode.

Perl6 ♥ Unicode

Phil H

Posted 2018-03-14T06:00:51.810

Reputation: 1 376

@JonathanAllan: It sets the order using the 4 base cards (they are in the required suit order), but well spotted on the Knights - I hadn't noticed that. Fixed at the cost of a few more chars. – Phil H – 2018-03-14T12:54:08.990

@JonathanAllan: There's some incorrectness in other answer's counting - everyone says 'bytes' when they mean chars. The ones with compression are the most egregious offenders. – Phil H – 2018-03-14T13:05:45.277

3I think you'll find that the ones with compression strings containing what is presented as Unicode actually have their own code-pages (this is certainly true for Jelly, Husk, Charcoal & 05AB1E). – Jonathan Allan – 2018-03-14T13:08:56.510

Thanks, I had not appreciated that at all. – Phil H – 2018-03-14T13:21:08.193

@PhilH If you doubt the byte count is correct, you can ask them to provide a hexdump. – user202729 – 2018-03-14T14:31:46.213

For your test you probably want for 0..^52 {…} (or for ^52 {…}) as you currently have an off-by-one error. – Brad Gilbert b2gills – 2018-03-14T15:00:57.717

Neat idea. I like your approach. – Makotosan – 2018-03-16T13:23:42.090

9

05AB1E, 54 bytes

0-indexed

“»€Å‹¡Šdesž…“#“‚•„í†ìˆÈŒšï¿Ÿ¯¥Š—¿—ÉŸÄ‹ŒÁà“#âí" of "ýsè

Try it online!

Explanation

“»€Å‹¡Šdesž…“#                                          # push list of suits
              “‚•„í†ìˆÈŒšï¿Ÿ¯¥Š—¿—ÉŸÄ‹ŒÁà“#             # push list of ranks
                                           â            # cartesian product
                                            í           # reverse each
                                             " of "ý    # join on " of "
                                                    sè  # index into cardlist with input

Emigna

Posted 2018-03-14T06:00:51.810

Reputation: 50 798

@PhilH 05AB1E uses a codepage, as do most of the answers in golfing languages here on PPCG

– dzaima – 2018-03-14T13:14:20.610

Apologies, had not realised this was so common. – Phil H – 2018-03-14T13:19:27.653

@PhilH eh, many have done exactly the same of thinking that the unicode shown is actually the submissions score. I would however like if it was standard here to always hyperlink the codepage in the title (like on my SOGL answer) – dzaima – 2018-03-14T13:22:45.210

@dzaima: I did that for a while, but I still got comments so I stopped. But I agree, it would be nice if it was included in the TIO template. – Emigna – 2018-03-14T14:04:07.267

LOL, I didn't look at this answer... “»€Å‹ spadesž…“#"of "ì“‚•„í†ìˆÈŒšï¿Ÿ¯¥Š—¿—ÉŸÄ‹ŒÁà“#âí» - 54 Bytes as Well!

– Magic Octopus Urn – 2018-03-15T11:24:49.957

I am stupid, you take in an integer, soo... with swap, a[b] that answer was the same. – Magic Octopus Urn – 2018-03-15T11:29:09.183

6

Python 2, 167 148 bytes

n=input();print 'two three four five six seven eight nine ten jack queen king ace'.split()[n%13]+' of '+['hearts','diamonds','spades','clubs'][n/13]

Zero-indexed.

Try It Online!

EDIT: Bubbler made a great point using the split method (and providing a shorter answer). On the second block using split() yields the same byte count.

PHC

Posted 2018-03-14T06:00:51.810

Reputation: 61

Welcome! By default submissions must handle input and output; see the Python rules summary.

– xnor – 2018-03-14T06:31:51.753

Got it, thanks for pointing out! – PHC – 2018-03-14T06:37:47.527

1141 bytes with lambda and split. Tried interleaving the chars for [n%13::13]or something, but no luck. – Bubbler – 2018-03-14T06:54:01.613

Thanks for making me realize that split would save some bytes. Another byte goes away with Python2's default integer division. – PHC – 2018-03-14T07:14:46.367

4140 bytes using percent notation to factor out s; xnor pointed it out in chat. – Bubbler – 2018-03-14T07:32:21.183

6

Emojicode, 202 bytes

itwo.three.four.five.six.seven.eight.nine.ten.jack.queen.king.ace.i 13 of hearts.diamonds.spades.clubs.➗i 13

0 indexed. Try it online!

Explanation:

		start of the closure block
  i		 closure takes an integer argument i
  		 print:
    		  concatenate these strings:
      ....i 13  [a]
       of 
      ....➗i 13  [b]
    


[a]:
		tell Emojicode to dereference without checking
		 get the nth element of the following array
  		  create an array using the following string and separator
    ...
    .
   i 13	n, i mod 13

[b]
....➗i 13
same but with ⌊i÷13⌋

betseg

Posted 2018-03-14T06:00:51.810

Reputation: 8 493

10Somehow it seems right that "dereferencing without checking" is a mug of beer. – maxathousand – 2018-03-14T13:51:24.410

6

R, 154 bytes

paste(el(strsplit("Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King,Ace",",")),"of",rep(c("Hearts","Diamonds","Spades","Clubs"),e=13))[scan()]

Try it online!

Takes input (1-indexed) from STDIN and with source(...,echo=T) will print the result to console.

It's not pretty, BUT it comes in 2 bytes shorter than the best solution I could using outer (presented below), so let this be a reminder to examine another approach!

paste(                          # concatenate together, separating by spaces,
                                # and recycling each arg to match the length of the longest
el(strsplit("Two,...",",")),    # split on commas and take the first element
"of",                           # 
 rep(c("Hearts",...),           # replicate the suits (shorter as a vector than using strsplit
               e=13)            # each 13 times
                    )[scan()]   # and take the input'th index.

R, 156 bytes

outer(el(strsplit("Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King,Ace",",")),c("Hearts","Diamonds","Spades","Clubs"),paste,sep=" of ")[scan()]

Try it online!

Essentially the same as above; however, outer will do the recycling properly, but having to set sep=" of " for the paste made this just a hair longer.

Giuseppe

Posted 2018-03-14T06:00:51.810

Reputation: 21 077

6

Excel, 156 bytes

=TRIM(MID("two  threefour five six  seveneightnine ten  jack queenking ace",1+MOD(A1,13)*5,5))&" of "&CHOOSE(1+(A1/13),"hearts","diamonds","spades","clubs")

Cards from 0-51. Unfortunately, Excel does not feature a function to convert 1 to "one"...

Using TRIM and MID is shorter than using CHOOSE for the face values, but longer than using CHOOSE for the Suit.

Chronocidal

Posted 2018-03-14T06:00:51.810

Reputation: 571

Clever with the MID() and combining the words! – BruceWayne – 2018-03-15T04:14:50.517

5

Java 8, 141 bytes

n->"two;three;four;five;six;seven;eight;nine;ten;jack;queen;king;ace".split(";")[n%13]+" of "+"hearts;diamonds;spades;clubs".split(";")[n/13]

Input is 0-indexed.

Explanation:

Try it online.

n->         // Method with integer parameter and String return-type
  "two;three;four;five;six;seven;eight;nine;ten;jack;queen;king;ace".split(";")[n%13]
            //  Take `n` modulo-13 as 0-indexed card value
   +" of "  //  append " of "
   +"hearts;diamonds;spades;clubs".split(";")[n/13]
            //  append `n` integer-divided by 13 as 0-indexed suit

Kevin Cruijssen

Posted 2018-03-14T06:00:51.810

Reputation: 67 575

4

Kotlin, 154 152 140 bytes

i->"two,three,four,five,six,seven,eight,nine,ten,jack,queen,king,ace".split(',')[i%13]+" of ${"heart,diamond,spade,club".split(',')[i/13]}s"

Try it online!

Updated to use just lambda expression.

Makotosan

Posted 2018-03-14T06:00:51.810

Reputation: 503

That's completely fine. – Nissa – 2018-03-15T13:51:59.143

2Welcome to PPCG! I was discouraged by the golfing languages at first, but then somewhere someone told me "It's really the best answer in each language wins" and I realized it was a competition against other (your lang here) golfers. Keep it up, and I hope you enjoy your time here. – Giuseppe – 2018-03-15T13:56:31.613

Lambdas in Kotlin (unlike Java) always have a leading { and a trailing }. So maybe you should include and count them in your solution? – Roland Schmitz – 2018-06-09T14:08:53.710

3

Stax, 58 57 56 bytes

î↑à■?R╢8«E▄¡╔ÿ•L╫<<⌠ï∞∟⌡♪Ös1"TàLα╥▀¢¡◄└%≈δñM;;}'░o=⌡»╬í√

Run and debug it

Here's the commented ungolfed representation of the same program. It uses stax's compressed literals heavily. The input is 0-indexed. It's Emigna's 05AB1E algorithm.

`SsUI'S~pTU5T@T^Ez+`j   suits
`fV:l7eTkQtL*L2!CZb6u[&YNO4>cNHn;9(`j   ranks
|*  cross-product
@   index with input
r   reverse pair
`%+(`*  join with " of "

Run this one

recursive

Posted 2018-03-14T06:00:51.810

Reputation: 8 616

3

JavaScript ES6, 124 118 Bytes, 0-index

F= x=>(h=btoa`O
?NÞ{ñhº¿Å÷¿J,IëÞñ"6)Þý7§üô.yéÿ*)àüÿÿÿæ«·÷bjj'wû)i׿r[`.split`/`)[x%13]+` of ${h[x/13|16]}s`

console.log (F(51))

Base64 version

eD0+KGg9YnRvYWBPCj9OGt578Wi6v8WK979KLH9J696f8SKCG382Kd79N6f8lpyT9C556f8qKeD8Bx7///+F5qu392Jqaid3+ylp179yW5tgLnNwbGl0YC9gKVt4JTEzXStgIG9mICR7aFt4LzEzfDE2XX1zYA==

l4m2

Posted 2018-03-14T06:00:51.810

Reputation: 5 985

online test seems broken – l4m2 – 2018-03-14T11:40:59.900

does not work in chrome – Luis felipe De jesus Munoz – 2018-03-14T12:28:57.143

works on Firefox @Luis felipe De jesus Munoz – l4m2 – 2018-03-14T12:55:02.897

Your 118 byte version measures 107 chars 136 bytes here: https://mothereff.in/byte-counter

– Phil H – 2018-03-14T13:10:47.027

1

@PhilH if you decode the given base64 of the code to a list of bytes (e.g. using this), you'll see that it actually results in the mentioned 118 bytes.

– dzaima – 2018-03-14T13:27:27.033

Can you please add a TIO link? – Shaggy – 2018-03-14T16:44:18.153

Not that directly. TIO don't have javascript with atob – l4m2 – 2018-03-15T03:49:33.267

Use CodePen or JSFiddle, so. – Shaggy – 2018-03-15T20:03:43.990

3

Husk, 52 bytes

;↔!Πmw¶¨×‼sÿẋδẎ₆ṡ⁷Ḃ6‰fωθ»&⌈θƒV₆x⁵▼Ëġ`nıEṅ'jĊk⁸"eïkÄc

Try it online!

I'm always happy to show off Husk's string compression system :D

Explanation

The majority of the program (from ¨ onwards) is obviously a compressed string. When uncompressed it turns into:

hearts diamonds spades clubs
of
two three four five six seven eight nine ten jack queen king ace

The program then is:

;↔!Πmw¶¨…
       ¨…    The previous string
      ¶      Split on lines
    mw       Split each line into words
             - we now have a list of lists of words
   Π         Cartesian product of the three lists
             - with this we obtain all possible combinations of suits and values
               with "of" between the two (e.g. ["spades","of","king"])
  !          Pick the combination at the index corresponding to the input
 ↔           Reverse it, so words are in the correct order
;            Wrap it in a list. Result: [["king","of","spades"]]

There are a couple of things left to explain:

  • We build the cards with suits before values because of how the cartesian product Π works: if we did it the other way around, the list of cards would be ordered by value (i.e. two of hearts, two of diamonds, two of spades, two of clubs, three of hearts...). As a consequence, we have to reverse our result.

  • The result of the program is a two-dimensional matrix of strings. This is automatically printed by Husk as a single string built by joining rows of the matrix with newlines and cells with spaces. The reason we build this matrix instead of using the more straightforward w (join a list of words with spaces) is that if using w the type inferencer guesses another interpretation for the program, producing a different result.

Leo

Posted 2018-03-14T06:00:51.810

Reputation: 8 482

3

Bash, 133 bytes

V=(two three four five six seven eight nine ten jack queen king ace hearts diamonds spades clubs)
echo ${V[$1%13]} of ${V[$1/13+13]}

Choosing to use 0 based as per the option given, supporting 0 (two of hearts) through 51 (ace of clubs)

crystalgecko

Posted 2018-03-14T06:00:51.810

Reputation: 51

Welcome to PPCG! – Martin Ender – 2018-03-14T14:41:55.003

2

Octave, 155 153 151 150 bytes

@(x)[strsplit(' of ,s,heart,diamond,spade,club,ace,two,three,four,five,six,seven,eight,nine,ten,jack,queen,king',','){[mod(x,13)+7,1,ceil(2+x/13),2]}]

Try it online!

This creates a string starting with ' of ' and 's', then all the suits followed by all the ranks. This string is split at commas into separate strings. The suits are before the ranks, because since that saves a byte when creating the indices. After this, we index it using square brackets with the following indices:

{[mod(x,13)+7,1,ceil(2+x/13),2]}

which is the rank, followed by the first element ' of ', followed by the suit, followed by 's'.

Having the 's' as part of the suits (hearts,diamonds,spades,clubs) instead of a separate string is the exact same length but less fun.

Splitting on the default separator would save 4 bytes in the strsplit-call, but the spaces around ' of ' would be removed and would have to be added manually, costing more bytes.

Stewie Griffin

Posted 2018-03-14T06:00:51.810

Reputation: 43 471

2

mIRCScript, 157 bytes

c echo $token(ace two three four five six seven eight nine ten jack queen king,$calc(1+$1% 13),32) of $token(clubs spades diamonds hearts,$calc(-$1// 13),32)

Load as an alias, then use: /c N. mIRC is 1-indexed, so floor division (//) on the negative value of the input produces -1 to -4 as required.

jaytea

Posted 2018-03-14T06:00:51.810

Reputation: 467

2

PowerShell, 207 192 182 174 165 163 161 157 bytes

0-Indexed

$args|%{(-split'two three four five six seven eight nine ten jack queen king ace')[$_%13]+' of '+('hearts','diamonds','spades','clubs')[$_/13-replace'\..*']}

Try it online!

4 bytes saved thanks to AdmBorkBork in the comments

Nik Weiss

Posted 2018-03-14T06:00:51.810

Reputation: 91

You can unary -split on whitespace to save 6 bytes -split'two three four five six seven eight nine ten jack queen king ace' and another byte using inline replace instead of floor $_/13-replace'\..*'

– AdmBorkBork – 2018-03-19T13:22:08.490

@AdmBorkBork Thanks for the tips! How are you getting 6 bytes from changing -split? I only see savings of 3 bytes. It seems to still need the parentheses, so I am just removing the ',' and re-ordering the rest. – Nik Weiss – 2018-03-19T14:25:17.720

I'm not sure how I came up with 6, it is indeed only a savings of 3, lol. – AdmBorkBork – 2018-03-19T14:37:35.877

2

V, 154 147 144 142 Bytes

-7 Bytes thanks to DJMcMayhem

13i1heart
2diamond
3spade
4club
ÚGxCtwo
three
four
five
six
seven
eight
nine
ten
jack
queen
king
aceH$A of 012j$d4ñ13jPñÍ «/ 
{ÀjYHVGpAs

Try it online!

Hexdump:

00000000: 3133 6931 6865 6172 740a 3264 6961 6d6f  13i1heart.2diamo
00000010: 6e64 0a33 7370 6164 650a 3463 6c75 620a  nd.3spade.4club.
00000020: 1bda 1647 7843 7477 6f0a 7468 7265 650a  ...GxCtwo.three.
00000030: 666f 7572 0a66 6976 650a 7369 780a 7365  four.five.six.se
00000040: 7665 6e0a 6569 6768 740a 6e69 6e65 0a74  ven.eight.nine.t
00000050: 656e 0a6a 6163 6b0a 7175 6565 6e0a 6b69  en.jack.queen.ki
00000060: 6e67 0a61 6365 1b16 4824 4120 6f66 201b  ng.ace..H$A of .
00000070: 3016 3132 6a24 6434 f131 336a 50f1 cd20  0.12j$d4.13jP.. 
00000080: ab2f 200a 7bc0 6a59 4856 4770 4173       ./ .{.jYHVGpAs

oktupol

Posted 2018-03-14T06:00:51.810

Reputation: 697

Here's the sort-shortcut: Try it online! Always happy to see someone new use V :)

– James – 2018-03-14T20:35:55.713

Here's some tips: 1) « == \+ 2) 12dj == 13D – James – 2018-03-14T20:42:28.387

Thanks! :) And how do I use ò? I tried ò13j0Pò instead of 4ñ13j0Pñ, but that didn't terminate – oktupol – 2018-03-14T20:52:20.110

I actually tried that too. I'm not sure why it doesn't terminate. Maybe it's because it doesn't hit the bottom because the P adds new lines? Also, are you sure you need the 0 in that part? It seems to me like it would probably work without – James – 2018-03-14T20:56:46.883

Oh, that's indeed the case. And you're right, the 0 is unnecessary – oktupol – 2018-03-14T21:02:56.670

2

C#, 219 207 202 197 bytes (0 indexed)

static string O(int i){string[]s={"two","three","four","five","six","seven","eight","nine","ten","jack","queen","king","ace","hearts","diamonds","spades","clubs"};return s[i%13]+" of "+s[i/14+13];}

thanks to input from @Ciaran_McCarthy and @raznagul

Takes an input of int I, subtracts 1 to match 0 indexing of the string array and outputs the number based on I mod 13 and the suit based on i/14+13.

works pretty well for my second code golf, just wondering if i could get it shorter using LINQ or something else.

James m

Posted 2018-03-14T06:00:51.810

Reputation: 41

2Down to 200 by removing the i--; and doing --i in the first array index instead (i gets decremented before the modulo, and stays like that for the following division), removing the ,"of" in the array (it's not needed?), and removing the brackets around the return statement and adding one whitespace between return and s[... – Ciaran_McCarthy – 2018-03-14T15:13:40.407

1

The challenge allows the input to be 0-indexed so the i++ can be removed completely. By converting the function to a lambda I got it down to 178 bytes.

– raznagul – 2018-03-15T10:26:42.533

2

Initally I came up with an answer for 163 bytes (see link above). I decided to not post it, because a 1 to 1 port of @KevinCruijssens Java answer will still be shorter. Maybe later I try to come up with a Linq answer just for the sake of having one. But I doubt it will be shorter. Especially because Linq starts with a 18 byte deficit for the using-Statement. Anyway +1 from me.

– raznagul – 2018-03-15T10:34:08.733

Thanks to both Ciaran_McCarthy an raznagul for your input, got it down to 202 now, let me know if you see anything else that can be additionally golfed – James m – 2018-03-15T11:46:33.773

1You still have the superfluous "of" in the array. – raznagul – 2018-03-15T12:05:28.700

just noticed that right now hahaha @raznagul fixed now :) – James m – 2018-03-15T12:10:50.047

I know it's been a while, but your answer is currently incorrect (the i/14 should be i/13). Also, you can use var s="two,three,four,five,six,seven,eight,nine,ten,jack,queen,king,ace,hearts,diamonds,spades,clubs".Split(','); to golf it to 151 bytes.

– Kevin Cruijssen – 2019-04-19T11:16:55.787

2

C (gcc), 148 bytes

f(n){printf("%.5s of %.7ss","two\0 threefour\0five\0six\0 seveneightnine\0ten\0 jack\0queenking\0ace"+n%13*5,"heart\0 diamondspade\0 club"+n/13*7);}

Try it online!

0-based.

gastropner

Posted 2018-03-14T06:00:51.810

Reputation: 3 264

You should be able to save 10 bytes by replacing the \0 with literal null bytes. – caird coinheringaahing – 2018-03-14T19:04:19.473

2

Haskell, 132 bytes

(!!)[v++" of "++s|s<-words"hearts diamonds spades clubs",v<-words"two three four five six seven eight nine ten jack queen king ace"]

Try it online!

An anonymous function, using list comprehension to build all the combinations of suit and value, and indexing into the resulting list with the input.

Leo

Posted 2018-03-14T06:00:51.810

Reputation: 8 482

2

F#, 174 168 bytes

Removed some extra whitespace as noted by Manish Kundu. Thanks!

let c x=["two";"three";"four";"five";"six";"seven";"eight";"nine";"ten";"jack";"queen";"king";"ace"].[(x-1)%13]+" of "+["hearts";"diamonds";"spades";"clubs"].[(x-1)/13]

Try it online!

I'll be honest - I'm new at code golf, so I don't know if it's more appropriate to answer with a pure function like this (with parameters, but no I/O) or with a working code block with user I/O.

Ciaran_McCarthy

Posted 2018-03-14T06:00:51.810

Reputation: 689

1-4 bytes by only removing un-necessary spaces – Manish Kundu – 2018-03-14T14:20:41.823

The whitespace completely got past me. Well spotted! Thanks very much! – Ciaran_McCarthy – 2018-03-14T14:31:48.737

1

CJam, 114 bytes

riDmd"two three four five six seven eight nine ten jack queen king ace"S/=" of "@"hearts diamonds spades clubs"S/=

Try it online!

Zero-indexed. Will probably be beaten by languages with dictionary compression, but oh well...

Esolanging Fruit

Posted 2018-03-14T06:00:51.810

Reputation: 13 542

1

Julia 0.6, 156 bytes

f(n)=print(split(" of ,hearts,diamonds,spades,clubs,two,three,four,five,six,seven,eight,nine,ten,jack,queen,king,ace",',')[[(n-1)%13+6,1,div(n-1,13)+2]]...)

Try it online!

-2 bytes thanks to @Stewie Griffin

niczky12

Posted 2018-03-14T06:00:51.810

Reputation: 301

1

Jelly, 61 bytes

d13Uị"“¢¶/CŻ)Gụ+Ḷ}ċ<ʂḤaỴ£k7Ỵ€^ḥæ]¿9Ụ“¡¢|hḂṗƬßĖ,$ðĿȧ»Ḳ€¤j“ of 

0-indexing. Try it online!

user202729

Posted 2018-03-14T06:00:51.810

Reputation: 14 620

“...“...»Ḳ€¤Œpị@j“ of is probably shorter. – Jonathan Allan – 2018-03-14T10:49:58.863

1

Haskell, 144 bytes

f n=words"two three four five six seven eight nine ten jack queen king ace"!!(n`mod`13)++" of "++words"hearts diamonds spades clubs"!!(n`div`13)

Try it online!

This hits all kinds of Haskell's pain points.

totallyhuman

Posted 2018-03-14T06:00:51.810

Reputation: 15 378

1

SOGL V0.12, 53 bytes

ķζ≡◄Τū┌p9P7šH□≡⅝'╗ΦqΖ▒ƨM.‘θ.wo"C█}y1+►ΚqΚ‘θJ¼ o.'⁰/wp

Try it Here!

dzaima

Posted 2018-03-14T06:00:51.810

Reputation: 19 048

1

Javascript 149 143 140 bytes

a=_=>"two three four five six seven eight nine ten jack queen king ace".split` `[_%13]+' of '+["hearts","diamonds","spades","clubs"][_/13|0]

-3 bits thanks to @rick hitchcock

a=_=>"two three four five six seven eight nine ten jack queen king ace".split` `[_%13]+' of '+["hearts","diamonds","spades","clubs"][_/13|0]
console.log(a(14))
console.log(a(34))
console.log(a(51))
console.log(a(8))
console.log(a(24))

Luis felipe De jesus Munoz

Posted 2018-03-14T06:00:51.810

Reputation: 9 639

1Save 3 bytes by not splitting the second array, and by indexing it with [_/13|0]. For example: ["hearts","diamonds","spades","clubs"][_/13|0] – Rick Hitchcock – 2018-03-14T14:10:08.177

I don't think you need the a= since your function isn't recursive. – Oliver – 2018-03-14T19:52:20.303

1

Jelly, 58 55 bytes

“ıĿ⁽⁷lg3e/“ẏ“£Ṣ¢÷yḊ⁾⁶ƭ⁼ẎẇḄṡ¿Onṃ⁶ḂḂfṠȮ⁻ẉÑ%»Ḳ€p/⁸ịṚj“ of 

Try it online!

Erik the Outgolfer

Posted 2018-03-14T06:00:51.810

Reputation: 38 134

1

Perl 5 -p, 119 bytes

0-based

$_=(TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN,JACK,QUEEN,KING,ACE)[$_%13].' OF '.(HEART,DIAMOND,SPADE,CLUB)[$_/13].S

Try it online!

Xcali

Posted 2018-03-14T06:00:51.810

Reputation: 7 671

1

Japt, 91 86 bytes

0-indexed.

I used a tool written by @Shaggy to generate the compressed lists.

`{`twodÈ(‚fÆfivÀ£xç P ightdÍÂdȈjackdquÁÈkˆg»­`qd gU}  {`Ê#tsk¹aڈäi£kclubs`qk gUzD

Try it online!

Explanation:

The first compressed string contains the card values delimited by d. The second compressed string contains the card ranks delimited by k.

These chars were picked using Shaggy's tool, which generates a string delimited by a char that is optimally compressed using shoco (the compression that Japt uses). This allows us to create a list of card values and ranks.

We use backticks ` to decompress these strings, then we split the string using q, followed by the char to split on.

Once we have the lists, we map through the card values, then we get the index of the input. It is important to note that Japt wraps its indexes, so we don't have to modulo by 13.

At each item, we loop through the card ranks. We get the index by dividing the input by 13.

Once we have both items, we concatenate them with " of ", which produces the final string.

Oliver

Posted 2018-03-14T06:00:51.810

Reputation: 7 160

1

dc, 161 160 158 142 bytes

[TWO][THREE][FOUR][FIVE][SIX][SEVEN][EIGHT][NINE][TEN][JACK][QUEEN][KING][ACE][HEART][DIAMOND][SPADE][CLUB][z2-:rz1<A]dsAxD~;rP[ OF ]PD+;rP83P

Try it online!

Over a year later, I was looking for something in my old golfs and found 16 bytes of inefficiency in the previous code:

[ACE][KING][QUEEN][JACK][TEN][NINE][EIGHT][SEVEN][SIX][FIVE][FOUR][THREE][TWO]0[d1+si:rlidD>A]dsAxr[CLUB]3:s[SPADE]2:s[DIAMOND]1:s[HEART]0:sD~;rP[ OF ]P;sP83P

Try that chunkier version online!

Top of stack used as input. 0-indexed.

In the old version, the array of ranks (r) was created using an iterator to run through the strings on the stack (0[d1+si:rlidD>A]dsAxr), but with only four items, it's shorter to manually assign the suits (3:s, etc.). The new version simply uses one array for all of it and also doesn't check against a fixed stack size. [z2-:rz1<A]dsAx replaces the above stack-crawler, popping everything into r at the position (stack depth)-2; it stops when there's one value left.

After all that setup is done, we divide by 13 leaving remainder & quotient on stack (D~). In the old version, we used these values to pick from the two arrays. In the new version, without a separate array of suits, we add 14 (E+) to the suit value, since our suits are after our ranks in array r. Print the strings, and print OF in the middle (;rP[ OF ]PE+;rP).

(First two golfs: -1 byte by outputting uppercase; this allows me to remove the 4 ses from the end of the suits, and instead print it via its ASCII value with the 3-byte 83P at the end. -2 bytes because I repeatedly misread the challenge as requiring 1-indexing. Now 0-indexed.)

brhfl

Posted 2018-03-14T06:00:51.810

Reputation: 1 291

1

Ruby, 124 bytes

->n{%w{two three four five six seven eight nine ten jack queen king ace}[n%13]+" of #{%w{heart diamond spade club}[n/13]}s"}

Try it online!

Zero indexed. Very straightforward.

Level River St

Posted 2018-03-14T06:00:51.810

Reputation: 22 049

1

Kotlin, 138 bytes

"two|three|four|five|six|seven|eight|nine|ten|jack|queen|king|ace".split("|")[i%13]+" of "+"hearts|diamonds|spades|clubs".split("|")[i/13]

Beautified

"two|three|four|five|six|seven|eight|nine|ten|jack|queen|king|ace".split("|")[i%13]+" of "+"hearts|diamonds|spades|clubs".split("|")[i/13]

Test

fun i(i:Int) =
"two|three|four|five|six|seven|eight|nine|ten|jack|queen|king|ace".split("|")[i%13]+" of "+"hearts|diamonds|spades|clubs".split("|")[i/13]
fun main(args: Array<String>) {
    for (i in (1..52)) {
        println(i(i))
    }
}

TIO

TryItOnline

jrtapsell

Posted 2018-03-14T06:00:51.810

Reputation: 915

1

QBasic, 190 178 bytes

a$="ace  two  threefour five six  seveneightnine ten  jack queenking
b$="hearts  diamondsspades  clubs
INPUT i
?RTRIM$(MID$(a$,(i MOD 13)*5+1,5))+" of "+MID$(b$,((i-1)\13)*8+1,8)

As usual, @DLosc saved me some bytes. I should listen to my own advice more often.

Encoding all the text is expensive here...

Explanation of previous version, same principles still apply.

                ' String containing 13 card values - each position is 5 chars long
a$="ace  two  threefour five six  seveneightnine ten  jack queenking
b$(1)="hearts   ' Four card suits. Now it might look like these string lits 
b$(2)="diamonds ' are not terminated, but that's because they aren't
b$(3)="spades   ' QBasic 4.5 auto-adds the quotes on a line break 
b$(4)="clubs    ' (kinda like it also auto-expands ? to PRINT)
INPUT i         ' Take the base-1 index, and 
?               ' PRINT
RTRIM$(         ' a string with all the spaces removed from the right end
MID$(a$,        ' of a substring of our card values
(i MOD 13)*5+1  ' starting at pos 0 (for the ace) through 12, * 5 (0, 5, 10 ... 60) 
                ' + 1 (QBasic has 1-based strings)
,5))            ' and running for 5 characters
+" of "+        ' followed by " of " and
b$((i-1)\13+1)  ' the suit, taken from the array after int-divide op i

steenbergh

Posted 2018-03-14T06:00:51.810

Reputation: 7 772

@DLosc Thanks, that saved 12 bytes. Solution is 1-based btw. 1 --> two of hearts and 52 --> ace of clubs. – steenbergh – 2018-06-11T17:27:49.883

Oh, whoops--I didn't notice that suits were supposed to start at two instead of ace. – DLosc – 2018-06-11T22:06:28.637

1

Excel VBA, 138 bytes

An anonymous function that takes input from [A1] and outputs to STDOUT. 0 indexed.

?Split("Two Three Four Five Six Seven Eight Nine Ten Jack Queen King Ace")([A1]Mod 13)" of "Split("Hearts Diamonds Spades Clubs")([A1/13])

Taylor Scott

Posted 2018-03-14T06:00:51.810

Reputation: 6 709

0

Red, 176 bytes

func[n][i: 0
foreach s["hearts""diamonds""spades""clubs"][foreach c split{two three four five six seven eight nine ten jack queen king ace}" "[if n = i: i + 1[print[c"of"s]]]]]

Try it online!

As always, the facts that the indexing is 1-based in Red and the math expressions are always long because of the obligatory spaces, are the reasons not to use modulo in my solution.

Galen Ivanov

Posted 2018-03-14T06:00:51.810

Reputation: 13 815

0

Charcoal, 78 bytes

§⪪”↶⌈=≔≡Ss←⊟✂sδ‴/﹪]7v1θ/✂eS'⌊D↔⎚B^#ïG⎚¦J←↥§”¶Iθ of §⪪”↶↧#σïP/M≡#↗÷⌕ρTM”¶÷Iθ¹³s

Try it online! Link is to verbose version of code. 0-indexed. Explanation:

  ”...”         Compressed string "two\nthree...king\ace"
 ⪪     ¶        Split on newlines
§       Iθ      Cyclically index on input cast to integer
                Implicitly print
 of             Literal string " of "
                Implicitly print
  ”...”         Compressed string "heart\ndiamond\nspade\nclub"
 ⪪     ¶        Split on newlines
         Iθ     Cast input to integer
        ÷  ¹³   Integer divide by 13
§               Index into list
                Implicitly print
s               Literal string "s"
                Implicitly print

Note: Without the limitation on suit ordering it can be done in 72 bytes:

F⪪”↶±″ς#₂g⎇¹αF+9r@c?×πλ¿BRu≦Zθx⊗Zj⊗✳R⦄ηs⊖▶fB‽⁺P4!⁶E~p↥Fºχβ|»J↧H¦”¶§⪪ιyIθ

Try it online! Link is to verbose version of code. Explanation:

  ”...”         Compressed string "twoy...yace\n of \nheartsy...yclubs"
 ⪪     ¶        Split on newlines
F               Loop over each element
         ⪪ιy    Split on literal string "y"
        §   Iθ  Cyclically index on input cast to integer
                Implicitly print

Neil

Posted 2018-03-14T06:00:51.810

Reputation: 95 035

0

Python 3, 157 bytes

lambda n:'two,three,four,five,six,seven,eight,nine,ten,jack,queen,king,ace'.split(',')[(n-1)%13]+' of '+['hearts','diamonds','spades','clubs'][int((n-1)/13)]

Try it online!

Dat

Posted 2018-03-14T06:00:51.810

Reputation: 879

0

Javascript 137 bytes

i=>"two,three,four,five,six,seven,eight,nine,ten,jack,queen,king,ace".split`,`[i%13]+` of ${["heart","diamond","spade","club"][0|i/13]}s`

f=

i=>"two,three,four,five,six,seven,eight,nine,ten,jack,queen,king,ace".split`,`[i%13]+` of ${["heart","diamond","spade","club"][0|i/13]}s`

console.log(f(38))

nl-x

Posted 2018-03-14T06:00:51.810

Reputation: 306

0

///, 460 bytes

/*/\/\///52/M!*51/L!*50/K!*49/J!*48/I!*47/H!*46/G!*45/F!*44/E!*43/D!*42/C!*41/B!*40/A!*39/M@*38/L@*37/K@*36/J@*35/I@*34/H@*33/G@*32/F@*31/E@*30/D@*29/C@*28/B@*27/A@*26/M#*25/L#*24/K#*23/J#*22/I#*21/H#*20/G#*19/F#*18/E#*17/D#*16/C#*15/B#*14/A#*13/M$*12/L$*11/K$*10/J$*9/I$*8/H$*7/G$*6/F$*5/E$*4/D$*3/C$*2/B$*1/A$*M/ace.*L/king.*K/queen.*J/jack.*I/ten.*H/nine.*G/eight.*F/seven.*E/six.*D/five.*C/four.*B/three.*A/two.*./ of *!/clubs*@/spades*#/diamonds*$/hearts/

Try it online!

The index of the card you want gets placed after the last /. I've left all indices in the TIO-link as a test suite. Also, because I've placed that test-suite in the Footer part of TIO, it adds a linebreak at the top.

steenbergh

Posted 2018-03-14T06:00:51.810

Reputation: 7 772

0

PHP 209 Bytes

No way this is the shortest, just a fun challenge.

<?php
 $a=$_GET['q'];$s=$a<53?'clubs':0;$s=$a<40?'spades':$s;$s=$a<27?'diamonds':$s;$s=$a<14?'hearts':$s;$v=$a%13;$v=$v==1?'ace':$v;$v=$v==11?'jack':$v;$v=$v==12?'queen':$v;$v=$v===0?'king':$v;echo "$v of $s";

Ungolfed

<?php
    $a=$_GET['q'];//input
    $s= $a<53?'clubs':0;//Ordered descending to overwrite extra positives, (prevent two checks per number like
    $s= $a<40?'spades':$s;//                                                                           a>=x>=b
    $s= $a<27?'diamonds':$s;
    $s= $a<14?'hearts':$s;

    $v=$a%13;
    $v = $v==1?'ace': $v;
    $v = $v==11?'jack': $v;
    $v = $v==12?'queen': $v;
    $v = $v===0?'king': $v;

    echo "$v of $s";

Ben

Posted 2018-03-14T06:00:51.810

Reputation: 131

0

Python 3, 143 bytes

s='hearts diamonds spades clubs two three four five six seven eight nine ten jack queen king ace'.split()
lambda n:f'{s[n%13+4]} of {s[n//13]}'

RootTwo

Posted 2018-03-14T06:00:51.810

Reputation: 1 749

0

Pip -s, 114 bytes

Yay I tied CJam!

[("two three four five six seven eight nine ten jack queen king ace"^sa)"of"("heart diamond spade club"^sa/13).'s]

Uses 0-indexing. Try it online! Or, verify all inputs!

DLosc

Posted 2018-03-14T06:00:51.810

Reputation: 21 213

0

PHP, 148 137 133 bytes

<?=[two,three,four,five,six,seven,eight,nine,ten,jack,queen,king,ace][($a=$argv[1])%13]." of ".[hearts,diamonds,spades,clubs][$a/13];

Try it online!

Zero based.

Jo.

Posted 2018-03-14T06:00:51.810

Reputation: 974

0

T-SQL, 173 bytes

SELECT TRIM(SUBSTRING('two  threefour five six  seveneightnine ten  jack queenking ace'
 ,i%13*5+1,5))+' of '+TRIM(SUBSTRING('hearts  diamondsspades  clubs',i/13*8+1,8))FROM t

0-based input is taken via pre-existing table t with int field i, per our IO standards.

Saw the trick using string position and TRIM in Chronocidal's Excel answer, I'm using it to "look up" both the card value and the suit.

TRIM() is new to SQL 2017, prior to that add 2 characters for RTRIM() instead.

BradC

Posted 2018-03-14T06:00:51.810

Reputation: 6 099

0

Pyth, 104 bytesSBCS

jd[@c." X$¾k¼q}V©>##!Tâò°Ùisö÷¼ÞÊ?.Q \\v,X*nã¬ËÇè"d%Q13"OF"@cr." uô¶)BMÙ¼)|Ó¹MÃ"1d/Q13

Try it online!

NOTE: This code make use of unprintable characters which are not copy/paste-able from this post. The link provided leads to the correct version of the program, which is copy/paste-able.

0-indexed.

Python 3 translation:
Q=eval(input())
print(" ".join(["TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE TEN JACK QUEEN KING ACE".split()[Q%13],"OF","hearts diamonds spades clubs".upper().split()[Q//13]]))

A few oddities here and there due to Pyth's string compression deciding to just not work sometimes, but I managed to work around them.

hakr14

Posted 2018-03-14T06:00:51.810

Reputation: 1 295

0

Yabasic, 186 bytes

An anonymous function that takes input, n and outputs to STDOUT. 0 indexed.

input""n
dim s$(13),t$(4)
k=Split("Two Three Four Five Six Seven Eight Nine Ten Jack Queen King Ace",s$())
k=Split("Hearts Diamonds Spades Clubs",t$())
?s$(Mod(n,13)+1)+" of "+t$(n/13+1)

Try it online!

Taylor Scott

Posted 2018-03-14T06:00:51.810

Reputation: 6 709