w3resource

Python: Sort numbers based on strings

Python Programming Puzzles: Exercise-41 with Solution

Write a Python program to sort numbers based on strings.

Input:  six one four one two three
Output:
one two three four six

Input:  six one four three two nine eight
Output:
one two three four six eight nine

Input:  nine eight seven six five four three two  one
Output:
one two three four five six seven eight nine

Visual Presentation:

Python: Sort numbers based on strings.

Sample Solution-1:

Python Code:

# License: https://bit.ly/3oLErEI

# Define a function named 'test' that takes a string 'strs' as input
def test(strs):
    # Split the string of words into a list and filter only those present in the predefined list
    selected_numbers = [x for x in 'one two three four five six seven eight nine'.split() if x in strs]

    # Join the selected numbers into a string with spaces in between
    result = ' '.join(selected_numbers)

    # Return the final string
    return result

# Assign a specific string 'strs' to the variable
strs = "six one four one two three"
# Print a message indicating the original string
print("Original string:", strs)
# Print a message indicating the operation to be performed
print("Sort numbers based on said strings:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable
strs = "six one four three two nine eight"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Sort numbers based on said strings:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable
strs = "nine eight seven six five four three two  one"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Sort numbers based on said strings:")
# Print the result of the test function applied to 'strs'
print(test(strs))

Sample Output:

Original string: six one four one two three
Sort numbers based on said strings:
one two three four six

Original string: six one four three two nine eight
Sort numbers based on said strings:
one two three four six eight nine

Original string: nine eight seven six five four three two  one
Sort numbers based on said strings:
one two three four five six seven eight nine

Flowchart:

Flowchart: Python - Find string s that, when case is flipped gives target where vowels are replaced by chars two later.

Sample Solution-2:

Python Code:

# License: https://bit.ly/3oLErEI

# Define a function named 'test' that takes a string 'strs' as input
def test(strs):
    # Define a list of words representing numbers from zero to nine, repeated multiple times
    nums = 'zero zero zero zero zero zero zero zero zero zero one one one one one one one one one one one one two two two two three three three three three four four four four four four five five five five five five five six six six six six six six seven seven seven seven seven seven seven seven eight eight eight eight eight eight eight eight nine nine nine nine nine nine nine nine nine'.split()

    # Split the input string into a list of words and map the indices of the numbers to the corresponding words
    sorted_indices = sorted([nums.index(x) for x in strs.split()])

    # Create a new list by selecting numbers from the predefined list based on the sorted indices
    sorted_numbers = [nums[i] for i in sorted_indices]

    # Join the sorted numbers into a string with spaces in between and remove trailing whitespaces
    result = " ".join(sorted_numbers).rstrip()

    # Return the final string
    return result

# Assign a specific string 'strs' to the variable
strs = "six one four one two three"
# Print a message indicating the original string
print("Original string:", strs)
# Print a message indicating the operation to be performed
print("Sort numbers based on said strings:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable
strs = "six one four three two nine eight"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Sort numbers based on said strings:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable
strs = "nine eight seven six five four three two zero one"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Sort numbers based on said strings:")
# Print the result of the test function applied to 'strs'
print(test(strs))

Sample Output:

Original string: six one four one two three
Sort numbers based on said strings:
one one two three four six

Original string: six one four three two nine eight
Sort numbers based on said strings:
one two three four six eight nine

Original string: nine eight seven six five four three two zero one
Sort numbers based on said strings:
zero one two three four five six seven eight nine

Flowchart:

Flowchart: Python - Find string s that, when case is flipped gives target where vowels are replaced by chars two later.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Find string s that, when case is flipped gives target where vowels are replaced by chars two later.
Next: Find the set of distinct characters in a string, ignoring case.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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://198.211.115.131/python-exercises/puzzles/python-programming-puzzles-41.php