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

LostKobrakai
This topic is about Day 9 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
bjorng
This topic is about Day 15 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums): https://adve...
New
lud
Hello everyone! This year is going to be shorter, but the difficulty will grow faster. Today I already feel that this is not standard “D...
New
rugyoga
part 1 https://github.com/rugyoga/aoc2021/blob/main/day7.exs part 2 https://github.com/rugyoga/aoc2021/blob/main/day7b.exs
New
bjorng
Note: This topic is to talk about Day 18 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New
Qqwy
Note by the Moderators: This topic is to talk about Day 6 of the Advent of Code. For general discussion about the Advent of Code 2018 an...
New
gangstead
This is my second year doing AoC in Elixir and my first year doing it with Livebook. When I was doing just plain Elixir I usually set up...
New
rugyoga
part 1: https://github.com/rugyoga/aoc2021/blob/main/day8.exs part 2: https://github.com/rugyoga/aoc2021/blob/main/day8b.exs
New
seeplusplus
Hello all, hopefully I post this before someone else does and I don’t dupe. IMO Day 4 was much easier than Day 3 (yay, I can sleep befor...
New
bjorng
Note: This topic is to talk about Day 9 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New

Other popular topics Top

Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

We're in Beta

About us Mission Statement