How do I find the max integer of a string of integer without using a list?
<p>I must use a for loop, no list allowed. May assume string is non-empty. What I have:</p>

<pre><code>def max_numbers(s):
    n_string = ''
    for char in s:
        if char.isdigit():
            n_string += char
    return max(n_string)

max_numbers('22  53 123 54 243')
5
expected 243
</code></pre>

<p>Please don't use any advance methods, I am still a beginner. I wanted 243 to show up but ended up with 5 instead.</p>

<p>The idea is to separate out the integers from the string (as you are already doing, but a bit wrongly), and keep track of the max so far. When a character is not digit, you convert the <code>n_string</code> to <code>int</code> and compare with <code>max_</code> which keeps track of maximum int found so far, and then set <code>n_string</code> empty for next number.</p>

<p>The final checking is for the last number, since at the end of the string there may not always be a non-digit, so the last iteration will not be in the <code>else</code> part.</p>

<pre><code>def max_numbers(s):
    n_string = ''
    max_ = 0
    for char in s:
        if char.isdigit():
            n_string += char
        else:
            if n_string and int(n_string) &gt; max_:
                max_ = int(n_string)
            n_string = ''

    if n_string and int(n_string) &gt; max_:
        max_ = int(n_string)

    return max_

print(max_numbers('22  53 123 54 243'))
</code></pre>

<p>Output:</p>

<pre><code>243
</code></pre>

<hr>

<p><strong>Edit:</strong></p>

<p>If <code>n_string</code> is empty, <code>int(n_string)</code> fails with an error</p>

<blockquote>
  <p>ValueError: invalid literal for int() with base 10: ' '</p>
</blockquote>

<p>So in order to avoid that, a check to see if <code>n_string</code> is empty or not is required <strong>before</strong> doing <code>int(n_string)</code>. In python, <a href="https://docs.python.org/3.5/library/stdtypes.html#truth-value-testing" rel="nofollow">empty string evaluates to <code>False</code>, and non-empty string to <code>True</code></a>. So, simply writing</p>

<pre><code>if n_string and int(n_string) &gt; max_:
</code></pre>

<p>checks if <code>n_string</code> is non-empty <em>and</em> <code>n_string</code> as integer is greater than <code>max_</code>. When <code>n_string</code> is empty, the second part of the <code>if</code> is not evaluated due to <a href="http://stackoverflow.com/questions/2580136/does-python-support-short-circuiting">short-circuiting</a> behaviour of <code>if</code>, thus the error is avoided.</p>

<hr>

<p>Some related links:</p>

<ul>
<li>Short-circuit in boolean expressions in python: <a href="http://stackoverflow.com/questions/2580136/does-python-support-short-circuiting">Link</a></li>
<li>Truth value testing in python: <a href="https://docs.python.org/3.5/library/stdtypes.html#truth-value-testing" rel="nofollow">official docs</a> and <a href="http://stackoverflow.com/questions/53513/best-way-to-check-if-a-list-is-empty/34558732#34558732">an answer by me</a></li>
</ul>

python