Skip to main content

Connect to MYSQL and create a database

Nov 19, 2022CodeCatch
Loading...

More Python Posts

Untitled

Jun 16, 2024lagiath

0 likes • 1 view

print('hello, world')

return multiple values from a function

Jun 1, 2023CodeCatch

0 likes • 0 views

def calculate_values():
value1 = 10
value2 = 20
return value1, value2
result1, result2 = calculate_values()
print("Result 1:", result1)
print("Result 2:", result2)

UNT CSCE 2100 Assignment 6

Nov 18, 2022AustinLeath

0 likes • 0 views

"""
Assignment 6
The goal is to make a graph of
who bit who and who was bitten.
There should be 10 nodes and 15 edges.
3 arrows of biting each other and
3 arrows of someone biting themselves.
Networkx can not do self biting
arrows, but it is in the code.
"""
from graphviz import Digraph as DDotGraph
from graphviz import Graph as UDotGraph
import networkx as nx
from networkx.algorithms.dag import transitive_closure
import graphviz as gv
import matplotlib.pyplot as plt
import numpy as np
from numpy.linalg import matrix_power
"""
class DGraph:
def __init__(self):
self.d = dict()
def clear(self):
self.d = dict()
def add_node(self,n):
if not self.d.get(n):
self.d[n] = set()
def add_edge(self,e):
f,t=e
self.add_node(f)
self.add_node(t)
vs=self.d.get(f)
if not vs:
self.d[f] = {t}
else:
vs.add(t)
def add_edges_from(self,es):
for e in es:
self.add_edge(e)
def edges(self):
for f in self.d:
for t in self.d[f]:
yield (f,t)
def number_of_nodes(self):
return len(self.d)
def __repr__(self):
return self.d.__repr__()
def show(self):
dot = gv.Digraph()
for e in self.edges():
#print(e)
f, t = e
dot.edge(str(f), str(t), label='')
#print(dot.source)
show(dot)
# displays graph with graphviz
def show(dot, show=True, file_name='graph.gv'):
dot.render(file_name, view=show)
def showGraph(g,label="",directed=True):
if directed:
dot = gv.Digraph()
else:
dot = gv.Graph()
for e in g.edges():
print(e)
f, t = e
dot.edge(str(f), str(t), label=label)
print(dot.source)
show(dot)
def bit():
G = DGraph()
G.add_edge(("Blade","Samara"))
G.add_edge(("Shadow","Wolfe"))
G.add_edge(("Raven", "Austin"))
G.add_edge(("Blade", "Alice"))
G.add_edge(("Alice","Brandon"))
G.add_edge(("Blade", "Wolfe"))
G.add_edge(("Samara", "Robin"))
G.add_edge(("Samara", "Raven"))
G.add_edge(("Samara", "Hamed"))
G.add_edge(("Wolfe", "Blade"))
G.add_edge(("Hamed", "Samara"))
G.add_edge(("Wolfe", "Shadow"))
G.add_edge(("Brandon", "Brandon"))
G.add_edge(("Hamed", "Hamed"))
G.add_edge(("Austin", "Austin"))
showGraph(G, label="bit")
bit()
def bitten():
G=DGraph()
G.add_edge(("Samara","Blade"))
G.add_edge(("Wolfe","Shadow"))
G.add_edge(("Austin", "Raven"))
G.add_edge(("Alice","Blade"))
G.add_edge(("Brandon", "Alice"))
G.add_edge(("Wolfe", "Blade" ))
G.add_edge(("Robin", "Samara"))
G.add_edge(("Raven", "Samara"))
G.add_edge(("Hamed", "Samara"))
G.add_edge(("Blade", "Wolfe"))
G.add_edge(("Samara", "Hamed"))
G.add_edge(("Shadow", "Wolfe"))
G.add_edge(("Brandon", "Brandon"))
G.add_edge(("Hamed", "Hamed"))
G.add_edge(("Austin", "Austin"))
showGraph(G, label="bitten by")
#bitten()
family = ["Blade", "Samara", "Shadow", "Wolfe", "Raven", "Alice"]
"""
#Do transitive closure call out and the
#matrix power operation should be the same
D = nx.DiGraph()
#D.add_nodes_from("SamaraBladeWolfeShadowAliceRavenBrandonRobinHamedAustin")
D.add_edge("Blade","Samara")
D.add_edge("Shadow","Wolfe")
D.add_edge("Raven", "Austin")
D.add_edge("Blade", "Alice")
D.add_edge("Alice","Brandon")
D.add_edge("Blade", "Wolfe")
D.add_edge("Samara", "Robin")
D.add_edge("Samara", "Raven")
D.add_edge("Samara", "Hamed")
D.add_edge("Wolfe", "Blade")
D.add_edge("Hamed", "Samara")
D.add_edge("Wolfe", "Shadow")
D.add_edge("Brandon", "Brandon")
D.add_edge("Hamed", "Hamed")
D.add_edge("Austin", "Austin")
T = transitive_closure(D)
for e in D.edges(): print(e)
for n in D.nodes(): print(n)
def show(H):
nx.draw(H, with_labels=True, font_weight='bold')
plt.show()
#Use nx.to_numpy_matrix instead of nx.adjacency_matrix
# M = nx.adjacency_matrix(D)
# MT = nx.adjacency_matrix(T)
M = nx.to_numpy_matrix(D)
MT = nx.to_numpy_matrix(T)
M2 = M@M
def mPower(M, k): #M is numpy matrix
assert k >= 1
P = M
for _ in range(k):
P = P @ M
return P
def tc(M):
#compute transitive closure
pass
D1 = nx.DiGraph(M)
D2 = nx.DiGraph(M2)
print('Matrix for Original\n', M)
N = nx.to_numpy_array(D,dtype=int)
print('np_array for Original\n', N)
print('\nMatrix for Transitive Closure\n', MT)
N2 = nx.to_numpy_array(T,dtype=int)
print('np_array for Transitive Closure\n', N2)
show(D) #can use D, T, and numpy matrix power operation
show(T)
show(T)

LeetCode Flood Fill

Oct 15, 2022CodeCatch

0 likes • 0 views

class Solution(object):
def floodFill(self, image, sr, sc, newColor):
R, C = len(image), len(image[0])
color = image[sr][sc]
if color == newColor: return image
def dfs(r, c):
if image[r][c] == color:
image[r][c] = newColor
if r >= 1: dfs(r-1, c)
if r+1 < R: dfs(r+1, c)
if c >= 1: dfs(r, c-1)
if c+1 < C: dfs(r, c+1)
dfs(sr, sc)
return image

Print "X" pattern

Nov 19, 2022CodeCatch

0 likes • 0 views

def print_x_pattern(size):
i,j = 0,size - 1
while j >= 0 and i < size:
initial_spaces = ' '*min(i,j)
middle_spaces = ' '*(abs(i - j) - 1)
final_spaces = ' '*(size - 1 - max(i,j))
if j == i:
print(initial_spaces + '*' + final_spaces)
else:
print(initial_spaces + '*' + middle_spaces + '*' + final_spaces)
i += 1
j -= 1
print_x_pattern(7)

Reverse a linked list

Nov 19, 2022CodeCatch

0 likes • 0 views

# Python program to reverse a linked list
# Time Complexity : O(n)
# Space Complexity : O(n) as 'next'
#variable is getting created in each loop.
# Node class
class Node:
# Constructor to initialize the node object
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
# Function to initialize head
def __init__(self):
self.head = None
# Function to reverse the linked list
def reverse(self):
prev = None
current = self.head
while(current is not None):
next = current.next
current.next = prev
prev = current
current = next
self.head = prev
# Function to insert a new node at the beginning
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
# Utility function to print the linked LinkedList
def printList(self):
temp = self.head
while(temp):
print temp.data,
temp = temp.next
# Driver program to test above functions
llist = LinkedList()
llist.push(20)
llist.push(4)
llist.push(15)
llist.push(85)
print "Given Linked List"
llist.printList()
llist.reverse()
print "\nReversed Linked List"
llist.printList()