小编Gar*_*ich的帖子

使用两个左下角和右上角检查两个矩形是否在python中重叠

class Point:

    def __init__(self, xcoord=0, ycoord=0):
        self.x = xcoord
        self.y = ycoord

class Rectangle:
    def __init__(self, bottom_left, top_right, colour):
        self.bottom_left = bottom_left
        self.top_right = top_right
        self.colour = colour

    def intersects(self, other):
Run Code Online (Sandbox Code Playgroud)

我试图看看两个矩形是否基于右上角和左下角相交,但是当我创建函数时:

def intersects(self, other):
    return self.top_right.x>=other.top_right.x>=self.bottom_left.x and self.top_right.x>=other.bottom_left.x>=self.bottom_left.x and self.top_right.y>=other.top_right.y>=self.bottom_left.y and self.top_right.x>=other.bottom_left.x>=self.bottom_left.x
Run Code Online (Sandbox Code Playgroud)

输入时该函数将返回false:

r1=Rectangle(Point(1,1), Point(2,2), 'blue')
r3=Rectangle(Point(1.5,0), Point(1.7,3), 'red')
r1.intersects(r3)
Run Code Online (Sandbox Code Playgroud)

进壳.

python python-3.x

10
推荐指数
2
解决办法
1万
查看次数

设计一个递归函数,使用digit_sum计算数字之和

def digit_sum(n):
    if n==0 or n==1:
        return n
    else:
        return n+digit_sum(n-1)

def digital_root(n):

    if n<10:
        return n
    else:
        return digit_sum((n // 10) + n % 10)
Run Code Online (Sandbox Code Playgroud)

我试图用来digit_sum计算digital_root有人帮助我的数字总和.我正在尝试使用递归函数digital_root.

在Python shell中运行该文件:

digital_root(1969)
Run Code Online (Sandbox Code Playgroud)

这应该计算1 + 9 + 6 + 9 = 25然后因为25大于10它应该计算其数字2 + 5的总和,以便最终答案是7.

python python-3.x

4
推荐指数
1
解决办法
523
查看次数

标签 统计

python ×2

python-3.x ×2