我正在研究名為 Fat Fingers 的代碼戰中的 kata,以下是說明:
Freddy 的左手小指很胖,每次 Freddy 嘗試輸入 A 時,他都會不小心按到 CapsLock 鍵!給定一個 Freddy 想要輸入的字串,模擬鍵盤未命中的情況,假設按下的每個 A 都被 CapsLock 替換,并回傳 Freddy 實際輸入的字串。字串中的 A 是否大寫都沒有關系。啟用 CapsLock 時,大小寫顛倒,但不影響標點符號。
以下是示例:
“敏捷的棕色狐貍跳過了懶狗。” -> “敏捷的棕色狐貍跳過了 lZY 狗。”
“建立、維持和管理政府的目的是確保政治體的存在,保護它,并賦予組成它的個人安全和安寧地享受自然權利的權力,以及生命之福:當這些偉大的目標得不到實作時,人民有權改變政府,并為他們的安全、繁榮和幸福采取必要的措施。” ->“該機構的目的,即政府的管理和管理,是為了確保政治團體的存在,保護它,并為組成它的個人提供享受安全和自由的權力, ND THE BLESSING OF LIFE: nd 每當這些偉大的目標沒有得到時,人們有權改變政府,
“啊啊啊啊啊啊啊”->“”
我的代碼:
def fat_fingers(string):
#create a variable called capitalized ans initial it to false
#iterate a string and check if the current letter equal to A
#if its equal to a i will reverse the capitalize var.
#when i will append the c letter i will check if the capitalized = true
#then i will append it to a string Uppercased else i will do it Undercased
caps=False
stri=""
string2= list(string)
for i in string2:
letter = string2[i]
if letter == "a" or letter == "A" :
caps = not caps
else:
if caps :
stri = letter.upper()
else:
stri =letter
return str(stri)
uj5u.com熱心網友回復:
for 回圈for i in string2:遍歷 的字母string2。相反,您期望它為您提供字串上的索引。
只需i用作字母:
def fat_fingers(string):
#create a variable called capitalized ans initial it to false
#iterate a string and check if the current letter equal to A
#if its equal to a i will reverse the capitalize var.
#when i will append the c letter i will check if the capitalized = true
#then i will append it to a string Uppercased else i will do it Undercased
caps=False
stri=""
string2= list(string)
for letter in string2:
if letter == "a" or letter == "A" :
caps = not caps
else:
if caps :
stri = letter.upper()
else:
stri =letter
return str(stri)
uj5u.com熱心網友回復:
def fat_fingers(string):
caps=False
stri=""
string2= list(string)
for i in string2:
print (i)
# This line was incorrect
letter = i
if letter == "a" or letter == "A" :
caps = not caps
else:
if caps :
stri = letter.upper()
else:
stri =letter
return str(stri)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/404876.html
標籤:
上一篇:如何創建變數并在一行中做某事
下一篇:如果2 記錄,保持最高保費
