0

I have a sorting algorithm, that sorts 2-d matrices by sums of elements in column:

def task(x): 
   while isDone < len(arr[0]) - 1:
        isDone = 0
        for x in range(len(arr[0])):
            try:
                if arr[0][x] + arr[1][x] < arr[0][x + 1] + arr[1][x + 1]:
                    tmp0 = arr[0][x]
                    tmp1 = arr[1][x]
                    arr[0][x] = arr[0][x + 1]
                    arr[1][x] = arr[1][x + 1]
                    arr[0][x + 1] = tmp0
                    arr[1][x + 1] = tmp1

                else:
                    isDone += 1
            except:
                pass

The if condition sorts arrays in descending order. Can i dynamically change operator '<' to '>' by passing argument into function, so condition will change, but code that executes, remains the same?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153

3 Answers3

1

You could use a comparison function which is passed in and the operator library.

def task(x, comparison_function): 
   while isDone < len(arr[0]) - 1:
        isDone = 0
        for x in range(len(arr[0])):
            try:
                if comparison_function(arr[0][x] + arr[1][x], arr[0][x + 1] + arr[1][x + 1]):
                    tmp0 = arr[0][x]
                    tmp1 = arr[1][x]
                    arr[0][x] = arr[0][x + 1]
                    arr[1][x] = arr[1][x + 1]
                    arr[0][x + 1] = tmp0
                    arr[1][x + 1] = tmp1

                else:
                    isDone += 1
            except:
                pass

import operator

less_than = task(3, operator.lt)
greater_than = task(3, operator.gt)
blueteeth
  • 3,330
  • 1
  • 13
  • 23
0

You need to think about this differently....

If you know the "condition" which would trigger you to want to change the operator, which obviously you must, then you can just write that into an if-elif block. Let's say that you wanted to change the operation after you get to 500 records. (Just an example, it could be any logical condition). Then you have something like:

if a > b:
  do stuff
elif num_records > 500 and a < b:
  do other stuff
...
num_records += 1

Edit

Inside of a function, I would just use a boolean argument and, when needed, negate the "if" expression with xor like:

def compare(x, y, ascending=True):
    if (x < y) ^ ascending:
        print('x is selected')
    else:
        print('y is selected')

Note the XOR flips the condition. Try it out.

Your posted code has a handful of errors in it, so I didn't edit it directly.

AirSquid
  • 10,214
  • 2
  • 7
  • 31
  • yes, I know about this approach, but the point is i don't want to re-write stuff, because code after condition is the same, but the condition should change my bad for incomplete question, let me rewrite it – 4lll0w_3v1l Oct 10 '22 at 22:44
-1

If you must, you can do something like this...

def foo(operator = '>'):
    a = [1, 2]
    expr = f'a[0] + a[1] {operator} a[1]'
    print(eval(expr))

foo('>')
foo('<')
===============
True
False
mike01010
  • 5,226
  • 6
  • 44
  • 77
  • thanks, that's what I was trying to find, but @blueteeth 's approach works a lot faster – 4lll0w_3v1l Oct 11 '22 at 08:21
  • Using eval is not recommended. Ever. You could do `foo('; import requests; requests.get("some.malicious.server");')` for a code injection here. – blueteeth Oct 14 '22 at 18:06
  • Hence 'if u must '. If the function isnt exposed to public or bad actors...quick and dirty – mike01010 Oct 15 '22 at 19:17