[英]python challenge 3 : print strange output
我是python的初學者,我玩python挑戰,但遇到了問題#3,我的代碼工作正常,但輸出是視圖源中的奇怪問題字符串
x = 0
small = ''
while (x<len(s)) :
#take string with length 7 one at a time e.g : 'AKLoKPL'
y = s[x : x+7]
op = ord(y[0])-65
if (0<=op<=25) : #check if the first char is capital
if (0 <= ord(y[1]) - 65 <= 25 and 0<= ord(y[2]) - 65 <=25 ) :
if(0 <= ord(y[4])- 65 <=25 ) : #check other side
if (0 <= ord(y[5]) - 65 <= 25 and 0<= ord(y[6]) - 65 <=25 ) :
if (0<= ord(y[3])-97 <= 25) :
small += y[3] # add small char (o to small )
x +=7
print(small)
我得到以下輸出: egtdjbjfjhcxjkpntpdufhqfwxhqhqskeffmdiudiujmkinphjzujpbfhmxvvgoudtqfuspuzcashuszom
我做錯了什么? 如果我是對的,下一步該怎么做?
使用這樣的正則表達式並刪除所有這些循環'[AZ] {3} [az] [AZ] {3}'
import re
pattern = re.compile('[A-Z]{3}[a-z][A-Z]{3}')
print(re.match(pattern, <your_string>).group())
您需要使用以下模式來匹配由3個大寫字母包圍的小寫字母:
(?<![A-Z])[A-Z]{3}([a-z])[A-Z]{3}(?![A-Z])'
(使用否定的環顧四周斷言來避免匹配項包含超過3個大寫字母)
>>> pattern = r'(?<![A-Z])[A-Z]{3}([a-z])[A-Z]{3}(?![A-Z])'
>>> re.findall(pattern, 'ABCDeFGHI') # surrounded by 4 uppercase letters
[]
>>> re.findall(pattern, 'ABCDeFGHi') # surrounded by 4/3 uppercase letters
[]
>>> re.findall(pattern, 'aBCDeFGHi')
['e']
letters = re.findall(r'(?<![A-Z])[A-Z]{3}([a-z])[A-Z]{3}(?![A-Z])', given_string)
answer = ''.join(letters)
import string
def three_upper(s):
return len(s) == 3 and all(x in string.ascii_uppercase for x in s)
def check(s):
for i in range(3, len(s)-3):
if three_upper(s[i-3:i]) and s[i] in string.ascii_lowercase and three_upper(s[i+1:i+4]):
return True
return False
這是一個非正則表達式的解決方案。 string
常量使我們免於混亂,因為ord
可能會引起混亂
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.