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.
python recursion
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:
str.replace
915 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
612 GDB gets version error when attempting to debug with the Presense SDK (IDE)
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
618 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.