summaryrefslogtreecommitdiff
path: root/cmpsc132_homework_1-1.py
blob: a451474d17c90be9c5f251986c1504fa33745d93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# -*- coding: utf-8 -*-
"""CMPSC132 - Homework 1.ipynb

I have included that code snippets for the <a href= "https://runestone.academy/runestone/books/published/pythonds3/Introduction/ObjectOrientedProgramminginPythonDefiningClasses.html">PSADS book below</a>. You need to extend the code in order to meet the criteria listed at the end of the chapter.
"""

def gcd(m, n):
    while m % n != 0:
        m, n = n, m % n
    return n

def chk_frac(frac):
    try:
        onum = frac.num
        oden = frac.den
    except AttributeError:
        onum = frac
        oden = 1
    return onum, oden

class Fraction:
    def __init__(self, top, bottom):
        if (type(top) is not int) or (type(bottom) is not int):
            raise TypeError

        if bottom < 0:
            bottom *= -1
            top *= -1

        cmmn = gcd(top, bottom)
        self.num = top // cmmn
        self.den = bottom // cmmn


    def __str__(self):
        return "{:d}/{:d}".format(self.num, self.den)

    def __eq__(self, other_fraction):
        first_num = self.num * other_fraction.den
        second_num = other_fraction.num * self.den
        return first_num == second_num

    def __add__(self, other_fraction):
        onum, oden = chk_frac(other_fraction)
        new_num = self.num * oden \
        + self.den * onum
        new_den = self.den * oden
        return Fraction(new_num, new_den)

    def __sub__(self, other_fraction):
        onum, oden = chk_frac(other_fraction)
        new_num = self.num * oden \
        - self.den * onum
        new_den = self.den * oden
        return Fraction(new_num, new_den)

    def __mul__(self, other_fraction):
        onum, oden = chk_frac(other_fraction)
        new_num = self.num * onum
        new_den = self.den * oden
        return Fraction(new_num, new_den)

    def __truediv__(self, other_fraction):
        onum, oden = chk_frac(other_fraction)
        new_num = self.num * oden
        new_den = self.den * onum
        return Fraction(new_num, new_den)

    def __gt__(self, other_fraction):
        onum, oden = chk_frac(other_fraction)
        snum = self.num * oden
        onum = self.den * onum
        if snum > onum:
            return True
        return False

    def __ge__(self, other_fraction):
        onum, oden = chk_frac(other_fraction)
        snum = self.num * oden
        onum = self.den * onum
        if snum >= onum:
            return True
        return False

    def __lt__(self, other_fraction):
        onum, oden = chk_frac(other_fraction)
        snum = self.num * oden
        onum = self.den * onum
        if snum < onum:
            return True
        return False

    def __le__(self, other_fraction):
        onum, oden = chk_frac(other_fraction)
        snum = self.num * oden
        onum = self.den * onum
        if snum <= onum:
            return True
        return False

    def __ne__(self, other_fraction):
        onum, oden = chk_frac(other_fraction)
        snum = self.num * oden
        onum = self.den * onum
        if snum != onum:
            return True
        return False

    def __radd__(self, other_fraction):
        """
            __radd__ is for when the reverse adding is needed
            like 1 + x instead of x + 1
        """
        return self.__add__(other_fraction)

    def __iadd__(self, ofrac):
        """
            __iadd__ is for increment addition
            x += 5
        """
        onum, oden = chk_frac(ofrac)
        self.num = self.num * oden + onum * self.den
        self.den = self.den * oden
        return self

    def __repr__(self):
        """
            __repr__ is similar to __str__ because it returns a string that
            displays the state of the object but __repr__ can be used to
            recreate the object while __str__ prints the state in a human
            readable format.
        """
        return f"Fraction({self.num}, {self.den})"

    def show(self):
        print("{:d}/{:d}".format(self.num, self.den))

    def get_num(self):
        return self.num

    def get_den(self):
        return self.den

x = Fraction(1, 2)
x.show()
y = Fraction(2, 3)
print(y)
assert y == Fraction(2,3)
print(x + y)
assert x + y == Fraction(7,6)
print(x == y)

"""# COMPLETE THE FRACTION CLASS
<a href= "https://runestone.academy/runestone/books/published/pythonds3/Introduction/Exercises.html"> You can also find these questions in the book. </a><br>
1. Implement the simple methods get\_num and get\_den that will return the numerator and denominator of a fraction.

2. In many ways it would be better if all fractions were maintained in lowest terms right from the start. Modify the constructor for the Fraction class so that GCD is used to reduce fractions immediately. Notice that this means the \_\_add\_\_ function no longer needs to reduce. Make the necessary modifications.

3. Implement the remaining simple arithmetic operators (\_\_sub\_\_, \_\_mul\_\_, and \_\_truediv\_\_).

4. Implement the remaining relational operators (\_\_gt\_\_, \_\_ge\_\_, \_\_lt\_\_, \_\_le\_\_, and \_\_ne\_\_).

5. Modify the constructor for the fraction class so that it checks to make sure that the numerator and denominator are both integers. If either is not an integer, the constructor should raise an exception.

6. In the definition of fractions we assumed that negative fractions have a negative numerator and a positive denominator. Using a negative denominator would cause some of the relational operators to give incorrect results. In general, this is an unnecessary constraint. Modify the constructor to allow the user to pass a negative denominator so that all of the operators continue to work properly.

7. Research the \_\_radd\_\_ method. How does it differ from \_\_add\_\_? When is it used? Implement \_\_radd\_\_.

8. Repeat the last question but this time consider the \_\_iadd\_\_ method.

9. Research the \_\_repr\_\_ method. How does it differ from \_\_str\_\_? When is it used? Implement \_\_repr\_\_.
"""

#Test 1
x.get_num()
assert x.get_num() == 1
y.get_den()
assert y.get_den() == 3

# Test 2
z = Fraction(3,6)
print(z)  #should be 1/2
assert z == Fraction(1,2)

# Test 3
# __sub__
z = x-y
print(z)
assert z == Fraction(-1,6)
# __mul__
z = x*y
print(z)
assert z == Fraction(1,3)
# __truediv__
# from __future__ import division  #this might need to be imported
z = x/y
print(z)
assert z == Fraction(3,4)

# Test 4
# __gt__
assert (x>y) == False
# __ge__
assert (x>=y) == False
# __lt__
assert (x<y) == True
# __le__
assert (x<=y) == True
# __ne__
assert (x!=y) == True

#Test 5
try:
    alpha = Fraction(1.2,2.2)
except:
    print('that doesn\'t work!')

#Test 6
beta = Fraction(3, -5)
print(beta)
print(beta.get_num())
print(beta.get_den())
assert beta == Fraction(-3, 5)

#Test 7 radd
print(x + 1)
print(1 + x)
assert (x + 1) == Fraction(3,2)
assert (1 + x) == Fraction(3,2)

#Test 8 iadd
for i in range(y.get_den()):
    x += i
    print(x)
assert x ==  Fraction(7,2)

#Test 9
"""
Research the __repr__ method. How does it differ from __str__? When is it used? Implement __repr__.
WRITE A STATEMENT HERE!

__repr__ is similar to __str__ because it returns a string that displays the
state of the object but __repr__ can be used to recreate the object while
__str__ prints the state in a human readable format.
"""
class LogicGate:

    def __init__(self, lbl):
        self.name = lbl
        self.output = None

    def get_label(self):
        return self.name

    def get_output(self):
        self.output = self.perform_gate_logic()
        return self.output


class BinaryGate(LogicGate):

    def __init__(self, lbl, shared):
        super(BinaryGate, self).__init__(lbl)

        self.pin_a = None
        self.pin_b = None
        self.shared = shared

    def get_pin_a(self):
        if self.pin_a == None:
            return int(input("Enter pin A input for gate " + self.get_label() + ": "))
        elif self.shared:
            return self.pin_a
        else:
            return self.pin_a.get_from().get_output()

    def get_pin_b(self):
        if self.pin_b == None:
            return int(input("Enter pin B input for gate " + self.get_label() + ": "))
        elif self.shared:
            return self.pin_b
        else:
            return self.pin_b.get_from().get_output()

    def set_next_pin(self, source):
        if self.pin_a == None:
            self.pin_a = source
        else:
            if self.pin_b == None:
                self.pin_b = source
            else:
                print("Cannot Connect: NO EMPTY PINS on this gate")


class AndGate(BinaryGate):

    def __init__(self, lbl, shared):
        BinaryGate.__init__(self, lbl, shared)

    def perform_gate_logic(self):

        a = self.get_pin_a()
        b = self.get_pin_b()
        if a == 1 and b == 1:
            return 1
        else:
            return 0

class NandGate(BinaryGate):

    def __init__(self, lbl, shared):
        BinaryGate.__init__(self, lbl, shared)

    def perform_gate_logic(self):

        a = self.get_pin_a()
        b = self.get_pin_b()
        if a == 1 and b == 1:
            return 0
        else:
            return 1

class OrGate(BinaryGate):

    def __init__(self, lbl, shared):
        BinaryGate.__init__(self, lbl, shared)

    def perform_gate_logic(self):

        a = self.get_pin_a()
        b = self.get_pin_b()
        if a == 1 or b == 1:
            return 1
        else:
            return 0

class NorGate(BinaryGate):

    def __init__(self, lbl, shared):
        BinaryGate.__init__(self, lbl, shared)

    def perform_gate_logic(self):

        a = self.get_pin_a()
        b = self.get_pin_b()
        if a == 0 or b == 0:
            return 1
        else:
            return 0

class XorGate(BinaryGate):

    def __init__(self, lbl, shared):
        BinaryGate.__init__(self, lbl, shared)

    def perform_gate_logic(self):

        a = self.get_pin_a()
        b = self.get_pin_b()
        if a != b:
            return 1
        else:
            return 0

class UnaryGate(LogicGate):

    def __init__(self, lbl):
        LogicGate.__init__(self, lbl)

        self.pin = None

    def get_pin(self):
        if self.pin == None:
            return int(input("Enter pin input for gate " + self.get_label() + ": "))
        else:
            return self.pin.get_from().get_output()

    def set_next_pin(self, source):
        if self.pin == None:
            self.pin = source
        else:
            print("Cannot Connect: NO EMPTY PINS on this gate")


class NotGate(UnaryGate):

    def __init__(self, lbl):
        UnaryGate.__init__(self, lbl)

    def perform_gate_logic(self):
        if self.get_pin():
            return 0
        else:
            return 1


class Connector:

    def __init__(self, fgate, tgate):
        self.from_gate = fgate
        self.to_gate = tgate

        tgate.set_next_pin(self)

    def get_from(self):
        return self.from_gate

    def get_to(self):
        return self.to_gate

class HalfAdder:

    def __init__(self, a, b):
        self.g1 = XorGate("G1", True)
        self.g2 = AndGate("G2", True)
        self.g1.pin_a = a
        self.g1.pin_b = b
        self.g2.pin_a = a
        self.g2.pin_b = b

    def perform_circut_logic(self):
        s = self.g1.get_output()
        c = self.g2.get_output()
        return s, c


g1 = AndGate("G1", False)
g2 = AndGate("G2", False)
g3 = OrGate("G3", False)
g4 = NotGate("G4")
c1 = Connector(g1, g3)
c2 = Connector(g2, g3)
c3 = Connector(g3, g4)
print(g4.get_output())

"""# LOGIC GATE PROBLEM
Research other types of gates that exist (such as NAND, NOR, and XOR). Add them to the circuit hierarchy. How much additional coding did you need to do?

Not much additional coding is needed because you just need to copy and paste
and change the logic of the given gates

The most simple arithmetic circuit is known as the half adder. Research the simple half-adder circuit. Implement this circuit.

Bonus: 10 Points - Extend that circuit and implement an 8-bit full adder.
"""

print("Half Adder Test:\nEnter a = 0 b = 0\nAns: s = 0 c = 0")
h1 = HalfAdder(0, 0)
s, c = h1.perform_circut_logic()
assert s == 0
assert c == 0
print(f"s = {s} c = {c}")

print("Half Adder Test:\nEnter a = 0 b = 1\nAns: s = 1 c = 0")
h1 = HalfAdder(0, 1)
s, c = h1.perform_circut_logic()
assert s == 1
assert c == 0
print(f"s = {s} c = {c}")

print("Half Adder Test:\nEnter a = 1 b = 0\nAns: s = 1 c = 0")
h1 = HalfAdder(1, 0)
s, c = h1.perform_circut_logic()
assert s == 1
assert c == 0
print(f"s = {s} c = {c}")

print("Half Adder Test:\nEnter a = 1 b = 1\nAns: s = 0 c = 1")
h1 = HalfAdder(1, 1)
s, c = h1.perform_circut_logic()
assert s == 0
assert c == 1
print(f"s = {s} c = {c}")