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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:37:26+00:00 2026-05-27T18:37:26+00:00

I was wondering what would be a a fairly ‘common’ or normal way of

  • 0

I was wondering what would be a a fairly ‘common’ or normal way of doing this. Wasn’t really looking for the shortest possible answer like a 2-liner or anything. I’ve just quickly put this piece of code together but I can’t not feel like there’s way too much in there.
Also if there are any libraries that could help with this, that would be very nice.

def get_cycle(line):
    nums = line.strip().split(' ')

    # 2 main loops, for x and y
    for x in range(2, len(nums)): # (starts at 2, assuming the sequence requires at least 2 members)
        for y in range(0, x):
            # if x is already in numbers before it
            if nums[x] == nums[y]:
                seq = [nums[x]] # (re)start the sequence
                adder = 1       # (re)set the adder to 1
                ok = True       # (re)set ok to be True
                # while the sequence still matches (is ok) and
                # tail of y hasn't reached start of x
                while ok and y + adder < x:
                    if nums[x + adder] == nums[y + adder]:  # if next y and x match
                        seq.append(nums[x + adder])         # add the number to sequence
                        adder += 1                          # increase adder
                    else:
                        ok = False                          # else the sequence is broken
                # if the sequence wasn't broken and has at least 2 members
                if ok and len(seq) > 1:
                    print(' '.join(seq))    # print it out, separated by an empty space
                    return
  • 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-05-27T18:37:27+00:00Added an answer on May 27, 2026 at 6:37 pm

    I may not be properly understanding this, but I think there is a very simple solution with regex.

    (.+ .+)( \1)+
    

    Here is an example:

    >>> regex = re.compile(r'(.+ .+)( \1)+')
    >>> match = regex.search('3 0 5 5 1 5 1 6 8')
    >>> match.group(0)    # entire match
    '5 1 5 1'
    >>> match.group(1)    # repeating portion
    '5 1'
    >>> match.start()     # start index of repeating portion
    6
    
    >>> match = regex.search('2 0 6 3 1 6 3 1 6 3 1')
    >>> match.group(1)
    '6 3 1'
    

    Here is how it works, (.+ .+) will match at least two numbers (as many as possible) and place the result into capture group 1. ( \1)+ will match a space followed by the contents of capture group 1, at least once.

    And an extended explanation for the string '3 0 5 5 1 5 1 6 8':

    • (.+ .+) will originally match the entire string, but will give up characters off the end because ( \1)+ will fail, this backtracking will occur until (.+ .+) cannot match at the beginning of the string at which point the regex engine will move forward in the string and try again
    • This will happen until the capture group starts at the second 5, it will give up characters at the end until '5 1' is captured, at which point the regex is looking for any number of ' 5 1' for ( \1)+, it will of course find this and the match will succeed
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering what would be considered best practise for this. It's fairly inconsequential but
i was wondering what would be the best way to code a browser plugin
Am wondering if there would be any security flaw in this approach. I am
I am wondering which way would end up being faster, selecting elements by: $('element[href=#my_link]');
I am fairly new to Objective-C and was wondering what the best way to
This is a fairly generic question in that I'm just wondering what potential options
I'm fairly new to javascript and I'm just wondering if there is a way
I am fairly new to jQuery, and I was wondering if it's possible to
I'm fairly new to xcode and iPhone development and I'm wondering if it's possible
I'm wondering how $this->load->vars() works in CodeIgniter. The documentation is fairly vague about it.

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.