【每日一题1112】正方形坐标

:mage:t2: 给定一个由四个点(x, y)坐标组成的列表,请编写一个函数,判断这四个点是否能组成正方形。

示例:
输入:[(1,1), (3,3), (1,3), (3,1)],输出:True
输入:·[(0, 0), (0,2), (2,0), (2,1)],输出:False

def is_square() -> bool:
    pass

assert is_square([(0, 0), (0, 2), (2, 0), (2, 1)]) is True
assert is_square([(1,1), (3,3), (1,3), (3,1)]) is True
assert is_square([(0, 0), (0,2), (2,0), (2,1)]) is False

题目难度:简单
题目来源:codewars: Do the points form a square?

为什么第一个是true,第三个是false,这两不是一样的吗?

def is_square(coordinate_list) -> bool:
    # 四个点可能得顺序方式
    angle_list = [(0, 1, 2, 3), (0, 1, 3, 2), (0, 2, 3, 1), (0, 2, 1, 3), (0, 3, 2, 1), (0, 3, 1, 2)]
    for coor in angle_list:
        length_list = []
        for i in range(4):
            x1, y1 = coordinate_list[coor[i]][0], coordinate_list[coor[i]][1]
            x2, y2 = coordinate_list[coor[i + 1] if i < 3 else coor[0]][0], \
                     coordinate_list[coor[i + 1] if i < 3 else coor[0]][1]
            length = abs(pow((x1 - x2) ** 2 + (y1 - y2) ** 2, 1.0 / 2))
            length_list.append(length)
        # 判断四边是否相等,即是否为菱形
        if length_list[0] == length_list[1] == length_list[2] == length_list[3]:
            x1, y1 = coordinate_list[coor[0]][0], coordinate_list[coor[0]][1]
            x3, y3 = coordinate_list[coor[2]][0], coordinate_list[coor[2]][1]
            c_length_square = abs((x1 - x3) ** 2 + (y1 - y3) ** 2)
            # 判断是否存在直角
            if length_list[0] ** 2 + length_list[0] ** 2 == c_length_square:
                return True
    return False


# 修正了该断言
assert is_square([(0, 0), (0, 2), (2, 0), (2, 2)]) is True
assert is_square([(1, 1), (3, 3), (1, 3), (3, 1)]) is True
assert is_square([(0, 0), (0, 2), (2, 0), (2, 1)]) is False

def is_square(dots:list)->bool:
‘’’
给定一个由四个点(x, y)坐标组成的列表,请编写一个函数,判断这四个点是否能组成正方形。
:param dots:
:return:
‘’’
def dot_len(dotA,dotB)->int:
‘’’
给定两个点,判断两个点的距离
:param dots:
:return:
‘’’
if len(dotA)!= 2 or len(dotB)!=2:
return 0
return pow(pow(dotA[0] - dotB[0], 2) + pow(dotA[1] - dotB[1], 2), 0.5)
len_result =
for i in range(0, 3):
for j in range(i+1, 4):
data = dot_len(dots[i], dots[j])
len_result.append(data)
result_set = set(len_result)
print(len_result)
if len(result_set) == 2:
return True
return False
pass

def test_is_square():
assert is_square([(1, 1), (3, 3), (1, 3), (3, 1)]) == True
assert is_square([(0, 0), (0,2), (2,0), (2,1)]) == False

import math

def is_square1(l: list) → bool:

result = False

###先将列表中的四个坐标点排序

l.sort()

####求出四个点间的边长

a = math.sqrt((l[1][0] - l[0][0])**2 + (l[1][1] - l[0][1])**2)

b = math.sqrt((l[2][0] - l[0][0])**2 + (l[2][1] - l[0][1])**2)

c = math.sqrt((l[3][0] - l[2][0])**2 + (l[3][1] - l[2][1])**2)

d = math.sqrt((l[3][0] - l[1][0])**2 + (l[3][1] - l[1][1])**2)

####边长相等即为正方形

if a == b == c == d :

    if len(set(l)) == 4:

        result = True

return result

assert is_square1([(0, 0), (0, 2), (2, 0), (2, 2)]) is True

assert is_square1([(-1,0),(0,1),(1,0),(0,-1)]) is True

assert is_square1([(1,1), (3,3), (1,3), (3,1)]) is True

assert is_square1([(0, 0), (0,2), (2,0), (2,1)]) is False

assert is_square1([(0,0),(0,0),(0,0),(0,0)]) is False

因为是assert吧

# 【每日一题1112】正方形坐标


def is_square(input_l: list) -> bool:
    """
    给定一个由四个点(x, y)坐标组成的列表,请编写一个函数,判断这四个点是否能组成正方形。
    示例:
    输入:[(1,1), (3,3), (1,3), (3,1)],输出:True。
    输入:·[(0, 0), (0,2), (2,0), (2,1)],输出:False。
    :return:
    """
    # 尝试一:正方形的四边长度相等,“上底+下底”=“左边+右边”之和。即当所有x轴之和和y轴之和相等,即可以组成正方形。
    # 结果一:解题思路有误,未考虑到菱形、且x1+x2==y1+y2相等的场景会有巧合,不代表就是4边相等
    # sum_x = 0
    # sum_y = 0
    # for x, y in input_l:
    #     sum_x += x
    #     sum_y += y
    # return sum_x == sum_y
    #
    # 尝试二:正方形的对角线相等。利用辅助线可得到规律:两点之间的x轴及y轴之差、与另两点之间的y轴及x轴之差的绝对值相等
    # 如满足:abs(A(x)-B(x)) == (C(y)-D(y)) and abs(A(y)-B(y)) == abs(C(x)-D(x))
    # 4个点的顺序不确定,所以遍历完所有点x、y组合的相减场景,只要有一个场景满足即可
    # 结果二:目前的论证有效
    A = input_l[0]
    B = input_l[1]
    C = input_l[2]
    D = input_l[3]
    if abs(A[0] - B[0]) == abs(C[1] - D[1]) and abs(A[1] - B[1]) == abs(C[0] - D[0]):
        return True
    elif abs(A[0] - C[0]) == abs(B[1] - D[1]) and abs(A[1] - C[1]) == abs(B[0] - D[0]):
        return True
    elif abs(A[0] - D[0]) == abs(B[1] - C[1]) and abs(A[1] - D[1]) == abs(B[0] - C[0]):
        return True
    elif abs(B[0] - C[0]) == abs(A[1] - D[1]) and abs(B[1] - C[1]) == abs(A[0] - D[0]):
        return True
    elif abs(C[0] - D[0]) == abs(A[1] - B[1]) and abs(C[1] - D[1]) == abs(A[0] - B[0]):
        return True
    # 以下场景与第2点重复
    # elif abs(A[1] - C[1]) == abs(B[0] - D[0]) and abs(A[0] - C[0]) == abs(B[1] - D[1]):
    #     return True
    else:
        return False


assert is_square([(-1, 0), (0, 1), (1, 0), (0, -1)]) is True
assert is_square([(-1, 0), (0, 1), (2, 0), (0, -1)]) is False
assert is_square([(0, 0), (0, 2), (2, 0), (2, 2)]) is True
assert is_square([(1, 1), (3, 3), (1, 3), (3, 1)]) is True
assert is_square([(0, 0), (0, 2), (2, 0), (2, 1)]) is False
assert is_square([(1, 1), (4, 4), (7, 1), (4, -2)]) is True
# 以下为菱形的四个点坐标值,预期为False,否则校验方法有误
assert is_square([(0, 0), (2, 0), (1, -2), (-1, -2)]) is False

def is_square(num_list: list) -> bool:
    # 列表排序
    num_list.sort()
    # 判断第一坐标的两个值相等,且最后一个坐标两个值相等,否则返回False
    if num_list[0][0] == num_list[0][1] and num_list[3][0] == num_list[3][1]:
        # 继续判断首尾两个坐标值不等,否则返回False
        if num_list[0] != num_list[3]:
            # 开始比较中间两个元素,先转成列表
            a = list(num_list[1])
            b = list(num_list[2])
            # 反转b
            b.reverse()
            # 判断两个坐标值相等,且任意一个坐标的两个值不等,否则返回False
            if a == b and a[0] != a[1]:
                # 继续判断 第二个坐标,第一个元素值和第一个坐标的第一个元素值相等,第二个元素和最后一个坐标的第二个值相等
                if a[0] == num_list[0][0] and a[1] == num_list[3][1]:
                    return True
                else:
                    return False
            else:
                return False
        else:
            return False
    else:
        return False

assert is_square([(0, 0), (0, 2), (2, 0), (2, 2)]) is True
assert is_square([(1, 1), (3, 3), (1, 3), (3, 1)]) is True
assert is_square([(0, 0), (0, 2), (2, 0), (2, 1)]) is False

def is_square(input) -> bool:
    pointsL = []
    for item in input:
        pointsL += list(item)
    pointsL = list(set(pointsL))
    if len(pointsL) ==2 and (pointsL[0],pointsL[0]) in input and \
         (pointsL[0], pointsL[1]) in input and \
         (pointsL[1], pointsL[0]) in input and \
         (pointsL[1], pointsL[1]) in input :
        return True
    else:
        return False

assert is_square([(1,1), (3,3), (1,3), (3,1)]) is True
assert is_square([(0, 0), (0,2), (2,0), (2,1)]) is False
1 个赞

思路是能够组成正方形的坐标列表一定是由两个坐标组成的笛卡尔积列表,所以用product方法遍历列表中每个元组的两个坐标数值,生成的笛卡尔积列表排序后等于坐标组成的列表,则返回True,如果遍历完了也没有就返回False

def is_square(nums: list) -> bool:
    def get_product(x, y):
        from itertools import product
        list1 = list(product((x, y), repeat=2))
        return list1

    for i in nums:
        a = sorted(get_product(i[0], i[1]))
        b = sorted(nums)
        if a == b:
            return True
    return False


assert is_square([(0, 0), (0, 2), (2, 0), (2, 2)]) is True
assert is_square([(1, 1), (3, 3), (1, 3), (3, 1)]) is True
assert is_square([(0, 0), (0, 2), (2, 0), (2, 1)]) is False
def is_square(data: list) -> bool:
    l = []
    # 每2组数据进行比较,如果横坐标相同或者纵坐标相同,则计算2个点之前的长度
    for i in range(0, 3):
        for j in range(i + 1, 4):
            if data[i][0] == data[j][0] or data[i][1] == data[j][1]:
                length = ((abs(data[i][0] - data[j][0])) ** 2 + (abs(data[i][1] - data[j][1])) ** 2) ** 0.5
                l.append(length)
    # 列表去重,如果列表中有4个长度,且4个长度相同,那么集合只有1个数
    if len(l) == 4 and len(set(l)) == 1:
        return True
    else:
        return False


assert is_square([(0, 0), (0, 2), (2, 0), (2, 2)]) is True
assert is_square([(1, 1), (3, 3), (1, 3), (3, 1)]) is True
assert is_square([(0, 0), (0, 2), (2, 0), (2, 1)]) is False