Python code to remove punctuation from the string
def r_punc():
test_str = "end, is best : for ! Nlp ;"
print("The original string is : " + test_str)
punc = '''!()-[]{};:'"\, <>./?@#$%^&*_~'''
for ele in test_str:
if ele in punc:
test_str = test_str.replace(ele, "")
print("The string after punctuation filter : " + test_str)
htness_4
Python program to implement Gnome Sort
def gnomeSort(arr, n):
index = 0
while index < n:
if index == 0:
index = index + 1
if arr[index] >= arr[index - 1]:
index = index + 1
else:
arr[index], arr[index - 1] = arr[index - 1], arr[index]
index = index - 1
return arr
arr = [34, 2, 10, -9]
n = len(arr)
arr = gnomeSort(arr, n)
print("Sorted seqquence after applying Gnome Sort :")
for i in arr:
print(i)
Python program to implement 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=" ")
Python program to implement stooge sort
def stoogesort(arr, l, h):
if l >= h:
return
if arr[l] > arr[h]:
t = arr[l]
arr[l] = arr[h]
arr[h] = t
if h - l + 1 > 2:
t = (int)((h - l + 1) / 3)
stoogesort(arr, l, (h - t))
stoogesort(arr, l + t, (h))
stoogesort(arr, l, (h - t))
arr = [2, 4, 5, 3, 1]
n = len(arr)
stoogesort(arr, 0, n - 1)
for i in range(0, n):
print(arr[i], end= \' \')
Python program to find the difference between two times
def difference(h1, m1, h2, m2):
t1 = h1 * 60 + m1
t2 = h2 * 60 + m2
if (t1 == t2):
print("Both are same times")
return
else:
diff = t2 - t1
h = (int(diff / 60)) % 24
m = diff % 60
print(h, ":", m)
difference(7, 20, 9, 45)
difference(15, 23, 18, 54)
difference(16, 20, 16, 20)
def convert24(str1):
if str1[-2:] == "AM" and str1[:2] == "12":
return "00" + str1[2:-2]
elif str1[-2:] == "AM":
return str1[:-2]
elif str1[-2:] == "PM" and str1[:2] == "12":
return str1[:-2]
else:
return str(int(str1[:2]) + 12) + str1[2:8]
print(convert24("08:05:45 PM"))
Python 3 program to find time for a given angle.
def calcAngle(hh, mm):
hour_angle = 0.5 * (hh * 60 + mm)
minute_angle = 6 * mm
angle = abs(hour_angle - minute_angle)
angle = min(360 - angle, angle)
return angle
Python function to print all time when angle between hour hand and minute
def printTime(theta):
for hh in range(0, 12):
for mm in range(0, 60):
if (calcAngle(hh, mm) == theta):
print(hh, ":", mm, sep="")
return
print("Input angle not valid.")
return
theta = 90.0
printTime(theta)
Write a python function to count number of times a function is called
def counter(fn):
count = 0
def inner(*args, **kwargs):
nonlocal count
count += 1
print(f'Function {fn.__name__} was called {count} times.')
return fn(*"args, **kwargs)
return inner
Write a python function to remove duplicate items from the list
def remove_duplicatesinlist(lst):
return len(lst) == len(set(lst))