Challenges of Python Course
Challenges of Python Course
------------
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())
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
------------
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)
dict3 = {}
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'''
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
return days