Search code examples
pythonpython-3.xlistoutputlogic

Logical Error in Code - Not Outputting Properly


Question: There are “n” candidates participating in the online quiz competition. The questions, answer key and mark allocation are given below:

Q.No. 1 2 3 4 5

Ans: A B B A C

Each question carries 2 marks. For every wrong answer, 25 % of a mark should be reduced. There is no mark deduction for every unanswered question. Candidates should choose the option either “A” or “B” or “C” or “D” for answering a question. If a candidate wishes to not answer for a question, he/she should choose the option X.

Write a python function such that accepts number of candidates and the answer options of the individuals in the ascending order of questions and compute the rank of participating candidates based on the total marks.

Input Format

Number of candidates

Answer options

Output Format

Rank name Total

(Note here C1, C2, C3, …. in the output denotes Candidate 1, candidate 2, candidate 3….) CODE

A=[]
G=[]
C=["A","B","B","A","C"]
Candidates=[]
n=int(input())
for i in range(0,n):
    A.append(list(map(str,input().split(" "))))
for i in range(0,n):
    G.append(0)
    Candidates.append("C"+str(i+1))
    for j in range(0,5):
        if A[i][j]==C[j]:
            G[i]+=2
        elif A[i][j]=="X":
            G[i]+=0
        else:
            G[i]-=0.5


#find the elements of the array and print the name and marks


class output:
    def __init__(self) -> None:
        pass
    def search(i):
        cand=[]
        x=max(G)
        ind =G.index(x)
        cand.append(Candidates[ind])
        G[ind]=0
        y=cand[0]
        while x==max(G):
            next_num = G.index(x)
            cand.append(Candidates[next_num])
            G[next_num]=0
        if len(cand)>2:
            y = ','.join(map(str, cand)) 
        print(i+1,y,x)
        return x
        
print("Rank Candidates Total")        
for i in range(0,n):
    x = output.search(i) 

OUTPUT

Test 1: 1
Incorrect program output
--- Input ---
5
A C A X B
A B C D X
X X X X C
A A A A A
B X X B A

--- Program output ---
Rank Candidates Total
1 C2 3.0
2 C4 2.5
3 C3 2
4 C1 0.5
5 C1 0

--- Expected output (text)---
Rank Candidates Total
1 C2 3.0
2 C4 2.5
3 C3 2.0
4 C1 0.5
5 C5 -1.5

Test 2: 2
Incorrect program output
--- Input ---
3
A D D D D
C C C C C
X X X X X

--- Program output ---
Rank Candidates Total
1 C1 0.0
2 C1 0
3 C1 0

--- Expected output (text)---
Rank Candidates Total
1 C1,C2,C3 0.0

Solution

  • If you have not done so already, print A, G, C, and Candidates after the input phase to see if everything looks okay. My guess is that it is the output phase that is tripping you up. Here is a different way to come at the problem if you want to revisit your choice of data structures.

    from collections import defaultdict
    
    answers = ["A", "B", "B", "A", "C"]
    cands_by_score = defaultdict(list)
    
    num_cands = int(input())
    for cand in range(1, num_cands + 1):
        cand_choices = input().split()
        score = sum([
            2 if choice == answer else -0.5
            for choice, answer in zip(cand_choices, answers)
            if choice != "X"
        ])
        cands_by_score[score].append(f"C{cand}")
    
    print("Rank Candidates Total")
    for rank, score in enumerate(sorted(cands_by_score.keys(), reverse=True), 1):
        print(rank, end=" ")
        for i, cand in enumerate(cands_by_score[score]):
            print(f"{',' if i > 0 else ''}{cand}", end="")
        print(f" {score:0.1f}"