Your goal is to find the largest number in a list of integers. What should be in place of A and B in the code below? def finding_max(lst): if len(lst) > 0: max = lst[−1] for a in lst[A:B]: if a > max: max = a return max else: print("Your list is empty.") A=0, B=-1 A=-1, B=0 A=1, B=len(lst) A=1, B=len(lst)+1
In this case, A and B should describe the range of numbers that the for-loop checks. It sounds like the program is supposed to check every number in a list, so the range should start at the beginning of the list (A), or the first number in the list, and end at the last number of the list (B) which would also be the same as the length of the list. The length of the list is the same as the number of things in the list, so another way to think of it is starting at space 1 and going through all the spaces until getting to the end--all through the length of the list.
Hmmmm okay I see this question is kinda tricky
This question is a bit tricky though, because some languages, like Python, do not include the last number within a range as part of the range. For example, range(1,4) would go through 1, 2, 3, but not include 4. To include 1,2,3,4 you would need to use range(1,5), adding 1 more to the range you actually want.
Since you want the for-loop to include all the spaces in the list, you may have to add 1 to the end of the range. In that case, A would still be 1 since you're starting at the beginning of the list (in some languages, it's conventional for the first space to be 0 instead of 1, but that doesn't seem to be the case here). And B would tell us how far down the list to go. We want to include all the items in the list, all the way to the end, all down the length. So, we would use len(lst) to find this length; but we also want to make sure the last space is included, so we may need to set B as len(lst)+1 instead. Perhaps you should review the language you're using in order to figure out whether B should be len(lst) or len(lst)+1?
So I'm thinking from the information you provided I can get rid of both the first and second option
Okay so I feel like B will be len(lst)+1 and I say this because of the +1
Sounds good to me :)
Join our real-time social learning platform and learn together with your friends!