Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find multiple values on three parallel arrays using count occurence algorithm

I have to find the number of pupils who achieved 75% or more in three different exams. I wrote the code in two different versions where version 2 is successful but I am unable to find the correct value from version 1.

Version 1

literacy = [42,72,80,82,76]
numeracy = [55,70,75,79,48]
technology = [77,64,55,82,52]

count = 0

for counter in range(len(literacy)):
  if literacy[counter] >= 75 and numeracy[counter] >= 75 and technology[counter] >= 75:
    count = count + 1
        

print(count)

output (should result 7)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1

second version

literacy = [42,72,80,82,76]
numeracy = [55,70,75,79,48]
technology = [77,64,55,82,52]

count = 0

for counter in range(len(literacy)):
  if literacy[counter] >= 75:
    count = count + 1
  if numeracy[counter] >= 75:
    count = count + 1
  if technology[counter] >= 75:
    count = count + 1
        
print(count)

output

7

Any advise how to fix the first version.

>Solution :

For the first version, It’s iterated only 5 times so if you increment values by 1 you will not get the expected result 7. So the fixing of first version is your second version itself.

Here is another attempt from me.

count = 0
for i in literacy + numeracy + technology:
    if i >= 75:
        count = count + 1

In one line code,

In [1]: len([i for i in literacy + numeracy + technology if i >= 75])
Out[1]: 7 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading