As your flight approaches the regional airport where you’ll switch to a much larger plane, customs declaration forms1 are distributed to the passengers.
The form asks a series of 26 yes-or-no questions marked a
through z
. All you need to do is identify the questions for which anyone in your group answers “yes”. Since your group is just you, this doesn’t take very long.
However, the person sitting next to you seems to be experiencing a language barrier and asks if you can help. For each of the people in their group, you write down the questions for which they answer “yes”, one per line. For example:
abcx
abcy
abcz
In this group, there are 6
questions to which anyone answered “yes”: a
, b
, c
, x
, y
, and z
. (Duplicate answers to the same question don’t count extra; each question counts at most once.)
Another group asks for your help, then another, and eventually you’ve collected answers from every group on the plane. Each group’s answers are separated by a blank line, and within each group, each person’s answers are on a single line. For example:
abc
a
b
c
ab
ac
a
a
a
a
b
This list represents answers from five groups:
3
questions: a
, b
, and c
.3
questions: a
, b
, and c
.3
questions: a
, b
, and c
.1
question, a
.1
question, b
.In this example, the sum of these counts is 3 + 3 + 3 + 1 + 1 =
11
.
For each group, count the number of questions to which anyone answered “yes”. What is the sum of those counts? Determine this in the following way:
group_count
that takes the “yes” answers (str
) of one group. The answers from each person in the group are separated by spaces. The function must return the number (int
) of questions to which anyone in the group answered “yes”.plane_count
that takes the pathname (str
) of a text file that collects the “yes” answers from every group on the plane. The function must return the sum (int
) of the number of questions to which anyone in the group answered “yes”.In this interactive session we assume the text file forms.txt
2 to be located in the current directory.
>>> group_count('abc')
3
>>> group_count('a b c')
3
>>> group_count('ab ac')
3
>>> group_count('a a a a')
1
>>> group_count('b')
1
>>> plane_count('forms.txt')
11