Square Mania

import turtle as t

pen = t.Turtle()

pen.color("cyan")

pen.speed(0)

colors = ["green","black","orange","navy","green","black","orange","navy"]

def draw_square(color):

        for side in range(4):

                pen.forward(100)

                pen.right(90)

                for side in range(4):

                        pen.forward(50)

                        pen.right(90)

pen.penup()

pen.back(40)

pen.pendown()

for color in colors:

        pen.color(color)

        draw_square(color)

        pen.forward(50)

        pen.left(45)


pen.hideturtle()

t.done()

Program for Print Number series without using loop

def PrintNumber(N, Original, K, flag):

print(N, end = " ")

if (N <= 0):

if(flag==0):

flag = 1

else:

flag = 0

if (N == Original and (not(flag))):

return


if (flag == True):

PrintNumber(N - K, Original, K, flag)

return

if (not(flag)):

PrintNumber(N + K, Original, K, flag);

return

N = 20

K = 6

PrintNumber(N, N, K, True)


Regex to extract maximum numeric value from a string

import re

def extractMax(input):

numbers = re.findall('\d+',input)

numbers = map(int,numbers)

print (max(numbers))

if __name__ == "__main__":

input = '100klh564abc365bg'

extractMax(input)


Pigeonhole Sort

def pigeonhole_sort(a):

my_min = min(a)

my_max = max(a)

size = my_max - my_min + 1

holes = [0] * size

for x in a:

assert type(x) is int, "integers only please"

holes[x - my_min] += 1

i = 0

for count in range(size):

while holes[count] > 0:

holes[count] -= 1

a[i] = count + my_min

i += 1

a = [8, 3, 2, 7, 4, 6, 8]

print("Sorted order is : ", end =" ")

pigeonhole_sort(a)

for i in range(0, len(a)):

print(a[i], end =" ")


Order Tuples by List

lst = [('for', 3), ('python', 9), ('engineers', 10)]

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

ord_list = ['python', 'for', 'engineers']

temp = dict(lst)

res = [(key, temp[key]) for key in ord_list]

print("The ordered tuple list : " + str(res))


Check if binary representations of two numbers are anagram

from collections import Counter

def checkAnagram(num1,num2):

bin1 = bin(num1)[2:]

bin2 = bin(num2)[2:]

zeros = abs(len(bin1)-len(bin2))

if (len(bin1)>len(bin2)):

bin2 = zeros * '0' + bin2

else:

bin1 = zeros * '0' + bin1

dict1 = Counter(bin1)

dict2 = Counter(bin2)

if dict1 == dict2:

print('Yes')

else:

print('No')

if __name__ == "__main__":

num1 = 8

num2 = 4

checkAnagram(num1,num2)


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