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

Can anyone see a fault in the logic of these two algorithms?

I’m trying to work out what the logical fault between these two algorithms is and I can’t seem to crack it, does anyone have an idea? Both the programs execute and get the correct answer but I can’t work out what the logical error is.

data = [["Jacob", "91"], ["John", "81"], ["James", "71"], ["Joe", "61"]]
name = "Joe"
not_found = True
index = 0
marks = 0

# algorithm A
while not_found:
    if data[index][0] == name:
        marks = data[index][1]
        not_found = False
    index = index + 1
print(marks)

# algorithm B
for i in range(len(data)):
    if name == data[i][0]:
        marks = data[i][1]
print(marks)

>Solution :

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

Since you’re looking for "logical" errors in the algorithm, algorithm A has one.

while not_found:

If the name doesn’t exist in the array, you’ll get an index error. It should be

while not_found and index < len(data):
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