osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Design a function that finds all positive numbers


I'm willing to bet "sorted" is a sort of the list of strings.  The result is certainly not what I'd expect if the list contained numeric values.

So: make a new list that holds the values in your "Array" (which is probably a list) converted to numbers.  Sort the new list.  That should give you're a numerically sorted result.

After that, you can look through the negative numbers on the new list (they will all be before any positive value), and try to find the positive value in the new list.  Output the positive value.
Warning: keep track of the values you have already checked, and don't output the same value twice.  You the values you've already looked for into a set (sets do not ever store duplicate values, not important in this case) and check if a subsequent value is already in the set.  I say this because there are three -2 values, and two 2 values, but I think you only want to print 2 once.

--- Joseph S.


-----Original Message-----
From: Ben Bacarisse <ben.usenet at bsb.me.uk> 
Sent: Tuesday, April 9, 2019 7:33 AM
To: python-list at python.org
Subject: Re: Design a function that finds all positive numbers

Ranjith Bantu <ranjithbantu at gmail.com> writes:

> A numeric array of length 'N' is given. you need to design a function 
> that finds all positive numbers in the array that have their opposites 
> in it swell. give the approach for solving the optimal average or best 
> case performance. answer will be your obsolute.
> Array: -7,4,-3, 2, 2, -8, -2, 3, 3, 7, -2, 3, -2
> sorted: -2, -2, -2 ,2 ,2, -3, 3, 3, 4, -7, 7, -8?
>
> can I solve any problems like this by learning python?

You need to learn how to solve problems as well as learning Python -- they go hand in hand -- but Python is a good language to get started with.

> if anybody
> knows this answer or any mistakes in this question, please explain to 
> me in detail?

That would require quite a long essay!  As to the question, it's a bit odd to talk of opposites, but it's not ambiguous.  More problematic is what to do with duplicates.  The question should be worded so that it's either clear what is wanted, or so that it is clear that you should decide what to do.

--
Ben.