Appearance
question:Fulfill the following criteria in Python Given an array of numbers, find the length of the longest increasing subsequence
answer:def longest_increasing_subsequence(nums): # Dynamic programming approach # to maintain a list of increasing subsequences # with their respective lengths subseq_lengths = [1 for _ in range(len(nums))] # Traverse through pairs of numbers to find sequences for i in range(1, len(nums)): for j in range(0, i): if nums[i] > nums[j] and subseq_lengths[i] < subseq_lengths[j] + 1: subseq_lengths[i] = subseq_lengths[j]+1 # Return the maximum length of increasing subsequence return max(subseq_lengths)
question:Create a Python class to represent a circle having radius, color and area as its data members.
answer:class Circle: def __init__(self, radius, color): self.radius = radius self.color = color self.area = 3.14 * (radius**2) def get_radius(self): return self.radius def get_color(self): return self.color def get_area(self): return self.area if __name__ == '__main__': c = Circle(4, 'blue') print("Radius of circle:", c.get_radius()) print("Color of circle:", c.get_color()) print("Area of circle:", c.get_area())
question:Create a function in Python to find the sum of two numbers in a given array and return their indices. [3, 5, 7, 10]
answer:def sumIndices(arr, n): Indices = [] # traverse array elements for i in range(0, n): # complement will be checked in # same array for j in range(i + 1, n): # if complement found, # return the indices if(arr[i] + arr[j] == n): return (i, j) arr = [3, 5, 7, 10] n = 10 Indices = sumIndices(arr, n) print(Indices)
question:Generate a Python class to store a student’s information including their name, age, and phone number.
answer:class Student: def __init__(self, name, age, phone_number): self.name = name self.age = age self.phone_number = phone_number