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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
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
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
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (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!