idursun
I am trying to write a little function to determine whether a given hand of cards forms a gin.
Here are the rules:
- A hand contains 10 cards from a standard 52-card deck.
- A run is formed if it contains at least 3 cards or more of the same suit and their value increases without gaps.
- A set is formed if it contains at least 3 or more cards of the same value.
- A hand is a gin if every card in the hand either belongs to a set or a run.
I am curious to see alternative ways of coding this in idiomatic Elixir.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
joey_the_snake
What have you tried?
dimitarvp
“Alternatives” means there’s a first reference implementation against which they will be compared.
Where’s your code?
idursun
I haven’t provided any code not to influence anyone.
My solution is a simple backtracking algorithm that more or less goes like the following:
I take the first card in the hand and then try to find a set of 4, if found, then remove the members of the set and recurse into the remaining. If not found, then try to find a set of 3, if found, remove them and recurse into the remaining. If not, then try to find a run, if found, recurse into the remaining, if not then return false.
However, I quickly realised that matching a set of 4 whenever I can doesn’t work for some cases where one member of the set can help to complete a straight, so I ended up trying out the permutations of the set. I have later on made a similar observation about matching the longest runs doesn’t help when the last elements of the run can actually complete a set, etc.
I ended up special casing so many things in the end, which prompted me to see what people could come up with.
I hope this gives some direction.
dimitarvp
You will not. Let’s see your try at it and discuss it.
idursun
My code is like the following:
I used the following test case to verify that it is working as expected.
al2o3cr
The tricky part is dealing with cards that could be in either a set or a run - for instance if you have 5/6/7/8 of diamonds, 5 of clubs and 5 of hearts.
I’d implement this check by first rewriting it into a recursive definition:
Some of these clauses are trickier than others:
There’s a corresponding clause in
GinCalc.gin?for all the bullet points above:I used tuples to represent cards because they
Enum.sortin a reasonable way (grouped into suits, then by number) and are easy to pattern-match, but using structs would only require light modifications.dpreston
You might find this approach to ranking poker hands helpful. Playing Poker with Elixir (pt. 1)
idursun
Nice! It looks better than mine.
I like the usage of
reduce_whilefor halting the loop early. I might steal thatidursun
Thanks! That’s exactly what I was looking for! Pity the blog doesn’t exist anymore.