Skip to main content

return multiple values from a function

0 likes • Jun 1, 2023 • 0 views
Python
Loading...

More Python Posts

Shuffle Deck of Cards

0 likes • May 31, 2023 • 1 view
Python
import random
# Define the ranks, suits, and create a deck
ranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
deck = [(rank, suit) for rank in ranks for suit in suits]
# Shuffle the deck
random.shuffle(deck)
# Display the shuffled deck
for card in deck:
print(card[0], "of", card[1])

Create a Pascal’s Triangle

0 likes • May 31, 2023 • 0 views
Python
def generate_pascals_triangle(num_rows):
triangle = []
for row in range(num_rows):
# Initialize the row with 1
current_row = [1]
# Calculate the values for the current row
if row > 0:
previous_row = triangle[row - 1]
for i in range(len(previous_row) - 1):
current_row.append(previous_row[i] + previous_row[i + 1])
# Append 1 at the end of the row
current_row.append(1)
# Add the current row to the triangle
triangle.append(current_row)
return triangle
def display_pascals_triangle(triangle):
for row in triangle:
for number in row:
print(number, end=" ")
print()
# Prompt the user for the number of rows
num_rows = int(input("Enter the number of rows for Pascal's Triangle: "))
# Generate Pascal's Triangle
pascals_triangle = generate_pascals_triangle(num_rows)
# Display Pascal's Triangle
display_pascals_triangle(pascals_triangle)

Caesar Encryption

0 likes • Mar 10, 2021 • 0 views
Python
import string
def caesar(text, shift, alphabets):
def shift_alphabet(alphabet):
return alphabet[shift:] + alphabet[:shift]
shifted_alphabets = tuple(map(shift_alphabet, alphabets))
final_alphabet = "".join(alphabets)
final_shifted_alphabet = "".join(shifted_alphabets)
table = str.maketrans(final_alphabet, final_shifted_alphabet)
return text.translate(table)
plain_text = "Hey Skrome, welcome to CodeCatch"
print(caesar(plain_text, 8, [string.ascii_lowercase, string.ascii_uppercase, string.punctuation]))

Using logic with sets

0 likes • Nov 18, 2022 • 0 views
Python
#Sets
U = {0,1,2,3,4,5,6,7,8,9}
P = {1,2,3,4}
Q = {4,5,6}
R = {3,4,6,8,9}
def set2bits(xs,us) :
bs=[]
for x in us :
if x in xs :
bs.append(1)
else:
bs.append(0)
assert len(us) == len(bs)
return bs
def union(set1,set2) :
finalSet = set()
bitList1 = set2bits(set1, U)
bitList2 = set2bits(set2, U)
for i in range(len(U)) :
if(bitList1[i] or bitList2[i]) :
finalSet.add(i)
return finalSet
def intersection(set1,set2) :
finalSet = set()
bitList1 = set2bits(set1, U)
bitList2 = set2bits(set2, U)
for i in range(len(U)) :
if(bitList1[i] and bitList2[i]) :
finalSet.add(i)
return finalSet
def compliment(set1) :
finalSet = set()
bitList = set2bits(set1, U)
for i in range(len(U)) :
if(not bitList[i]) :
finalSet.add(i)
return finalSet
def implication(a,b):
return union(compliment(a), b)
###########################################################################################
###################### Problems 1-6 #######################################
###########################################################################################
#p \/ (q /\ r) = (p \/ q) /\ (p \/ r)
def prob1():
return union(P, intersection(Q,R)) == intersection(union(P,Q), union(P,R))
#p /\ (q \/ r) = (p /\ q) \/ (p /\ r)
def prob2():
return intersection(P, union(Q,R)) == union(intersection(P,Q), intersection(P,R))
#~(p /\ q) = ~p \/ ~q
def prob3():
return compliment(intersection(P,R)) == union(compliment(P), compliment(R))
#~(p \/ q) = ~p /\ ~q
def prob4():
return compliment(union(P,Q)) == intersection(compliment(P), compliment(Q))
#(p=>q) = (~q => ~p)
def prob5():
return implication(P,Q) == implication(compliment(Q), compliment(P))
#(p => q) /\ (q => r) => (p => r)
def prob6():
return implication(intersection(implication(P,Q), implication(Q,R)), implication(P,R))
print("Problem 1: ", prob1())
print("Problem 2: ", prob2())
print("Problem 3: ", prob3())
print("Problem 4: ", prob4())
print("Problem 5: ", prob5())
print("Problem 6: ", prob6())
'''
Problem 1: True
Problem 2: True
Problem 3: True
Problem 4: True
Problem 5: True
Problem 6: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
'''

return byte size

0 likes • Nov 19, 2022 • 1 view
Python
def byte_size(s):
return len(s.encode('utf-8'))
byte_size('😀') # 4
byte_size('Hello World') # 11

delay time lambda

0 likes • Nov 19, 2022 • 0 views
Python
from time import sleep
def delay(fn, ms, *args):
sleep(ms / 1000)
return fn(*args)
delay(lambda x: print(x), 1000, 'later') # prints 'later' after one second