PeterCcT
Hi guys!
I’m super new to Elixir, and my English can be a little bad, so I’m sorry about that.
Well, I’m here to get some advice on my code, it works well, but I don’t know if there’s anything I can do to make it more like the “Elixir way of things”. So if anyone can rate it and give me some tips ![]()
Ps: The challenge is only for numbers up to 3000
Here’s the code
defmodule Converter do
@one "I"
@five "V"
@ten "X"
@fifty "L"
@onehundread "C"
@fivehundread "D"
@onethousand "M"
def to_roman_number(number,roman_number \\ "",n \\ 3)
def to_roman_number(number,roman_number,_n) when number == 4,do: roman_number<>@one<>@five
def to_roman_number(number,roman_number,_n) when div(number,10) == 4 do
roman_conversion = roman_number<>@ten<>@fifty
rest = rem(number,10)
to_roman_number(rest,roman_conversion,0)
end
def to_roman_number(number,roman_number,_n) when div(number,100) == 4 do
roman_conversion = roman_number<>@onehundread<>@fivehundread
rest = rem(number,100)
to_roman_number(rest,roman_conversion,1)
end
def to_roman_number(number,roman_number,_n) when number == 5, do: roman_number<>@five
def to_roman_number(number,roman_number,_n) when div(number,10) == 5 do
roman_conversion = roman_number<>@fifty
rest = rem(number,10)
to_roman_number(rest,roman_conversion,0)
end
def to_roman_number(number,roman_number,_n) when div(number,100) == 5 do
roman_conversion = roman_number<>@fivehundread
rest = rem(number,100)
to_roman_number(rest,roman_conversion,1)
end
def to_roman_number(number,roman_number,_n) when number == 9,do: roman_number<>@one<>@ten
def to_roman_number(number,roman_number,_n) when div(number,10) == 9 do
roman_conversion = roman_number<>@ten<>@onehundread
rest = rem(number,10)
to_roman_number(rest,roman_conversion,0)
end
def to_roman_number(number,roman_number,_n) when div(number,100) == 9 do
roman_conversion = roman_number<>@onehundread<>@onethousand
rest = rem(number,100)
to_roman_number(rest,roman_conversion,1)
end
def to_roman_number(number,roman_number,n) do
base = trunc(:math.pow(10,n))
quotient = div(number,base)
rest = rem(number,base)
cond do
n == 3 ->
roman_conversion = roman_number<>String.duplicate(@onethousand,quotient)
to_roman_number(rest,roman_conversion,n-1)
n == 2 ->
roman_conversion = roman_number<>String.duplicate(@onehundread,quotient)
to_roman_number(rest,roman_conversion,n-1)
n == 1 ->
roman_conversion = roman_number<>String.duplicate(@ten,quotient)
to_roman_number(rest,roman_conversion,n-1)
n == 0 ->
roman_conversion = roman_number<>String.duplicate(@one,quotient)
IO.puts "The converted number is "<>roman_conversion
end
end
end
Trending in Challenges
Other Trending Topics
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #performance











Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hauleth
It is repeating topic there, as I believe this is task in Exercism or other project like that. So check out if any other topic on that task helps you.
yreuvekamp
Since this is Exercism, I suggest requesting a mentor there to look at your code for some tips. That’s why they’re there
Besides that, take a look at the most upvoted solutions for some inspiration. That really helped me think differently.
Edit: having said that, your code could definitely be more concise. Make use of the fact that you know all the possible mappings.
PeterCcT
Sorry for the delay to answer hahahaha
I will check the other answers, thanks brother!
PeterCcT
Sorry for the delay in responding too my brother
Thank you very much for the tip, the solution is a bit verbose, I’ll try to improve it!