Home > other >  Can someone please explain me this algorithm?
Can someone please explain me this algorithm?

Time:12-24

n1 = int(input("Number 1: "))
n2 = int(input("Number 2: "))
n3 = int(input("Number 3: "))
n4 = int(input("Number 4: "))

if n1 > n2 and n1 > n3 and n1 > n4:
    greatest = n1
elif n2 > n3 and n2 > n4:
    greatest = n2
elif n3 > n4:
    greatest = n3
else:
    greatest = n4

print(f" {greatest} is the greatest of the numbers.")

Why aren't we comparing n2 with n1 and n3 with n1 and n2? Can someone please explain this algorithm the simple way?

CodePudding user response:

we are elimination numbers here.

for for first condition if n1 > n2 and n1 > n3 and n1 > n4, we can say that n1 is will be greatest if it is greater than n2, n3, n4.

now say this condition fails, ie there is a number which is greater than n1 in [n4, n2, n3], so we will search for that number only, now we search in that scope only ie [n2,n3,n4]

so we follow condition 2 ie n2 > n3 and n2 > n4 , similarly here if this condition true, we will get n2 as greatest number, but if it fails, we can say [n3, n4] as greatest value among given number

so we narrow our scope to ccondiiton 3 ie n3> n4, if it is true we can say n3 is greatest number, if not then we can say n4 is greatest among all

CodePudding user response:

This algo is calculating the maximum among 4 numbers as input.

  1. It takes input at first of 4 different numbers.
  2. Then it first checks for 1st number if it greater than all other numbers if this is true it assigns greatest as 1st number if not :
  3. It goes to second case where we check if 2nd number is greater than 3rd and 4th. we are not checking 1st because it was not greater than the remaining numbers. If 2nd number is greater than it assigns greatest 2nd number and prints it but again if not:
  4. It then checks third and now we have two smaller numbers that are 1st and 2nd. so if 3rd is greater than 4th it assigns 3rd number to greatestelse it assigns 4th number because all other numbers are smaller than that of 4th number.

And then at last it prints the greatest number.

CodePudding user response:

The idea is to eliminate.

First condition if n1 > n2 and n1 > n3 and n1 > n4 we compare n1 with n2, n3, n4. if n1 is greater than all, then we have got the largest number (n1) but if n1 is not the largest then the first if fails so we have eliminated n1 as not largest.

Similarly we check if n2 is largest with remaining number (n3, n4).

so on and so forth.

CodePudding user response:

The reason has to do with the order the computer goes through the code. We only care about finding the biggest number.

  1. if n1 > n2 and n1 > n3 and n1 > n4 If this is TRUE then we know that n1 is the biggest number and we have found what we are looking for and the operation will stop. If it is FALSE then we know that n1 is not the biggest number and move on to operation 2.
  2. elif n2 > n3 and n2 > n4 If we make it this far in the operation, we already know from 1 that n1 is not the biggest number, therefore we don't need to compare any number to n1 any more since all we care about is finding the biggest number. If this is TRUE then n2 is the biggest number. If this is FALSE then we know that n2 is not the biggest number and move on to operation 3.
  3. elif n3 > n4 Now we know that n1 is not the biggest number AND n2 is not the biggest number, therefore we know that either n3 or n4 is the biggest number and all we need to do is compare those two remaining numbers. If this is TRUE then n3 is the biggest number, if it is false we move on to 4.
  4. else If we reach this point and nothing is TRUE yet, then we know that n1 is not the biggest number, n2 is not the biggest number, and n3 is not the biggest number. There are only 4 numbers (n1, n2, n3, n4) so we now know that n4 is the biggest number.

An important side note: If two numbers are the same, the program will still print out the biggest number but will find the second instance of that number, not the first one. For example: say

n1 = 10
n2 = 5
n3 = 3
n4 = 10

then the program will go through with the results: 1) FALSE 2) FALSE 3) FALSE 4) TRUE even though n1 is also the greatest number, operation 1 is false because n1 is not greater than n4 (10 is not greater than 10)

Hope this helps!

CodePudding user response:

Short answer:

If you remove a number that is known not to be the largest, the largest does not change.

E.g., the largest of { 3, 6, 9, 1 } is also the largest of { 6, 9, 1 }, which is also the largest of { 9, 1 }.

  • Related