想要实现一个带有

时间:2018-02-09 04:47:12

标签: python

我有一个伪代码,我需要实现一个python代码。 这是我的伪代码

find_bully_1(A):
    for student in class:
        if student is afraid of no one:
            candidate = student

    if no such candidate found:
        return None

    if everyone is afraid of candidate:
        return candidate as bully
    else:
        return None

以下是我所做的,但有错误,它无法输出一些东西。我虽然不熟悉python

def find_bully_1(A):

    n = len(A)
    candidate = 0
    bully = 0
    a = []

    for i in range(n):
        count = 0
        for j in range(n):
            if A[i][j] == 0:
                count = count + 1
        if count == n:
            candidate += 1
            a = [a,i]

    count_scare = 0

    for k in [a]:
        count_scare = 0
        for g in range(n):
            if (zip(*A))[k][g] == 1:
                count_scare += 1
                if count_scare == n-candidate:
                    bully += 1

    return bully


x = [[1,1,1,1],
     [0,0,0,1],
     [0,0,0,0],
     [0,0,0,0]]

1 个答案:

答案 0 :(得分:2)

我认为您需要了解Python并遵循few tutorials。虽然,我喜欢Python,并希望证明它是多么容易和可读。以下代码几乎都是伪代码。

find_bully_in(my_class):
    for student, afraid in enumerate(my_class):
        if not any(afraid):
            candidate = student
        else:
            continue

        other_students = [student for student, _ in enumerate(my_class) if student != candidate]

        if all([afraid_of[candidate] for afraid_of in other_students]):
            return candidate
    else:
        return None

我认为任何其他语言都无法超越Python可以实现的可读性。

另一方面,这并不意味着你无法实现不可读的单行:

find_bully_in(cls):
    return next(filter(lambda s: not any(s[1]) and list(zip(*cls))[s[0]].count(1) == len(cls) - 1, enumerate(cls)), None)