import itertools
n = 4
s = ['H', 'U', 'P', 'M']
perm = itertools.product(s, repeat=n)
for i, j in enumerate(list(perm)):
permutation = ''
for item in j:
permutation += str(item)
with open('answer.txt', 'a') as text_file:
print(permutation.strip(), file=text_file)
EDIT:
The problem has finally been solved! Apparently, the answer SHOULD be sorted alphabetically, even though it says not to in the note below the problem statement. Also, according to someone in the questions section, the permutations should not be separated with new lines but instead with blank spaces. This is rather annoying because this is exactly what my first attempt at the program did, but then I read the note and changed it to the code above... Anyhow, I can now move on with my life. Below is the new code that produces results that are accepted by Rosalind, although that isn't what they asked for...
import itertools
n = 4
s = ['H', 'U', 'P', 'M']
perm = itertools.product(s, repeat=n)
answer = []
for i, j in enumerate(list(perm)):
permutation = ''
for item in j:
permutation += str(item)
answer.append(permutation)
sorted_answer = sorted(answer)
with open('answer.txt', 'a') as text_file:
print(*sorted_answer, sep=' ', file=text_file)
Here is a much simpler solution.
ReplyDeleteimport itertools
n = 2
s='A C G T'
lst = list(map(str,s.split()))
x = list(itertools.product(lst, repeat=n))
#print(x)
#print(len(x))
print(*[''.join(i) for i in x],sep='\n')