鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: 冬雪雪冬

[技术交流] Python:每日一题 163

[复制链接]
发表于 2018-3-13 01:09:28 | 显示全部楼层
class Nint(int):
    def __truediv__(self,other):
        if float(self) % float(other) == 0.0:
            return int(int(self) / int(other))
            
        else:
            return float(float(self) / float(other))
            
    def __rtruediv__(other,self):
        if float(self) % float(other) == 0.0:
            return int(int(self) / int(other))
            
        else:
            return float(float(self) / float(other))

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-13 09:26:54 | 显示全部楼层
def fun(n,m):
    if n % m == 0:
        return "%d"%(n /m)
    return n%m

n = int(input("enter a num:"))
m = int(input("enter a num:"))

print(fun(n,m))

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-13 10:58:43 | 显示全部楼层
  1. class Devision():
  2.     def __init__(self,devidend,devisor):
  3.         self.devidend = devidend
  4.         self.devisor = devisor
  5.         self.result = self.devision()      
  6.    
  7.    
  8.     def devision(self):
  9.         try:
  10.             remainder = self.devidend % self.devisor
  11.             result = self.devidend / self.devisor
  12.             if remainder == 0:
  13.                 result = int(result)
  14.         except ZeroDivisionError:
  15.             result = '0不能作为除数'
  16.         return result
  17.             

  18. if __name__ == '__main__':
  19.     devidend = 5                            # 被除数
  20.     devisor = 1                             # 除数
  21.     devison = Devision(devidend,devisor)
  22.     print(devison.result)
复制代码

点评

要用魔法方法  发表于 2018-3-16 20:49
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-13 13:10:59 | 显示全部楼层
本帖最后由 graceasyi 于 2018-3-13 14:11 编辑

第二个参数为Nint类型时,不知道怎么处理好,运算符重载这一块还不是很熟,还得多多学习……

  1. class Nint():
  2.     def __init__(self, value):
  3.         self.data = value

  4.     def __truediv__(self, other):
  5.         try:
  6.             return self.data / other if self.data % other else self.data // other
  7.         except TypeError:
  8.             return self.data / other.data if self.data % other.data else self.data // other.data

  9.     def __rtruediv__(self, other):
  10.         try:
  11.             return other / self.data if other % self.data else other // self.data
  12.         except TypeError:
  13.             return other.data / self.data if other.data % self.data else other.data // self.data


  14. a = Nint(48)
  15. b = Nint(21)
  16. c = Nint(12)

  17. print("a / 8  = " + str(a / 8))
  18. print("b / 3  = " + str(b / 3))
  19. print("60 / a = " + str(60 / a))
  20. print("60 / b = " + str(60 / b))
  21. print("a / c  = " + str(a / c))
  22. print("b / c  = " + str(b / c))
复制代码


结果:
a / 8  = 6
b / 3  = 7
60 / a = 1.25
60 / b = 2.857142857142857
a / c   = 4
b / c   = 1.75

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-13 13:44:04 | 显示全部楼层
本帖最后由 天圆突破 于 2018-3-15 14:58 编辑
  1. class Nint(int):
  2.     def __init__(self, num):
  3.         int.__init__(self)
  4.         self.num = num

  5.     def __truediv__(self, other):
  6.         if self.num/other == int(self.num/other):return int(self.num/other)
  7.         else:return self.num/other

  8.     def __rtruediv__(self, other):
  9.         if other/self.num == int(other/self.num):return int(other/self.num)
  10.         else:return other/self.num

  11. if __name__ == '__main__':
  12.     a = Nint(9)
  13.     b = Nint(8)
  14.     c = Nint(4)
  15.     print(a/c)
  16.     print(b/c)
  17.     print(12/c)
  18.     print(13/c)
  19.     print(a/3)
  20.     print(a/5)
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-13 13:53:36 | 显示全部楼层
class Nint(int):
        def __truediv__(self,other):
                result = int(self)/int(other)
                if result % 1 == 0:
                        result = int(result)
                return result
        def __rtruediv__(self,other):
                result = int(other)/int(self)
                if result % 1 == 0:
                        result = int(result)
                return result

a = Nint(9)
b = Nint(8)
c = Nint(4)
print(a/c)
print(b/c)
print(12/c)          #这里用的是__rtruediv__,c是self,12是other
print(13/c)
print(a/3)
print(a/5)

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-13 14:48:39 | 显示全部楼层
  1. class Nint(int):
  2.     def __truediv__(self,other):#真正的除法
  3.         if int(self) % int(other) == 0:
  4.             return int(int(self) / int(other))
  5.         else:
  6.             return int(self) / int(other)
  7.     def __rtruediv__(self,other):#反运算
  8.         if int(other) % int(self) == 0:
  9.             return int(int(other) / int(self))
  10.         else:
  11.             return int(other) / int(self)
  12. #测试
  13. a = Nint(9)
  14. b = Nint(8)
  15. c = Nint(4)
  16. print(a/c)
  17. print(b/c)
  18. print(12/c)
  19. print(13/c)
  20. print(a/3)
  21. print(a/5)
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-13 15:02:57 | 显示全部楼层
  1. class Nint(int):
  2.     def __truediv__(self, other):
  3.         div = int(self) / int(other)
  4.         return int(div) if div == int(div) else div
复制代码

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
冬雪雪冬 + 2 + 2

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-13 21:31:55 | 显示全部楼层
class Nint(int):
    def __truediv__(self,other):
        a=int.__truediv__(self,other)
        if a==int(a):
            return int(a)
        else:
            return a

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
冬雪雪冬 + 2 + 2

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-13 23:33:03 | 显示全部楼层
  1. class Nint():
  2.     def __init__(self,a):
  3.         self.number=a
  4.     def __truediv__(self,num):
  5.         if self.number%num>0:
  6.             return self.number/num
  7.         else:
  8.             return self.number//num
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-14 00:25:45 | 显示全部楼层
本帖最后由 此用户不合法 于 2018-3-14 00:29 编辑

答案设为本人可见咋讨论呢,12/c那一项不通过连描述都不知道怎么描述


class Nint(int):
        
    def __truediv__(self,v):
        if self % intv == 0:
            return int(int.__truediv__(self,v))
        else:
            return int.__truediv__(self,v)
        
        

点评

现在可以看到别人的答案了  发表于 2018-3-16 20:54
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-14 20:46:41 | 显示全部楼层
  1. class Nint(int):
  2.     def __truediv__(self,other):
  3.         if int.__mod__(self, other):
  4.             return int.__truediv__(self, other)
  5.         else:
  6.             return int.__floordiv__(self, other)

  7.     def __rtruediv__(self,other):
  8.         if int.__rmod__(self, other):
  9.             return int.__rtruediv__(self, other)
  10.         else:
  11.             return int.__rfloordiv__(self, other)
复制代码

结果:
>>> a, b, c = Nint(9), Nint(4), Nint(3)
>>> a/b
2.25
>>> a/c
3
>>> 12/c
4
>>> 12/a
1.3333333333333333
>>> a/5
1.8

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-14 20:50:48 | 显示全部楼层
评分截至标记。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-14 23:23:22 | 显示全部楼层
class Nint(int):
    def  __init__(self,nn):
        self.nn=nn
        
    def  __truediv__(self,other):
        aa=self.nn/other
        bb=self.nn//other
        if aa==bb :
            return bb
        else :
            return aa
        
    def  __rtruediv__(self,other):
        cc=other/self.nn
        dd=other//self.nn
        if cc==dd :
            return dd
        else :
            return cc
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-15 20:45:28 | 显示全部楼层
本帖最后由 忽视 于 2018-3-15 20:47 编辑
  1. class truediv(int):
  2.     def __truediv__(self,other):
  3.         b = int(self) / int(other)
  4.         b = str(b)
  5.         if len(b) >= 3:
  6.             print(float(b))
  7.         else:
  8.             if int(b[2]) == 0:
  9.                 print(int(b[0]))
  10.             else:
  11.                 print(float(b))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 00:04:53 | 显示全部楼层
class Nint(int):
        def __init__(self,num):
                self.num = num
        def __truediv__(self,num):
                print("__truediv__")
                if int.__truediv__(self,num) == self//num:
                        return self//num
                else:
                        return int.__truediv__(self,num)
        def __rtruediv__(self,num):
                print('...rtruediv..')
                if int.__rtruediv__(self,num) == num//self:
                        return num//self
                else:
                        return int.__rtruediv__(self,num)
1.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 09:38:36 | 显示全部楼层
python2:
>>> 4/2
2
>>> 4/3
1
>>> 4//3
1

python3:
>>> 4/2
2.0
>>> 4/3
1.3333333333333333
>>>
>>>
>>> int(4/2)
2
>>>
>>>
>>>
>>> 4//3
1
>>>

这道题目的主要功能就是复写python除法功能。以python3为例,贴出主要代码,如下:
  1. def division(self):
  2.                 if self.b==0:
  3.                         return -1
  4.                 elif self.a%self.b==0:
  5.                         return int(self.a/self.b)
  6.                 else:
  7.                         return self.a/self.b
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 10:16:04 | 显示全部楼层
luokaoge 发表于 2018-3-12 20:02
class Division():
    def __init__(self,a,b):
        c = a/b

学习中
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 10:31:08 | 显示全部楼层

忘了还有rtruediv愚了= =
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 10:47:48 | 显示全部楼层
  1. class Nint(int):
  2.     def __truediv__(self,other):
  3.         if self % other == 0:
  4.             return super(Nint,self).__floordiv__(other)
  5.         else:
  6.             return super(Nint,self).__truediv__(other)
  7.    
  8.     def __rtruediv__(self,other):
  9.         if other % self == 0:
  10.             return super(Nint,self).__rfloordiv__(other)
  11.         else:
  12.             return super(Nint,self).__rtruediv__(other)
  13.    
  14. a = Nint(9)
  15. b = Nint(8)
  16. c = Nint(4)
  17. print(a / c)
  18. print(b / c)
  19. print(12 / c)
  20. print(13 / c)
  21. print(a / 3)
  22. print(a / 5)

  23. # 结果
  24. # 2.25
  25. # 2
  26. # 3
  27. # 3.25
  28. # 3
  29. # 1.8
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 16:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表