Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8152471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:43:29+00:00 2026-06-06T15:43:29+00:00

I often end up writing a bit of code twice when using a loops.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:43:31+00:00Added an answer on June 6, 2026 at 3:43 pm

    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:

    • @JFSebastian – 8.9939801693
    • @srgerg – 3.13302302361
    • yours – 2.8182990551.

    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:

    • Your code is as efficient as your algorithm that you have in mind.
    • The ifs 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).
    • Additionally compilers can also spot and modify/move the blocks around your code.
    • If there’s a language feature/library that makes your code fast and at the same time readable, use it. (Other answers here point out what python offers :))
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often end up writing code like if x == 1 or x ==
In our project, we often end up writing complex joins over like 3 tables.
I'm developing Scala code using Eclipse, often when I run tests I get this
I often end up with blocks of code like this: public class CustomFile {
I often use the website www.cplusplus.com as a reference when writing C code. I
When I am writing code in Python, I often need to remove items from
I often find myself writing var message = Hello {0}; and then going back
When I program I often end up making 5 or 6 different versions so
In my java coding, I often end up with several Map<String,Map<String,foo>> or Map<String,List<String>> and
This is a problem I come across every so often; I always end up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.