Python:每日一题85(答题领鱼币)
题目:编写一个字符串处理函数,1)如果输入的字符串不包含元音字母(aeiouAEIOU)则输出这个字符且首字符小写其他大写;2)如果输入的字符串包含元音字母,则输入这个字符串且首个元音字母后面紧跟的一个辅音字母(非aeiouAEIOU)大写,其他小写,如果是连续元音字母则后推到后面第一个辅音字母;3)如果输入的字符串包含元音字母,按照2)无法找到辅音字母则输出这个字符串且全体大写。
def fun(string):
pass
print(fun('str'))
print(fun('FishC'))
print(fun('boolean'))
print(fun('too'))
sTR
fiShc
booLean
TOO
楼主的解法:
**** Hidden Message ***** 耍宝写法,return嵌套
def fun(string):
string = string.lower()
return string+string.upper() if not set(string)&set('aeiou') else (string.upper() if set(string)<=set('aeiou') else +string.upper()+string for i in range(1, len(string)-1) if string in 'aeiou' and string not in 'aeiou']) def fun(string):
s = string.lower()
flag = 0 #没有元音
y = 0 #存放连续元音最后一个的索引
for (i,j) in zip(s, range(len(s))):
if i in 'aeiou':
if j == len(s)-1:
flag = 1 #元音在最后的位置
elif j+2 <= len(s):
if s in 'aeiou':
continue
else:
y = j # 有连续元音
flag = 2
break
if flag == 0:
s = s + s.upper()
elif flag == 1:
s = s.upper()
elif flag == 2:
s = s[:y+1] + s.upper() + s
return s 3种情况:1、无元音
2.1、元音后有辅音
2.2、元音后无辅音
用re,找元音后第一个辅音
import re
def fun(string):
s = string.lower()
if set(s).intersection(set("aeiou")):
m = re.search(r"(?<=)[^aeiou]", s)
return s[:m.start()]+s.capitalize() if m else s.upper()
else:
return s.capitalize().swapcase()
如果不用 re 的话
我想先找出所有辅音,然后用字母外的东东拼长,
再与原string对比,,元素不同且不为元音的就是目标,取其索引
def fun2(string):
s = string.lower()
cons =
nums = {k for i,j,k in zip(cons+list('。'*len(s)),s,range(len(s))) if i!=j and j not in "aeiou"}
if cons == list(s):
return s.capitalize().swapcase()
else:
return s[:min(nums)]+s.capitalize() if nums else s.upper() def fun(str0):
index0 = -1
index1 = -1
for i in str0:
if(i in ('aeiouAEIOU')):
index0 = str0.index(i)
for j in str0:
if(j not in ('aeiouAEIOU')):
index1 = str0.index(j)
break
break
if(index0 == -1):
return str0.lower()+str0.upper()
elif (index1!=-1):
return str0.lower()+str0.upper()+str0.lower()
else:
return str0.upper()
str0=input('请输入一个字符串:')
print(fun(str0))
运行结果:
>>>
============== RESTART: C:\Users\每日一题\85.py==============
请输入一个字符串:str
sTR
>>>
============== RESTART: C:\Users\每日一题\85.py==============
请输入一个字符串:FishC
fiShc
>>>
============== RESTART: C:\Users\每日一题\85.py==============
请输入一个字符串:boolean
booLean
>>>
============== RESTART: C:\Users\每日一题\85.py ==============
请输入一个字符串:too
TOO def fun(string):
s = string.lower()
flag = 0 #没有元音
y = 0 #存放连续元音最后一个的索引
for (i,j) in zip(s, range(len(s))):
if i in 'aeiou':
if j == len(s)-1:
flag = 1 #元音在最后的位置
elif j+2 <= len(s):
if s in 'aeiou':
continue
else:
y = j # 有连续元音
flag = 2
break
if flag == 0:
s = s + s.upper()
elif flag == 1:
s = s.upper()
elif flag == 2:
s = s[:y+1] + s.upper() + s
return s def fun():
str1 = input('请输入:')
for x in str1:
if x in 'aeiouAEIOU':
if str1.endswith(x):
return str1.upper()
for t in range(str1.find(x)+1, len(str1)):
if str1 not in 'aeiouAEIOU':
return str1[:t].lower() + str1.upper() + str1.lower()
else:
return str1.upper()
return str1.lower() + str1.upper()
print(fun()) def is_vowel(s):
if s.lower() in 'aeiou':
return True
else:
return False
def fun(string):
string = string.lower()
i = 0
while i < len(string):
if is_vowel(string) and not is_vowel(string):
return string[:i+1] + string.upper() + string
else:
i += 1
if i == len(string)-1 and is_vowel(string):
return string.upper()
else:
return string + string.upper() def fun(string):
string = string.lower()
return string+string.upper() if not set(string)&set('aeiou') else (string.upper() if set(string)<=set('aeiou') else +string.upper()+string for i in range(1, len(string)-1) if string in 'aeiou' and string not in 'aeiou']) yuanYin = 'aeiouAEIOU'
fuYin = 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'
def hasYuanyin(string):
for char in string:
if char in yuanYin:
return True
return False
def fun(string):
if not hasYuanyin(string):
s =
s = s.lower()
return ''.join(s)
else:
length = len(string)
s =
startYuanyin = 0# 起始元音位置
continuousYuanyin = 0 # 首个元音后连续元音个数(含首个元音)
startFuyin = 0
for i in range(length):
if s in yuanYin:
startYuanyin = i
break
if startYuanyin == length - 1:
return string.upper()
else:
for i in range(startYuanyin, length):
if s in yuanYin:
continuousYuanyin += 1
else:
break
startFuyin = startYuanyin + continuousYuanyin
if startFuyin == length:
return string.upper()
else:
s = s.upper()
return ''.join(s)
print(fun('str'))
print(fun('FishC'))
print(fun('boolean'))
print(fun('too'))
print(fun('jkjkjkljljlo'))
sTR
fiShc
booLean
TOO
JKJKJKLJLJLO
def func(string):
check = 'aeiouAEIOU'
for i in range(len(string)):
if string in check and i == len(string)-1:
return string.upper()
elif string in check and string not in check:
return string[:i+1].lower()+string.upper()+string.lower()
else:
return string.lower()+string.upper() kankan 本帖最后由 咕咕鸡鸽鸽 于 2019-3-2 13:48 编辑
。 本帖最后由 咕咕鸡鸽鸽 于 2019-3-2 13:48 编辑
。 def fun85(str1):
vowel = ["a","e","i","o","u"]
alive = False
for each in str1:
if each.lower() in vowel:
alive = True
index = str1.index(each)
break
if alive:
while str1 in vowel:
index += 1
if index == len(str1)-1:
return str1.upper()
return str1[:index+1].lower() + str1.upper() + str1.lower()
else:
return str1.lower() + str1.upper()
print(fun85("str"))
print(fun85("FishC"))
print(fun85("boolean"))
print(fun85("too")) import re
def f_85(string: str) -> str:
if not re.match(r'.*.*', string):
return string.lower() + string.upper()
else:
first_vowels = re.search(r'+', string)
consonants = re.split(first_vowels.group(), string)
if consonants == '':
return string.upper()
else:
return consonants.lower() + first_vowels.group().lower() + consonants.capitalize()
print(f_85('aOOl')) import re
def f_85(string: str) -> str:
if not re.match(r'.*.*', string):
return string.lower() + string.upper()
else:
first_vowels = re.search(r'+', string)
consonants = re.split(first_vowels.group(), string)
if consonants == '':
return string.upper()
else:
return consonants.lower() + first_vowels.group().lower() + consonants.capitalize()
print(f_85('aOOl')) def fun(string):
vowel_list = ["a", "e", "i", "o", "u"]
string = string.lower()
last_char = "c"
result = 0
for s in string:
# 查找[元音+辅音]的组合
if (s not in vowel_list) and (last_char in vowel_list):
# 找到后返回辅音的下标
result = string.find(s)
break
else:
last_char = s
if result:
# 存在[元音+辅音]的组合
return string[:result] + string.capitalize()
print(result)
elif last_char in vowel_list:
# 没有[元音+辅音]的组合,但有元音
return string.upper()
else:
# 没有元音
return string.capitalize().swapcase() 1 学习
页:
[1]
2