-1
import sys
trig = []
base, area = input().strip().split(' ')
base, area = [int(base), int(area)]
for i in range(1,area+1):
    form = abs(((base*i)/2)-area)
    trig.append(form)

for i in range(1,area+1):
    form = abs(((base*i)/2)-area)
    if form == min(trig):
        print(i)

I am trying to find the minimum value from the first for loop but I would like to avoid using a list since I want the program to run faster. How can I do this without using lists?

Kevin Rajan
  • 827
  • 8
  • 28
  • Correct me if I'm wrong, but won't the first value always be the smallest one...? – Aran-Fey Jun 09 '17 at 16:39
  • 3
    What exactly are you trying to achieve here? – zwer Jun 09 '17 at 16:39
  • 1
    You could store that min value in a variable, like `minTrig = min(trig)` and then use that value in `if form == minTrig:`. By doing that you will not be calling `min()` every time in the second for loop. – DarkCygnus Jun 09 '17 at 16:40
  • The first one will not always have the smallest value. I just tried running a small test and the minumum value comes later on in the trig array. – Kevin Rajan Jun 09 '17 at 16:42

2 Answers2

0

I'll go on limb and try to guess what you want based from your code - this will essentially keep just one variable and check for the minimum on each loop (and update accordingly, if needed):

base, area = input().strip().split()
base, area = int(base), int(area)

min_pos = 1
min_trig = abs(base / 2 - area)  # calculate the first immediately
for i in range(2, area + 1):  # loop from the second...
    form = abs((base * i) / 2 - area)
    if form < min_trig:
        min_pos = i
        min_trig = form

print(min_pos)

UPDATE

As stated in the comments, the above will find indexes only for a single minimum so it would be correct only if there is one minimum. If you need a list of minimums you can do as following:

base, area = input().strip().split()
base, area = int(base), int(area)

min_pos = [1]  # initialize the first index as a minimum immediately
min_trig = abs(base / 2 - area)  # calculate the first trig as a minimum immediately
for i in range(2, area + 1):  # loop from the second...
    trig = abs((base * i) / 2 - area)
    if trig == min_trig:
        min_pos.append(i)
    elif trig < min_trig:
        min_pos = [i]
        min_trig = trig

print(min_pos)  # prints you a list of all minimum positions

That way you don't have to store everything in a separate list nor you need to do double loops. Pure O(N) solution.

zwer
  • 24,943
  • 3
  • 48
  • 66
  • this solution does not work since the OP wanted the (index + 1) for all occurrences of the minimum value. The least time complex solution to this is O(N) since the list must first be created, then the minimum value must be found. – Kevin Rajan Jun 09 '17 at 16:55
  • 1
    Mea culpa, couldn't read the OP's mind... He wants the position, which is even easier, updated above. – zwer Jun 09 '17 at 17:04
  • Instead of making min_pos a single index, if it is an array, it can account for multiple indices. change `min_pos = i` to `min_pos.append(i)`. I did something very similar in my solution. – Kevin Rajan Jun 09 '17 at 17:07
  • That wouldn't help it - the above works only for singular solutions as it doesn't have the power to predict the future. If `min_pos` was a list it would contain multiple _indexes of minimum values at the time_. – zwer Jun 09 '17 at 17:09
-1

Based on what your code does now, I have created another version of your segment that essentially does the same thing. I tested it out with the input string "5 16" and it seems to be working. The code I used is as following:

import sys
trig = []
base, area = input().strip().split(' ')
base, area = [int(base), int(area)]
minTrig = (base * area) / 2 # set an upperbound on minTrig so that the max value in the trig array will be less than this value.
for i in range(1,area+1):
    form = abs(((base * i) / 2) - area)
    trig.append(form)
    minTrig = min(minTrig, form)
indices = [i + 1 for i, x in enumerate(trig) if x == minTrig]
print(indices)

This uses list comprehension to create a new array of all the indices at which the minimum number occurs.

The code used to find all occurrences of the minimum value element can be found here.

When I test out my code with "5 16" the output is [6, 7].

Kevin Rajan
  • 827
  • 8
  • 28