# [closed] Why is my solution of 3.4 incorrect?

 0 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 151●5●10●26 accept rate: 0%

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

 2 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: Setting temp to the value of p[i]. Setting p[i] to the value of great. 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. We set tmp to 5. We set p[1] to 4. 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. answered 14 Mar '12, 07:19 randomling-4 1.1k●2●10●22
 2 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 answered 14 Mar '12, 07:44 mik 46●3
 1 Your procedure fails for the empty list. The line "great=p[0]" doesn't work if p[0] doesn't exist. answered 14 Mar '12, 07:19 MarkC 2.0k●13●42
 0 You don't take care of the null list possibility answered 14 Mar '12, 07:22 jimgb-2 7.3k●30●77●148
 0 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 answered 14 Mar '12, 07:47 dantonov 144●1●12

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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

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