CtCI - Integer to String

Given an unsigned integer, return the corresponding ASCII character representation without using language built-in features, for example, if the input integer is (int) 1234 the program has to return (string) "1234" as an array of characters or string.

In JavaScript we could simply (1234).toString() but one of the rules in the exercise is that we cannot use built-in language features like toString(). We have to find a way to separate each digit in the original number and convert it to its individual character representation, maybe using an array of numbers from zero to nine.

How do we separate 1 from 1234? We can divide the number by one thousand and then drop the decimals, but in order to do this we have to know the length of the number, in this case it has four digits so the length is a multiple of 1,000 but what if we pass a bigger number? We will not know its length so easily.

Lets go backwards, lets go from right-to-left and extract 4 dividing by ten, if the number is lower than then we can simply print the associated character from the number map, otherwise we will divide the number by ten multiple times until there are no more digits to translate.

1234 / 10         = 123.4
Math.floor(123.4) = 123
123 * 10          = 1230
1234 - 1230       = 4

As you can see above, if we divide the number by ten we will get the right-most digit as a decimal point, we need to revert that number back to an integer but dropping the decimal, there is no rule saying we cannot use built-in math functions so we will leverage Math.floor. The reverted number can be obtained by multiplying the result of the previous operation by the same number used for the division, in this case, the number ten. We will get a number that is slightly different than the original, the difference between them is the value that we will translate to characters. We will repeat this operation over and over until there is only one digit left.

func solution(number int) string {
    var dict = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}

    if number < 10 {
        return dict[number]
    }

    var decimal int = number / 10
    var base int = int(math.Floor(float64(decimal)))
    var parent int = base * 10
    var diff int = number - parent

    return solution(base) + dict[diff]
}
7 months ago
  • 8da57acFix minor bugs found by the code static ana…
7 months ago
  • caa417fAdd option to configure the malware scanner…
7 months ago
  • 9c86744Modify default value for some of the alert …
7 months ago
  • 84dd39dAdd option to stop sending the failed login…
7 months ago
  • eb05935Add pre-checks for every plugin page for si…
7 months ago
  • d21a062Modify mechanism to ignore files from integ…
7 months ago
  • b1a9169Add developer option to disable failed pass…
7 months ago
  • 4e3ef13Add support for other English and Spanish b…
7 months ago
  • 2d07b4eFix error interception for Firewall API err…
7 months ago
pushed to master at cixtor/slackapi
  • 4a2c1c8Modify data type for methods related to cha…
  • 6716199Add CLI handler for the users.identity API …
  • f9c448dAdd CLI handler for the mpim.open API endpo…
  • 305d1c4Add CLI handler for the mpim.mark API endpo…
  • 8bb89afAdd CLI handler for the mpim.close API endp…
  • 202a017Add CLI handler for the dnd.teamInfo API en…
  • 68819e9Add CLI handler for the dnd.info API endpoi…
  • 9a2b29aAdd CLI handler for the dnd.endSnooze API e…
  • e7dc86aAdd CLI handler for the dnd.setSnooze API e…
  • 111c53fAdd CLI handler for the dnd.endDnd API endp…
  • cdb620dFix token usage when there are no extra par…
  • View comparison for these 11 commits
7 months ago
pushed to master at cixtor/slackapi
7 months ago
  • 6497e80Remove unnecessary automatic blacklisting o…
7 months ago
opened pull request Sucuri/sucuri-wordpress-plugin#40
Fix multiple bugs with the API calls and queue system
18 commit with 793 additions and 293 deletion
7 months ago
7 months ago
7 months ago
  • 38cc02aModify timing for the dashboard alerts afte…
7 months ago
  • 350c074Fix infinite loop with email alerts and SMT…
7 months ago
  • acff4aaFix detection of base URL with built-in fun…
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
opened pull request Sucuri/sucuri-wordpress-plugin#39
Add queue system for the security logs and cache improvement
5 commit with 517 additions and 541 deletion
7 months ago
7 months ago
  • 4c51445Fix static function call of non-static Site…
7 months ago
7 months ago
  • af47581Add changelog to release version 1.8.6
7 months ago
opened pull request Sucuri/sucuri-wordpress-plugin#38
Add changelog to release version 1.8.5
6 commit with 4062 additions and 1841 deletion
7 months ago
  • dc1a05aAdd changelog to release version 1.8.5
7 months ago
Do you have a project idea? Let's make it together!