Skip to content
ExamSpine

Lesson 5.13 · Write it

Questions everyone got right

A functionPython or Java11 tests, 6 of them hidden

On a phone? Put the lines in order instead

A quiz is marked in a grid. Each row is one student, and each column is one question. A cell holds 1 if that student got that question right, and 0 if not. In Python the grid is a list of rows; in Java it is an int[][].

Write a function that returns how many questions every student got right.

  1. A question counts only if its column has a 1 in every row.
  2. There is at least one student and at least one question, and every row has the same length.
  3. The number of students and the number of questions can be different.
  4. Every cell is 0 or 1.

Only the number your function returns is checked.

def questions_all_right(answers: list[list[int]]) -> int

Examples

Your function is tested on 11 cases: the 5 examples below, and 6 hidden ones that check the rules above.

  • questions_all_right([[1, 1, 0], [1, 0, 0]]) should return 1: 2 students, 3 questions: only question 0 has a 1 in every row
  • questions_all_right([[0, 1], [1, 1], [1, 1]]) should return 1: 3 students, 2 questions: question 0 has a 0, question 1 counts (rule 1)
  • questions_all_right([[1]]) should return 1: one student and one question (rule 2)
  • questions_all_right([[0, 0], [0, 0]]) should return 0: no question counts
  • questions_all_right([[1, 1, 1, 1]]) should return 4: one student who got all 4 questions right (rule 3)

public static int questionsAllRight(int[][] answers)

Checking whether Java answers can run here.

Examples

Your function is tested on 11 cases: the 5 examples below, and 6 hidden ones that check the rules above.

  • questionsAllRight(new int[][]{new int[]{1, 1, 0}, new int[]{1, 0, 0}}) should return 1: 2 students, 3 questions: only question 0 has a 1 in every row
  • questionsAllRight(new int[][]{new int[]{0, 1}, new int[]{1, 1}, new int[]{1, 1}}) should return 1: 3 students, 2 questions: question 0 has a 0, question 1 counts (rule 1)
  • questionsAllRight(new int[][]{new int[]{1}}) should return 1: one student and one question (rule 2)
  • questionsAllRight(new int[][]{new int[]{0, 0}, new int[]{0, 0}}) should return 0: no question counts
  • questionsAllRight(new int[][]{new int[]{1, 1, 1, 1}}) should return 4: one student who got all 4 questions right (rule 3)
solution.pySolution.java