nishank
nishank

Reputation: 43

how to stop the looping if the condition satisfies in the first level

if the difference of the any two elements in my list is equal to n, then I have show the out as True else False

I have updated the code details i the "SHOW SOME CODE" area.please refer it

my_list = [1,3,4,9]
n = len(my_list) 
z = 3
def checking(my_list,z): 
    count = 0
    for i in range(0, n):           
        for j in range(i+1, n):         
            if my_list[i] - my_list[j] == z or my_list[j] - my_list[i] == z: 
                print('yes')
            else:
                print('No')
print(checking(my_list,z))

Expecting : if my list is-[1,3,4,9] and Z=3 then I am expecting just true as my output (becauase 4-1=3) I used 2 loops .can I solve this with only one loop

Actual:in the same example above I am getting the below output No yes No No No No None

Upvotes: 0

Views: 1206

Answers (6)

sarath madala
sarath madala

Reputation: 41

Use one flag. when the condition is true just set the flag to 1 and break the loop. if flag is not set to 1 continue to check.

my_list = [1,3,4,9]
n = len(my_list) 
z = 3
flag = 0
def checking(my_list,z): 
    for i in range(0, n) and flag is 0:           
        for j in range(i+1, n) and flag is 0:         
            if my_list[i] - my_list[j] == z or my_list[j] - my_list[i] == z: 
                flag = 1
if flag is 1:
  print("True")
else:
  print("False")

Upvotes: 0

codrelphi
codrelphi

Reputation: 1065

Here is another way to achieve your goal by using a list comprehensions.

Here, I use the absolute difference abs(). The real difference version is below (in the update part).

my_list = [1,3,4,9]
z = 3

def checking(my_list,z):
    outputs = [abs(i-j) for i in my_list for j in my_list if i != j and abs(i-j) == z]

    if len(outputs):
        print("Yes") # you can delete this line
        return True
    else:
        print("No") # you can delete this
        return False

# testing
print(checking(my_list, z)) 

Output:

Yes
True

UPDATE: The version below use the real difference and not the absolute difference.

my_list = [1,3,4,9]
z = 3

def checking(my_list,z):
    outputs = [(i-j) for i in my_list for j in my_list if i != j and (i-j) == z]

    if len(outputs):
        print("Yes") # you can delete this line
        return True
    else:
        print("No") # you can delete this
        return False

# testing
print(checking(my_list, z)) 

Output:

Yes
True

With this version, if z = -3, the result will be:

Yes
True

Upvotes: 0

iamsan
iamsan

Reputation: 111

For : "Expecting : if my list is-[1,3,4,9] and Z=3 then I am expecting just true as my output (becauase 4-1=3) I used 2 loops .can I solve this with only one loop"

Here is the solution using binary search [IMP: to perform binary search your list should be sorted]:

def checking(my_list, z):
    low = 0
    high = len(my_list)-1
    while low < high:
        find = my_list[high] - my_list[low]
        if find == z:
            return True
        else:
            if find > z:
                high -=1
            else:
                low +=1
    return False

print(checking([1,3,4,9],3))
print(checking([1,2,2,3],9))

Visit this link great resource on Data Structure using Python: https://runestone.academy/runestone/books/published/pythonds/SortSearch/TheBinarySearch.html

There is another approach to make this more efficient i.e. Hashing. Will leave that for you to research for solution.

Upvotes: 0

RightmireM
RightmireM

Reputation: 2492

import sys

def checking(my_list,z): 
    for i in range(0, n):           
        for j in range(i+1, n):
            if abs(my_list[i] - my_list[j]) == z : 
                return 'yes' # return ends the loop, by ending the entire function call
            # else: # comment out if you don't want the "no"s printed
            #     print('No. (Iteration i={}, j={})'.format(i,j)) # comment out if you don't want the "no"s printed
    # if it reaches here, then the both the loops finished 
    # without finding a True condition. 
    # So simply return the No (no reason for an else.)
    return ("No") 

my_list = [1,3,4,9]
n = len(my_list) 
z = 3
print("First run with [{}]".format(my_list))
print(checking(my_list,z))

my_list = [1,1,1,1]
n = len(my_list) 
z = 3
print("First run with [{}]".format(my_list))
print(checking(my_list,z))

OUTPUT:

First run with [[1, 3, 4, 9]]
yes
First run with [[1, 1, 1, 1]]
No

Upvotes: 0

Chris Doyle
Chris Doyle

Reputation: 12027

You can use combinations from itertools module to generate pairs of your list and return True if the condition is satisfied otherwise return false. I have added a print line just so you can see which combinations it processes. I have also used True or False as thats what you say in you

from itertools import combinations


def checking(my_list, z):
    for i, j in combinations(my_list, 2):
        print("Checking pair: i=",  i,  ", j=", j)
        if i - j == z or j - i == z:
            return True
    print("no matches found")
    return False


my_list = [1, 3, 4, 9]
my_list2 = [1, 3, 5, 9]
z = 3
print(checking(my_list, z))
print(checking(my_list2, z))

OUTPUT

Checking pair: i= 1 , j= 3
Checking pair: i= 1 , j= 4
True
Checking pair: i= 1 , j= 3
Checking pair: i= 1 , j= 5
Checking pair: i= 1 , j= 9
Checking pair: i= 3 , j= 5
Checking pair: i= 3 , j= 9
Checking pair: i= 5 , j= 9
no matches found
False

Upvotes: 2

A Bear
A Bear

Reputation: 59

Try using a break statement after the print statement in the conditional. This is used to step out of whatever conditional or loop you are in. This is assuming you are not doing this in a method, which in that case I'd suggest returning 'yes' which automatically exits the function. Either or should work.

Upvotes: 0

Related Questions