I want to make a list of all combinations.

Asked 1 years ago, Updated 1 years ago, 331 views

There is a text file with 10,000 words (one new line at a time) consisting of five alphabetic characters.
Find out how many 5-character words are in the text file that can be created by rearranging the characters in the given n-character (5<=n) alphabet.
並び When sorting, all of the n characters given can only be used once.
意味 Some words have no meaning.Please take it as just a list of characters.
*No import

Assume that the string 'caarto' is given.
There are 720 permutations (in permutations) of these 6 to 5 characters.Of these 720 combinations, answer how many words appear in the text file.

First of all, I would like to make a correct list of 720 combinations, but the current code only shows 240 combinations.However, if you change the assumed string to 'cabrto' by one character and eliminate the duplication of 'a', 720 characters will be displayed correctly.I think the reason for this is that I can't distinguish the two 'a's well.
When I tried looking for a permutation to choose two characters from 'caarto' with the same code, I found that there were only 28 permutations, though it should have been 30 permutations correctly.

Now that I understand it, I'm stuck thinking about how to keep 'caarto' and display 720 different things.Please let me know.

Both python and jupyternotebook are the latest versions.This code generates only 240 copies, but if you remove the 'a' duplication, it will have 720 copies.

python recursion

2022-12-08 07:16

1 Answers

I will reply with a few explanations, although it overlaps with the comments.

new_s=s.replace(i, '') has erased the same character as the string i.
(For example, 'caatro'.replace('a',') returns 'ctro')
If you want to limit the number of character replacements to just one, set the third argument of the replace function to 1.

new_s=s.replace(i, '')#Before Modification
            new_s=s.replace(i, '', 1)# Corrected

Reference:


2022-12-08 20:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.