Skip to main content

lambda example

0 likes • Nov 19, 2022 • 0 views
Python
Loading...

More Python Posts

Radix sort

0 likes • Nov 19, 2022 • 1 view
Python
# Python program for implementation of Radix Sort
# A function to do counting sort of arr[] according to
# the digit represented by exp.
def countingSort(arr, exp1):
n = len(arr)
# The output array elements that will have sorted arr
output = [0] * (n)
# initialize count array as 0
count = [0] * (10)
# Store count of occurrences in count[]
for i in range(0, n):
index = (arr[i]/exp1)
count[int((index)%10)] += 1
# Change count[i] so that count[i] now contains actual
# position of this digit in output array
for i in range(1,10):
count[i] += count[i-1]
# Build the output array
i = n-1
while i>=0:
index = (arr[i]/exp1)
output[ count[ int((index)%10) ] - 1] = arr[i]
count[int((index)%10)] -= 1
i -= 1
# Copying the output array to arr[],
# so that arr now contains sorted numbers
i = 0
for i in range(0,len(arr)):
arr[i] = output[i]
# Method to do Radix Sort
def radixSort(arr):
# Find the maximum number to know number of digits
max1 = max(arr)
# Do counting sort for every digit. Note that instead
# of passing digit number, exp is passed. exp is 10^i
# where i is current digit number
exp = 1
while max1/exp > 0:
countingSort(arr,exp)
exp *= 10
# Driver code to test above
arr = [ 170, 45, 75, 90, 802, 24, 2, 66]
radixSort(arr)
for i in range(len(arr)):
print(arr[i]),

Nodes and Trees

0 likes • Nov 18, 2022 • 0 views
Python
import random
class Node:
def __init__(self, c):
self.left = None
self.right = None
self.color = c
def SetColor(self,c) :
self.color = c
def PrintNode(self) :
print(self.color)
def insert(s, root, i, n):
if i < n:
temp = Node(s[i])
root = temp
root.left = insert(s, root.left,2 * i + 1, n)
root.right = insert(s, root.right,2 * i + 2, n)
return root
def MakeTree(s) :
list = insert(s,None,0,len(s))
return list
def MakeSet() :
s = []
count = random.randint(7,12)
for _ in range(count) :
color = random.randint(0,1) == 0 and "Red" or "White"
s.append(color)
return s
def ChangeColor(root) :
if (root != None) :
if (root.color == "White") :
root.SetColor("Red")
ChangeColor(root.left)
ChangeColor(root.right)
def PrintList(root) :
if root.left != None :
PrintList(root.left)
else :
root.PrintNode()
if root.right != None :
PrintList(root.right)
else :
root.PrintNode()
t1 = MakeTree(MakeSet())
print("Original Colors For Tree 1:\n")
PrintList(t1)
ChangeColor(t1)
print("New Colors For Tree 1:\n")
PrintList(t1)
t2 = MakeTree(MakeSet())
print("Original Colors For Tree 2:\n")
PrintList(t2)
ChangeColor(t2)
print("New Colors For Tree 2:\n")
PrintList(t2)
t3 = MakeTree(MakeSet())
print("Original Colors For Tree 3:\n")
PrintList(t3)
ChangeColor(t3)
print("New Colors For Tree 3:\n")
PrintList(t3)

Size of tuple

0 likes • Nov 19, 2022 • 0 views
Python
import sys
# sample Tuples
Tuple1 = ("A", 1, "B", 2, "C", 3)
Tuple2 = ("Geek1", "Raju", "Geek2", "Nikhil", "Geek3", "Deepanshu")
Tuple3 = ((1, "Lion"), ( 2, "Tiger"), (3, "Fox"), (4, "Wolf"))
# print the sizes of sample Tuples
print("Size of Tuple1: " + str(sys.getsizeof(Tuple1)) + "bytes")
print("Size of Tuple2: " + str(sys.getsizeof(Tuple2)) + "bytes")
print("Size of Tuple3: " + str(sys.getsizeof(Tuple3)) + "bytes")

convert bytes to a string

0 likes • Jun 1, 2023 • 0 views
Python
bytes_data = b'Hello, World!'
string_data = bytes_data.decode('utf-8')
print("String:", string_data)

Delete all even numbers

0 likes • Nov 19, 2022 • 0 views
Python
# Deleting all even numbers from a list
a = [1,2,3,4,5]
del a[1::2]
print(a)

Lonely Integer

0 likes • Feb 26, 2023 • 0 views
Python
#84 48 13 20 61 20 33 97 34 45 6 63 71 66 24 57 92 74 6 25 51 86 48 15 64 55 77 30 56 53 37 99 9 59 57 61 30 97 50 63 59 62 39 32 34 4 96 51 8 86 10 62 16 55 81 88 71 25 27 78 79 88 92 50 16 8 67 82 67 37 84 3 33 4 78 98 39 64 98 94 24 82 45 3 53 74 96 9 10 94 13 79 15 27 56 66 32 81 77
# xor a list of integers to find the lonely integer
res = a[0]
for i in range(1,len(a)):
res = res ^ a[i]