Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Okay here are lots of problems in you python code. </p> <p>Well start with the simple </p> <pre><code>while ip['cp'] != []: </code></pre> <p>Could be written to simply </p> <pre><code>while ip['cp']: </code></pre> <p>It will do while <code>ip['cp']</code> isn't a falsly value. <code>[], None, ''</code> are a falsly value for example.</p> <p>Instead of: </p> <pre><code>if ip['dp'] == '&gt;': ip['cp'].next() elif ip['dp'] == '&lt;': ip['cp'].prev() else: raise NameError('incorrect dp-value') </code></pre> <p>write</p> <pre><code>if ip['dp'] == '&gt;': ip['cp'].next() elif ip['dp'] == '&lt;': ip['cp'].prev() else: raise NameError('incorrect dp-value') </code></pre> <p>Even if in some case, writing inline code can work. Think about readability. Inline code is almost never a good start to make code readable and easy to debug. It will also make 90% of your syntax error easy to fix.</p> <p>Here's where the fun starts...</p> <pre><code> if( cp-&gt;val == '0' ) { cp-&gt;prev-&gt;next = cp-&gt;next; cp-&gt;next-&gt;prev = cp-&gt;prev; } else cp-&gt;val--; </code></pre> <p>and</p> <pre><code> if ip['cp'] == '0': ip['cp'] = [] else: ip['cp'] -= 1 </code></pre> <p>In python '0' is the string <code>0</code>. In python you're working with string when you have a string object. You're working with numbers when you have numbers... In other words the code you're trying to write in python can't be purely translated to python. The last char in a python string is not '\0'. It's the last char in the string. In other words, you can really test for the last char like in C. </p> <h1>Now lets talk about iterators</h1> <p>That said, disregarding all other problems in your code, here is how to work with iterators.</p> <p><code>iter()</code> is a builtin function that returns an iterator. In most case you don't have to call it yourself. There are constructions that will do that for you.</p> <p>for example, you can write :</p> <pre><code>iterable = iter('string') iterable.next() == 's' iterable.next() == 't' ... Until StopIteration is raised for i in iter('string'): </code></pre> <p>Is the same as:</p> <pre><code>for i in 'string': </code></pre> <p>That said, iterators are used to iter over something. What you are really looking for isn't an iterator. Because iterator are one way thing. You can go to the end and you can't go back. There is no previous. Your <code>C</code> code implement a linked-list that can be used with an iterable but in your code, you're changing the position of some nodes.</p> <h1>If you want to write python code, don't write C code in python</h1>
 

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