Unique Character Sorting in Python

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to remove duplicate characters from a given string and output the processed string in ascending order. This is a common programming task that helps in data cleaning and preprocessing.

👀 Preview

## Example 1
$ python3 distinct.py
sfafgxdhgdvdfndfzcsf
acdfghnsvxz

## Example 2
$ python3 distinct.py
asdyadsysdy
adsy

🎯 Tasks

In this project, you will learn:

  • How to use Python's built-in functions to remove duplicate characters from a string
  • How to sort the unique characters in ascending order
  • How to join the sorted unique characters back into a string

🏆 Achievements

After completing this project, you will be able to:

  • Write a function that removes duplicate characters from a string
  • Implement a solution to sort the unique characters in ascending order
  • Combine the unique and sorted characters back into a new string
  • Test your solution with different input strings and verify the correctness of the output

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-302760{{"`Unique Character Sorting in Python`"}} python/variables_data_types -.-> lab-302760{{"`Unique Character Sorting in Python`"}} python/conditional_statements -.-> lab-302760{{"`Unique Character Sorting in Python`"}} python/function_definition -.-> lab-302760{{"`Unique Character Sorting in Python`"}} python/standard_libraries -.-> lab-302760{{"`Unique Character Sorting in Python`"}} python/data_collections -.-> lab-302760{{"`Unique Character Sorting in Python`"}} python/build_in_functions -.-> lab-302760{{"`Unique Character Sorting in Python`"}} end

Understand the Problem

In this step, you will understand the problem statement and the requirements for the "Remove Duplicates" project.

The problem statement is as follows:
Given a string, remove the duplicate elements and output the processed string in ascending order.

The requirements are:

  • Complete the remove_duplicates function in the distinct.py file as required.
  • The function should take a string as input and return the string with duplicate characters removed, and the remaining characters sorted in ascending order.
  • The program should be able to handle different input strings and output the processed string correctly.

Implement the remove_duplicates Function

In this step, you will implement the remove_duplicates function in the distinct.py file.

Here's the starter code for the distinct.py file:

def remove_duplicates(string):
    """
    your code
    """


if __name__ == "__main__":
    string = input("")
    result = remove_duplicates(string)
    print(result)

To implement the remove_duplicates function, follow these steps:

  1. Convert the input string to a set to remove duplicate characters. This will give you a collection of unique characters.
  2. Sort the unique characters using the sorted() function.
  3. Join the sorted unique characters back into a string using the join() method.

Here's the complete implementation of the remove_duplicates function:

def remove_duplicates(string):
    unique_chars = sorted(set(string))  ## Remove duplicates and sort the characters
    return "".join(unique_chars)  ## Join the unique characters back into a string

Test the remove_duplicates Function

In this step, you will test the remove_duplicates function with different input strings.

  1. Run the distinct.py script:
python3 distinct.py
  1. Enter the first example string: sfafgxdhgdvdfndfzcsf

    • The output should be: acdfghnsvxz
  2. Enter the second example string: asdyadsysdy

    • The output should be: adsy
  3. Try testing the function with your own input strings and verify that the output is correct.

Congratulations! You have successfully implemented the "Remove Duplicates" project. If you have any questions or need further assistance, feel free to ask.

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like