Recently I started learning python and in parallel I solve problems with http://pythontutor.ru/ . And stuck on a task for cycles while.

Task text: Condition
A sequence consists of natural numbers and ends with the number 0. Determine the index of the largest element of the sequence. If there are several largest elements, print the index of the first one. The numbering of the elements starts from zero.

My solution code is:

max = 0 element = -1 index = -1 while element != 0: element = int(input()) if element > max: max = element index += 1 print(index) 

However, two options do not pass. For example: Input: 2 1 3 0 Answer: 2 (I get 1)

I see an error in general, but I don’t understand how to implement it, which would be solved not only if the numbers are not entered evenly in ascending / descending order (such options were all decided)

    4 answers 4

    You check the increase of the index only when you find a larger number, but you always need to increase it.

     max = 0 element = -1 index = -1 index_max = 0 while element != 0: element = int(input()) index += 1 if element > max: max = element index_max = index print(index_max) 

    And your code can be improved if we take for initial values ​​not -1 and 0, but the value of the first element and its index.

    • hmm, then it does not correctly solve other options, such as: Input: 1 7 9 0 Answer: 2 (issues 1) - Howuhh

    To find the index of the largest element of a sequence, you can use the built-in function max ():

     numbers = map(int, iter(input, '0')) index, max_value = max(enumerate(numbers), key=lambda i_v: i_v[1]) print(index) 

    The code accepts integers from the user — one number on each new line, until zero ( '0' ) is encountered. The built-in function iter(function, end_value) generates a sequence by calling the function() until end_value .

    The built-in map() function converts strings to numbers in this case.

    The built-in function enumerate() generates pairs of index, value. lambda i_v: i_v[1] function retrieves a value from a pair, serving as a key for comparison, so the pairs are compared by their respective values.

    max() returns the pair with the highest value.


    Using max() in Python code is more preferable than a while loop more suitable for C code to solve the "Find the greatest element in a sequence" task in Python. Try to use the appropriate idioms for the selected language. If the task itself is not interesting, but you just want to find an error in the while loop, then ask about it.

    • Perhaps, but for a beginner, most of what you brought does not say anything, for I simply don’t know about that. Although this is certainly interesting - Howuhh
    • @Howuhh as I said, if you study cycles, then ask about cycles. Answers to Stack Overflow are not just for you personally, but for visitors from web search engines who are interested in finding the maximum and calling max () is the more idiomatic way to find the highest value / index than the while in Python. For the future, keep in mind when writing the title of the question (what the person expects as an answer). - jfs
    • Headline was not adjusted by me: / - Howuhh
    • @Howuhh your headline was too general, the fix is ​​completely justified. More precisely, it was possible to use: "find the index of the highest value using the while loop" (now no longer need to be edited). Avoid zero-type headers: "help, not working." Imagine that someone has already asked a question about your problem, write a title that you would click on in a search engine (the title should reflect the problem as you understand it). - jfs
    • OK I understood. Thank. - Howuhh

    Try it like this
    I changed the output so that it displays not only the index of the value, but also the value itself. In order not to be confused. And the algorithm is corrected, because the root of all troubles is 'index = -1', but you need to count from one

     max = 0 index = 1 element = - 1 while element != 0: element = int(input()) if element > max: max = element max_index = index index += 1 print(str(max_index)+'('+str(max) +')') 
    • Thank! I will try - Howuhh
     elements = [] while True: element = int(input()) if element: elements.append(element) else: break index = elements.index(max(elements)) 
    • Thank you, I knew that this can be done, but by the condition of the problem book it is assumed that I am not yet familiar with the lists. - Howuhh