0% found this document useful (0 votes)
18 views

Python Code Practice Solution

Method 1 (Naive) checks every triplet combination in an array to find a Pythagorean triplet (where a2 = b2 + c2) by calculating the squares of each element and comparing sums, returning true if a triplet is found. Method 2 (Use Sorting) squares all elements, sorts the array, then fixes one element and uses two indices moving inward to check if any triplet sums to the fixed element, returning true if so. The program rotates a square matrix 90 degrees clockwise in-place without using extra space by iterating through the matrix and swapping each element with the element in its clockwise position. The sliding window maximum program finds the maximum of each contiguous subarray of

Uploaded by

socialhari6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Python Code Practice Solution

Method 1 (Naive) checks every triplet combination in an array to find a Pythagorean triplet (where a2 = b2 + c2) by calculating the squares of each element and comparing sums, returning true if a triplet is found. Method 2 (Use Sorting) squares all elements, sorts the array, then fixes one element and uses two indices moving inward to check if any triplet sums to the fixed element, returning true if so. The program rotates a square matrix 90 degrees clockwise in-place without using extra space by iterating through the matrix and swapping each element with the element in its clockwise position. The sliding window maximum program finds the maximum of each contiguous subarray of

Uploaded by

socialhari6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Find a triplet a, b, c such that a2 = b2 + c2

Method 1 (Naive)
# Python program to check if there is Pythagorean
# triplet in given array Returns true if there is Pythagorean triplet in ar[0..n-1]

def isTriplet(ar, n):


j=0
for i in range(n - 2):
for k in range(j + 1, n):
for j in range(i + 1, n - 1):
# Calculate square of array elements
x = ar[i]*ar[i]
y = ar[j]*ar[j]
z = ar[k]*ar[k]
if (x == y + z or y == x + z or z == x + y):
return 1

# If we reach here, no triplet found


return 0
# Driver program to test above function
ar = [3, 1, 4, 6, 5]
ar_size = len(ar)

if(isTriplet(ar, ar_size)):
print("Yes")
else:
print("No")

Method 2 (Use Sorting)


# Python program that returns true if there is a Pythagorean Triplet in a given array. Returns
true if there is Pythagorean triplet in ar[0..n-1]
def isTriplet(ar, n):
# Square all the elemennts
for i in range(n):
ar[i] = ar[i] * ar[i]

# sort array elements


ar.sort()
# fix one element and find other two i goes from n - 1 to 2
for i in range(n-1, 1, -1):
# start two index variables from two corners of the array and move them toward
each other
j=0
k=i-1
while (j < k):
# A triplet found
if (ar[j] + ar[k] == ar[i]):
return True
else:
if (ar[j] + ar[k] < ar[i]):
j=j+1
else:
k=k-1
# If we reach here, then no triplet found
return False
# Driver program to test above function */
ar = [3, 1, 4, 6, 5]
ar_size = len(ar)
if(isTriplet(ar, ar_size)):
print("Yes")
else:
print("No")

2. Given an square matrix, turn it by 90 degrees in anti-clockwise direction without


using any extra space.
# Python3 program to rotate a matrix by 90 degrees
N=4

# An Inplace function to rotate N x N matrix by 90 degrees in anti-clockwise direction


def rotateMatrix(mat):

# Consider all squares one by one


for x in range(0, int(N/2)):

# Consider elements in group of 4 in current square


for y in range(x, N-x-1):
# store current cell in temp variable
temp = mat[x][y]
# move values from right to top
mat[x][y] = mat[y][N-1-x]
# move values from bottom to right
mat[y][N-1-x] = mat[N-1-x][N-1-y]
# move values from left to bottom
mat[N-1-x][N-1-y] = mat[N-1-y][x]

# assign temp to left


mat[N-1-y][x] = temp

# Function to pr the matrix


def displayMatrix( mat ):
for i in range(0, N):
for j in range(0, N):
print (mat[i][j], end = ' ')
print ("")
# Driver Code
mat = [[0 for x in range(N)] for y in range(N)]
# Test case 1
mat = [ [1, 2, 3, 4 ],
[5, 6, 7, 8 ],
[9, 10, 11, 12 ],
[13, 14, 15, 16 ] ]

'''
# Test case 2
mat = [ [1, 2, 3 ], [4, 5, 6 ],[7, 8, 9 ] ]
# Test case 3
mat = [ [1, 2 ],[4, 5 ] ] '''

rotateMatrix(mat)

# Print rotated matrix


displayMatrix(mat)

3. Sliding Window Maximum (Maximum of all subarrays of size k)

# Python program to find the maximum for each and every contiguous subarray of size k
# Method to find the maximum for each and every contiguous subarray of s of size k
def printMax(arr, n, k):
max = 0
for i in range(n - k + 1):
max = arr[i]
for j in range(1, k):
if arr[i + j] > max:
max = arr[i + j]
print(str(max) + " ", end = "")

# Driver method
if __name__=="__main__":
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
n = len(arr)
k=3
printMax(arr, n, k)

You might also like