PeterCcT

PeterCcT

Roman number conversion challenge question

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 :slight_smile:

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


Most Liked

hauleth

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

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 :wink: 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.

Where Next?

Popular in Challenges Top

Aetherus
This topic is about Day 3 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
Aetherus
I tried to use combinatorial to solve today’s puzzles but failed (my brain burned out :exploding_head:). In the end I just used brute for...
New
bjorng
Note: This topic is to talk about Day 25 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New
adamu
I said I was on a break, but I took a sneak peak and it looked fun so… Part 1 completes in half a millisecond with a single pass of the ...
New
NobbZ
Note by the Moderators: This topic is to talk about the Day 2 of the Advent of Code. For general discussion about the Advent of Code 201...
New
bjorng
Note: This topic is to talk about Day 3 of the Advent of Code 2019 . There is a private leaderboard for elixirforum members. You can jo...
New
bjorng
This topic is about Day 2 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums): https://adven...
New
jkwchui
Monkeys fitted squarely as GenServers in my head. My initial problem was using cast instead of call; I imagine impolite monkeys slinging...
New
Aetherus
Today’s problem is really tense. I don’t think I can do it without libgraph.
New
bjorng
This topic is about Day 6 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums ): https://adve...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement