Python: Sort the numbers by the sum of their digits
Python Programming Puzzles: Exercise-38 with Solution
Write a Python program to sort the numbers in a given list by the sum of their digits.
Input: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Output: [10, 11, 20, 12, 13, 14, 15, 16, 17, 18, 19] Input: [23, 2, 9, 34, 8, 9, 10, 74] Output: [10, 2, 23, 34, 8, 9, 9, 74]
Visual Presentation:
Sample Solution-1:
Python Code:
# License: https://bit.ly/3oLErEI
# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# Sort the numbers in 'nums' based on the sum of their digits
return sorted(nums, key=lambda n: sum(int(c) for c in str(n) if c != "-"))
# Assign a specific list of numbers 'nums' to the variable
nums = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
# Print a message indicating the original list of numbers
print("Original list of numbers:", nums)
# Print a message indicating the operation to be performed
print("Sort the numbers of the said list by the sum of their digits:")
# Print the result of the test function applied to 'nums'
print(test(nums))
# Assign another specific list of numbers 'nums' to the variable
nums = [23, 2, 9, 34, 8, 9, 10, 74]
# Print a message indicating the original list of numbers
print("\nOriginal list of numbers:", nums)
# Print a message indicating the operation to be performed
print("Sort the numbers of the said list by the sum of their digits:")
# Print the result of the test function applied to 'nums'
print(test(nums))
Sample Output:
Original list of numbers: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Sort the numbers of the said list by the sum of their digits: [10, 11, 20, 12, 13, 14, 15, 16, 17, 18, 19] Original list of numbers: [23, 2, 9, 34, 8, 9, 10, 74] Sort the numbers of the said list by the sum of their digits: [10, 2, 23, 34, 8, 9, 9, 74]
Flowchart:
Sample Solution-2:
Python Code:
# License: https://bit.ly/3oLErEI
# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# Create a copy of the original list 'nums' to preserve the original order
unordered = nums.copy()
# Initialize an empty list to store the ordered numbers
ordered = []
# Continue the process until there are no more unordered numbers
while unordered:
# Select the first number in the unordered list as the smallest
smallest = unordered[0]
# Calculate the sum of digits for the current smallest number
s = sum(int(c) for c in str(smallest) if c != "-")
# Iterate through the remaining unordered numbers
for t in unordered:
# Calculate the sum of digits for the current number 't'
t_s = sum(int(c) for c in str(t) if c != "-")
# Compare the sum of digits and update the smallest if needed
if t_s < s:
smallest = t
s = t_s
# Append the smallest number to the ordered list
ordered.append(smallest)
# Remove the smallest number from the unordered list
unordered.remove(smallest)
# Return the final ordered list
return ordered
# Assign a specific list of numbers 'nums' to the variable
nums = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
# Print a message indicating the original list of numbers
print("Original list of numbers:", nums)
# Print a message indicating the operation to be performed
print("Sort the numbers of the said list by the sum of their digits:")
# Print the result of the test function applied to 'nums'
print(test(nums))
# Assign another specific list of numbers 'nums' to the variable
nums = [23, 2, 9, 34, 8, 9, 10, 74]
# Print a message indicating the original list of numbers
print("\nOriginal list of numbers:", nums)
# Print a message indicating the operation to be performed
print("Sort the numbers of the said list by the sum of their digits:")
# Print the result of the test function applied to 'nums'
print(test(nums))
Sample Output:
Original list of numbers: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Sort the numbers of the said list by the sum of their digits: [10, 11, 20, 12, 13, 14, 15, 16, 17, 18, 19] Original list of numbers: [23, 2, 9, 34, 8, 9, 10, 74] Sort the numbers of the said list by the sum of their digits: [10, 2, 23, 34, 8, 9, 9, 74]
Flowchart:
Python Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Find the largest integer divisor of a number n that is less than n.
Next: Determine which triples sum to zero.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/puzzles/python-programming-puzzles-38.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics