Skip to main content

Sieve of Eratosthenes

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

More Python Posts

AnyTree Randomizer

0 likes • Apr 15, 2021 • 0 views
Python
import anytree as at
import random as rm
# Generate a tree with node_count many nodes. Each has a number key that shows when it was made and a randomly selected color, red or white.
def random_tree(node_count):
# Generates the list of nodes
nodes = []
for i in range(node_count):
test = rm.randint(1,2)
if test == 1:
nodes.append(at.Node(str(i),color="white"))
else:
nodes.append(at.Node(str(i),color="red"))
#Creates the various main branches
for i in range(node_count):
for j in range(i, len(nodes)):
test = rm.randint(1,len(nodes))
if test == 1 and nodes[j].parent == None and (not nodes[i] == nodes[j]):
nodes[j].parent = nodes[i]
#Collects all the main branches into a single tree with the first node being the root
for i in range(1, node_count):
if nodes[i].parent == None and (not nodes[i] == nodes[0]):
nodes[i].parent = nodes[0]
return nodes[0]

Differentiate Between del, remove, and pop on a List

0 likes • May 31, 2023 • 0 views
Python
my_list = [1, 2, 3, 4, 5]
removed_element = my_list.pop(2) # Remove and return element at index 2
print(removed_element) # 3
print(my_list) # [1, 2, 4, 5]
last_element = my_list.pop() # Remove and return the last element
print(last_element) # 5
print(my_list) # [1, 2, 4]

Read Dataset from excel file

0 likes • Oct 7, 2022 • 0 views
Python
import pandas as pd
x = pd.read_excel(FILE_NAME)
print(x)

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

Convert Decimal to Binary and Hexadecimal

0 likes • May 31, 2023 • 0 views
Python
# Prompt user for a decimal number
decimal = int(input("Enter a decimal number: "))
# Convert decimal to binary
binary = bin(decimal)
# Convert decimal to hexadecimal
hexadecimal = hex(decimal)
# Display the results
print("Binary:", binary)
print("Hexadecimal:", hexadecimal)

clamp number

0 likes • Nov 19, 2022 • 0 views
Python
def clamp_number(num, a, b):
return max(min(num, max(a, b)), min(a, b))
clamp_number(2, 3, 5) # 3
clamp_number(1, -1, -5) # -1