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 8763569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:48:33+00:00 2026-06-13T15:48:33+00:00

I’m trying to use a for loop in order to formulate this: expand(‘*+123456’) should

  • 0

I’m trying to use a for loop in order to formulate this:

  • expand('*+123456') should return '1*2+3*4+5*6'

  • expand('++123') should return '1+2+3'

  • expand('+*1234') should return '1+2*3+4'

A symbol is chosen from the first two characters of the given string in an alternating fashion and placed between the proceeding digits.

Here is what I’ve been working with:

def expand(original):
    var = ""
    symbols = original[0:2]
    for i in range(len(original)):
        var = var + symbols + original[i]
    return var

I realize that there also must be a original[2:] but I don’t know where I can fit this into in here.

I’m an amateur and I’ve been trying to figure out this question for a long time.

  • 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-13T15:48:34+00:00Added an answer on June 13, 2026 at 3:48 pm

    Yes, your function would be improved by adding [2:] in there, but it could also do with losing the range(len(original)). Whenever you find yourself writing range(len(something)), you should step back and think about what you’re actually trying to do.

    In this case, you’re looking for the characters in the string, and you can get those more straightforwardly with for x in string. Here’s a slightly improved version, taking those ideas into account:

    def expand(original):
        var = ""
        symbols = original[:2]
        for digit in original[2:]:
            var += (digit + symbols)
        return var
    

    This stops you from getting the weird mixture of symbols at the beginning of the output, but it’s still not perfect:

    >>> expand('+*1234')
    '1+*2+*3+*4+*'
    

    We need to find a way of

    1. taking the symbols alternately, and
    2. leaving them off the end of the string.

    We can use itertools.cycle to handle the first of these, and string slicing for the second:

    from itertools import cycle
    
    def expand(original):
        var = ""
        symbols = cycle(original[:2])
        for digit in original[2:]:
            var += (digit + next(symbols))
        return var[:-1]
    

    That works, but at some point, someone’s going to pipe up and tell you you shouldn’t use + or += to build strings, because it’s inefficient. If we build a list instead, and then use str.join() to turn that into a string, we can improve things slightly:

    from itertools import cycle
    
    def expand(original):
        chars = []
        symbols = cycle(original[:2])
        for digit in original[2:]:
            chars.extend([digit, next(symbols)])
        return "".join(chars[:-1])
    

    However, we can do better than that. Rather than having to call next(symbols) every time, we can use zip() to get the next symbol and next digit a pair at a time:

    from itertools import cycle
    
    def expand(original):
        chars = []
        for symbol, digit in zip(cycle(original[:2]), original[2:]):
            chars.extend([digit, symbol])
        return "".join(chars[:-1])
    

    … and that’s probably enough 🙂

    EDIT: Since in a comment to another answer, you’ve said you’re not allowed to import anything from the standard library (a rather silly restriction IMO, but there it is), you can use the Python implementation of cycle() described at the link earlier in this answer:

    def cycle(iterable):
        # cycle('ABCD') --> A B C D A B C D A B C D ...
        saved = []
        for element in iterable:
            yield element
            saved.append(element)
        while saved:
            for element in saved:
                yield element
    

    … but you’ll probably have to be prepared to convince your teacher that you understand it, which means you need to understand the yield keyword.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

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.