Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are 4 things I'd like to mention. Here's some code and its output:</p> <pre><code>def srk_func(words): current = [] lastc = [] for x in words: if len(current) == 0: current.append(int(x)) elif len(current) == 1: if current[0] &lt; int(x): current.append(int(x)) else: if len(current) &gt;= len(lastc): lastc = current current[:] = [] current.append(int(x)) elif len(current) &gt;= 2: if current[-1] &lt; int(x): current.append(int(x)) else: if len(current) &gt;= len(lastc): lastc = current elif len(current) &lt; len(lastc): current[:] = [] current[:] = [] current.append(int(x)) return lastc def jm_func(words): current = [] lastc = [] for w in words: x = int(w) if not current: # this happens only on the first element current = [x] continue if x &gt; current[-1]: current.append(x) else: # no increase, so current is complete if len(current) &gt;= len(lastc): lastc = current current = [x] # end of input, so current is complete if len(current) &gt;= len(lastc): lastc = current return lastc tests = """\ 1 1 5 5 1 1 5 7 7 5 1 1 5 7 0 1 5 7 0 3 1 5 7 0 2 4 6 8 1 3 5 7 9 11 0 2 """ for test in tests.splitlines(): wds = test.split() print wds print srk_func(wds) print jm_func(wds) print 8&lt;-------------------------------------------------- ['1'] [] [1] ['1', '5'] [] [1, 5] ['5', '1'] [1] [1] ['1', '5', '7'] [] [1, 5, 7] ['7', '5', '1'] [1] [1] ['1', '5', '7', '0'] [0] [1, 5, 7] ['1', '5', '7', '0', '3'] [0, 3] [1, 5, 7] ['1', '5', '7', '0', '2', '4', '6', '8'] [0, 2, 4, 6, 8] [0, 2, 4, 6, 8] ['1', '3', '5', '7', '9', '11', '0', '2'] [0, 2] [1, 3, 5, 7, 9, 11] [] [] [] </code></pre> <p>Topic 1: Test your code.</p> <p>Topic 2: Redundancy: Your code for <code>len(current) == 1</code> is functionally identical to your code for len >= 2, and the latter is bloated by having the following unnecessary two lines:</p> <pre><code> elif len(current) &lt; len(lastc): current[:] = [] </code></pre> <p>You can combine the two cases; see my version.</p> <p>Topic 3: It often happens in this kind of algorithm where you are processing input and keeping some "state" (in this case, current and lastc) that you can't immediately pack up and go home when you reach the end of the input; you need to do something with that state.</p> <p>Topic 4: This is going to get technical, but it's a common trap for new Python players. </p> <pre><code>&gt;&gt;&gt; current = [1, 2, 3, 4, 5] &gt;&gt;&gt; lastc = current # lastc and current refer to THE SAME LIST; no copying! &gt;&gt;&gt; print current [1, 2, 3, 4, 5] &gt;&gt;&gt; print lastc [1, 2, 3, 4, 5] &gt;&gt;&gt; current[:] = [] # The list to which current refers is cleared &gt;&gt;&gt; print current [] &gt;&gt;&gt; print lastc # lastc refers to the same list [] </code></pre> <p>It's better to just assign the name <code>current</code> to a new list; see my code.</p> <p>Topic 5 (bonus extra): Consider using 4-space indentation, not 8. Causing both the horizontal and vertical scroll-bars to appear in an SO question or answer == FAIL :-)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload