0% found this document useful (0 votes)
4 views3 pages

Error EliminatorPython Set

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)
4 views3 pages

Error EliminatorPython Set

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/ 3

Error Eliminator

2nd Round

Python Questions

1) Def is_even(n):
If n % 2 = 0:

Return True

Else:

Return False

# Example usage:

Print(is_even(4)) # Output should be: True

Print(is_even(5)) # Output should be: False

2) 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] + fib_sequence[I – 1])

Return fib_sequence

# Example usage:

Print(fibonacci(5)) # Output should be: [0, 1, 1, 2, 3]

3) def sqaure(n):
return n * n + n
# Example usage:

print(square(3)) # Output should be: 9

4) 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]:

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

5) def is_leap_year(year):

if (year % 4 == 0 and year % 100 != 0) or (year % 100 == 0 and

year % 400 == 0):

return True

else:

return False

def leap_years_in_range(start_year,end_year):

leap_year=[]
for year in range(start_year,end_year+1):

if is_leap_year(year):

leap_years.append(year)

#Example Usage:

start_year=1900

end_year=2020

print(f”Leap years from {start_year} to {end_year}:”)

print(leap_years_in_range(start_year,end_year))

#Output should be:[1904,1908,1912,…,2016,2020]

You might also like