Note: If you are thinking of getting a chance in top MNC companies then try to solve all the competitive programming challenges given below and crack any MNC recruitment interview test.
We have covered all important topic from the Data Structure [searching, sorting, Graphs, Trees, heaps, queues, recursion, etc.], Network and socket programming, Mathematic problems, and a lot of different programming challenges. Let's solve it.
Question 1: Python List Challenge working on elements.
Consider a list (list = []). You can perform the following commands:
1. insert i e : Insert integer e at position i.
2. print : Print the list.
3. remove e : Delete the first occurrence of integer e.
4. append e : Insert integer e at the end of the list.
5. sort : Sort the list.
6. pop : Pop the last element from the list.
7. reverse : Reverse the list.
Initialize your list and read in the value of n followed by n lines of commands where each command will be of the 7 types listed above. Iterate through each command in order and perform the corresponding operation on your list.
Continue here:
Example 1:
N = 12
append 1
append 2
insert 3 1
print
insert 6 1
insert 0 3
print
sort
append 30
append 50
pop
print
Explanation of Example 1:
append 1 : Append 1 to the list, arr = [1]. append 2 : Append 2 to the list, arr = [1, 2]. insert 3 1 : Insert 3 at index 1, arr = [1, 3, 2]. print : Print the array. insert 6 1 : Insert 6 at index 1, arr=[1,6,3,2]. insert 0 3 : Insert 0 at index 3, arr=[1,6,3,0,2]. print : Print the array. sort : sort the array in ascending order.
append 30 : Append 30 to the list, arr =[1,6,3,0,2,30]. append 50 : Append 1 to the list, arr =[1,6,3,0,2,30,50]. pop : pop the item from the list. arr =[1,6,3,0,2,30]. print : Print the array. Final Output: [1,3,2] [1,6,3,0,2] [0,1,2,3,6,30]
Solution
if __name__=='__main__': N=int(input()) s=[] for i in range(N): e=input().strip().split() s.append(e) # In some interview exam the inbuild sort() function will not work, # then you might need to sort the elements manually like this: def sort_list_fun(lst): new_sort_list=[] while lst: minimum = lst[0] # arbitrary number in list for x in lst: if x < minimum: minimum = x new_sort_list.append(minimum) lst.remove(minimum) return new_sort_list f= [] for i in s: if i[0]== "sort": f = sort_list_fun(f) elif i[0]== "insert": f.insert(int(i[1]), int(i[2])) elif i[0]== "remove": f.remove(int(i[1])) elif i[0]== "append": f.append(int(i[1])) elif i[0]== "pop": f.pop() elif i[0]== "reverse": f.reverse() else: print(f)
luis@martin:~$ python3 list_solution.py [1,3,2] [1,6,3,0,2] [0,1,2,3,6,30] luis@martin:~$ luis@martin:~$ python3 list_solution.py > python_output.txt #Save output into the new file. luis@martin:~$ cat python_output.txt [1,3,2] [1,6,3,0,2] [0,1,2,3,6,30] luis@martin:~$
Question 2: "Python 2D Array challlenge".
Problem Description: Consider a function 'hourglassSum' which has the following parameter:
* int arr[6][6]: an array of integers.
And the function returns:
* int value: the maximum hourglass sum.
Given a 6 x 6 2D Array, arr:
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
An hourglass in A is a subset of values with indices falling in this pattern in arr's graphical representation:
a b c
d
e f g
Continue here:
There are 16 hourglasses in arr. An hourglass sum is the sum of an hourglass' values. Calculate the hourglass sum for every hourglass in arr, then print the maximum hourglass sum.
The array will always be 6 x 6.
Sample Input 1: -9 -9 -9 1 1 1 0 -9 0 4 3 2 -9 -9 -9 1 2 3 0 0 8 6 6 0 0 0 0 -2 0 0 0 0 1 2 4 0 The 16 hourglass sums are: -63, -34, 9, 12, -10, 0, 28, 23, -27, -11, -2, 10, 9, 17, 25, 18 Output: 28 [Hint] The highest hourglass sum is 28 from the hourglass beginning at row 1, column 2: 0 4 3 1 8 6 6 Sample Input 2: 1 1 1 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 2 4 4 0 0 0 0 2 0 0 0 0 1 2 4 0 Sample Output: 19 Explanation: arr contains the following hourglasses: 1 1 1 1 1 0 1 0 0 0 0 0 1 0 0 0 1 1 1 1 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 2 0 2 4 2 4 4 4 4 0 1 1 1 1 1 0 1 0 0 0 0 0 0 2 4 4 0 0 0 0 0 2 0 2 0 2 0 0 0 0 2 0 2 4 2 4 4 4 4 0 0 0 2 0 0 0 1 0 1 2 1 2 4 2 4 0 [Hint - ] The hourglass with the maximum sum (19) is: 2 4 4 2 1 2 4
Solution:
#!/bin/python3 import math import os import random import re import sys # # Complete the 'hourglassSum' function below. # # The function is expected to return an INTEGER. # The function accepts 2D_INTEGER_ARRAY arr as parameter. # def hourglassSum(arr): input_list = arr # input_list= [[1, 1, 1, 0, 0, 0], [0, 1, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0], [0, 0, 2, 4, 4, 0], [0, 0, 0, 2, 0, 0], [0, 0, 1, 2, 4, 0]] temp_list = [] for i in range(6): for j in range(4): temp_list.append([input_list[i][j],input_list[i][j+1],input_list[i][j+2]]) startlist =temp_list n = 4 endlist = [[] for _ in range(n)] for index, item in enumerate(startlist): endlist[index % n].append(item) temp_list_2 = [] r= [] for i in range(4): # r.append(u) for j in range(4): item_sum = sum(endlist[i][j])+ sum(endlist[i][j+2]) temp_list_2.append(item_sum) r.append(temp_list_2) temp_list_2 = [] six_pair_items = list(map(list, zip(*r))) l = [] g = [] for i in range(1,5): for j in range(1,5): l.append(input_list[i][j]) startlist =l n = 4 endlist = [[] for _ in range(n)] for index, item in enumerate(startlist): endlist[index % n].append(item) single_pair_item= list(map(list, zip(*endlist))) temp_list_3 = [] max_hourglassSum =[] for i in range(4): for j in range(4): s =six_pair_items[i][j]+ single_pair_item[i][j] temp_list_3.append(s) max_hourglassSum.append(temp_list_3) temp_list_3 = [] return max([ max(i) for i in max_hourglassSum]) # Refer '__main__' method code which is given below if required.
# if __name__ == '__main__':
if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') arr = [] for _ in range(6): arr.append(list(map(int, input().rstrip().split()))) result = hourglassSum(arr) fptr.write(str(result) + '\n') fptr.close()
Question 3: Write a programe to return the absolute difference between the sums of the matrix's two diagonals as a integer.
Problem description-
The Function "diagonalDifference" takes the array type single parameter:
• int arr[n][m]: an array of integers
Return-
int: the absolute diagonal difference
Constraints-
•-100 ≤ arr[i][j] ≤ 100
Continue here:
Input Format- The first line contains a single integer, n, the number of rows and columns in the square matrix arr. Each of the next n lines describes a row, arr[i], and consists of ʼn space-separated integers arr[i][j]. Output Format- return the absolute difference between the sums of the matrix`s two diagonals Sample Input- 3 11 2 4 4 5 6 10 8 -12 Sample Output- 15 Explanation- The primary diagonal is- 11 5 -12 The Secondary diagonal is- 4 5 10 Difference: |(11+5-12) - (4+5+10)| = 15
Solution:
#!/bin/python3 import math import os import random import re import sys # # Complete the 'diagonalDifference' function below. # # The function is expected to return an INTEGER. # The function accepts 2D_INTEGER_ARRAY arr as parameter. # def diagonalDifference(arr): # Write your code here l = len(arr) k = l-1 a_sum = 0 b_sum = 0 for i in range(l): a = arr[i][i] a_sum += a # print("a_sum: ", a_sum) for i in range(l): b = arr[i][k] b_sum += b k-=1 # print("b_sum: ",b_sum) return abs( a_sum - b_sum ) # Refer '__main__' method code which is given below if required.
# if __name__ == '__main__':
if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') n = int(input().strip()) arr = [] for _ in range(n): arr.append(list(map(int, input().rstrip().split()))) result = diagonalDifference(arr) fptr.write(str(result) + '\n') fptr.close()
Question : "Python List Comprehensions".
Solution:
# You are given three integer 'x','y' and 'z' representing the dimensions of a cuboid along with an integer 'n'. if __name__ == '__main__': x = int(input()) y = int(input()) z = int(input()) n = int(input()) # Print a list of all possible coordinates given by (i,j,k) on a 3D grid, # where the sum of 'i+j+k' is not equal to 'n'. # Here, 0<= i <=x; # 0<= j <=y; # 0<= k <=k; # Please use list comprehensions rather than multiple loops, as a learning exercise. lst = [ [ i,j,k] for i in range(x+1) for j in range(y+1) for k in range(z+1) if i + j + k != n ] print(lst)