0% found this document useful (0 votes)
6 views6 pages

Find First Non-Repeating Character

The document outlines two programming problems: finding the first non-repeating character in a string and sorting a list of integers by frequency and value. It provides solution code for both problems, along with test cases and explanations for potential failures. The document also suggests improvements for handling mixed case inputs, empty strings, and special characters in the first problem, and emphasizes correct sorting logic for the second problem.

Uploaded by

sm9395
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Find First Non-Repeating Character

The document outlines two programming problems: finding the first non-repeating character in a string and sorting a list of integers by frequency and value. It provides solution code for both problems, along with test cases and explanations for potential failures. The document also suggests improvements for handling mixed case inputs, empty strings, and special characters in the first problem, and emphasizes correct sorting logic for the second problem.

Uploaded by

sm9395
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Problem Statement
Write a program to find the first non-repeating character in a
given string. If all characters repeat, the program should
output an appropriate message.
2. Solution Code
def first_non_repeating_character(string):
# Create a dictionary to count character frequencies
char_count = {}

# Count each character's occurrences in the string


for char in string:
char_count[char] = char_count.get(char, 0) + 1

# Find the first character with a frequency of 1


for char in string:
if char_count[char] == 1:
return char

return None # No non-repeating character found

# Input string
string = input("Enter a string: ")

# Get the result


result = first_non_repeating_character(string)

if result:
print(f"The first non-repeating character is: '{result}'")
else:
print("No non-repeating characters found in the string.")
3. Screenshot of Execution
Correct Test Case:

Input:swiss | output: w | Expected Output: w

Refute Test Case:

Input: aabbcc | output: No non-repeating character | Expected Output: a


[Link] of Failure
Input with Mixed Case:
Input: "aAbBcC"
Failure: It may treat uppercase and lowercase letters as
distinct ('A' != 'a').
Solution: Normalize the string to lowercase or uppercase
before processing:
python
string = [Link]()

Empty String:

Input: ""
Failure: The program may return nothing or crash due to no
characters.
Solution: Add a check for empty input at the start:
if not [Link]():
print("Input cannot be empty.")
exit()

Special Characters or Spaces:

Input: "a b c a b"


Failure: Spaces may be treated as characters.
Solution: Ignore non-alphabetic characters during
processing:
python
string = ''.join(filter([Link], string))
1. Problem Statement
Write a program that takes a list of integers as input and
sorts the list in descending order of frequency. If two
numbers have the same frequency, sort them in ascending
order of value.
2. Solution Code
from collections import Counter

def custom_sort(numbers):

# Count the frequency of each number

freq = Counter(numbers)

# Sort based on frequency (descending) and value (ascending)

sorted_numbers = sorted(numbers, key=lambda x: (-freq[x], x))

return sorted_numbers

# Input from the user

numbers = list(map(int, input("Enter numbers separated by spaces:


").split()))

# Perform custom sort

result = custom_sort(numbers)

# Display the sorted list

print("Sorted list:", result)


3. Screenshot of Execution
Correct Test Case:

 Correct Test Case:


 Input: 4 5 6 5 4 3 4
 Output: [4, 4, 4, 5, 5, 3, 6]
o Explanation: 4 appears 3 times (highest
frequency), 5 appears 2 times, followed by 3 and
6 (1 time each). Within the same frequency,
smaller numbers come first.
Refute Test Case:

Refute Test Case:


 Input: 4 4 4 5 5 6 6 6
 Output: [6, 6, 6, 4, 4, 4, 5, 5]
o Explanation: 6 and 4 have the same frequency
but may be ordered incorrectly due to sorting
logic or ties not resolved.
4. Explanation of Failure

To resolve the failure, ensure that the sorting key


explicitly prioritizes frequency first (descending) and
value second (ascending).
Updated Code Snippet:
python
# Fix: Ensure proper tie-breaking in the sorting logic
sorted_numbers = sorted(numbers, key=lambda x: (-
freq[x], x))
Why This Works:
 The -freq[x] ensures descending order of
frequency.
 The x ensures ascending order of values when
frequencies are the same.

You might also like