Re: Looking for a slick way to classify relationship between two numbers, without tons of if/else



palewire wrote:
In my application, I'd like to have a function that compares two values,
either of which may be null, and then classify the result depending on
whether one is higher or lower than the other.

I find myself writing something clunky like this, and I'm curious whether
anyone might know a more elegant way for parsing this out.

def compare(a, b):
if not a and not b:
return "No data"
else:
if a == 0 and b == 0:
return "Never had"
else:
if a == b:
return "Stayed the same"
elif a < b:
return "Gained"
elif a > b and b > 0:
return "Lost Some"
elif a > b and b == 0:
return "Lost All"

If there's some obvious way to search for this solution online, feel free to
slap me with that. I tried digging around on Google and couldn't come up
with much.

Thanks much.
Before one can "optimize" a function, one needs to get the function correct. Looks to me that if a and b are both zero, it'll say "No data" and never get to the "Never had" test. And if a is positive and b is None, it'll return None, rather than any string.

Anyway, any "optimized" construct is going to be nearly as big as this. You'd have to define a list of strings, and then compute an index value that gave you the appropriate value. While I could probably come up with such, I hardly think it's worthwhile. The cases are confusing enough to me that I'd not want to make the code any less readable by some formula & lookup.

That brings us to the question of what is more elegant. To me the elegant code is readable first, and efficient (space & time) second. I suspect efficiency would be nearly optimal by careful ordering of the nested ifs. Find the most likely cases, and test for them first.

If this is a programming puzzle, for entertainment purposes, here's the way I'd tackle making it "efficient" obfuscated code. Start by checking if either value is None. Something like:

table = ("Lost Some","Lost All","Gained","Gained","Stayed the same","Never had, never will")
def compare2(a,b):
global table
if a is None or b is None:
return "Invalid data"
return table[4*(a==b)+2*(a<b)+(b==0)]

davea
.



Relevant Pages

  • Re: One liner challenge
    ... Who cares about elegant code? ... Your users don't care how elegant, cute, or clever your code is. ... I bet Microsoft can create cute some code, but it always doesn't work, does it? ... What is an elegant way of referring to the first string item delimited by a space without ...
    (microsoft.public.vb.general.discussion)
  • Re: chop, separate, split a STRING into sections?
    ... code looping such as DO WHILE or FOR NEXT. ... Public Function Chunk(ByVal str As String, ByVal chunkSize As Integer) As String ... an "elegant" solution. ... While first off, I don't see the problem with mid, substring, or looping:) ...
    (microsoft.public.dotnet.languages.vb)
  • Re: chop, separate, split a STRING into sections?
    ... Here's a sample of what I'm looking for: break apart a string into ... and there's no 'single function' or elegant 'one- ... Public Function Chop(ByVal sIn As String, ByVal iCars As Integer) As List(Of ... Dim los As New List ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Finding 1 of 3 different strings in a string
    ... I doubt if anyone would call this "elegant", but it should do what you want... ... Note the absolute cell addresses used in the example formulas being executed inside the CHOOSE function in response to your 3 error code messages... ... It would nice if Findreturned 0 if the string was ...
    (microsoft.public.excel.worksheet.functions)