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

Error Eliminator2nd Round Python Answer

Uploaded by

sumanthshetty080
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 views2 pages

Error Eliminator2nd Round Python Answer

Uploaded by

sumanthshetty080
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
You are on page 1/ 2

Error Eliminator

2nd Round

Answers

Answer

def is_even(n):
if n % 2 == 0: # Corrected '=' to '=='
return True
else:
return False

# Example usage:
print(is_even(4)) # Output: True
print(is_even(5)) # Output: False

Answer

def fibonacci(n):
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]

fib_sequence = [0, 1]
for i in range(2, n):
fib_sequence.append(fib_sequence[i-1] + fib_sequence[i-2]) #
Corrected indices
return fib_sequence

# Example usage:
print(fibonacci(5)) # Output: [0, 1, 1, 2, 3]

Answer

def square(n):
return n * n # Corrected the error

# Example usage:
print(square(3)) # Output: 9
Answer

def merge_sorted_lists(list1, list2):


Merged_list = []
I, j = 0, 0

while I < len(list1) and j < len(list2):


if list1[I] <= list2[j]: # Corrected to handle equal elements
properly
Merged_list.append(list1[I])
I += 1
else:
Merged_list.append(list2[j])
j += 1

while I < len(list1):


Merged_list.append(list1[I])
I += 1

while j < len(list2):


Merged_list.append(list2[j])
j += 1

return Merged_list

# Example usage:
print(merge_sorted_lists([1, 3, 5], [2, 4, 6])) # Output: [1, 2, 3, 4,
5, 6]
Answer

def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
else:
return False
def leap_year_in_range(start_year,end_year):
leap_years=[]
for year in range(start_year,end_year+1):
if is_leap_year(year):
leap_years.append(year)
return leap_years
#Example Usage
start_year=1900
end_year=2020
print(f"Leap years from {start_year} to {end_year}:")
print(leap_year_in_range(start_year,end_year))
#output should be: [1904,1908,1912,...,2016,2020]

You might also like