Skip to content

Commit dd066f0

Browse files
committed
new codes
1 parent 07cd40d commit dd066f0

File tree

4 files changed

+234
-0
lines changed

4 files changed

+234
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.vscode/launch.json
44
Gen2_0_PP/Homeworks/codeforces_525B.o
55
Codepedia.code-workspace
6+
*.pyc

local/RH/hashTable.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class HashTable:
2+
def __init__(self):
3+
self.size = 11
4+
self.slots = [None] * self.size
5+
self.data = [None] * self.size
6+
7+
def put(self,key,data):
8+
hashvalue = self.hashfunction(key,len(self.slots))
9+
10+
if self.slots[hashvalue] == None:
11+
self.slots[hashvalue] = key
12+
self.data[hashvalue] = data
13+
else:
14+
if self.slots[hashvalue] == key:
15+
self.data[hashvalue] = data #replace
16+
else:
17+
nextslot = self.rehash(hashvalue,len(self.slots))
18+
while self.slots[nextslot] != None and self.slots[nextslot] != key:
19+
nextslot = self.rehash(nextslot,len(self.slots))
20+
21+
if self.slots[nextslot] == None:
22+
self.slots[nextslot]=key
23+
self.data[nextslot]=data
24+
else:
25+
self.data[nextslot] = data #replace
26+
27+
def hashfunction(self,key,size):
28+
return key%size
29+
30+
def rehash(self,oldhash,size):
31+
return (oldhash+1)%size
32+
33+
def get(self,key):
34+
startslot = self.hashfunction(key,len(self.slots))
35+
36+
data = None
37+
stop = False
38+
found = False
39+
position = startslot
40+
while self.slots[position] != None and \
41+
not found and not stop:
42+
if self.slots[position] == key:
43+
found = True
44+
data = self.data[position]
45+
else:
46+
position=self.rehash(position,len(self.slots))
47+
if position == startslot:
48+
stop = True
49+
return data
50+
51+
def __getitem__(self,key):
52+
return self.get(key)
53+
54+
def __setitem__(self,key,data):
55+
self.put(key,data)
56+
57+
H=HashTable()
58+
H[54]="cat"
59+
H[26]="dog"
60+
H[93]="lion"
61+
H[17]="tiger"
62+
H[77]="bird"
63+
H[31]="cow"
64+
H[44]="goat"
65+
H[55]="pig"
66+
H[20]="chicken"
67+
print(H.slots)
68+
print(H.data)
69+
70+
print(H[20])
71+
72+
print(H[17])
73+
H[20]='duck'
74+
print(H[20])
75+
print(H[99])

local/RH/logicGates.py

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#oops
2+
3+
class LogicGate:
4+
5+
def __init__(self,n):
6+
self.name = n
7+
self.output = None
8+
9+
def getLabel(self):
10+
return self.name
11+
12+
def getOutput(self):
13+
self.output = self.performGateLogic()
14+
return self.output
15+
16+
17+
class BinaryGate(LogicGate):
18+
19+
def __init__(self,n):
20+
super(BinaryGate, self).__init__(n)
21+
22+
self.pinA = None
23+
self.pinB = None
24+
25+
def getPinA(self):
26+
if self.pinA == None:
27+
return int(input("Enter Pin A input for gate "+self.getLabel()+"-->"))
28+
else:
29+
return self.pinA.getFrom().getOutput()
30+
31+
def getPinB(self):
32+
if self.pinB == None:
33+
return int(input("Enter Pin B input for gate "+self.getLabel()+"-->"))
34+
else:
35+
return self.pinB.getFrom().getOutput()
36+
37+
def setNextPin(self,source):
38+
if self.pinA == None:
39+
self.pinA = source
40+
else:
41+
if self.pinB == None:
42+
self.pinB = source
43+
else:
44+
print("Cannot Connect: NO EMPTY PINS on this gate")
45+
46+
47+
class AndGate(BinaryGate):
48+
49+
def __init__(self,n):
50+
BinaryGate.__init__(self,n)
51+
52+
def performGateLogic(self):
53+
54+
a = self.getPinA()
55+
b = self.getPinB()
56+
if a==1 and b==1:
57+
return 1
58+
else:
59+
return 0
60+
61+
class OrGate(BinaryGate):
62+
63+
def __init__(self,n):
64+
BinaryGate.__init__(self,n)
65+
66+
def performGateLogic(self):
67+
68+
a = self.getPinA()
69+
b = self.getPinB()
70+
if a ==1 or b==1:
71+
return 1
72+
else:
73+
return 0
74+
75+
class UnaryGate(LogicGate):
76+
77+
def __init__(self,n):
78+
LogicGate.__init__(self,n)
79+
80+
self.pin = None
81+
82+
def getPin(self):
83+
if self.pin == None:
84+
return int(input("Enter Pin input for gate "+self.getLabel()+"-->"))
85+
else:
86+
return self.pin.getFrom().getOutput()
87+
88+
def setNextPin(self,source):
89+
if self.pin == None:
90+
self.pin = source
91+
else:
92+
print("Cannot Connect: NO EMPTY PINS on this gate")
93+
94+
95+
class NotGate(UnaryGate):
96+
97+
def __init__(self,n):
98+
UnaryGate.__init__(self,n)
99+
100+
def performGateLogic(self):
101+
if self.getPin():
102+
return 0
103+
else:
104+
return 1
105+
106+
107+
class Connector:
108+
109+
def __init__(self, fgate, tgate):
110+
self.fromgate = fgate
111+
self.togate = tgate
112+
113+
tgate.setNextPin(self)
114+
115+
def getFrom(self):
116+
return self.fromgate
117+
118+
def getTo(self):
119+
return self.togate
120+
121+
122+
def main():
123+
g1 = AndGate("G1")
124+
g2 = AndGate("G2")
125+
g3 = OrGate("G3")
126+
g4 = NotGate("G4")
127+
c1 = Connector(g1,g3)
128+
c2 = Connector(g2,g3)
129+
c3 = Connector(g3,g4)
130+
print(g4.getOutput())
131+
132+
main()

local/RH/rollerClass.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import random
2+
3+
class Dice:
4+
def __init__(self, num_sides):
5+
self.num_sides = num_sides
6+
self.__randomSeed = 0 #just a typical private variable
7+
self.current_value = self.roll()
8+
9+
def roll(self):
10+
self.current_value = random.randrange(1,self.num_sides+1)
11+
return self.current_value
12+
13+
def __str__(self):
14+
return str(self.current_value)
15+
16+
def __repr__(self):
17+
return "Dice({}) : {}".format(self.num_sides, self.current_value)
18+
19+
20+
my_die = Dice(6)
21+
for i in range(5):
22+
print(my_die)
23+
my_die.roll()
24+
25+
d_list = [Dice(6), Dice(20)]
26+
print(d_list)

0 commit comments

Comments
 (0)