Ask your own question, for FREE!
Computer Science 25 Online
OpenStudy (anonymous):

Python help. Question below:

OpenStudy (anonymous):

l = [5, 7,8,4,10,30,25] i, m = 0, 0 l_min, l_max= [], [] for i in range(0, len(l), 2): if l[i] <= l[i+1]: l_min.append(l[i]) l_max.append(l[i+1]) m += 1 elif l[i] >= l[i+1]: l_min.append(l[i+1]) l_max.append(l[i]) m += 1 print l_min, l_max

OpenStudy (anonymous):

It shows an index error. Why is it happening?

OpenStudy (anonymous):

Your list `l` has 7 items, which means that `len(l)` will be 7. That means that the highest number `i` will get to is 6, which is the last index in your list. now `l[i+1]` corresponds to `l[7]` which leads to an index error. Your code seems to work on pairs of items in the list, are you sure your list supposed have an odd number of items?

OpenStudy (anonymous):

No, it is not fixed to odd number of inputs. What I was trying to do is, to find the maximum and minimum in a array of numbers. I tried the brute force, it worked. I am now trying to do it using divide and conquer. So, it is not really fixed to a off number of inputs.

OpenStudy (anonymous):

Anyway, I've modified it, as you suggested. :) l = [5, 7,8,4,10,30,25] m, i = 0, 0 l_min, l_max= [], [] if len(l)%2==1: l.append(0) for i in range(0, len(l)-1, 2): if l[i] <= l[i+1]: l_min.append(l[i]) l_max.append(l[i+1]) i,m = i+2, m+1 elif l[i] >= l[i+1]: l_min.append(l[i+1]) l_max.append(l[i]) i,m = i+2, m+1 print l_min, l_max And this works fine. Thanks for the help! :)

OpenStudy (anonymous):

Ah, ye in that case I wanted to suggest adding a dummy number heh Sure you're welcome =)

OpenStudy (anonymous):

By the way, if you just want to find a maximum or minimum why not run through all the numbers and keep aside the highest number you encounter?

OpenStudy (anonymous):

There is also a max() function that works in iterators that you could use heh

OpenStudy (anonymous):

That would take 2n number of steps. I wanted to do it in 3/2 number of steps. Yeah, max function helps, but I am trying to get a fluency on coding. I am kind of new to this.

OpenStudy (anonymous):

why 2n? it should take just n

OpenStudy (anonymous):

The worst case scenario. I have to iterate through all of the numbers twice to put the max and min in the variable. So, it has got 2n steps. n for finding max, n for finding min. In the other method, I plan to pairwise compare the numbers in the array and put the min and max in 2 new arrays. And then run min and max on the new arrays. So, n/2 for splitting, n/2 for finding max, n/2 for finding min. The other one takes more space, though.

OpenStudy (anonymous):

However, the two methods doesn't matter much for very large input sizes. Both act the same way.

OpenStudy (anonymous):

Remember that complexity of both is linear. that classification is important when you want to know how the number of steps will vary for longer inputs. So since they are both linear, they are both going to behave the same for longer input. double the input and you double the steps. Comparing the number of steps does not tell you which is faster. you could have twice as many small steps or half of it big steps. How about this code: ```python max = min = l[0] for n in l[1:]: if(n > max): max = n elif(n < min): min = n ```

OpenStudy (anonymous):

This does it in 'n' steps

OpenStudy (anonymous):

Yeah very true. Both behave the same for large input sizes. I was trying to do the 2nd one to get some familiarity with divide and conquer paradaigms.

OpenStudy (anonymous):

Yeah, true. Sure, I'll try that. Thanks, I appreciate your help :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!