Thursday, January 4, 2024

Python Programs for interview FAQ

 #Q1: Reverse a String in Python

def reverse_string(input_str): reversed_str = '' for character in input_str: reversed_str = character + reversed_str return reversed_str # Example original_string = "Satyam" reversed_string = reverse_string(original_string) print("Original String:", original_string) print("Reversed String:", reversed_string) #Q2: Checking a word is palindrome or not in python def is_palindrome(input_str): # Remove spaces and convert to lowercase for case-insensitive comparison cleaned_str = ''.join(input_str.split()).lower() # Check if the cleaned string is the same when reversed return cleaned_str == cleaned_str[::-1] # Example word = input("Enter a word: ") if is_palindrome(word): print(f"{word} is a palindrome.") else: print(f"{word} is not a palindrome.") #Q3: Write a program for fibonacci series def fibonacci(n): fib_series = [0, 1] while len(fib_series) < n: fib_series.append(fib_series[-1] + fib_series[-2]) return fib_series # Example: Generate the first 10 numbers in the Fibonacci series k = 10 fibonacci_series = fibonacci(k) print(f"Fibonacci Series (first {k} numbers):", fibonacci_series) Fibonacci Series (first 10 numbers): [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

No comments:

Post a Comment