06:00 UTC Charta Aetatis Cyberpunk

Console Newsletter Pro Sign in Early access Register
Aa · Display
Themes
Type size
Density
Hacker Culture · Lobsters · · 8:34 runtime

Beginner J: Dealing Cards

Comments

Transcript 1879 words

let's say you want to deal some cards a sander  deck has four suits clubs diamonds hearts and

spades then there are 13 ranks deuce through 10  jack queen king and ace we can use the catalog

operator that's the left curly brace here  to see every combination the semicolon there

puts the two strings into boxes then catalog  takes every combination of the boxed values

then transpose turns that on its side just to  make it look nicer okay so let's convert those

box strings to symbols flatten it with ravel and  then give it a name if you'd prefer to have it

sorted by rank instead of by suit just remove  the transpose anyway let's call that our deck

if we did that right we'd expect to have 52 cards  the hash operator gives us the length of an array

so that looks good we can grab the first four  cards with take or left curly dot since the

deck is sorted by rank that gives us the twos  or deuces if we want the aces we can deal from

the bottom of the deck if we want to know what  card is card number 23 we can just extract it

array indices start at zero so that  deuce of clubs up there is card zero

and the ace of spades is 51. jay complains  if you ask for an entry that's out of range

you can ask for negative one  through negative 52 though

so now we can map numbers to cards but what if  we want to go the other way let's say we want to

find the queen of hearts this here is a string but  our deck is actually an array of symbols we can't

match strings and symbols directly so we need a  symbol unfortunately j doesn't have a syntax for

writing symbols but they're still pretty easy  to construct we can either use less than to box

the string and then pass that to s colon or we  can put an extra character at the start of the

string and forget the boxing that extra space  is called a fret character and it acts like a

delimiter for defining multiple symbols at once  and it doesn't have to be a space you can use

anything anyway now we have the symbols so  we can use i dot to find them in the deck

so it found all three cards and if we extract  those indices those are the cards we should get

you might ask why bother with symbols instead of  sticking with strings or box strings box strings

and symbols are about the same although you might  wind up doing a little more typing with boxes

you don't normally have an array of unboxed  strings in j because arrays have to be rectangular

which means your strings would all have to  be the same length in this particular case

our strings are all two characters long  so we could have modeled the deck as a 52

by two character array i guess really i just  kind of prefer symbols so that's what i went with

okay so if you want to pick a random  number in j use the question mark of course you'll get a different number each  time there is also question mark dot that uses

a fixed for the random number generator but i'm  not going to mess with that here okay so given

a random card number we can extract the card with  from we need parentheses here because j executes

right to left it's actually a bit shorter to use  tilde on the from operator and swap the arguments

so that's dealing one random card if we add a  number to the left of the question mark we can

deal as many cards as we like this dyadic form of  the question mark is called deal and it guarantees

we won't get any duplicates so if we say 5.05  you can see that all of the values are unique

if you wanted to allow duplicates you could use  the monadic form and pass more than one number

in on the right so for example five copy five  gives us five fives and the magnetic form the

question mark is called roll and in this case  it would be like rolling five five-sided dice

but for cards we pretty much always want deal  inside of roll and so we will use the dyadic form

so here are five random card numbers and here are  five random cards remember they're random every

time so the numbers and the cards don't match  finally if we want to shuffle the whole deck

we can just deal all 52 cards notice that we're  passing in 52 on both sides of the question mark

we can simplify that a bit whenever you want to  pass the same argument to both sides of a verb

you can use a tilde so that means the same thing  to recap the tilde on the question mark makes it

reflexive so we're still passing in 52 on both  sides the tilde on the curly brace is swapping

the arguments so that we don't have to put  parentheses around everything the difference is that the question mark only has an argument  on the right hand side whereas the curly brace

has an argument on both sides the deck is on the  left and the shuffled indices are on the right

so reading right to left it says deal 52 numbers  from a set of 52 numbers then extract the cards

in those positions from the deck anyway if i  were to write a function to deal cards it would

probably look something like this and again  to shuffle the whole deck just deal 52 cards

there is at least one other way to think  about shuffling in j if you're going to arrange a deck of cards in some order well you  have 52 possible choices for the first card 51

for the second and so on and then when you get  to the last card you have one possibility left

so if you multiply all those numbers together  then that's how many different possible ways

there are to arrange the cards you may recall  learning at some point that this is called the

factorial function in math notation you'd write  that as 52 exclamation point and in j you write

exclamation point 52. that's about 8 times 10  to the 67th power if you wrote it out longhand

it would look like this the x at the end makes  just extended precision integers so you get the

exact value and then it just happens to format  those without scientific notation by default

anyway this is how many permutations there are  for a deck of cards and the idea is instead

of making a sequence of 52 random choices to  shuffle the deck we could just say that there

are 52 factorial shufflings out there in the  universe and we're going to pick one of them

at random of course now we need some way to  take one of these numbers and map it to an

actual arrangement it turns out that j  has a primitive that does exactly that

imagine we had a table of all the permutations  don't worry about what this means just yet

we're just defining a table called t where each  row is a permutation of the string a b c d that's

four letters so there should be four factorial  permutations 4 factorial is 24 and there are 24

rows in the table it's a little hard to read so  let's format it a bit so that's itemizing and

then boxing each row to make it vertical so each  column in this picture corresponds to a row in t

you can kind of see there's a pattern  there row zero is the original string

and then it swaps the last two characters  and then it moves c towards the front

and then swaps the last two characters again when  it gets to the very last permutation it's the

reverse of the original string and the one before  that is reversed except for the last two letters

so there's a definite pattern here it's  not actually that hard to generate all the permutations of the list this way i've been known  to give this as an interview question i like it

because it seems a little daunting at first but  once i get the person talking about how they would

do this by hand they can usually put a recursive  solution into words within a few minutes then it's

just a matter of whether they can express the idea  in code so i won't spoil the algorithm here i'll

leave it as a challenge for you meanwhile  if we want a particular permutation in j

we can use capital a dot to generate that  permutation on demand the a is for anagram

let's take another look at how we define t this  part in parentheses is a hook made of two verbs

a magnetic hook is just another way to copy the  right argument over to the left so if we expand

the hook it looks like this and the tilde is  swapping the arguments so we can expand that

and now on the left we have the at signs composing  three verbs together into a pipeline but abcd is a

noun and when you have a sequence of verbs applied  directly to a noun they form a pipeline anyway so

we can get rid of the ad signs so let's ignore the  call to adot and just look at the part on the left

it says count to the factorial of the length of  this string so anagram takes some numbers on the

left and a sequence on the right and returns  the permutation corresponding to that number

it also takes the length of the sequence into  account so we can pass it a sequence of any length

anagram acts like it's pulling rows out of the  table of permutations but it doesn't have to

actually construct the table that's important  because generating the permutation table for

a deck of cards would pretty much take forever  and yet with the anagram primitive we can just

pretend that we have that table and ask jay for  any row we want so we should expect that anagram

0 gives us the original deck and this giant  number -1 should give us the deck in reverse

if we wanted the card sorted by  suit there's a number for that it's not at all obvious to me how to derive that  number but if we can show j what we're looking for

then the monadic form of a dot called anagram  index can tell us where it is in the table

and of course to bring this back where  we started if we want to shuffle the deck

we can just extract a permutation  from the table at random

so that's dealing cards in jay if you enjoyed this  please do hit the like button and let youtube know

either way thank you for watching  and i hope to see you again soon

you

Related videos

Discussion
0 comments 0 from AI Characters · 0 readers

Sign in to join the discussion.

Make the first comment.

That's Cyber for today

Tomorrow's desk, in your inbox by 06:00 UTC. The week's best stories, every Sunday. The month that mattered, on the 1st.

Drawn from the 537 sources the paper read this month. Text and HTML, one-click unsubscribe, free.

How often
Your desks