Skip to main content

Find Coin

0 likes • Oct 4, 2023 • 9 views
Python
Loading...

More Python Posts

read file contents into a list

0 likes • Jun 1, 2023 • 0 views
Python
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)

UNT CSCE 2100 Assignment 6

0 likes • Nov 18, 2022 • 0 views
Python
"""
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)

CSCE 2100 Question 3

0 likes • Nov 18, 2022 • 3 views
Python
# question3.py
from itertools import product
V='∀'
E='∃'
def tt(f,n) :
xss=product((0,1),repeat=n)
print('function:',f.__name__)
for xs in xss : print(*xs,':',int(f(*xs)))
print('')
# this is the logic for part A (p\/q\/r) /\ (p\/q\/~r) /\ (p\/~q\/r) /\ (p\/~q\/~r) /\ (~p\/q\/r) /\ (~p\/q\/~r) /\ (~p\/~q\/r) /\ (~p\/~q\/~r)
def parta(p,q,r) :
a=(p or q or r) and (p or q or not r) and (p or not q or r)and (p or not q or not r)
b=(not p or q or r ) and (not p or q or not r) and (not p or not q or r) and (not p or not q or not r)
c= a and b
return c
def partb(p,q,r) :
a=(p or q and r) and (p or not q or not r) and (p or not q or not r)and (p or q or not r)
b=(not p or q or r ) and (not p or q or not r) and (not p or not q or r) and (not p or not q or not r)
c= a and b
return c
print("part A:")
tt(parta,3)
print("part B:")
tt(partb,3)

Lonely Integer

0 likes • Feb 26, 2023 • 0 views
Python
#84 48 13 20 61 20 33 97 34 45 6 63 71 66 24 57 92 74 6 25 51 86 48 15 64 55 77 30 56 53 37 99 9 59 57 61 30 97 50 63 59 62 39 32 34 4 96 51 8 86 10 62 16 55 81 88 71 25 27 78 79 88 92 50 16 8 67 82 67 37 84 3 33 4 78 98 39 64 98 94 24 82 45 3 53 74 96 9 10 94 13 79 15 27 56 66 32 81 77
# xor a list of integers to find the lonely integer
res = a[0]
for i in range(1,len(a)):
res = res ^ a[i]

Return Letter Combinations

0 likes • Nov 18, 2022 • 0 views
Python
# @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, [''])

integer to roman numeral

0 likes • Nov 19, 2022 • 0 views
Python
def to_roman_numeral(num):
lookup = [
(1000, 'M'),
(900, 'CM'),
(500, 'D'),
(400, 'CD'),
(100, 'C'),
(90, 'XC'),
(50, 'L'),
(40, 'XL'),
(10, 'X'),
(9, 'IX'),
(5, 'V'),
(4, 'IV'),
(1, 'I'),
]
res = ''
for (n, roman) in lookup:
(d, num) = divmod(num, n)
res += roman * d
return res
to_roman_numeral(3) # 'III'
to_roman_numeral(11) # 'XI'
to_roman_numeral(1998) # 'MCMXCVIII'