Skip to main content

UNT CSCE 2100 Assignment 6

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

More Python Posts

hex to rgb

0 likes • Nov 19, 2022 • 2 views
Python
def hex_to_rgb(hex):
return tuple(int(hex[i:i+2], 16) for i in (0, 2, 4))
hex_to_rgb('FFA501') # (255, 165, 1)

Mad libs generator

0 likes • Nov 19, 2022 • 0 views
Python
#Loop back to this point once code finishes
loop = 1
while (loop < 10):
#All the questions that the program asks the user
noun = input("Choose a noun: ")
p_noun = input("Choose a plural noun: ")
noun2 = input("Choose a noun: ")
place = input("Name a place: ")
adjective = input("Choose an adjective (Describing word): ")
noun3 = input("Choose a noun: ")
#Displays the story based on the users input
print ("------------------------------------------")
print ("Be kind to your",noun,"- footed", p_noun)
print ("For a duck may be somebody's", noun2,",")
print ("Be kind to your",p_noun,"in",place)
print ("Where the weather is always",adjective,".")
print ()
print ("You may think that is this the",noun3,",")
print ("Well it is.")
print ("------------------------------------------")
#Loop back to "loop = 1"
loop = loop + 1

Binary search

0 likes • Sep 22, 2023 • 11 views
Python
# Python binary search function
def binary_search(arr, target):
left = 0
right = len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
# Usage
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target = 7
result = binary_search(arr, target)
if result != -1:
print(f"Element is present at index {result}")
else:
print("Element is not present in array")

Finding NULL values within set

0 likes • Oct 7, 2022 • 1 view
Python
x[cat_var].isnull().sum().sort_values(ascending=False)

Dictionary Sort

0 likes • Nov 18, 2022 • 0 views
Python
mydict = {'carl':40, 'alan':2, 'bob':1, 'danny':0}
# How to sort a dict by value Python 3>
sort = {key:value for key, value in sorted(mydict.items(), key=lambda kv: (kv[1], kv[0]))}
print(sort)
# How to sort a dict by key Python 3>
sort = {key:mydict[key] for key in sorted(mydict.keys())}
print(sort)

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]