Edit: To the people that downvoted: I was perfectly clear that I did not want code and that I had already tried it myself. All I was looking for was an explanation of what mathematical process yielded the sample results.
First question. I have done a lot of research and finally resorted to asking, so if I missed the answer somewhere I apologize. I have a problem I am really struggling with:
Write a Python 3 script that takes three command line arguments:
1. The name of a text file that contains n strings separated by white spaces.
2. A positive integer k.
3. The name of a text file that the script will create in order to store all possible subsequences of k unique strings out of the n strings from the input file, one subsequence per line.
For example, assume the command line is gen.py input.txt 3 output.txt and the file input.txt contains the following line:
Python Java C++ Java Java Python
Then the program should create the file output.txt containing the following lines (in any order):
Python Java C++
Python C++ Java
Java C++ Python
C++ Java Python
The combinations should be generated with your implementation of a generator function (i.e. using the keyword yield).
From my understanding, based on the sample output this doesn't quite follow the definition of a subsequence; nor are they quite permutations, so I'm at a loss for how to go about this. I know how to do the file IO and command line argument portions, I just can't get the right subsequences. I don't need a direct answer as I am supposed to solve this, but if someone could give me some helpful insight it would be much appreciated.
If you're allowed to use itertools:
import itertools
import sys
def unique_substrings(txt_lst:list, k:int) -> set:
return set([' '.join(combo) for combo in itertools.combinations(txt_lst, 3) \
if len(set(combo))==3])
if __name__ == "__main__":
infile, k, outfile = sys.argv[1:]
with open(infile) as inf:
txt_lst = infile.read().split()
with open(outfile) as outf:
for line in unique_substrings(txt_lst, k):
outf.write(line + "\n")
However from your instructor's comment:
The combinations should be generated with your implementation of a generator function (i.e. using the keyword yield).
It doesn't look like that's actually going to work.
itertools.combinations
could be re-implemented with something approximating the following (from the docs):
def combinations(iterable, r):
# combinations('ABCD', 2) --> AB AC AD BC BD CD
# combinations(range(4), 3) --> 012 013 023 123
pool = tuple(iterable)
n = len(pool)
if r > n:
return
indices = list(range(r))
yield tuple(pool[i] for i in indices)
while True:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range(i+1, r):
indices[j] = indices[j-1] + 1
yield tuple(pool[i] for i in indices)