Skip to content
🤔prompts chat🧠
🔍
question:Generate a python program to print the given sentence in reverse. Sentence: This is an example

answer:def reverse_string(s): return s[::-1] print(reverse_string("This is an example"))

question:Create a python program to calculate Fibonacci sequence for a given number. n = 8

answer:def fibonacci(n): if n == 0: return 0 elif n == 1: return 1 else: return fibonacci(n-1) + fibonacci(n-2) if __name__ == '__main__': n = 8 print(fibonacci(n))

question:Develop an algorithm in Python to find the longest common prefix in an array of strings. array = [apple, ape, app, application]

answer:def commonPrefix(array): result = "" if len(array) > 0: # Find the minimum length of the word in the array. min_length = min([len(i) for i in array]) # Iterate through the letters in the word with the minimum length. for i in range(min_length): curr_char = array[0][i] flag = True # For each letter, check if each of the words have that letter. for j in range(len(array)): if array[j][i] != curr_char: flag = False break if flag: result += curr_char else: break return result commonPrefix(["apple", "ape", "app", "application"])

question:Design a program in Python to implement a queue data structure.

answer:class Queue: # Initialize the queue def __init__(self): self.items = [] # Add an element to the queue def enqueue(self, item): self.items.insert(0, item) # Remove the last element from the queue def dequeue(self): return self.items.pop() # Check if the queue is empty def isEmpty(self): return self.items == [] # Get the size of the queue def size(self): return len(self.items)

Released under the MIT License.

has loaded