## two-digit integer

0 likes • Feb 26, 2023 • 0 views
Python

## More Python Posts

#### check prop lambda

0 likes • Nov 19, 2022 • 0 views
Python
`def check_prop(fn, prop):  return lambda obj: fn(obj[prop])  check_age = check_prop(lambda x: x >= 18, 'age')user = {'name': 'Mark', 'age': 18}check_age(user) # True`

#### Bogo Sort

0 likes • Nov 19, 2022 • 0 views
Python
```# Python program for implementation of Bogo Sortimport random
# Sorts array a[0..n-1] using Bogo sortdef bogoSort(a):	n = len(a)	while (is_sorted(a)== False):		shuffle(a)
# To check if array is sorted or notdef is_sorted(a):	n = len(a)	for i in range(0, n-1):		if (a[i] > a[i+1] ):			return False	return True
# To generate permuatation of the arraydef shuffle(a):	n = len(a)	for i in range (0,n):		r = random.randint(0,n-1)		a[i], a[r] = a[r], a[i]
# Driver code to test abovea = [3, 2, 4, 1, 0, 5]bogoSort(a)print("Sorted array :")for i in range(len(a)):	print ("%d" %a[i]),```

#### CSCE 2100 Question 3

0 likes • Nov 18, 2022 • 3 views
Python
```# question3.pyfrom itertools import productV='âˆ€'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)```

#### UNT CSCE 2100 Assignment 6

0 likes • Nov 18, 2022 • 0 views
Python
```"""Assignment 6
The goal is to make a graph ofwho bit who and who was bitten.There should be 10 nodes and 15 edges.3 arrows of biting each other and3 arrows of someone biting themselves.Networkx can not do self bitingarrows, but it is in the code."""
from graphviz import Digraph as DDotGraphfrom graphviz import Graph as UDotGraphimport networkx as nxfrom networkx.algorithms.dag import transitive_closureimport graphviz as gvimport matplotlib.pyplot as pltimport numpy as npfrom 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 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 graphvizdef 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)

bit()
#bitten()
T = transitive_closure(D)
for e in D.edges(): print(e)for n in D.nodes(): print(n)
# 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 operationshow(T)show(T)```

#### return multiple values from a function

0 likes • Jun 1, 2023 • 0 views
Python
```def calculate_values():    value1 = 10    value2 = 20    return value1, value2
result1, result2 = calculate_values()print("Result 1:", result1)print("Result 2:", result2)```

#### 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'```