### collect dictionary

Nov 19, 2022CodeCatch

## More Python Posts

```"""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 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 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()
family = ["Blade", "Samara", "Shadow", "Wolfe", "Raven", "Alice"]"""
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 operationshow(T)show(T)```

#### Lonely Integer

Feb 26, 2023wabdelh

0 likes • 0 views

`#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 integerres = a[0]    for i in range(1,len(a)):        res = res ^ a[i]`

#### Mad libs generator

Nov 19, 2022CodeCatch

0 likes • 0 views

`#Loop back to this point once code finishesloop = 1while (loop < 10):#All the questions that the program asks the user    noun = input("Choose a noun: ")    p_noun = input("Choose a plural noun: ")    noun2 = input("Choose a noun: ")    place = input("Name a place: ")    adjective = input("Choose an adjective (Describing word): ")    noun3 = input("Choose a noun: ")#Displays the story based on the users input    print ("------------------------------------------")    print ("Be kind to your",noun,"- footed", p_noun)    print ("For a duck may be somebody's", noun2,",")    print ("Be kind to your",p_noun,"in",place)    print ("Where the weather is always",adjective,".")    print ()    print ("You may think that is this the",noun3,",")    print ("Well it is.")    print ("------------------------------------------")#Loop back to "loop = 1"    loop = loop + 1`

#### Find URL in string

Nov 19, 2022CodeCatch

0 likes • 0 views

```# Python code to find the URL from an input string# Using the regular expressionimport re
def Find(string):
# findall() has been used	# with valid conditions for urls in string	regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"	url = re.findall(regex,string)		return [x[0] for x in url]	# Driver Codestring = 'My Profile: https://codecatch.net'print("Urls: ", Find(string))```

#### Rock paper scissors

Nov 19, 2022CodeCatch

0 likes • 0 views

`""" Rock Paper Scissors----------------------------------------"""import randomimport osimport reos.system('cls' if os.name=='nt' else 'clear')while (1 < 2):    print "\n"    print "Rock, Paper, Scissors - Shoot!"    userChoice = raw_input("Choose your weapon [R]ock], [P]aper, or [S]cissors: ")    if not re.match("[SsRrPp]", userChoice):        print "Please choose a letter:"        print "[R]ock, [S]cissors or [P]aper."        continue    // Echo the user's choice    print "You chose: " + userChoice    choices = ['R', 'P', 'S']    opponenetChoice = random.choice(choices)    print "I chose: " + opponenetChoice    if opponenetChoice == str.upper(userChoice):        print "Tie! "    #if opponenetChoice == str("R") and str.upper(userChoice) == "P"    elif opponenetChoice == 'R' and userChoice.upper() == 'S':              print "Scissors beats rock, I win! "        continue    elif opponenetChoice == 'S' and userChoice.upper() == 'P':              print "Scissors beats paper! I win! "        continue    elif opponenetChoice == 'P' and userChoice.upper() == 'R':              print "Paper beat rock, I win! "        continue    else:               print "You win!"`

#### find parity outliers

Nov 19, 2022CodeCatch

0 likes • 0 views

```from collections import Counter
def find_parity_outliers(nums):  return [    x for x in nums    if x % 2 != Counter([n % 2 for n in nums]).most_common()[0][0]  ]
find_parity_outliers([1, 2, 3, 4, 6]) # [1, 3]```