I was working today on a simple Python problem that said:

Define a function that takes two numbers as arguments. If both numbers are even, return the smaller one of them. If one numbers or both is odd, return the larger number between the two.

And, for this simple problem, I propose a simple solution:



First, I defined the function called ‘lesser’ that takes 2 arguments: a & b.

Afterwards, I asked the computer to check if both numbers are even. If so, the check if a is larger than b or b is larger than a. If the first one is correct, return b. Otherwise return a. 

If both numbers are not even, then it jumps to the second case, where it verifies if either one of the numbers is odd. If a is odd and lager than b, the program returns a. Else if b is odd and larger than a, the program returns b.

The problem may sound complicated, but, after you realise the logic behind it, the solution is actually a very simple function.

And that concludes this article about a simple Python solution.

I wish you good luck in solving this problem and finding a different solution!

Lasă un răspuns