Thousand Separator

In Python, you can solve this problem using the format function with specific formatting rules.

Here’s how to implement it:

1
2
3
class Solution:
    def thousandSeparator(self, n: int) -> str:
        return '{:,}'.format(n).replace(',', '.')

The format function is used here with :, as the format specifier, which adds a comma as the thousands separator. We then use the replace function to replace every comma with a dot.

The code reads an integer n and transforms it into a string with dots as the thousands separators. This method ensures that the original number remains unchanged while the output string reflects the requirement. The formatting is done from right to left, grouping every three digits together and placing a dot before each group. The output is a string representation of the original number with the correct separators.

1
2
3
4
5
6
7
8
9
string thousandSeparator(int n) {
    string s = to_string(n), res;
    for (int i = 0; i < s.size(); ++i) {
        if (i > 0 && (s.size() - i) % 3 == 0)
            res += ".";
        res += s[i];
    }
    return res;
}

The C++ function thousandSeparator takes an integer n and returns a string that represents n with commas inserted every three digits from the right.

Here’s how the function works:

  1. It starts by converting the input integer n to a string s using the to_string function. It also initializes an empty string res to store the result.

  2. It then iterates over each character in the string s. For each index i, it checks whether i is greater than 0 and (s.size() - i) % 3 equals 0. The condition (s.size() - i) % 3 == 0 is true when i is three positions away from the end of the string s.

  3. If the condition is true, it appends a dot to res.

  4. It then appends the character at index i of s to res.

  5. Finally, it returns res, which is the string representation of n with a dot inserted every three digits from the right.

This function effectively formats the string representation of n in a single pass, resulting in a time complexity of O(log n), where n is the input integer. The space complexity is also O(log n), as it creates a string of size proportional to the number of digits in n.

The code is in C++, and it uses a loop to iterate through the string representation of the number. The if condition inside the loop checks whether we’ve moved three characters forward in the string, and if so, it adds a dot to the result.

Python code:

1
2
3
4
5
6
7
8
9
class Solution:
    def thousandSeparator(self, n: int) -> str:
        s = str(n)
        res = ''
        for i in range(len(s)):
            if i > 0 and (len(s) - i) % 3 == 0:
                res += "."
            res += s[i]
        return res

In this code, the input integer is first converted to a string. Then, for every character in the string, if the index is not zero and the remaining length of the string is a multiple of three, a dot is appended to the result string. After checking the condition, the character is added to the result string. This process continues until all characters from the input string have been processed. Finally, the resulting string is returned.