0% found this document useful (0 votes)
14 views5 pages

21bcs7877 - Manoj Gogoi - It Skills - Day 6

The document contains code solutions for four problems: Bulls and Cows, Sort Character by Frequency, First Unique Character in a String, and Maximum Gap. Each solution is implemented as a class method that utilizes data structures like dictionaries and lists to solve the respective problems efficiently. The learning outcomes highlight skills in string analysis, frequency sorting, unique character identification, and maximum gap calculation.

Uploaded by

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

21bcs7877 - Manoj Gogoi - It Skills - Day 6

The document contains code solutions for four problems: Bulls and Cows, Sort Character by Frequency, First Unique Character in a String, and Maximum Gap. Each solution is implemented as a class method that utilizes data structures like dictionaries and lists to solve the respective problems efficiently. The learning outcomes highlight skills in string analysis, frequency sorting, unique character identification, and maximum gap calculation.

Uploaded by

yegan13026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

NAME : Manoj Gogoi

UID : 21BCS7877
SECTION/GROUP : 21BCS-CC-652-B
SUBJECT : IT SKILLS

DAY 6

CODE:

1.Bulls And Cows

class Solution:
def getHint(self, secret: str, guess: str) -> str:
bulls = 0 # Count of digits in the correct position
cows = 0 # Count of digits in the wrong position

# Create a dictionary to store the count of digits in the secret number


secret_dict = {}
for char in secret:
secret_dict[char] = secret_dict.get(char, 0) + 1

# Iterate through the guess


for i in range(len(guess)):
char = guess[i]

# If the character exists in the secret number and hasn't been used up for bulls yet
if char in secret_dict and secret_dict[char] > 0:
# If the character is in the correct position, it's a bull
if char == secret[i]:
bulls += 1
# Otherwise, it's a cow
else:
cows += 1
# Decrement the count of that character in the secret number dictionary
secret_dict[char] -= 1

return f"{bulls}A{cows}B" # Return the hint formatted as “xAyB”

2. Sort Character by Frequency

class Solution:
def frequencySort(self, s: str) -> str:
# Create a dictionary to store the count of each character
char_counts = {}
for char in s:
char_counts[char] = char_counts.get(char, 0) + 1

# Sort the characters by their count in descending order


sorted_chars = sorted(char_counts, key=char_counts.get, reverse=True)

# Build the result string by appending characters based on their count


result = ""
for char in sorted_chars:
result += char * char_counts[char]

return result
3. First Unique Character in a String

class Solution:
def firstUniqChar(self, s: str) -> int:
seen_once = set()
seen_twice = set()
for char in s:
if char in seen_once:
seen_twice.add(char)
seen_once.remove(char)
elif char not in seen_twice:
seen_once.add(char)

for i, char in enumerate(s):


if char in seen_once:
return i
return -1

4. Maximum Gap

class Solution:
def maximumGap(self, nums: List[int]) -> int:
n = len(nums)
if n < 2:
return 0

min_val = min(nums)
max_val = max(nums)
gap = (max_val - min_val) / (n - 1) if n - 1 > 0 else 0
buckets = [None] * (n - 1)

for num in nums:


bucket_index = int((num - min_val) / gap)
if 0 <= bucket_index < len(buckets):
buckets[bucket_index] = num if buckets[bucket_index] is None else
max(buckets[bucket_index], num)

max_gap = 0
prev = None
for bucket in buckets:
if bucket is not None:
if prev is not None:
max_gap = max(max_gap, bucket - prev)
prev = bucket

return max_gap
OUTPUT :
LEARNING OUTCOMES :

• String Analysis (Bulls and Cows): Learned to analyze strings for character patterns using
dictionaries and conditional statements.
• Frequency Sorting: Mastered sorting characters by frequency using dictionaries and string
manipulation.
• Unique Character Finding: Explored finding the first non-repeating character with options for
dictionaries or two pointers.
• Maximum Gap in Array: Implemented the bucket approach for finding the maximum difference
in a sorted array.

You might also like