0 like 0 dislike
0 like 0 dislike
Hey guys.

I'm trying make a loop that goes through "AAAA" to "ZZZZZZ" but I only want to loop through cases where only 2 letters are used. For example "DBBB" HSHS" "LLLS" and so on. What is the best way to make this algorithm?
0 like 0 dislike
0 like 0 dislike
Outer loop that iterates through each combo of two letters. AB, AC, AD, ...

Inner loop that creates combinations of the two letters.
0 like 0 dislike
0 like 0 dislike
Start with an easier problem.

How would you create all 2 letter combinations with 2 different letters?
0 like 0 dislike
0 like 0 dislike
If only two letters are used, you basically have binary numbers. Loop through all combinations of 2 letters, and for each combination, get all the 4-digit to 6-digit binary numbers and replace the 0s with your first letter and 1s with your second letter.
0 like 0 dislike
0 like 0 dislike
If len(set(letters)) <= 2:
  Iterate
0 like 0 dislike
0 like 0 dislike
It's probably in the book.
0 like 0 dislike
0 like 0 dislike
can you make all 2 letter combinations of length N? ( think recursion )

just loop i from 4 to 6 and make all possible 2 letter combinations of length i.
by
0 like 0 dislike
0 like 0 dislike
Knuth “The Art of Computer Programming” Vol 4A Ch. 7.2.1.3 “Generating all Combinations” has lots of material on this topic.
0 like 0 dislike
0 like 0 dislike
Go through all letters and compare them. That's it. And this is as explanatory as your question is.

Also maybe just look in your programming books or ask your professor instead of asking a forum to do your homework?
0 like 0 dislike
0 like 0 dislike
You could use nested loops: First you create all permutations of length 2  from the letters of the alphabet using itertools.permutations setting the 2nd (length) argument to '2'. For each of these permutations build all Cartesian products with itertools.product, setting the 'repeat' argument to 4 through 6 using a further loop. This is brute force, but I'm not aware of a more efficient approach.

No related questions found

33.4k questions

135k answers

0 comments

33.7k users

OhhAskMe is a math solving hub where high school and university students ask and answer loads of math questions, discuss the latest in math, and share their knowledge. It’s 100% free!