Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assignment help
#1
Hi guys just trying my luck here as i am having some trouble with my assignment. Any help is appreciated thank you!

In a class with n students where n>=2 , a bully is a student who is afraid of no one in the class but every other person in the class is afraid of them. You are asked to determine if there is a bully in a class. In your task, you are allowed to consider any pair of students (i,j) and ask student i if they are afraid of student j. Although you are allowed to ask as many (possibly intersecting) pairs of students as you wish, your objective is to minimise the number of such questions you need to ask, since the interrogation process can be rather traumatic for bullied students. Assume that all students are truthful when asked.

We will represent the class as an n-by-n matrix A, where the entry A[i][j] is 1 if student i is afraid of student j and 0 otherwise. We will set A[i][i] as 0.

Note: "student i is afraid of student j" does *not* imply that student j is a bully.


PseudoCode:

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
Reply
#2
def find_bully_1(A):
n=len(A)
candidate=0
afraid=0
for i in range(n):
for j in range(n):
if A[i][j]==1:
candidate=i
afraid+=1
if afraid==0:
return None
count=0
for j in range(n):
if A[candidate][j]==1:
count+=1
if count==n-1:
return candidate
else:
return None

i have implemented this code in python so far but i get no results and nothing comes out of the function.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020