Print a line if conditions were not met

Hi stackoverflowers colleagues, I am practicing my Python with the example given to me (actually a Google interview practice question) and ran into a problem I didn't know how to: a) represent correctly (hence the vague name), b) overcome.

The question arises: for an array of numbers (given or random), find unique pairs of numbers in the array, which, when summed, give a given number. EG: find pairs of numbers in the array, below which add 6.

[1 2 4 5 11] 

      

So in the above case:

[1,5] and [2,4]

      

The code I wrote is:

from secrets import *

i = 10

x = randbelow(10)

number = randbelow(100) #Generate a random number to be the sum that we are after#

if number == 0:
        pass
else:
        number = number

array = []

while i>0: #Generate a random array to use#
        array.append(x)
        x = x + randbelow(10)
        i -= 1

print("The following is a randomly generated array:\n" + str(array))
print("Within this array we are looking for a pair of numbers which sum to " + str(number))

for i in range(0,10):
        for j in range(0,10):
                if i == j or i>j:
                        pass
                else:
                        elem_sum = array[i] + array[j]
                        if elem_sum == number:
                                number_one = array[i]
                                number_two = array[j]
                                print("A pair of numbers within the array which satisfy that condition is: " + str(number_one) + " and " + str(number_two))
                        else:
                                pass

      

If no pairs are found, I want the string "No pairs found". I thought to try / exclude, but wasn't sure if it was correct or how to implement it. Also, I'm not sure how to stop duplicate pairs (unique pairs only), for example if I wanted 22 as a sum and had an array:

[7, 9, 9, 13, 13, 14, 23, 32, 41, 45]

[9,13] would appear twice

      

Finally, forgive me if there is redundancy / code not written very efficiently, I am slow to participate, so any other advice would be greatly appreciated!

Thanks for reading:)

+3


source to share


3 answers


You can simply add a boolean by keeping the answer to "was there at least one pair found?"

initialize it like found = false

at the beginning of your code.

Then when you find a pair (condition block containing the current command print

), just add found = true

.



after all your search (double loop for

), add the following:

if not found:
    print("No pairs were found")

      

0


source


Instead of actually comparing each pair of numbers, you can simply iterate over the list once, subtract the current number from the target number, and see if the remainder is in the list. If you convert the list to first set

, this search can be done in O (1), reducing the overall complexity from O (n²) to just O (n). Alternatively, all of this can be done in one line with the list:

>>> nums = [1, 2, 4, 5, 11]    
>>> target = 6    
>>> nums_set = set(nums)
>>> pairs = [(n, target-n) for n in nums_set if target-n in nums_set and n <= target/2]
>>> print(pairs)
[(1, 5), (2, 4)]

      

You can use a keyword to print pairs or some message or

. x or y

interpreted as x if x else y

, so if the result set is empty, the message is printed, otherwise the result itself.

>>> pairs = []    
>>> print(pairs or "No pairs found")
No pairs found

      




Update: The above may fail if the number appended to it is equal to the target, but it is only contained once per set. In this case, you can use collections.Counter

instead of set

and check the plurality of this number first.

>>> nums = [1, 2, 4, 5, 11, 3]
>>> nums_set = set(nums)
>>> [(n, target-n) for n in nums_set if target-n in nums_set and n <= target/2]
[(1, 5), (2, 4), (3, 3)]
>>> nums_counts = collections.Counter(nums)
>>> [(n, target-n) for n in nums_counts if target-n in nums_counts and n <= target/2 and n != target-n or nums_counts[n] > 1]
[(1, 5), (2, 4)]

      

0


source


First, state your limits!

  • added numbers must be unique
  • only 2 numbers can be added.
  • array length can be arbitrary
  • the number to be added can be arbitrary

& Don't miss pre-processing! Reduce problem space.

2 things right off the bat:

Starting with two print statements, I would do array = list(set(array))

to reduce the problem space to [7, 9, 13, 14, 23, 32, 41, 45]

.

Assuming all the numbers in question are positive , I would drop the numbers above number

.:
array = [x for x in array if x < number]

Giving[7, 9, 9, 13, 13, 14]

Combine the last 2 steps into a list comprehension and then use it like array

:

smaller_array = [x for x in list(set(array)) if x < number]

which gives array == [7, 9, 13, 14]

After these two steps, you can do a bunch of things. I totally understand that I didn't answer your question, but from here you got this. ^ This is the kind of stuff I would assume google wants to see.

0


source







All Articles