LearnFrenzy
Sign in Take a free test

Python Built-in Functions · Built-in Functions Coding Problems · Question 10

Attendance report with enumerate, zip and sum in Python Solution, output & explanation

  • Program
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

Attendance is a string of P and A per student for 10 days. Print each student's attendance percentage, the students below 75%, and on which day numbers everyone was present.

Solution
main.py Run
names = ["Asha", "Ravi", "Meera"]
records = ["PPPPAPPPPP", "PAPAPPAPPA", "PPPPPPPPPP"]

for name, rec in zip(names, records):
    print(f"{name:<6}{rec.count('P') / len(rec):.0%}")
print("below 75%:", [n for n, r in zip(names, records) if r.count("P") / len(r) < 0.75])
print("full days:", [day for day, marks in enumerate(zip(*records), 1) if all(m == "P" for m in marks)])

Output Verified by running it on Python 3

Asha  90%
Ravi  60%
Meera 100%
below 75%: ['Ravi']
full days: [1, 3, 6, 8, 9]

zip(*records) gives each day's marks across students; enumerate numbers the days from 1 and all() checks that everyone was present.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links