Get Kth Column of Matrix using list comprehension

test_list = [[4, 5, 8], [8, 1, 20], [7, 12, 10]]

print("The original list is : " + str(test_list))

K = 2

res = [sub[K] for sub in test_list]

print("The Kth column of matrix is : " + str(res))


Words Frequency in String Shorthands

 # Using Counter() + split()

from collections import Counter

test_str = 'Python for engineers . Solution for python programs'

print("The original string is : " + str(test_str))

res = Counter(test_str.split())

print("The words frequency : " + str(dict(res)))


Program for Maximum height when coins are arranged in a triangle

def squareRoot(n):

x = n

y = 1

e = 0.000001 

while (x - y > e):

x = (x + y) / 2

y = n/x

return x

def findMaximumHeight(N):

n = 1 + 8*N

maxh = (-1 + squareRoot(n)) / 2

return int(maxh)

N = 12

print(findMaximumHeight(N))


Program for Product of unique prime factors of a number

import math

def productPrimeFactors(n):

product = 1

if (n % 2 == 0):

product *= 2

while (n%2 == 0):

n = n/2

for i in range (3, int(math.sqrt(n)), 2):

if (n % i == 0):

product = product * i

while (n%i == 0):

n = n/i

if (n > 2):

product = product * n

return product

n = 44

print (int(productPrimeFactors(n)))


Program in numpy percentile

import numpy as np

arr = [[14, 17, 12, 33, 44],

[15, 6, 27, 8, 19],

[23, 2, 54, 1, 4,]]

print("\narr : \n", arr)

print("\n50th Percentile of arr, axis = None : ",

np.percentile(arr, 50))

print("0th Percentile of arr, axis = None : ",

np.percentile(arr, 0))

print("\n50th Percentile of arr, axis = 0 : ",

np.percentile(arr, 50, axis =0))

print("0th Percentile of arr, axis = 0 : ",

np.percentile(arr, 0, axis =0))