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

  • Home
  • SEARCH
  • 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 6367411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:32:40+00:00 2026-05-25T00:32:40+00:00

I have the following code: def produceCombinations(text, maximumWindowWidth) combinations = [] for windowWidth in

  • 0

I have the following code:


def produceCombinations(text, maximumWindowWidth)

  combinations = []

  for windowWidth in range(1,maximumWindowWidth+1):
    for startingIndex in range(len(text)-windowWidth+1):
      combinations.append((text[startingIndex:startingIndex+windowWidth]))

  return combinations

produceCombinations(("1","2","3","4","5","6",),3)

which gives the following output:

('1',)
('2',)
('3',)
('4',)
('5',)
('6',)
('1', '2')
('2', '3')
('3', '4')
('4', '5')
('5', '6')
('1', '2', '3')
('2', '3', '4')
('3', '4', '5')
('4', '5', '6')

However I also want this algorithm to give me the additional combinations:

('1', '3') # hop of 1
('2', '4')
('3', '5')
('4', '6')
('1', '4') # hop of 2
('2', '5')
('3', '6')
('4', '7')
('1', '3', '4') # hop of 1 and 0
('2', '4', '5')
('3', '5', '6')
('1', '2', '4') # hop of 0 and 1
('2', '3', '5') 
('3', '4', '6')
('1', '3', '5') # hop of 1 and 1
('2', '4', '6')
('1', '2', '5') # hop of 0 and 2
('2', '3', '6')
('1', '3', '6') # hop of 1 and 2
('1', '4', '5') # hop of 2 and 0
('2', '5', '6')
('1', '4', '6') # hop of 2 and 1

where my function will have a new argument called maximumHop so as to limit the number of these extra combinations. For the above example the maximumHop is two since the combination (‘1’, ‘5’) is not possible.

Any suggestions regarding a nice way to do this?

Thanks,

Barry

  • 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-25T00:32:40+00:00Added an answer on May 25, 2026 at 12:32 am

    use itertools
    see http://docs.python.org/library/itertools.html#itertools.combinations

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

Sidebar

Related Questions

I have the following code: def causes_exception(lamb): try: lamb() return False except: return True
I have the following code: def foo(*args) print len(args) print args now I'd like
I have the following piece of code def show unless logged_in? login_required return end
I have the following code: def bootstrap_form_field(f, method, text, &block) content_tag :div, :class =>
I have the following code: def steps(low, hi, n): rn = range(n) newrn =
Let's say I have the following ruby code : def use_object(object) puts object.some_method end
I have the following code: class IncidentTag: def __init__(self,tag): self.tag = tag def equals(self,obj):
Let's say that I have the following code in application_helper.rb : def do_something if
I have the following example code: class A(object): def __init__(self, id): self.myid = id
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public

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.