简体   繁体   中英

How to compare two lists according to a specific requirement?

I have a reference list my_ref = [1,2,3] . I am trying to create a function that takes any arbitrary list of length 3 and checks if adding just number 1 to any element of that list, all its elements will be greater than equal to all elements of my_list . Since this explanation may be confusing, I present several examples as follows:

Example 1:

my_ref = [1,2,3]

 list1 = [0,2,4] 

If number 1 is added to one of the zeroes, then we have [0,2,4], so now all elements of list1 are greater than equal to all greatest elements of my_list, ie 1 = 1, 2=2 and 4 > 3. So the function should return True with that element to be added by 1, which was 0 in this example.

Example 2:

my_ref = [1,2,3]
list_2 = [2,2,1]

If number 1 is added to one of the two's, then we have [3,2,1], so all elements of list1 are greater than equal to all elements of my_list, ie 3 = 3, 2=2 and 1 = 1. Therefore, the function should return True with that element to be added by 1, which was 2 in this example.

Example 3:

my_ref = [1,2,3]
list_3 = [5,3,1]

By adding number 1 to any one's of list_3 , we will have: 5>3, 3>2, 2>1. Hence, the function returns True with the number 1.

BUT

Example 4:

my_ref = [1,2,3]
list_3 = [0,2,1]

There is no way to add number 1 to any elements of list_3 so that the explained requirement is met. Thus, the function should return False with None.

I have coded as follows:

def fun(l):
    my_ref = [1, 2, 3] 
    
    l.sort()
    for i in range(2, 0, -1):
        if l[i] < ref[i] and sum(l) >= 5:
            return True, l[i]

    return False, None

It works fine with most examples but not all. For instance, print(fun([4, 1, 0])) returns (True, 1) which is wrong, instead it should return (False, None) . I have tried a lot. Each time, the function with some examples work fine but with some examples doesn't. I hope my the problem is now clear.

How do I fix it?

Thank you.

def func(l):
    superior = 0
    ref = [0, 1, 2, 3]

    l1 = l[:]
    l2 = l[:]
    l3 = l[:]
    l4 = l[:]

   l1[0]+=1
   l2[1]+=1
   l3[2]+=1
   l4[3]+=1

   targets = [l1, l2, l3, l4]

   for i in range(len(targets)):
        if get_superior_count(targets[i]) >= 3:
            return True, i
   return False, None

def get_superior_count(l):
    ref = [0, 1, 2, 3]
    target = l[:]
    count = 0
    l.sort()
    m = 3
    n = 3

    while m > 0 and n > 0:
        if target[m] >= ref[n]:
            count+=1
            target.pop(m)
            ref.pop(n)
            m-=1
        n-=1
    return count

Not sure it is what you want (and it's a bit ugly) but seems working with your examples.

def checker(my_ref, l):
    if sum(l) +1 < sum(my_ref):
        return False
    return True

def align_lists(my_ref, l):
    return list(zip(sorted(my_ref), sorted(l)))

def compare_paired_elements(l, my_ref):
    requires_bump = 0

    if not checker(my_ref, l):
       return False
    
    for pair in align_lists(my_ref, l):
        ref_num = pair[0]
        num = pair[1]

        if num < ref_num:
            if num + 1 < ref_num:
                continue
            if num + 1 == ref_num:
                requires_bump += 1
                print(num, "needs a bump to match", ref_num)
    
    if requires_bump > 1:
        return False
    else:
        return True

Examples:

> my_ref = [1,2,3]
> l = [0,2,4]
> compare_paired_elements(l, my_ref)
0 needs a bump to match 1
True
> l = [4,1,0]
> compare_paired_elements(l, my_ref)
0 needs a bump to match 1
1 needs a bump to match 2
False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM