Skip to main content

screencap.py

0 likes • Jan 23, 2021 • 0 views
Python
Loading...

More Python Posts

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

Print "X" pattern

0 likes • Nov 19, 2022 • 0 views
Python
def print_x_pattern(size):
i,j = 0,size - 1
while j >= 0 and i < size:
initial_spaces = ' '*min(i,j)
middle_spaces = ' '*(abs(i - j) - 1)
final_spaces = ' '*(size - 1 - max(i,j))
if j == i:
print(initial_spaces + '*' + final_spaces)
else:
print(initial_spaces + '*' + middle_spaces + '*' + final_spaces)
i += 1
j -= 1
print_x_pattern(7)

Display Calendar

0 likes • May 31, 2023 • 0 views
Python
import calendar
# Prompt user for year and month
year = int(input("Enter the year: "))
month = int(input("Enter the month: "))
# Create a calendar object
cal = calendar.monthcalendar(year, month)
# Display the calendar
print(calendar.month_name[month], year)
print("Mon Tue Wed Thu Fri Sat Sun")
for week in cal:
for day in week:
if day == 0:
print(" ", end="")
else:
print(str(day).rjust(2), " ", end="")
print()

collect dictionary

0 likes • Nov 19, 2022 • 0 views
Python
from collections import defaultdict
def collect_dictionary(obj):
inv_obj = defaultdict(list)
for key, value in obj.items():
inv_obj[value].append(key)
return dict(inv_obj)
ages = {
'Peter': 10,
'Isabel': 10,
'Anna': 9,
}
collect_dictionary(ages) # { 10: ['Peter', 'Isabel'], 9: ['Anna'] }

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)

when predicate lambda

0 likes • Nov 19, 2022 • 0 views
Python
def when(predicate, when_true):
return lambda x: when_true(x) if predicate(x) else x
double_even_numbers = when(lambda x: x % 2 == 0, lambda x : x * 2)
print(double_even_numbers(2)) # 4
print(double_even_numbers(1)) # 1