I often end up writing a bit of code twice when using a loops. For example, while going over the Udacity computer science course, I wrote the code (for a function to find the most sequentially repeated element):
def longest_repetition(l):
if not l:
return None
most_reps = count = 0
longest = prv = None
for i in l:
if i == prv:
count += 1
else:
if count > most_reps:
longest = prv
most_reps = count
count = 1
prv = i
if count > most_reps:
longest = prv
return longest
In this case, I'm checking twice if the count is greater than the previously most repeated element. This happens both when the current element is different from the last and when I've reached the end of the list.
I've also run into this a few times when parsing a string character by character. There have also been a few times where it's been up to about 5 lines of code. Is this common, or a result of the way I think/code. What should I do?
edit: Similarly, in a contrived string splitting example:
def split_by(string, delimeter):
rtn = []
tmp = ''
for i in string:
if i == delimeter:
if tmp != '':
rtn.append(tmp)
tmp = ''
else:
tmp += i
if tmp != '':
rtn.append(tmp)
return rtn
edit: The exam this was from was written for students of the course who are not expected to have any outside knowledge of Python; only what was taught in the previous units. Although I do have prior experience in Python, I'm trying to adhere to these restrictions to get the most of the course. Things like str.split, lists, and a lot of the fundamentals of Python were taught, but nothing yet on imports - especially not things like groupby. That being said, how should it be written without any of the language features that probably wouldn't be taught in a programming introduction course.
Since you tagged language-agnostic
, I see that you wont be much interested in python-specific stuff you could use to make your code efficient, compact and readable. For the same reason, I am not going to show how beautiful a code can be written in python.
In some of the cases that extra if
at the end can be avoided depending on your algorithm, but most cases it's like "If it exists, it should be significant and/or efficient." I dont know about the how the python interpreter works, but in compiled languages like C/C++/etc. the compiler performs various kinds of loop optimisations, including moving the if-blocks out of a loop if it does the same thing.
I ran and compared the running time of various snippets:
It's not a generalisation that a trailing if
gives you the best time. My point is: just follow your algorithm, and try to optimise it. There's nothing wrong with an if
at the end. Probably alternative solutions are expensive.
About the second example you have put in: The check tmp == ''
is done to ensure only non-empty strings are returned. That actually is a sort of additional condition over your splitting algorithm. In any case, you need an additional rtn.append
after the loop because there's still something beyond the last delimiter. You could always push an if condition inside the loop like if curCharIndex == lastIndex: push items to list
which will execute in every iteration, and its sort of the same case again.
My answer in short:
if
s in the end are encountered in many cases -- no need to worry about them, they may be making the code more efficient than alternative approaches without such an if (examples are right here).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With