Last active
August 11, 2020 00:28
-
-
Save lukaswilkeer/fde9a44af93937f4d78139df3ed6ce1a to your computer and use it in GitHub Desktop.
Reverse a number before treating this as a big number.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(reverse). | |
-export([reverse/1]). | |
%% @doc reverse a number | |
-spec(reverse(string()) -> string()). | |
reverse(ANumber) -> | |
%% reverse the string | |
Nmbr = string:reverse(ANumber), | |
%% verify if is not a number | |
Check = not_a_number(Nmbr, 0, true), | |
if | |
Check == true -> "error"; | |
Check == false -> Nmbr | |
end. | |
%% @doc verify if it's a number | |
-spec(not_a_number(string(), number(), atom()) -> atom()). | |
not_a_number(Nmbr, Index, Result) -> | |
Result = list:contains(string:substr(Nmbr, Index, Index), [1,2,3,4,5,6,7,8,9]), | |
Index = Index + 1, | |
Length = string:len(Nmbr), | |
if | |
Result == false -> false; | |
Index <= Length -> not_a_number(Nmbr, Index, Result); | |
Index > Length -> true | |
end. | |
%% regex to check if it'a number or a recursion checking every possibility | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment