[closed] Why is my solution of 3.4 incorrect?

def greatest(p):
    great=p[0]
    temp=0
    i=0
    for e in p:
        if p[i]>great:
            temp=p[i]
            p[i]=great
            great=temp
        i=i+1
    return great

print greatest([4,23,1])
print greatest([0])

Please explain my mistake.

asked 14 Mar '12, 06:48

Estilo's gravatar image

Estilo
15151026
accept rate: 0%

closed 14 Mar '12, 07:49

The question has been closed for the following reason "The question is answered, right answer was accepted" by Estilo 14 Mar '12, 07:49


5 Answers:

Hrm.

Let's see if I can work through what your code is doing in English...

Okay, you're taking a list, p as an input.

You're setting the variable great to the first element of p, then setting temp and i to 0.

Then you're looping over each element of p. If the current element of p (which is identified as p[i]) is greater than great (which starts at p[0]), you're:

  1. Setting temp to the value of p[i].
  2. Setting p[i] to the value of great.
  3. Setting great to the value of p[i].

If p[i] is equal to or less than great, you're doing none of the above.

Either way, you're incrementing i.

At the end of the procedure, you're returning great.

I think your logic is off here. What's happening is:

Let's say you're checking p[1]. The value of p[1] is 5, and the value of great is 4.

So p[1] is greater than great. Awesome.

  1. We set tmp to 5.
  2. We set p[1] to 4.
  3. We set great to the value of p[1], which is now 4.

So you're not getting the correct value out of this, because your logic isn't working quite the same.

What you want to do, I think, is simply set great to the higher value, and then move on. No need for the tmp variable. (Or indeed for iterating i, when instead of p[i], you could simply use e in this for loop.

Hope that's helpful.

link

answered 14 Mar '12, 07:19

randomling-4's gravatar image

randomling-4
1.1k21022

When the list is empty your code breaks. You should assign 0 to great to start with. That's the problem, but there are other serious flaws in your code albeit not as critical.

First since you are using a for ... in statement, you don't have to use i as an index to go through all elements, the for statement does that for you. You just have to use e instead of p[i] since the for statement assigns it to the next element during each loop iteration.

The second problem concerns the three lines of code in your if statement. As I understand the three elements basically extract the greatest element and put the previously greatest element back in the list. All of this you shouldn't do. The question asks you to determine the greatest element, not to find it and remove it from the list. You can get to the answer without modifying any of the elements in the list. The statement

great = p[i]

in the if statement would have sufficed. or if you were to take my advice

great = e
link

answered 14 Mar '12, 07:44

mik's gravatar image

mik
463

Your procedure fails for the empty list. The line "great=p[0]" doesn't work if p[0] doesn't exist.

link

answered 14 Mar '12, 07:19

MarkC's gravatar image

MarkC
2.0k1342

You don't take care of the null list possibility

link

answered 14 Mar '12, 07:22

jimgb-2's gravatar image

jimgb-2
7.3k3077148

edited 14 Mar '12, 07:22

See MarkC's answer and your slightly modified code:

def greatest(p):
    great=0
    for e in p:
        if e>great:
            great=e
    return great
link

answered 14 Mar '12, 07:47

dantonov's gravatar image

dantonov
144112

edited 14 Mar '12, 07:48

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×15,345
×1,718
×515
×115
×34

Asked: 14 Mar '12, 06:48

Seen: 214 times

Last updated: 14 Mar '12, 07:49