問題
文 (文字列) にプレースホルダーを作成する必要があります。
例
My son is 6 years old and my dad is 61 years old.
My son is #0 years old and my dad is #1 years old.
これはたった一文です。センテンスは行ごとに区切られており、センテンス内の数字のさまざまな位置が含まれています (数字の位置は混在しており、長さが異なります)。
さまざまなコードを試してみましたが、それらのほとんどはパターン置換で機能します。私は手順を知っており、Python プログラミングの経験があれば、それを行うことができます。
手順
文章(テキストファイルの行)を読みます。文中の数字 (d+) の数を数え、文が 6 つの数字で構成されている場合は、文の最後の数字から最初の数字 (#6、#5、#4、...) に置き換え始めます。
例えば
My dog is 3 years old, 92.4 cm heigh and has 16 teeth.
さまざまな長さの数字の数を数えます (1a、1b1、1c11、1111、1.1 は 8 つの数字): 4.
1. ループ(4 番目の数字を置き換えます):
My dog is 3 years old, 92.4 cm heigh and has #4 teeth.
2.ループ:
My dog is 3 years old, 92.#3 cm heigh and has #4 teeth.
3.ループ:
My dog is 3 years old, #2.#3 cm heigh and has #4 teeth.
4.ループ:
My dog is #1 years old, #2.#3 cm heigh and has #4 teeth.
ファイルの末尾に行を追加し、ファイルから別の行を取得します。ファイルの最後まで手順を繰り返します。