Check if a given string is binary string or not

def check(string) :

p = set(string)

s = {'0', '1'}

if s == p or p == {'0'} or p == {'1'}:

print("Yes")

else :

print("No")


if __name__ == "__main__" :

string = "101010000111"

check(string)


Using itemgetter sort list of dictionaries by values

from operator import itemgetter

lis = [{ "name" : "Pooja", "age" : 22},

{ "name" : "Arjun", "age" : 20 },

{ "name" : "Nikhil" , "age" : 19 }]

print ("The list printed sorting by age:")

print (sorted(lis, key=itemgetter('age')))

print ("\r")

print ("The list printed sorting by age and name:")

print (sorted(lis, key=itemgetter('age', 'name')))

print ("\r")

print ("The list printed sorting by age in descending order:")

print (sorted(lis, key=itemgetter('age'),reverse = True))


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))