21bcs7877 - Manoj Gogoi - It Skills - Day 6
21bcs7877 - Manoj Gogoi - It Skills - Day 6
UID : 21BCS7877
SECTION/GROUP : 21BCS-CC-652-B
SUBJECT : IT SKILLS
DAY 6
CODE:
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
# 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
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
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)
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)
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.