Skip to main content

UNT CSCE 2100 Assignment 6

Nov 18, 2022AustinLeath
Loading...

More Python Posts

Return Letter Combinations

Nov 18, 2022AustinLeath

0 likes • 0 views

# @return a list of strings, [s1, s2]
def letterCombinations(self, digits):
if '' == digits: return []
kvmaps = {
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz'
}
return reduce(lambda acc, digit: [x + y for x in acc for y in kvmaps[digit]], digits, [''])

Connect to MYSQL and create a database

Nov 19, 2022CodeCatch

0 likes • 0 views

import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE mydatabase")

Topological sort

Nov 19, 2022CodeCatch

0 likes • 0 views

#Python program to print topological sorting of a DAG
from collections import defaultdict
#Class to represent a graph
class Graph:
def __init__(self,vertices):
self.graph = defaultdict(list) #dictionary containing adjacency List
self.V = vertices #No. of vertices
# function to add an edge to graph
def addEdge(self,u,v):
self.graph[u].append(v)
# A recursive function used by topologicalSort
def topologicalSortUtil(self,v,visited,stack):
# Mark the current node as visited.
visited[v] = True
# Recur for all the vertices adjacent to this vertex
for i in self.graph[v]:
if visited[i] == False:
self.topologicalSortUtil(i,visited,stack)
# Push current vertex to stack which stores result
stack.insert(0,v)
# The function to do Topological Sort. It uses recursive
# topologicalSortUtil()
def topologicalSort(self):
# Mark all the vertices as not visited
visited = [False]*self.V
stack =[]
# Call the recursive helper function to store Topological
# Sort starting from all vertices one by one
for i in range(self.V):
if visited[i] == False:
self.topologicalSortUtil(i,visited,stack)
# Print contents of stack
print(stack)
g= Graph(6)
g.addEdge(5, 2);
g.addEdge(5, 0);
g.addEdge(4, 0);
g.addEdge(4, 1);
g.addEdge(2, 3);
g.addEdge(3, 1);
print("Following is a Topological Sort of the given graph")
g.topologicalSort()

Sherlock Holmes Curious Lockbox Solver

Mar 12, 2021LeifMessinger

0 likes • 0 views

import copy
begining = [False,False,False,False,False,None,True,True,True,True,True]
#False = black True = white
its = [0]
def swap(layout, step):
layoutCopy = copy.deepcopy(layout)
layoutCopy[(step[0]+step[1])], layoutCopy[step[1]] = layoutCopy[step[1]], layoutCopy[(step[0]+step[1])]
return layoutCopy
def isSolved(layout):
for i in range(len(layout)):
if(layout[i] == False):
return (i >= (len(layout)/2))
def recurse(layout, its, steps = []):
if isSolved(layout):
its[0] += 1
print(layout,list(x[0] for x in steps))
return
step = None
for i in range(len(layout)):
if(layout[i] == None):
if(i >= 1): #If the empty space could have something to the left
if(layout[i - 1] == False):
step = [-1,i]
recurse(swap(layout,step), its, (steps+[step]))
if(i > 1): #If the empty space could have something 2 to the left
if(layout[i - 2] == False):
step = [-2,i]
recurse(swap(layout,step), its, (steps+[step]))
if(i < (len(layout)-1)): #If the empty space could have something to the right
if(layout[i + 1] == True):
step = [1,i]
recurse(swap(layout,step), its, (steps+[step]))
if(i < (len(layout)-2)): #If the empty space could have something to the right
if(layout[i + 2] == True):
step = [2,i]
recurse(swap(layout,step), its, (steps+[step]))
its[0] += 1
#return None
recurse(begining,its,[])
print(its[0])

read file contents into a list

Jun 1, 2023CodeCatch

0 likes • 0 views

filename = "data.txt"
with open(filename, "r") as file:
file_contents = file.readlines()
file_contents = [line.strip() for line in file_contents]
print("File contents:")
for line in file_contents:
print(line)

Nodes and Trees

Nov 18, 2022AustinLeath

0 likes • 1 view

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)