Skip to main content
added 43 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

sort Sort digits in python implementationPython

Given an integer, sort the digits in ascending order and return the new integer.

  • Ignore leading zeros.

Parameters:

  • Input: num {Integer}
  • Output: {Integer}

Constraints:

  • Do not convert the integer into a string or other data type.
  • Time: O(N) where N is the number of digits.
  • Space: O(1)

Examples:

  • 8970 --> 789
  • 32445 --> 23445
  • 10101 --> 111

Given an integer, sort the digits in ascending order and return the new integer.

  • Ignore leading zeros.

Parameters:

  • Input: num {Integer}
  • Output: {Integer}

Constraints:

  • Do not convert the integer into a string or other data type.
  • Time: O(N) where N is the number of digits.
  • Space: O(1)

Examples:

  • 8970 --> 789
  • 32445 --> 23445
  • 10101 --> 111

My code (as follows) works similar to the counting sort:

def sort_digits(n):
    digit_counts = {}
    result = 0

    while n > 0:
        digit = n % 10
        digit_counts[digit] = digit_counts.get(digit, 0) + 1
        n /= 10

    power = 0
    for i in range(10, -1, -1):
        if i in digit_counts:
            while digit_counts[i] >= 1:
                result +=  i * (10 ** (power))
                power += 1
                digit_counts[i] -= 1

    return result

sort digits in python implementation

Given an integer, sort the digits in ascending order and return the new integer.

  • Ignore leading zeros.

Parameters:

  • Input: num {Integer}
  • Output: {Integer}

Constraints:

  • Do not convert the integer into a string or other data type.
  • Time: O(N) where N is the number of digits.
  • Space: O(1)

Examples:

  • 8970 --> 789
  • 32445 --> 23445
  • 10101 --> 111

My code (as follows) works similar to the counting sort:

def sort_digits(n):
    digit_counts = {}
    result = 0

    while n > 0:
        digit = n % 10
        digit_counts[digit] = digit_counts.get(digit, 0) + 1
        n /= 10

    power = 0
    for i in range(10, -1, -1):
        if i in digit_counts:
            while digit_counts[i] >= 1:
                result +=  i * (10 ** (power))
                power += 1
                digit_counts[i] -= 1

    return result

Sort digits in Python

Given an integer, sort the digits in ascending order and return the new integer.

  • Ignore leading zeros.

Parameters:

  • Input: num {Integer}
  • Output: {Integer}

Constraints:

  • Do not convert the integer into a string or other data type.
  • Time: O(N) where N is the number of digits.
  • Space: O(1)

Examples:

  • 8970 --> 789
  • 32445 --> 23445
  • 10101 --> 111

My code (as follows) works similar to the counting sort:

def sort_digits(n):
    digit_counts = {}
    result = 0

    while n > 0:
        digit = n % 10
        digit_counts[digit] = digit_counts.get(digit, 0) + 1
        n /= 10

    power = 0
    for i in range(10, -1, -1):
        if i in digit_counts:
            while digit_counts[i] >= 1:
                result +=  i * (10 ** (power))
                power += 1
                digit_counts[i] -= 1

    return result
got rid of code formatting and changed to text
Source Link
Stephen Rauch
  • 4.3k
  • 12
  • 24
  • 36

Given an integer, sort the digits in ascending order and return the new integer.

  • Ignore leading zeros.

Parameters:

  • Input: num {Integer}
  • Output: {Integer}

Constraints:

  • Do not convert the integer into a string or other data type.
  • Time: O(N) where N is the number of digits.
  • Space: O(1)

Examples:

  • 8970 --> 789
  • 32445 --> 23445
  • 10101 --> 111

My code (as follows) works similar to the counting sort:

def sort_digits(n):
    digit_counts = {}
    result = 0

    while n > 0:
        digit = n % 10
        digit_counts[digit] = digit_counts.get(digit, 0) + 1
        n /= 10

    power = 0
    for i in range(10, -1, -1):
        if i in digit_counts:
            while digit_counts[i] >= 1:
                result +=  i * (10 ** (power))
                power += 1
                digit_counts[i] -= 1

    return result

Given an integer, sort the digits in ascending order and return the new integer.

  • Ignore leading zeros.

Parameters:

  • Input: num {Integer}
  • Output: {Integer}

Constraints:

  • Do not convert the integer into a string or other data type.
  • Time: O(N) where N is the number of digits.
  • Space: O(1)

Examples:

  • 8970 --> 789
  • 32445 --> 23445
  • 10101 --> 111

My code (as follows) works similar to the counting sort:

def sort_digits(n):
    digit_counts = {}
    result = 0

    while n > 0:
        digit = n % 10
        digit_counts[digit] = digit_counts.get(digit, 0) + 1
        n /= 10

    power = 0
    for i in range(10, -1, -1):
        if i in digit_counts:
            while digit_counts[i] >= 1:
                result +=  i * (10 ** (power))
                power += 1
                digit_counts[i] -= 1

    return result

Given an integer, sort the digits in ascending order and return the new integer.

  • Ignore leading zeros.

Parameters:

  • Input: num {Integer}
  • Output: {Integer}

Constraints:

  • Do not convert the integer into a string or other data type.
  • Time: O(N) where N is the number of digits.
  • Space: O(1)

Examples:

  • 8970 --> 789
  • 32445 --> 23445
  • 10101 --> 111

My code (as follows) works similar to the counting sort:

def sort_digits(n):
    digit_counts = {}
    result = 0

    while n > 0:
        digit = n % 10
        digit_counts[digit] = digit_counts.get(digit, 0) + 1
        n /= 10

    power = 0
    for i in range(10, -1, -1):
        if i in digit_counts:
            while digit_counts[i] >= 1:
                result +=  i * (10 ** (power))
                power += 1
                digit_counts[i] -= 1

    return result
got rid of code formatting and changed to text
Source Link
# Given an integer, soft the digits in ascending order and return the new integer.
# Ignore leading zeros.

# Parameters
# Input: num {Integer}
# Output: {Integer}

# Constraints
# Do not convert the integer into a string or other data type.

# Time: O(N) where N is the number of digits.
# Space: O(1)

# Examples
# 8970 --> 789
# 32445 --> 23445
# 10101 --> 111

Given an integer, sort the digits in ascending order and return the new integer.

  • Ignore leading zeros.

solution is hereParameters:

similar to counting sort

  • Input: num {Integer}
  • Output: {Integer}

Constraints:

  • Do not convert the integer into a string or other data type.
  • Time: O(N) where N is the number of digits.
  • Space: O(1)

Examples:

  • 8970 --> 789
  • 32445 --> 23445
  • 10101 --> 111

My code (as follows) works similar to the counting sort:

def sort_digits(n):
    digit_counts = {}
    result = 0

    while n > 0:
        digit = n % 10
        digit_counts[digit] = digit_counts.get(digit, 0) + 1
        n /= 10

    power = 0
    for i in range(10, -1, -1):
        if i in digit_counts:
            while digit_counts[i] >= 1:
                result +=  i * (10 ** (power))
                power += 1
                digit_counts[i] -= 1

    return result
# Given an integer, soft the digits in ascending order and return the new integer.
# Ignore leading zeros.

# Parameters
# Input: num {Integer}
# Output: {Integer}

# Constraints
# Do not convert the integer into a string or other data type.

# Time: O(N) where N is the number of digits.
# Space: O(1)

# Examples
# 8970 --> 789
# 32445 --> 23445
# 10101 --> 111

solution is here

similar to counting sort

def sort_digits(n):
    digit_counts = {}
    result = 0

    while n > 0:
        digit = n % 10
        digit_counts[digit] = digit_counts.get(digit, 0) + 1
        n /= 10

    power = 0
    for i in range(10, -1, -1):
        if i in digit_counts:
            while digit_counts[i] >= 1:
                result +=  i * (10 ** (power))
                power += 1
                digit_counts[i] -= 1

    return result

Given an integer, sort the digits in ascending order and return the new integer.

  • Ignore leading zeros.

Parameters:

  • Input: num {Integer}
  • Output: {Integer}

Constraints:

  • Do not convert the integer into a string or other data type.
  • Time: O(N) where N is the number of digits.
  • Space: O(1)

Examples:

  • 8970 --> 789
  • 32445 --> 23445
  • 10101 --> 111

My code (as follows) works similar to the counting sort:

def sort_digits(n):
    digit_counts = {}
    result = 0

    while n > 0:
        digit = n % 10
        digit_counts[digit] = digit_counts.get(digit, 0) + 1
        n /= 10

    power = 0
    for i in range(10, -1, -1):
        if i in digit_counts:
            while digit_counts[i] >= 1:
                result +=  i * (10 ** (power))
                power += 1
                digit_counts[i] -= 1

    return result
Source Link
NinjaG
  • 2.6k
  • 2
  • 30
  • 61
Loading