Error Eliminator
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
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
2) #include <stdio.h>
#include <string.h>
Void reverse_string(char *str) {
Int n = strlen(str);
For (int I = 0; I < n / 2; ++i) {
Char temp = str[i];
Str[i] = str[n – i];
Str[n – i] = temp;
Int main() {
Char str[] = “hello”;
Reverse_string(str);
Printf(“%s\n”, str); // Output should be: “olleh”
Return 0;
Answer
#include <stdio.h>
#include <string.h>
void reverse_string(char *str) {
int n = strlen(str);
for (int i = 0; i < n / 2; ++i) {
char temp = str[i];
str[i] = str[n - i - 1]; // Corrected index
str[n - i - 1] = temp; // Corrected index
int main() {
char str[] = "hello";
reverse_string(str);
printf("%s\n", str); // Output: "olleh"
return 0;
}
3) 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]
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]
4) #include <stdio.h>
#include <limits.h>
Int maxSubArraySum(int a[], int size) {
Int max_so_far = INT_MIN, max_ending_here = 0;
For (int I = 0; I < size; i++) {
Max_ending_here = max_ending_here + a[i];
If (max_so_far < max_ending_here)
Max_so_far = max_ending_here;
If (max_ending_here < 0)
Max_ending_here = max_so_far; // Error here
Return max_so_far;
Int main() {
Int a[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
Int n = sizeof(a) / sizeof(a[0]);
Int max_sum = maxSubArraySum(a, n);
Printf(“Maximum contiguous sum is %d\n”, max_sum); // Output should be: 6
Return 0;
}
Answer
#include <stdio.h>
#include <limits.h>
Int maxSubArraySum(int a[], int size) {
Int max_so_far = INT_MIN, max_ending_here = 0;
For (int I = 0; I < size; i++) {
Max_ending_here = max_ending_here + a[i];
If (max_so_far < max_ending_here)
Max_so_far = max_ending_here;
If (max_ending_here < 0)
Max_ending_here = 0; // Corrected to reset to 0 instead of max_so_far
Return max_so_far;
Int main() {
Int a[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
Int n = sizeof(a) / sizeof(a[0]);
Int max_sum = maxSubArraySum(a, n);
Printf(“Maximum contiguous sum is %d\n”, max_sum); // Output: 6
Return 0;
5) def square(n):
return n * n + n # Error here
# Example usage:
print(square(3)) # Output should be: 9
Answer
Def square(n):
Return n * n # Corrected the error
# Example usage:
Print(square(3)) # Output: 9
6) #include <stdio.h>
Int factorial(int n) {
If (n == 0) return 1;
Return n * factorial(n – 1);
Int main() {
Int n = 5;
Printf(“Factorial of %d is %d\n”, n, factorial(n)); // Output should be: 120
Return 0;
Answer
#include <stdio.h>
Int factorial(int n) {
If (n < 0) return -1; // Handle negative input
If (n == 0) return 1;
Return n * factorial(n – 1);
}
Int main() {
Int n = 5;
Printf(“Factorial of %d is %d\n”, n, factorial(n)); // Output: 120
Return 0;
7) Def is_palindrome(s):
Return s == s[::-1]
# Example usage:
Print(is_palindrome(“racecar”)) # Output should be: True
Print(is_palindrome(“hello”)) # Output should be: False
Answer
Def is_palindrome(s):
Return s == s[::-1]
# Example usage:
Print(is_palindrome(“racecar”)) # Output: True
Print(is_palindrome(“hello”)) # Output: False
8) #include <stdio.h>
Int binarySearch(int arr[], int l, int r, int x) {
If (r >= l) {
Int mid = l + (r – l) / 2;
If (arr[mid] == x)
Return mid;
If (arr[mid] > x)
Return binarySearch(arr, l, mid – 1, x);
Return binarySearch(arr, mid + 1, r, x);
Return -1;
Int main() {
Int arr[] = {2, 3, 4, 10, 40};
Int n = sizeof(arr) / sizeof(arr[0]);
Int x = 10;
Int result = binarySearch(arr, 0, n-1, x);
(result == -1) ? printf(“Element is not present in array\n”)
: printf(“Element is present at index %d\n”, result); // Output should be: Element is present at
index 3
Return 0;
Answer
#include <stdio.h>
Int binarySearch(int arr[], int l, int r, int x) {
If (r >= l) {
Int mid = l + (r – l) / 2;
If (arr[mid] == x)
Return mid;
If (arr[mid] > x)
Return binarySearch(arr, l, mid – 1, x);
Return binarySearch(arr, mid + 1, r, x);
Return -1;
Int main() {
Int arr[] = {2, 3, 4, 10, 40};
Int n = sizeof(arr) / sizeof(arr[0]);
Int x = 10;
Int result = binarySearch(arr, 0, n-1, x);
(result == -1) ? printf(“Element is not present in array\n”)
: printf(“Element is present at index %d\n”, result); // Output: Element is present at index 3
Return 0;
9)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
# Example usage:
Print(merge_sorted_lists([1, 3, 5], [2, 4, 6])) # Output should be: [1, 2, 3, 4, 5, 6]
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]