python魔术方块和三角形区域

时间:2015-12-13 14:08:53

标签: python

__author__ = 'student'

def is_magic_square(s):
    '''
    Return whether a two dimensional integer array s is magic, that is:
    1) The dimensions of s is nxn
    2) Every integer in [1,2,...,n*n] appears in s, exactly once.
    3) The sum of all rows in s is the same as the sum of all
    columns in s, is the same as the sum of the diagonal
    elements in s.

    :param s: A two dimensional integer array represented as a nested list.
    :return: True if square is magic, False otherwise

example outputs:
>>> is_magic_square([[1]])
True
>>> is_magic_square([[8, 3, 4], [1, 5, 9], [6, 7, 2]])
True


>>> is_magic_square([[1,2],[3,4],[5,6]])
False
>>> is_magic_square([[1,2],[3,4,5],[6,7]])
False

    nrows = len(s)
    ncols = 0
    if nrows != 0:
        ncols = len(s[0])
    if nrows != ncols:
        return False
    m = []
    for column in range(len(s[0])):
        t = 0
        for row in s:
            t += row[column]
        m.append(t)

    m[0] = sum(s[0])
    for row in s:
        sums = 0
        for elem in row:
            sums+=elem
        if sums != m[0]:
            return False
    sum([ s[i][i] for i in range(len(s))]) and  sum([ s[len(s)-i-1][i] for i in range(len(s))])


return True

我需要一个函数来判断是否给定矩阵:一组列表。检查,看看他们是否成了一个魔术广场。如果他们遵循并遵守上述3个条件。我有我的功能工作3x3矩阵,但当我使用1x1矩阵输出检查,看看它是否是一个nxn矩阵不起作用     ncols = len(s [0]) TypeError:类型' int'的对象没有len()

如果有人能告诉我为什么我的班级三角形不想返回区域部分中的区域,那么它说int对象不可调用:

Class Triangle:
    def __init__(self, side1, side2, side3):

        pass
        self.side1 = side1
        self.side2 = side2
        self.side3 = side3

    def get_perimeter(self):
        pass
        return self.side1+self.side2+self.side3

    def get_area(self):
        pass
        p = self.get_perimeter() / 2
        area = math.sqrt((p*(p - self.side1)(p-self.side2)(p-self.side3)))
        return area

    def __str__(self):

    pass 
        s = "A Triangle with side 1 {0} and side 2 {1} and side 3 {2}".format(self.side1, self.side2, self.side3)
        s = s + "area {0} and perimeter {1}".format(self.get_area(), self.get_perimeter())
        return s

1 个答案:

答案 0 :(得分:1)

我总是建议你把问题分解成模块。请检查下面的代码。

__author__ = 'student'

def counting_element_frequent(s):
    frequent_list = {}
    for i in s:
        for element in set(i):
            frequent_list[element] = frequent_list.get(element, 0)\
    return frequent_list.values()

def verifyuniqulity(s):
    len_element = sum(counting_element_frequent()) 
    return len_element == len(s)**2

def verifying_dimension_equality(s):
    rows_num = len(s)
    for index, item in enumerate(s):
        if not len(s[index]) == rows_num:
            return False
    return True

def row_sum(s):
    row_addition = []
    for i in range(len(s)):
        row_addition.append(sum(s[i]))
    return sum(row_addition)

def column_sum(s):
    column_addition = []
    for j in range(len(s)):
        for k in range(len(s)):
            column_addition.append(s[k][j]) 
    return sum(column_addition)

def diagonal_sum(s):
    diagonal_addition = []
    for v in range(len(s)):
        diagonal_addition.append(s[v][v])
    return sum(diagonal_addition)

def is_magic_square(s):
    ''' 
    Return whether a two dimensional integer array s is magic, that is:
    1) The dimensions of s is nxn
    2) Every integer in [1,2,...,n*n] appears in s, exactly once.
    3) The sum of all rows in s is the same as the sum of all
    columns in s, is the same as the sum of the diagonal
    elements in s.

    :param s: A two dimensional integer array represented as a nested list.
    :return: True if square is magic, False otherwise

    example outputs:
    >>> is_magic_square([[1]])
    True
    >>> is_magic_square([[8, 3, 4], [1, 5, 9], [6, 7, 2]])
    True


    >>> is_magic_square([[1,2],[3,4],[5,6]])
    False
    >>> is_magic_square([[1,2],[3,4,5],[6,7]])
    False
    '''
    if not verifying_dimension_equality(s):
        return False
    elif verifyuniqulity(s):
        row_addition = row_sum(s)
        column_addition = column_sum(s)
        diagonal_addition = diagonal_sum(s)        
        return row_addition == column_addition == diagonal_addition*len(s)
    return False

print is_magic_square([[8, 3, 4], [1, 5, 9], [6, 7, 2]])
print is_magic_square([[1,2],[3,4],[5,6]])
print is_magic_square([[1,2],[3,4,5],[6,7]])