how to generate all combinations of certain alphabets with repeat and without repeat

Started by
3 comments, last by alvaro 5 years, 6 months ago

hi. im looking to generate all combinations of certain numbers or alphabets but as i search in internet, non of those algorithms work perfect. 

is there any method or algorithm that can do it well????

Advertisement

It's called "permutations", and the algorithm is trivial.

If you have N elements that you want to permute (if that's a valid verb), you get N loops, one for each position (one element is at one position, so  for N elements you need N positions). Each loop iterates over the entire set elements, except that it skips the elements that a previous loop has currently selected.

In code:


elements = [1, 2, 3]

for a in elements:

  for b in elements:
    if b == a:
      continue

    for c in elements:
      if c == a or c == b:
        continue

      # .. and more loops if you have more elements, then finally
      print(a, b, c)

Generalizing to sets of elements with double entries (use an index to identify the element rather than the element itself), or doing the above for a set elements of unknown size (use a recursive function) are nice extensions to try one time.

Could you elaborate or provide an example of what you're looking for? It isn't clear if you're using the term "combination" literally, or colloquially to mean some other type of arrangement.

If you think those algorithms "don't work perfect", you must have some idea in mind of what you want them to do. Since you are not giving us any details or examples of what you want, we are likely to produce algorithms with the same problems than the ones you are finding on the web.

You've been around these forums long enough that you should know how to ask questions by now.

 

This topic is closed to new replies.

Advertisement