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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:03:01+00:00 2026-05-11T00:03:01+00:00

I have a string (of undertermined length) that I want to copy a lot

  • 0

I have a string (of undertermined length) that I want to copy a lot of times replacing one character at a time from an array (of undertermined length) of characters.

So say I have this string: ‘aa’
And this array: [‘a’, ‘b’, ‘c’, ‘d’]

after some magic for-looping stuff there would be an array like: [‘aa’, ‘ab’, ‘ac’, ‘ad’, ‘ba’, ‘bb’ … ‘dc’, ‘dd’]

How would you do this? I tried something using three for loops but I just can’t seem to get it.

Edit
The dependency on the string is the following:

Say the string is: ‘ba’
then the output should be: [‘ba’, ‘bb’, ‘bc’, ‘bd’, ‘ca’ … ‘dd’]

  • 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. 2026-05-11T00:03:01+00:00Added an answer on May 11, 2026 at 12:03 am

    If an order of strings in the result array doesn’t matter and all chars from the initial string are in the substitution array then:

    #!/usr/bin/env python from itertools import product  def allreplacements(seed, replacement_chars):     assert all(c in replacement_chars for c in seed)     for aset in product(replacement_chars, repeat=len(seed)):         yield ''.join(aset)  print(list(allreplacements('ba', 'a b c d'.split()))) # ['aa', 'ab', 'ac', 'ad', 'ba', 'bb', 'bc', 'bd', 'ca', 'cb', 'cc', #  'cd', 'da', 'db', 'dc', 'dd'] 

    Here’s a solution for a general case. Replacements are performed in a lexicographic order:

    #!/usr/bin/env python from itertools import product  def allreplacements(seed, replacement_chars):     '''Generate all possible replacements (with duplicates).'''     masks = list(product(range(2), repeat=len(seed))) # e.g., 00 01 10 11     for subs in product(replacement_chars, repeat=len(seed)):         for mask in masks:             # if mask[i] == 1 then replace seed[i] by subs[i]             yield ''.join(s if m else c for s, m, c in zip(subs, mask, seed))  def del_dups(iterable):     '''Remove duplicates while preserving order.      http://stackoverflow.com/questions/89178/in-python-what-is-the-fastest-algorithm-for-removing-duplicates-from-a-list-so#282589     '''     seen = {}     for item in iterable:         if item not in seen:            seen[item] = True            yield item  print(list(del_dups(allreplacements('ba', 'abcd')))) print(list(del_dups(allreplacements('ef', 'abcd')))) # ['ba', 'aa', 'bb', 'ab', 'bc', 'ac', 'bd', 'ad', 'ca', 'cb', 'cc', #  'cd', 'da', 'db', 'dc', 'dd']  # ['ef', 'ea', 'af', 'aa', 'eb', 'ab', 'ec', 'ac', 'ed', 'ad', 'bf', #  'ba', 'bb', 'bc', 'bd', 'cf', 'ca', 'cb', 'cc', 'cd', 'df', 'da', #  'db', 'dc', 'dd'] 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 74k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I am assuming that when you make the call to… May 11, 2026 at 2:22 pm
  • added an answer Did you try playing with the various caps and joints… May 11, 2026 at 2:21 pm
  • added an answer Depends on what you're trying to achieve. You could always… May 11, 2026 at 2:21 pm

Related Questions

I have a string of arbitrary length, and starting at position p0, I need
I have a string of test like this: <customtag>hey</customtag> I want to use a
I have a string of digits, e.g. 123456789, and I need to extract each
I have a string of values separated by a space that I return to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.