0% found this document useful (0 votes)
3 views

Challenges of Python Course

The document contains multiple coding challenges with solutions in Python. It includes functions for determining leap years, generating FizzBuzz sequences, performing set operations on sentences, and finding the second largest number in a list. Additionally, it covers tuple manipulation, dictionary operations, and calculations related to the area of a circle and the reduction of active cases.

Uploaded by

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

Challenges of Python Course

The document contains multiple coding challenges with solutions in Python. It includes functions for determining leap years, generating FizzBuzz sequences, performing set operations on sentences, and finding the second largest number in a list. Additionally, it covers tuple manipulation, dictionary operations, and calculations related to the area of a circle and the reduction of active cases.

Uploaded by

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

challenge 6

------------

1)class Solution:
def solve(self, A):
if A % 400 == 0:
return 1
elif A % 4 == 0 and A % 100 != 0:
return 1
else:
return 0

2)class Solution:
def fizzBuzz(self, A):
result = []
for i in range(1, A + 1):
if i % 3 == 0 and i % 5 == 0:
result.append("FizzBuzz")
elif i % 3 == 0:
result.append("Fizz")
elif i % 5 == 0:
result.append("Buzz")
else:
result.append(str(i))
return result

challenge 8
------------
1)def set_operation(sent1, sent2):
'''
input: sent1, sent2 - two sentences taken as inputs
output: return the sum of length of unique words.
'''
# Split the sentences into words and convert to sets to get unique words
unique_words1 = set(sent1.split())
unique_words2 = set(sent2.split())

# Return the total count of unique words from both sentences


return len(unique_words1) + len(unique_words2)

2)class Solution:
# @param A : string
# @param B : string
# @return an integer
def solve(self, A, B):
from collections import Counter

lenA = len(A)
lenB = len(B)
target_count = Counter(B)

result = 0
window_count = Counter()
question_marks = 0

for i in range(lenA):
char = A[i]
if char == '?':
question_marks += 1
else:
window_count[char] += 1

if i >= lenB:
out_char = A[i - lenB]
if out_char == '?':
question_marks -= 1
else:
window_count[out_char] -= 1
if window_count[out_char] == 0:
del window_count[out_char]

if i >= lenB - 1:
total_missing = 0
for c in target_count:
count_in_window = window_count.get(c, 0)
if target_count[c] > count_in_window:
total_missing += target_count[c] - count_in_window
if total_missing == question_marks:
result += 1

return result

challenge 9
------------
2)class Solution:
def solve(self, A):
if len(A) < 2:
return -1

first = second = -1

for num in A:
if num > first:
second = first
first = num
elif num < first and num > second:
second = num

return second
3)class Solution:
def solve(self, A):
N = len(A)
for i in range(N):
for j in range(N):
if i == j:
if A[i][j] != 1:
return 0
else:
if A[i][j] != 0:
return 0
return 1

challenge 10
------------

1)def get_index(tup, elem):


'''
input: elem - indicates the element in the tuple, tup - indicates the tuple
output: Return the index of the element.
'''
if elem in tup:
return tup.index(elem)
else:
return -1

2)def odd_even_split_tuple(tup):
'''
input: tup - indicates the tuple
output: print two tuples one for even indexed and odd indexed in the given
output format
'''
# Since input is 1-based indexing, adjust slicing accordingly:
odd_tuple = tup[0::2] # 1-based odd indices: 1,3,5... (0-based: 0,2,4...)
even_tuple = tup[1::2] # 1-based even indices: 2,4,6... (0-based: 1,3,5...)

print("Odd:", odd_tuple)
print("Even:", even_tuple)

challenge 11
------------

1)def returnSum(dict):
sum = 0
# Iterate through the dictionary values and add them
for value in dict.values():
sum += value
print(sum)

2)def commonKey(dict1, dict2):


'''dict1, dict2 are the two dictionaries
print the required dictionary'''

dict3 = {}

for key in dict1:


if key in dict2:
dict3[key] = dict1[key] + dict2[key]

print(dict3))

challenge 13
------------
1)def circle_area(r):
'''input: r = A numerical value as radius
output: Return the area of circle as ans upto 2 decimal places'''

# Use the value of pi as 3.14159


pi = 3.14159
# Calculate the area of the circle
ans = pi * (r ** 2)

# Round the result to 2 decimal places


ans = round(ans, 2)

# Return the calculated area


return ans

2)class Solution:
# @param A : integer
# @param B : integer
# @param C : integer
# @return an integer
def solve(self, A, B, C):
# The effective reduction in active cases per day
daily_reduction = A - B

# Calculate the number of days to reach 0 active cases


days = (C + daily_reduction - 1) // daily_reduction # This is equivalent
to math.ceil(C / daily_reduction)

return days

You might also like