Development Interview Questions (Python)
Dynamic Programming
Check if any valid sequence is divisible by MGiven an array of N integers, using ‘+’ and ‘-‘ between the elements check if there is a way to form a sequence of numbers which evaluate to a number divisible by M
Examples:
Input: arr = {1, 2, 3, 4, 6}
M = 4
Output: True,
There is a valid sequence i. e., (1 - 2 + 3 + 4 + 6), which evaluates to 12 that is divisible by 4
Input: arr = {1, 3, 9}
M = 2
Output: False
There is no sequence which evaluates to a number divisible by M.
Answer (in Python):
def check_if_any_valid_sequence_is_divisible(my_list, my_num): if len(my_list) == 0: return print("False") elif len(my_list) == 1: #print(my_list[0] // my_num) #print(my_list[0] % my_num) if my_list[0] == 0: return print("False") else: if ((my_list[0] % my_num) == 0) and ((my_list[0] // my_num) == 1): return print("True") else: return print("False") elif len(my_list) > 1: temp = False for data in my_list: if data != 0: # print(data // my_num) # print(data % my_num) if ((data % my_num) == 0) and ((data // my_num) == 1): temp = True return print(temp) def create_all_sequence(my_list, my_num): if len(my_list) == 0: return print("False") elif len(my_list) == 1: check_if_any_valid_sequence_is_divisible(my_list, my_num) elif len(my_list) > 1: previous = None temp_list = [] for data in my_list: if not previous: previous = data else: if not temp_list: temp_list.append(previous + data) temp_list.append(previous - data) else: #print(temp_list) temp_list1 = [] for info in temp_list: temp_list1.append(info + data) temp_list1.append(info - data) temp_list = temp_list1 #print(temp_list) check_if_any_valid_sequence_is_divisible(temp_list, my_num) m = 4arr = [] print("Check if any valid sequence in " + str(arr) + " is divisible by " + str(m)) create_all_sequence(arr, m) arr = [1] print("Check if any valid sequence in " + str(arr) + " is divisible by " + str(m)) create_all_sequence(arr, m) arr = [4] print("Check if any valid sequence in " + str(arr) + " is divisible by " + str(m)) create_all_sequence(arr, m) arr = [1, 2] print("Check if any valid sequence in " + str(arr) + " is divisible by " + str(m)) create_all_sequence(arr, m) arr = [1, 3] print("Check if any valid sequence in " + str(arr) + " is divisible by " + str(m)) create_all_sequence(arr, m) arr = [1, 2, 3] print("Check if any valid sequence in " + str(arr) + " is divisible by " + str(m)) create_all_sequence(arr, m) arr = [1, 2, 3, 4] print("Check if any valid sequence in " + str(arr) + " is divisible by " + str(m)) create_all_sequence(arr, m) arr = [1, 2, 4, 7, 9] print("Check if any valid sequence in " + str(arr) + " is divisible by " + str(m)) create_all_sequence(arr, m) arr = [1, 2, 3, 4, 6] print("Check if any valid sequence in " + str(arr) + " is divisible by " + str(m)) create_all_sequence(arr, m)
Arrays & String manipulation (palindrome)
Given a string, calculate and print all its possible palindromic partitions. (A palindrome is a word, phrase, or sequence that reads the same backwards as forwards)Examples:
Input: nitin
Output:
n i t i n
n iti n
nitin
Input: geeks
Output:
g e e k s
g ee k s
Answer (in Python):
def check_palindrome_str(given_str): # make string aggressive lower given_str = given_str.casefold() # reverse string reversed_str = reversed(given_str) #print(list(given_str)) #print(list(reversed_str)) # compare strings if list(given_str) == list(reversed_str): return True else: return False def palindrome_str(given_str): # set var num_pali = 0 list_pali = [] list_str = list(given_str) #print(given_str) #print(list_str) # create all combinations of substrings for letter1 in list_str: for letter2 in given_str: temp_str = letter1 + letter2 if temp_str in given_str: if temp_str not in list_str: list_str.append(temp_str) #print(temp_str) #print(list_str) # Loop thru words in list for words in list_str: #print(words) #print(check_palindrome_str(given_str)) # check if word is palindrome if check_palindrome_str(words): num_pali += 1 list_pali.append(words) print("'" + given_str + "' has " + str(num_pali) + " palindrome sub strings") if list_pali: for word in list_pali: print(word) my_str = "nitin"palindrome_str(my_str) my_str = "greeks"palindrome_str(my_str) my_str = "racecar"palindrome_str(my_str)
Comments
Post a Comment