Andres
Reverse an integer regardless of its sign
Hello.
Trying to reverse an integer regardless of its sign I wrote the following algorithm:
int = -123456789 #=> Expected result -987654321
def minus_one(int) do
int * -1
end
def sign(int) when int > 0 do
int
|> Integer.to_string()
|> String.reverse()
|> String.to_integer()
end
def sign(int) do
int
|> Integer.to_string()
|> String.replace("-", "")
|> String.reverse()
|> String.to_integer()
|> minus_one()
end
def reverse_integer(int) do
sign(int)
end
Is there a native way to get the sign of a number? like :math.sign()
Any suggestions to improve the algorithm is welcome.
Thanks.
Marked As Solved
NobbZ
Why converting to a string at all?
Just do the maths:
defmodule DR do
def r(n), do: r(n, 0)
def r(0, x), do: x
def r(n, x), do: r(div(n, 10), x * 10 + rem(n, 10))
end
6
Also Liked
Eiji
Eiji
Originally I noticed this on other data too, but I found that it is not a problem in this specific case, because as far as I know Integer.digits/1 will never return list like [1, -2, 3].
There is nothing surprising with 83 as result, because we have: (3 * 10^0) + (-2 * 10^1) + (1 * 10^2) i.e. (3 * 1) + (-2 * 10) + (1 * 100) i.e. 3 - 20 + 100 which gives 83.
4
LostKobrakai
def sign(int) when int >= 0, do: 1
def sign(int) when int < 0, do: -1
def reverse(int) when int >= 0 do
int
|> Integer.to_string()
|> String.reverse()
|> String.to_integer()
end
def reset_sign(int, 1), do: int
def reset_sign(int, -1), do: int * -1
def reverse_integer(int) do
int
|> abs()
|> reverse()
|> reset_sign(sign(int))
end
3
Eiji
Ah right, my bad - I have edited my first post
2
Andres
Thank you very much for clarifying it.
2
Popular in Questions
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
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
Student & New to elixir. Nice language.
I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
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
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
Hi all
I want to have a unix time, from the current time plus 1 hour.
DateTime.now + 1 hour
How to get it in elixir?
Thanks
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Other popular topics
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
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
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
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New








