Friday, March 27, 2015

Check if 3 numbers are equal without using conditional or loop statements - Python Program (Procedural)


Problem Question

Write a python script to check if 3 numbers are equal or not. Print the following output:
  • All Numbers Unique: If all numbers are unequal
  • Two Numbers Equal: If one set of numbers is equal, and one is not
  • All Numbers Equal: If all the three numbers are equal
The program should not use any conditional statements or looping statements like (if .. else) or (do … while) or (switch… case) or (for…loop).

Explanation of Problem

This is a straightforward and easy program if we were to check if 3 numbers are equal or not. However, the added condition of not using conditional or loop statments makes it a bit tricky. The user would input 3 numbers. We need to check if they are equal (all are equal, or two numbers are equal), or not, and give the suitable output as indicated in the problem statement.

Code

#@Title: check_equal_without_if
#@Language: Python 3
#@IDE: PyCharm Community Edition
#@Author: Rogue Coder
#@URL: http://letsplaycoding.blogspot.com/
#@Date: 27-Mar-2015

first_number = int(input("Please input First Number: "))
second_number = int(input("Please input Second Number: "))
third_number = int(input("Please input Third Number: "))
result_array = ['All Numbers Unique', 'Two Numbers Equal', 'Not Possible', 'All Numbers Equal']
result = int((first_number == second_number)) + int((second_number == third_number)) + int((first_number == third_number))
print (result_array[result])

Explanation of Code

input -> This is a standard python built-in function which is used for accepting user input. The function accepts a string parameter, which is output to the user as a prompt for input message. The value returned is the user input in string format, which can be stored in a variable. In our case, we are storing the inputs in variables, first_number, second_number, and third_number. However, we typecast the user intput to int. Refer the standard documentation here.

int() -> This function is used to tyepcast a string into integer. The default conversion is done on base 10, however, we can specify the base as a second argument if needed. You may refer the standard documentation here.

result_array = ['All Numbers Unique', 'Two Numbers Equal', 'Not Possible', 'All Numbers Equal'] -> This is a string array, where we are storing the output that we would display to the user. The index of values is as under.
All Numbers Unique 0
Two Numbers Equal 1
Not Possible 2
All Numbers Equal 3



result = int((first_number == second_number)) + int((second_number == third_number)) + int((first_number == third_number)) -> This is the core logic of this program. The program equates each value with the other. The comparison returns a bool value. This value is either true if the condition is true, else false. In Python, like most programing languages out there, these values correspond to integer values 1 for true and 0 for false. Thus, each comparison is type-casted to int. The result of each expression typecasted to int is summed up, and stored in result variable.
Thus, if all values are unique, all expressions will evaluate to false, hence 0, causing the result to be 0 + 0 + 0, hence 0. If 2 values are equal, one of the three expressions would be true, rest false. Hence, result variable would store value 1 + 0 + 0, OR 0 + 1 + 0, OR 0 + 0 + 1, depending on which of the two values are equal. If all values are equal, then all expressions will evaluate to ture, hence 1. Thus, result variable in this case would store value 1 + 1 + 1, which is 3. Note that value 2 is not possible, becuase if 2 of the 3 expressions are true, that means third has to be true. Like, if a = b and b = c, then a = c is implicitly derived. This value that we get in result variable, will next be used to get the result displayed out.

print (result_array[result]) -> This is the final statement of the program that uses the result derived in the last statement, to get the array index. The table I added before the last explanation would now come in handy. As you can observe, the vlue stored in result variable can be 0, 1, or 3. The array result_array, has values at indexes 0, 1, 2, and 3. The program would use the result of the expression evaluation done in previous statement as the array index, and this statement would print the value at that index, thus giving the expcted output.

print() -> This is a standard Python built-in function, used to display a statement to standard output stream by default, or to a file if the necessary arguments are passed to the function call. You can refer the standard documentation here.

Output(s)


Download Source Code


No comments:

Post a Comment

Need help?