ryanzidago
I’m trying to implement the Gale-Shapley algoritm in Elixir to solve the Stable Marriage Problem.
I managed to come up with an implementation of the algorithm in Ruby but I can’t achieve a solution with Elixir/the functional programming way; especially due to immutability.
Note that I’m trying to first implement this algorithm sequentially (without actors) so an implementation like this is not what I’m trying to achieve right now.
I have found this implementation in Elixir, but it does not pass Rosetta Code’s test case.
Here’s what I have so far.
How would you tackle this problem? ![]()
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
APB9785
It looks like you’re doing it very similarly to how I would. Is your code failing the tests?
ryanzidago
Yes it is failing the Rosetta Code test case (the one with the real names, not just the letters).
I think it’s because if a man is not available, I still pop his favorite woman from his preference list, even though he did not technically proposed to her. A man propose to a woman only if he is available.
I tried to not pop the favorite woman out of the preference list for this case, but it still did not work.
ityonemo
this might help:
especially note:
Also unrelated pedantry (sorry, this is a pet obession of mine)
is_functions should be reserved for guards; postifx sigil...?(like ruby) is the idiomatic way to indicate a boolean function in elixir.APB9785
What is stopping you from skipping over the unavailable men each round and not considering their favored women at all?
Also, I agree with @ityonemo that the
is_prefix should be replaced by a?suffix for booleans.ryanzidago
I would like to avoid relying on ETS right now and would rather try to do it the functional way.
But using digraph for solving this problem is definitely something that I’d like to explore later.
ryanzidago
I did try like this:
But surprisingly, I get some pairs in the wrong order/or in doubles in the final result while some other pairs are missing:
APB9785
That looks to be from this code:
Where sometimes you are using the man as the key (and woman as value) and other times you are adding the woman as the key (and man as value). I think you need to keep that consistent.
ryanzidago
The
statevariable holds the pairs from man to woman and woman to man. So if “a” and “z” forms a couple, thestatemap will have%{"a" => "z", "z" => "a"}. I need this to be able to find the current fiancé of any given woman.ryanzidago
But thank you for pointing me in the right direction, I actually found the culprit code:
Since I have both men and women in state, I cannot simply discard the state to only keep men-pairs like this. This works for the first test case but not the second one.
I ended up changing it like so:
So from the state, I only keep the men keys and put them into a tuple with thier fiancée.
And now the Rosetta Code’s test case passes!
APB9785
Nice work!