SethClydesdale / genki-study-resources

A collection of exercises for practicing what is taught in Genki: An Integrated Course in Elementary Japanese.
https://sethclydesdale.github.io/genki-study-resources/lessons-3rd/
MIT License
822 stars 280 forks source link

Need Help Getting All String Combinations #27

Closed SethClydesdale closed 4 years ago

SethClydesdale commented 4 years ago

Hello!

I'm currently at a roadblock of sorts and could use some help.

I would like to return all possible combinations for a string while maintaining the proper order of everything and avoiding duplicates. The reason for this? I'd like to make answers more flexible by allowing a mix of kana and kanji. As such, I require all possible combinations for comparison against the user's answer.

This is the current syntax of the function: (located here)

Genki.getAlts('{月曜日}と{水曜日}と{金曜日}に{日本語}のクラスがあります', 'げつようび|すいようび|きんようび|にほんご');

The text within curly braces is the text that will be replaced by the alternative text in the second argument, I'll refer to these simply as replacements. HOWEVER, the alternate text should ONLY replace the same index. That is:

To give a simple example of what I'd like to achieve. Say I have the following:

Genki.getAlts('...{A}...{B}...', '1|2', true);

I'd like it to return all combinations, such as below.

'...1...{B}...'
'...1...2...'
'...{A}...2...'
'...{A}...{B}...'

The current implementation works well with 2-7 given replacements, but when given more than 8, the total combo coverage begins to drop. The total amount of combinations can be calculated using this formula: Math.pow(2, 8), which would return "256" combinations for 8 replacements, but currently getAlts() is only returning 234 combos, which means we're missing 22, only giving us 91% combo coverage.

So that is where I'm currently stuck. You can review the current code via the links below. (and yes, it's rather hackish) Being self-taught I tried my best to get as many combos as possible, but I'm afraid that my skill with mathematics isn't that good. I'm sure there's a much simpler way of going about this and I'm just overthinking it.

As an example of the current algorithm's failure, open your console, and you should see a warning for the last problem, saying something along the lines of:

234/256 (91.40625% combo coverage for 8 replacements; 22 missing combos

Is there any possible and simplistic way of returning all the combinations for a string regardless of how many replacements are specified?

If you're able to help with this, or possibly know of an existing algorithm or framework for achieving this, please let me know. Also let me know if you require any further information. I've given myself quite the headache with this, so thank you in advance for any help.

P.S. This task is not urgent, so there's no need to rush. As things are, the algorithm works fine for 2-7 replacements, with very few problems reaching or going above this threshold.

SethClydesdale commented 4 years ago

This issue has been solved with the help of Patrick Roberts from Stack Overflow.

https://stackoverflow.com/a/59337819/12502093

Many thanks!