Home > database >  Finding Inequality of 4 variables using Nested If else only
Finding Inequality of 4 variables using Nested If else only

Time:11-06

Lets say I have 4 variables, a,b,c and d.

I'm trying to write a code(in the shortest formate) using only If Else (in nested formate) to arrange the variables in accensending order.

I'm sure their possibly are ways to do it smothly but using this method do you think if the code can be shortened

Do you guys have any simpler way of finding inequality in such a way

**```

a=19
b=10
c=1
d=8

if a>b:
    print()
    if a>c:
        print()                                                         #ALL OK
        if a>d:
            print()
            if b>c:
                print()
                if c>d:
                    print("a>b>c>d")
                else:
                    print("a>b>d>c")
            else:
                print()
                if b>d:
                    print("a>c>b>d")
                else:
                    print("a>c>d>b")
            
        else:
            print()
            if b>c:
                print("d>a>b>c")
            else:
                print("d>a>c>b")
    else:
        print()
        if a>d:
            print()
            if d>b:
                print("c>a>d>b")
            else:
                print("c>a>b>d")
        else:
            print()
            if c>d:
                print("c>d>a>b")
            else:
                print("d>c>a>b")
else:
    print()
    if b>c:
        print()
        if b>d:
            print()
            if a>c:
                print()
                if c>d:
                    print("b>a>c>d")
                else:
                    print("b>a>d>c")
                    
            else:
                print()
                if a>d:
                    print("b>c>a>d")
                else:
                    print("b>c>d>a")
        else:
            print()
            if c>a:
                print("d>b>c>a")
            else:
                print("d>b>a>c")
    else:
        print
        if c>d:
            print()
            if a>d:
                print("c>b>a>d")
            else:
                print("c>b>d>a")
        else:
            print("d>c>b>a")
```
`
```**

CodePudding user response:

Probably don't want to use nested statements in this scope, as you can see it get's unweildly very quickly.

a = 19
b = 10
c = 1
d = 8
# Note you can shorten the nesting by using multi-conditional (might not be the term!)
if a > b > c > d: print("a>b>c>d")
elif b > c > d > a: print("b>c>d>a")  # and so on..
# but you're still looking at hardcoding every possible permutation of [a, b, c,d] or in the event of a dataset with X elements..
# Nested if's are useful but not in this scope

# In the context of this problem it would be better to sort
# the data and create a string based on that
as_dict = {"a": a, "b": b, "c": c, "d": d}
sorted_d = sorted(as_dict, key=lambda x: as_dict[x])[::-1]
print(">".join(sorted_d))

CodePudding user response:

#Pure pythonic You can try uisng and which will be clean & Append results to results in order accordingly

a=19
b=10
c=1
d=8
s=''
how_many_var =4

for i in range(how_many_var):
    
    if a > b and a > c and a > d:
        print(a)
        a=0
        s  = 'a>'
        
    elif b > a and b > c and b > d:
        print(b)
        b=0
        
        s  = 'b>'
    elif c > a and c > b and c > d:
        print(c)
        c=0
       
        s  = 'c>'
    elif d > a and d > b and d > c:
        print(d)
        d=0
        
        s  = 'd>'
print(s)

output #

19
10
8
1
a>b>d>c>
  • Related