Initial transfer of larger repo from git. IF full history and branches are needed must go to github at https://github.com/DulcisL/CCUCoding.git

This commit is contained in:
dolcel committed 2026-10-20 23:59:02 +00:00
commit c09ce40be0
4048 files changed
+603256

No files matched your search

@@ -0,0 +1,31 @@
#linked list
#A linked list is a fast data structure that can store any type of data
#A linked list is unordered and has no index only links
#a node is a link in the chain
#a node stores the data and the link (reference)
#the nodes are connected by the reference to the next node
#LL has a head - the head node is marked as head in code
#LL has a tail the tail is determined by a null reference (next node)
#reference AKA next node
import LinkedListServices
ll = LinkedListServices.LinkedList()
ll.addNode(2)
ll.addNode(10)
ll.addNode(0)
ll.printNode()
print('remove the head node')
ll.deleteHeadNode()
ll.printNode()
print('create a link list with words')
#create a new link list instance
ll2 = LinkedListServices.LinkedList()
ll2.addNode('First name')
ll2.addNode('Second name')
ll2.addNode('Third name')
ll2.printNode()
print('Remove the head node')
ll2.deleteHeadNode()
ll2.printNode()
name1 = input("Please enter a name")
ll2.addNode(name1)
ll2.printNode()
@@ -0,0 +1,49 @@
#create a stack
#stack is a data structure LIFO format
#Last in First Out
#initalize an empty list in python
myStack = []
print('How many items in stack')
print(len(myStack))
#add data to the stack bottom up.
myStack.append('A')
myStack.append('B')
myStack.append('C')
myStack.append('D')
#print the raw stack to see location
print(myStack)
#print stack in a stacked form to the console
def printStack(myStack):
count = 1
for items in myStack:
print(f'{count} - {items}')
count += 1
#call my print stack method
printStack(myStack)
#remove an item from a stack
#use the method pop
print('-remove an item from stack (LIFO)- this should remove D')
#This will remove the last item entered
myPopedItem = myStack.pop()
printStack(myStack)
print('Value removed from stack')
print(myPopedItem)
#look at the data in the stack
#use the method peek()
#python uses slice to operate the peek of a stack
print('Peek at last item in list')
print(myStack[-1:])
print(myStack[-2:])
#check if the stack has any values
print('How many items in stack')
print(len(myStack))
@@ -0,0 +1,47 @@
# Queues are also a very fundamental and important concept to grasp since many other data structures
# are built on them.
# The way a queue works is that the first person to join the queue usually gets served first,
# all things being equal. The acronym FIFO best explains this. FIFO stands for first in, first out.
# When people are standing in a queue waiting for their turn to be served,
# service is only rendered at the front of the queue. The only time people exit the queue is
# when they have been served, which only occurs at the very front of the queue.
#create a list to use as a queue
queue = []
#add items to the queue
queue.append('A')
queue.append('B')
queue.append('C')
queue.append('D')
#print the raw queue
print('Raw Queue (do not use in a finished program)')
print(queue)
#print the queue for the console described
print('Queue described')
def printQueueDescribed():
count = 0
for item in queue:
print(f'{count} - {item}')
count += 1
printQueueDescribed()
#remove an item from the queue
#you can only remove an item at index 0
#if you remove using another index you are not using a queue
print('Remove an item from the queue (A)')
queue.pop(0)
print('Queue with an item removed')
printQueueDescribed()
#What data structure would be best to use?
#an undo function in a program - Stack
#who arrived first - Queue
#who would move first on a green light - Queue
#deconstruct a tower - Stack
#chop down a treen - Stack
@@ -0,0 +1,71 @@
class Node:
#attributes
_data = 0
_nextNode = None
#constructor
def __init__(self, data, nextNode=None):
self._data = data
self._nextNode = nextNode
#GetSet
def getData(self):
return self._data
def getNextNode(self):
return self._nextNode
def setData(self, data):
self._data = data
def setNextNode(self, nextNode):
self._nextNode = nextNode
#string
def __str__(self):
return f'Node Data: {self._data} NextNode: {self._nextNode}'
class LinkedList:
#attributes
_head = Node(0, None)
_size = 0
#constructor
def __init__(self, head=None):
self._head = head
self._size = 0
#getSet
def getHead(self):
return self._head
def getSize(self):
return self._size
def setHead(self, head):
self._head = head
def setSize(self, size):
self._size = size
#behaviors
#add Node
def addNode(self, data):
#construct a node
#to be the head node
newNode = Node(data, self._head)
self._head = newNode
self._size += 1
return True
#delete a node from the Head
def deleteHeadNode(self):
#Find the head node
oldHeadNode = self._head
#make the next node(Link) none
nextNodeOfHead = oldHeadNode.getNextNode()
#store the next Node(Link) as head node
self._head = nextNodeOfHead
#remove the nextNode(link) from the current(old) head
oldHeadNode.setNextNode(None)
#printNode
def printNode(self):
#find the current head node
current = self._head
#loop while the current variable is not empty
while current:
print(current.getData())
#store the next node into the current variable
current = current.getNextNode()
#str
@@ -0,0 +1,38 @@
#Binary Search Tree
#In computer science, a binary search tree (BST), also called an ordered or sorted binary tree,
# is a rooted binary tree data structure whose internal nodes each store a key greater than all
# the keys in the node's left subtree and less than those in its right subtree
#A BST is a fast data structure that stores data in an ordered fashion
#store and search is faster than linked list and most data types.
#a node is a data object in the tree
#a node stores the data and the link (reference)
#the nodes are connected by the reference to the next node
#BST has a root - the root node is marked as head in code
#BST has a leaf(s) the leaf is determined by a null reference (next node)
#parent
#child
#sibling
#leaf
#height of the tree
#forests - multiple trees used together
#NOTE No duplicate data is allowed in a BST
#create a tree instance
import TreeServices
tree = TreeServices.BinarySearchTree()
tree.addNode(20)
print(f'GetRoot {tree.getRoot()}')
tree.addNode(10)
tree.addNode(25)
tree.addNode(15)
tree.addNode(9)
tree.addNode(30)
tree.addNode(22)
tree.addNode(8)
print('Traverse throught the tree')
tree.printTree()
print('Traverse In order')
tree.traverseInorder()
print('Traverse pre order')
tree.traversePreorder()
@@ -0,0 +1,178 @@
#node Class
class Node:
_data = None
_left = None
_right = None
def __init__(self, data=None, left=None, right=None):
self._data = data
self._left = left
self._right = right
def getData(self):
return self._data
def getLeft(self):
return self._left
def getRight(self):
return self._right
def setData(self, data):
self._data = data
def setRight(self, right):
self._right = right
def setLeft(self, left):
self._left = left
def traversePreorder(self):
print(self._data)
if self._left:
self._left.traversePreorder()
if self._right:
self._right.traversePreorder()
def traverseInorder(self):
if self._left:
self._left.traverseInorder()
print(self._data)
if self._right:
self._right.traverseInorder()
def traversePostorder(self):
if self._left:
self._left.traversePostorder()
if self._right:
self._right.traversePostorder()
print(self._data)
def search(self, target):
if self._data == target:
print("Found it!")
return self
if self._left and self._data > target:
return self._left.search(target)
if self._right and self._data < target:
return self._right.search(target)
print("Value is not in tree")
def getNodesAtDepth(self, depth, nodes=[]):
if depth == 0:
nodes.append(self._data)
return nodes
if self._left:
self._left.getNodesAtDepth(depth - 1, nodes)
else:
nodes.extend([None] * 2 ** (depth - 1))
if self._right:
self._right.getNodesAtDepth(depth - 1, nodes)
else:
nodes.extend([None] * 2 ** (depth - 1))
return nodes
def insertNode(self, newData):
# Do nothing if no data
if self._data:
# Where data value is less than current, branch left
if newData < self._data:
if self._left is None:
self._left = Node(newData)
else:
self._left.insertNode(newData)
# Where data is greater than current, branch right
elif newData > self._data:
if self._right is None:
self._right = Node(newData)
else:
self._right.insertNode(newData)
def height(self, h=0):
leftHeight = self._left.height(h + 1) if self._left else h
rightHeight = self._right.height(h + 1) if self._right else h
return max(leftHeight, rightHeight)
def __str__(self):
return f'Node Data: {self._data} Left Value: {self._left} Right Value: {self._right}'
#BinarySearchTree Class
class BinarySearchTree:
_root = Node
_name = str
def __init__(self, root=Node()):
self._root = root
self._name = "NodeName"
def getRoot(self):
return self._root
def getStr(self):
return self._str
def setRoot(self, root):
self._root = root
def setStr(self, str):
self._str = str
def addNode(self, value):
if self._root.getData() is None:
self._root = Node(value)
else:
self._root.insertNode(value)
# if data > par.getData():
# print('Adding in right of ', par.getData(), 'new node ', data)
# par.setRight(node)
# else:
# print('Adding in left of ', par.getData(), 'new node ', data)
# par.setLeft(node)
#traverse through tree (used to print or find a node)
def _nodeToChar(self, n, spacing):
if n is None:
return '_' + (' ' * spacing)
spacing = spacing - len(str(n)) + 1
return str(n) + (' ' * spacing)
def printTree(self, label=''):
print(self._name + ' ' + label)
height = self._root.height()
spacing = 3
width = int((2 ** height - 1) * (spacing + 1) + 1)
# Root offset
offset = int((width - 1) / 2)
for depth in range(0, height + 1):
if depth > 0:
# print directional lines
print(' ' * (offset + 1) + (' ' * (spacing + 2)).join(
['/' + (' ' * (spacing - 2)) + '\\'] * (2 ** (depth - 1))))
row = self._root.getNodesAtDepth(depth, [])
print((' ' * offset) + ''.join([self._nodeToChar(n, spacing) for n in row]))
spacing = offset + 1
offset = int(offset / 2) - 1
print('')
#delete a node from the tree
def deleteNode(self, node: Node):
print("Create this method to use")
def traverseInorder(self):
self._root.traverseInorder()
def traversePreorder(self):
self._root.traversePreorder()
def traversePostorder(self):
self._root.traversePostorder()
def search(self, target):
return self._root.search(target)
def getNodesAtDepth(self, depth):
return self._root.getNodesAtDepth(depth)
def height(self):
return self._root.height()
+91
View File
@@ -0,0 +1,91 @@
class Node:
#attributes
_data = 0
_nextNode = None
#constructor
def __init__(self, data, nextNode=None):
self._data = data
self._nextNode = nextNode
#GetSet
def getData(self):
return self._data
def getNextNode(self):
return self._nextNode
def setData(self, data):
self._data = data
def setNextNode(self, nextNode):
self._nextNode = nextNode
#string
def __str__(self):
return f'Node Data: {self._data} NextNode: {self._nextNode}'
class LinkedList:
#attributes
_head = Node(0, None)
_size = 0
#constructor
def __init__(self, head=None):
self._head = head
self._size = 0
#getSet
def getHead(self):
return self._head
def getSize(self):
return self._size
def setHead(self, head):
self._head = head
def setSize(self, size):
self._size = size
#behaviors
#add Node
def addNode(self, data):
#construct a node
#to be the head node
newNode = Node(data, self._head)
self._head = newNode
self._size += 1
return True
#delete a node from the Head
def deleteHeadNode(self):
#Find the head node
oldHeadNode = self._head
#make the next node(Link) none
nextNodeOfHead = oldHeadNode.getNextNode()
#store the next Node(Link) as head node
self._head = nextNodeOfHead
#remove the nextNode(link) from the current(old) head
oldHeadNode.setNextNode(None)
#Iterate through the list
def iterate(self, list):
'''Description: This method is designed to iterate through the whole linked list and store in a list
Parameters:
list (list): Pass in the list that will store the variables
Return:
list (list): Nodes will be stored in the list
'''
current = self._head
#loop through the nodes
while current:
#return the data
list.append(current.getData())
#Get the next node
current = current.getNextNode()
return list
#printNode
def printNode(self):
#find the current head node
current = self._head
#loop while the current variable is not empty
while current:
print(current.getData())
#store the next node into the current variable
current = current.getNextNode()
#str
+177
View File
@@ -0,0 +1,177 @@
#node Class
class Node:
_data = None
_left = None
_right = None
def __init__(self, data=None, left=None, right=None):
self._data = data
self._left = left
self._right = right
def getData(self):
return self._data
def getLeft(self):
return self._left
def getRight(self):
return self._right
def setData(self, data):
self._data = data
def setRight(self, right):
self._right = right
def setLeft(self, left):
self._left = left
def traversePreorder(self):
print(self._data)
if self._left:
self._left.traversePreorder()
if self._right:
self._right.traversePreorder()
def traverseInorder(self):
if self._left:
self._left.traverseInorder()
print(self._data)
if self._right:
self._right.traverseInorder()
def traversePostorder(self):
if self._left:
self._left.traversePostorder()
if self._right:
self._right.traversePostorder()
print(self._data)
def search(self, target):
if self._data == target:
print("Found it!")
return self
if self._left and self._data > target:
return self._left.search(target)
if self._right and self._data < target:
return self._right.search(target)
print("Value is not in tree")
def getNodesAtDepth(self, depth, nodes=[]):
if depth == 0:
nodes.append(self._data)
return nodes
if self._left:
self._left.getNodesAtDepth(depth - 1, nodes)
else:
nodes.extend([None] * 2 ** (depth - 1))
if self._right:
self._right.getNodesAtDepth(depth - 1, nodes)
else:
nodes.extend([None] * 2 ** (depth - 1))
return nodes
def insertNode(self, newData):
# Do nothing if no data
if self._data:
# Where data value is less than current, branch left
if newData < self._data:
if self._left is None:
self._left = Node(newData)
else:
self._left.insertNode(newData)
# Where data is greater than current, branch right
elif newData > self._data:
if self._right is None:
self._right = Node(newData)
else:
self._right.insertNode(newData)
def height(self, h=0):
leftHeight = self._left.height(h + 1) if self._left else h
rightHeight = self._right.height(h + 1) if self._right else h
return max(leftHeight, rightHeight)
def __str__(self):
return f'Node Data: {self._data} Left Value: {self._left} Right Value: {self._right}'
#BinarySearchTree Class
class BinarySearchTree:
_root = Node
_name = str
def __init__(self, root=Node()):
self._root = root
self._name = "Family Tree"
def getRoot(self):
return self._root
def getStr(self):
return self._str
def setRoot(self, root):
self._root = root
def setStr(self, str):
self._str = str
def addNode(self, value):
if self._root.getData() is None:
self._root = Node(value)
else:
self._root.insertNode(value)
# if data > par.getData():
# print('Adding in right of ', par.getData(), 'new node ', data)
# par.setRight(node)
# else:
# print('Adding in left of ', par.getData(), 'new node ', data)
# par.setLeft(node)
#traverse through tree (used to print or find a node)
def _nodeToChar(self, n, spacing):
if n is None:
return '_' + (' ' * spacing)
spacing = spacing - len(str(n)) + 1
return str(n) + (' ' * spacing)
def printTree(self, label=''):
print(self._name + ' ' + label)
height = self._root.height()
spacing = 3
width = int((2 ** height - 1) * (spacing + 1) + 1)
# Root offset
offset = int((width - 1) / 2)
for depth in range(0, height + 1):
if depth > 0:
# print directional lines
print(' ' * (offset + 1) + (' ' * (spacing + 2)).join(
['/' + (' ' * (spacing - 2)) + '\\'] * (2 ** (depth - 1))))
row = self._root.getNodesAtDepth(depth, [])
print((' ' * offset) + ''.join([self._nodeToChar(n, spacing) for n in row]))
spacing = offset + 1
offset = int(offset / 2) - 1
print('')
#delete a node from the tree
def deleteNode(self, node: Node):
print("Create this method to use")
def traverseInorder(self):
self._root.traverseInorder()
def traversePreorder(self):
self._root.traversePreorder()
def traversePostorder(self):
self._root.traversePostorder()
def search(self, target):
return self._root.search(target)
def getNodesAtDepth(self, depth):
return self._root.getNodesAtDepth(depth)
def height(self):
return self._root.height()
+201
View File
@@ -0,0 +1,201 @@
''' Name: Lakota Dolce
Date: 4/15/2023
Assignment: Wk 13 Assignment 1
Pseudocode:(when needed)
'''
# Imports
from LinkedListServices import *
from random import *
#Create driving simulator
class Drive():
'''Description: This class is to create a text base driving simulator
Attributes:
light (int): Contains what status the light is in (0: Red, 1: Yellow, 2: Green)
right, left, forward, and reverse (int): determine the available directions (0 not available, 1 available)
distance (int): Contains the distance travelled
last(int): the last distance travelled
Behaviors:
driving (): Runs the simulation for the game
redLight(): Prints the users results and resets the game for another round
'''
#Attributes
_light = int
_right = int
_left = int
_forward = int
_reverse = int
_distance = int
_last = int
#Constructor
def __init__(self):
self._light = 2
self._right = 1
self._left = 1
self._forward = 1
self._reverse = 0
self._distance = 0
self._last = 0
#behaviors
def driving(self):
'''Description: This method is designed to run the simulator, take and check all user input and call other methods as needed
Parameters:
none
Return:
none
'''
#Get situation
light = self._light
right = self._right
left = self._left
forward = self._forward
reverse = self._reverse
#Check what the light, 0: Red, 1: Yellow, 2: Green
if light < 3 and light > 0:
#Print the choices
if left == 1:
print('Enter 1 to go Left')
if forward == 1:
print('Enter 2 to go Forward')
if right == 1:
print('Enter 3 to go Right')
if reverse == 1:
print('Enter 4 to go Backwards (doing so will take away distance and that direction)')
if left != 1 and forward != 1 and right != 1 and reverse != 1:
print(f"You have hit a dead end, you made it {self._distance} meters ")
self.redLight()
#set up environment
direction = 0
#Get user input
while direction <= 4:
try:
#Get direction and check it
direction = int(input("Which direction would you like to go? (enter any number over 4 to quit) "))
if direction > 4 or direction < 1:
raise ValueError
if direction != 4:
#keep last distance change
lastDistance = randrange(100, 1000, 1)
self._distance += lastDistance
self._last = lastDistance
print(f"You have travelled {self._distance} meters")
#Keep track of directions
node.addNode(direction)
#Add to the count
self.createNew()
self.driving()
if direction == 4:
#If they went back delete the last node if possible
if self._distance > 0:
node.deleteHeadNode()
self._distance -= self._last
print(f"You have travelled {self._distance} meters")
self.createNew()
#Check if the distance is at or below zero
if self._distance <= 0:
self._distance = 0
print("You can't go back anymore")
self._left = 1
self._right = 1
self._forward = 1
self. _reverse = 0
self.driving()
#If user wants to quit set light to zero and quit the program
except Exception:
light = 0
break
if light == 0:
#Tell user how far they went
print(f"You hit a red light! \nYou made it {self._distance} meters from the start")
self.redLight()
#Tell user the directions they went
def redLight(self):
'''Description: This method is designed to reset the instance, and linked list
Parameters:
none
Return:
-1 for a fail
'''
#Create list for directions
directions = []
#Iterate through the nodes and store data
directions = node.iterate(directions)
#Check the directions
for i in reversed(directions):
match i:
case 1:
#Print the directions
print('You travelled left')
case 2:
print('You travelled forward')
case 3:
print('You travelled right')
while True:
try:
#Ask the user to try again
again = input("Would you like to try again? (y for Yes and n for No): ")
#Check input
if again.lower() == 'y':
#Reset and run again
#Reset the node
try:
while node:
node.deleteHeadNode()
except Exception:
pass
#Reset the simulator
self.__init__()
#Start again
self.driving()
#If no then end the program
elif again.lower() == 'n':
print('Goodbye')
quit()
else:
raise ValueError
except Exception:
print("You entered an invalid input, please try again")
return -1
def createNew(self):
'''Description: This method is designed to create a new instance of the traffic light for the user
Parameters:
none
Return:
none
'''
#Change the environment
self._right = randrange(0,2,1)
self._left = randrange(0,2,1)
self._forward = randrange(0,2,1)
self._reverse = 1
self._light = randrange(0,2, 1)
#Start
print("-----Welcome to the driving simulator-----")
drive = Drive()
node = LinkedList()
drive.driving()