这篇文章主要介绍了python数据分析与应用选择题,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获,下面让小编带着大家一起了解一下。
1.8.1
class Fraction:
def __init__(self,num,den):
self.num = num #分子
self.den = den #分母
def getNum(self):
return self.num
def getDen(self):
return self.den
f = Fraction(3,5)
num = f.getNum()
den = f.getDen()
print("分子:" + str(num))
print("分母:" + str(den))`
1.8.2
class Fraction:
def __init__(self,num,den):
gcd = self.getGcd(num,den)#获取两者的最大公因数
self.num = num // gcd #分子
self.den = den // gcd #分母
def __add__(self, other):
newnum = int(self.num * other.den + self.den * other.num)
newden = int(self.den * other.den)
f = Fraction(newnum, newden)
return f
def getGcd(self,num1,num2):
while num1 % num2 != 0:
temp1 = num1
temp2 = num2
num1 = temp2
num2 = temp1 % temp2
return num2
def __str__(self):
print(str(self.num) + "/" + str(self.den))
f1 = Fraction(6,-10)
f1.__str__()
f2 = Fraction(1,5)
f2.__str__()
f = f2.__add__(f1)
1.8.3
class Fraction:
def __init__(self,num,den):
gcd = self.getGcd(num,den)#获取两者的最大公因数
self.num = num // gcd #分子
self.den = den // gcd #分母
def __add__(self, other):
newnum = int(self.num * other.den + self.den * other.num)
newden = int(self.den * other.den)
f = Fraction(newnum, newden)
return f
def getGcd(self,num1,num2):
while num1 % num2 != 0:
temp1 = num1
temp2 = num2
num1 = temp2
num2 = temp1 % temp2
return num2
def __str__(self):
print(str(self.num) + "/" + str(self.den))
def __sub__(self, other):
newnum = int(self.num * other.den - self.den * other.num)
newden = int(self.den * other.den)
f = Fraction(newnum, newden)
return f
def __mul__(self, other):
newnum = int(self.num * other.num)
newden = int(self.den * other.den)
f = Fraction(newnum, newden)
return f
def __truediv__(self, other):
if self.num == 0 or other.num == 0:
newnum = 0
newden = 1
else:
newnum = int(self.num * other.den)
newden = int(self.den * other.num)
f = Fraction(newnum, newden)
return f
1.8.4
__gt__ 大于
__ge__ 大于等于
__lt__ 小于
__le__ 小于等于
__ne__ 不等于
__eq__ 等于
class Fraction:
def __init__(self,num,den):
gcd = self.getGcd(num,den)#获取两者的最大公因数
self.num = num // gcd #分子
self.den = den // gcd #分母
def __str__(self):
print(str(self.num) + "/" + str(self.den)
def getGcd(self,num1,num2):
while num1 % num2 != 0:
temp1 = num1
temp2 = num2
num1 = temp2
num2 = temp1 % temp2
return num2
def __gt__(self, other):#大于
if self.num / self.den > other.num / other.den:
return True
else:
return False
def __lt__(self, other):#小于
if self.num / self.den < other.num / other.den:
return True
else:
return False
def __ge__(self, other): #大于等于
if self.num / self.den >= other.num / other.den:
return True
else:
return False
def __le__(self, other): #小于等于
if self.num / self.den <= other.num / other.den:
return True
else:
return False
def __ne__(self, other): #小于等于
if self.num / self.den != other.num / other.den:
return True
else:
return False
def __eq__(self, other): #等于
if self.num == other.num and self.den == other.den:
return True
else:
return False
1.8.5
class Fraction:
def __init__(self,num,den):
if isinstance(num,int) and isinstance(den,int) :
gcd = self.getGcd(num,den)#获取两者的最大公因数
self.num = num // gcd #分子
self.den = den // gcd #分母
else:
print("输入数据错误")
def __str__(self):
print(str(self.num) + "/" + str(self.den))
1.8.6
该题在求取两者最大公因数的已经解决,不需要重新写代码
例:输入6,-10, 求出两者的最大公因数为 -2,两个数就变为-3,5,对应输出为-3/5
例:输入-6,10, 求出两者的最大公因数为 2,两个数就变为-3,5,对应输出为-3/5
例:输入-6,-10, 求出两者的最大公因数为 -2,两个数就变为3,5,对应输出为3/5
例:输入1,-10, 求出两者的最大公因数为 -1,两个数就变为-1,10,对应输出为-1/10
1.8.7
__radd__()是当不存在__add__()时调用,且__radd__(self,other)中self和other不能来自同一个类中
__radd__(self, other)和__add__(self, other)都是定制类的加法
前者表示右加法other + self,后者表示左加法self + other
class Fraction1:
def __init__(self,num,den):
if isinstance(num,int) and isinstance(den,int) :
gcd = self.getGcd(num,den)#获取两者的最大公因数
self.num = num // gcd #分子
self.den = den // gcd #分母
else:
print("输入数据错误")
def __str__(self):
return str(self.num) + "/" + str(self.den)
def getGcd(self,num1,num2):
while num1 % num2 != 0:
temp1 = num1
temp2 = num2
num1 = temp2
num2 = temp1 % temp2
return num2
# def __add__(self, other):
# newnum = int(self.num * other.den + self.den * other.num)
# newden = int(self.den * other.den)
# f = Fraction(newnum, newden)
# return f
def __radd__(self, other):
newnum = int(self.num * other.den + self.den * other.num)
newden = int(self.den * other.den)
f = Fraction(newnum, newden)
return f
class Fraction2:
def __init__(self,num,den):
if isinstance(num,int) and isinstance(den,int) :
gcd = self.getGcd(num,den)#获取两者的最大公因数
self.num = num // gcd #分子
self.den = den // gcd #分母
else:
print("输入数据错误")
def __str__(self):
return str(self.num) + "/" + str(self.den)
def getGcd(self,num1,num2):
while num1 % num2 != 0:
temp1 = num1
temp2 = num2
num1 = temp2
num2 = temp1 % temp2
return num2
def __radd__(self, other):
newnum = int(self.num * other.den + self.den * other.num)
newden = int(self.den * other.den)
f = Fraction(newnum, newden)
return f
f1 = Fraction1(1,-10)
print(f1)
f2 = Fraction(1,5)
print(f2)
print(f1 + f2) #调用f1中的radd
print(f2 + f1) #调用f2中的radd
1.8.8
hasattr(int,'__iadd__') # False 不可变类型对象
hasattr(list,'__iadd__') # True 可变类型对象
__add__接收两个参数 返回两者的和,不会改变参数值
__iadd__接收两个参数将结果,将结果拼接到第一个参数上
a = [1,2,3,4]
b = [1,2,3,4]
a.__iadd__(b)
print(a)#[1, 2, 3, 4, 1, 2, 3, 4]
1.8.9
1.8.10
class AndnonGate(BinaryGate):#与非
def __init__(self,n):
super().__init__(n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if a == 1 and b == 1:
return 0
else:
return 1
class NorGate(BinaryGate):#或非
def __init__(self,n):
super().__init__(n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if a == 0 and b == 0:
return 1
else:
return 0
class EorGate(BinaryGate):#异或
def __init__(self,n):
super().__init__(n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if a == b:
return 1
else:
return 0
1.8.11
半加器真值表
被加数A | 加数 B | SUM | 进位C |
---|---|---|---|
0 | 0 | 0 | 0 |
0 | 1 | 1 | 0 |
1 | 0 | 1 | 0 |
1 | 1 | 0 | 1 |
#半加器
#sum为异或输出
#c为与门输出
class LogicGate:
def __init__(self,n):
self.label = n
self.output = None
def getLabel(self):
return self.label
def getOutput(self):
self.output = self.performGateLogic()
return self.output
class BinaryGate(LogicGate):
def __init__(self,n):
super().__init__(n)
self.pinA = None
self.pinB = None
def getPinA(self):
if self.pinA == None:
return int(input("Enter Pin A input for gate " + self.getLabel() + "-->"))
else:
return self.pinA
def getPinB(self):
if self.pinB == None:
return int(input("Enter Pin B input for gate " + self.getLabel() + "-->"))
else:
return self.pinB
def setNextPin(self, pinA, pinB):
if self.pinA == None:
self.pinA = pinA
if self.pinB == None:
self.pinB = pinB
class AndGate(BinaryGate):
def __init__(self,n):
super().__init__(n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if a == 1 and b == 1:
return 1
else:
return 0
class EorGate(BinaryGate):
def __init__(self,n):
super().__init__(n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if a == b:
return 0
else:
return 1
class HalfAdder():
def __init__(self, pinA, pinB):
# super.__init__()
# sum = EorGate("G1")
c = AndGate("G2")
c.setNextPin(pinA, pinB)
c = c.getOutput()
s = EorGate("G1")
s.setNextPin(pinA, pinB)
s = s.getOutput()
print(s,c)
HalfAdder(1,0)
1.8.12
全加器真值表
pinA | pinB | pinC | 进位C | 和S |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 0 | 1 | 0 | 1 |
0 | 1 | 0 | 0 | 1 |
0 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 0 | 1 |
1 | 0 | 1 | 1 | 0 |
1 | 1 | 0 | 1 | 0 |
1 | 1 | 1 | 1 | 1 |
class LogicGate:
def __init__(self,n):
self.label = n
self.output = None
def getLabel(self):
return self.label
def getOutput(self):
self.output = self.performGateLogic()
return self.output
class BinaryGate(LogicGate):
def __init__(self,n):
super().__init__(n)
self.pinA = None
self.pinB = None
def getPinA(self):
if self.pinA == None:
return int(input("Enter Pin A input for gate " + self.getLabel() + "-->"))
else:
return self.pinA
def getPinB(self):
if self.pinB == None:
return int(input("Enter Pin B input for gate " + self.getLabel() + "-->"))
else:
return self.pinB
def setNextPin(self, pinA, pinB):
if self.pinA == None:
self.pinA = pinA
if self.pinB == None:
self.pinB = pinB
class AndGate(BinaryGate):
def __init__(self,n):
super().__init__(n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if a == 1 and b == 1:
return 1
else:
return 0
class EorGate(BinaryGate):
def __init__(self,n):
super().__init__(n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if a == b:
return 0
else:
return 1
class OrGate(BinaryGate):
def __init__(self, n):
super().__init__(n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if a == 1 or b == 1:
return 1
else:
return 0
def HalfAdder(pinA, pinB):
c = AndGate("G2")
c.setNextPin(pinA, pinB)
c = c.getOutput()
s = EorGate("G1")
s.setNextPin(pinA, pinB)
s = s.getOutput()
return s,c
def FullAdder(pinA,pinB,pinC):
s1,c1 = HalfAdder(pinA,pinB)
s,c2 = HalfAdder(s1,pinC)
c = OrGate("G3")
c.setNextPin(c1,c2)
c = c.getOutput()
return s,c
s,c = FullAdder(1,0,1)
print(s,c)