Python:每日一题 164
本帖最后由 冬雪雪冬 于 2018-3-16 20:55 编辑我们的玩法做了一下改变:
1. 楼主不再提供答案。
2. 请大家先独立思考”,再参考其他鱼油的解答,这样才有助于自己编程水平的提高。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。
题目:
元音字母 有:a,e,i,o,u五个, 写一个函数,交换字符串的元音字符位置。
假设,一个字符串中只有二个不同的元音字符。
二个测试用例:
输入 apple输出 eppla
输入machin输出 michan
不符合要求的字符串,输出None,比如:
输入 abca (两个元音相同) 输出 None
输入 abicod (有三个元音) 输入None
感谢 @shigure_takimi 提供题目,也希望大家有适宜的题目提供给我们,共同把每日一题这个项目做好。请 @shigure_takimi 多回一帖,给予出题奖励。
要求:
题目难易适度,以python语言的运用为主,算法为辅。
对于提供优质题目的鱼油给予鱼币奖励。
可以发私信给我或直接发帖。 本帖最后由 sukiwhip 于 2018-3-16 10:24 编辑
list_a = ['a','e','i','o','u']
list_b = list(input('输入一个字符串:'))
count_1 = []
for i in list_a:
if list_b.count(i) == 1:
count_1.append(i)
t = True
else: #字符串有多个同原音或完全没有的返回 None
print('None')
t = False
break
if t:
if len(count_1) == 2: #只执行包含两个原音的字符串
a = count_1
b = count_1
position_a = list_b.index(count_1)
position_b = list_b.index(count_1)
del list_b
list_b.insert(position_a,b)
del list_b
list_b.insert(position_b,a)
word = ''.join(list_b)
print('结果是:',word)
else:
print('None')
list1 = ['a','e','i','o','u']
str1 = input('输入一个只有2个不同的元音字符:')
y = []
wz = []
num1 = 0
num2 = 0
for i in str1:
if i in list1:
num1 += 1
if i in y:
pass
else:
y.append(i)
num2 += 1
if num1 == num2 and num1 == 2:
for i in str1:
if i in y:
wz.append(str1.find(i))
str2 = str1[:wz] + str1] + str1 + 1:]
str2 = str2[:wz] + str1] + str2 + 1:]
print(str2)
else:
print('None')
# 不考虑大小写,默认都是小写。
def func(string):
yuanyin =
if len(yuanyin)==2 and len(set(yuanyin)) == 2:
i = string.index(yuanyin)
j = string.index(yuanyin)
string = list(string)
string, string = string, string
return ''.join(string)
else:
return None
a = 'apple'
b = 'machin'
c = 'appla'
d = 'abicod'
e = 'abbbbbbb'
f = 'bdfssdfs'
g = 'abocda'
print(func(a))#-- > eppla
print(func(b))#-- > michan
print(func(c))#-- > None
print(func(d))#-- > None
print(func(e))#-- > None
print(func(f))#-- > None
print(func(g))#-- > None
本帖最后由 shigure_takimi 于 2018-3-15 22:46 编辑
多回一贴,领奖励咯!
之前还写了一个比较墨迹的,粘贴于此。
def func(string):
yuanyin = 'aiueo'
container = []
firstYuanyin =
secondYuanyin =
for i in string:
if i in yuanyin:
if firstYuanyin == False:
firstYuanyin, firstYuanyin = string.index(i), True
container.append(i)
elif secondYuanyin == False:
if i in container:
return None, '重复元音'
else:
secondYuanyin, secondYuanyin = string.index(i), True
else:
return None, '多于两个元音'
if firstYuanyin == False:
return None, '没有元音'
elif secondYuanyin == False:
return None, '只有一个元音'
newSring = list(string)
newSring], newSring] = newSring], newSring]
return ''.join(newSring)
a = 'apple'
b = 'machin'
c = 'appla'
d = 'abicod'
e = 'abbbbbbb'
f = 'bdfssdfs'
g = 'abocda'
print(func(a))#-- > eppla
print(func(b))#-- > michan
print(func(c))#-- > (None, '重复元音')
print(func(d))#-- > (None, '多于两个元音')
print(func(e))#-- > (None, '只有一个元音')
print(func(f))#-- > (None, '没有元音')
print(func(g))#-- > (None, '多于两个元音') def check_vowel(str1):
standard = 'aeiou'
flag = {}
index_number = []
for i in range(len(str1)):
if str1 in standard:
if str1 in flag:
flag] += 1
else:
flag] = 0
index_number.append(str1)
if len(flag) != 2:
return 0
for each in flag.values():
if each == 1:
return 0
result = ''
cnt = 0
for each in str1:
if each not in standard:
result = result + each
else:
cnt = cnt + 1
result = result + index_number
return result
if __name__ == '__main__':
str1 = input('请输入字符串:')
if type(check_vowel(str1)) == str:
print(check_vowel(str1))
else:
print('None')
#################
请输入字符串:aeigh
None
***Repl Closed***
#################
请输入字符串:aadfg
None
***Repl Closed***
#################
请输入字符串:avbnm
None
***Repl Closed***
#################
请输入字符串:apple
eppla
***Repl Closed***
#################
请输入字符串:machin
michan
***Repl Closed***
vowel = 'aeiou'
def changev(l):
vlist = []#字符串中元音字母列表,用于对比出现元音字母是否相等
klist = []#字符串中元音字母索引列表,用于后面字母对换
vsum = 0#记录字符串中元音字母数量
for each in range(len(l)):
if l in vowel:
vsum += 1
klist.append(each)
vlist.append(l)
if vsum != 2:
print('None')
elif vlist == vlist:
print('None')
else:
llist = list(l)
temp = llist]
llist] = llist]
llist] = temp
result = ''.join(llist)
print(result)
str1 = input('输入字符串:')
changev(str1) def new_del(s):
k = []
for i in s:
if i not in k:
k.append(i)
return k
while True:
word = input("请输入你想要输入的字符:")
L_1 = list(word)
proving = ['a','e','i','o','u']
num = 0 #元音字母出现的次数
L_2 = []
for i in new_del(L_1):
if i in proving:
num += 1
if num != 2:#判断当输入字符不满足条件时
print('None')
#下面运行的代码是当输入字符满足条件时
else:
for i in L_1:
if i in proving:
L_2.append(L_1.index(i))
#变换
a = L_1]
L_1] = L_1]
L_1] = a
print("".join(L_1))
result = input("是否还想验证:请输入'Y'or'N'")
if result == 'N':
break ss=input()
s1=['a','e','i','o','u']
s2=list(ss)
zs=0
jl=[]
for i in range(len(s2)) :
if s2 in s1:
jl+=
if len(jl)==2 :
if jl==jl:
print('None')
else :
i=s2]
s2]=s2]
s2]=i
ss=''
for i in s2 :
ss+=str(i)
print(ss)
else :
print('None') list1 = list(input('请输入一个字符串:'))
list2 = ['a','e','i','o','u']
n=0
list3 = []
for i in list1:
if i in list2:
list3.append(list1.index(i))
n = n + 1
def fun(n):
if n==2:
list1],list1]=list1],list1]
return (''.join(list1))
else:
return None
print(fun(n)) while 1:
str1=input()
str1.replace('a','b')
x= 'auieo'
list1=[]
count=0
for i in range(len(str1)):
for r in range(5):
if str1==x:
count+=1
list1.append(i)
str2=list(str1)
if count==2 :
a=str2]
b=str2]
if a!=b:
str2]=b
str2]=a
str3=''.join(str2)#将列表转化为字符串
print(str3)
else:
print('none')
else:
print('none')
显示:
ersde
none
sdee
none
sdefa
sdafe
sdfeae
none 刚刚穿错了,加了个函数
def position():
str1=input()
x= 'auieo'
list1=[]
count=0
for i in range(len(str1)):
for r in range(5):
if str1==x:
count+=1
list1.append(i)
str2=list(str1)
if count==2 :
a=str2]
b=str2]
if a!=b:
str2]=b
str2]=a
str3=''.join(str2)#将列表转化为字符串
print(str3)
else:
print('none')
else:
print('none') def change(old):
vowel = 'aeiou'
#list1存储每个元音的个数
list1 = * 5
for i in range(5):
list1 = old.count(vowel)
if list1.count(1)!= 2:
return None
else:
#list2存储两个元音在原字符串里的索引值
list2 = []
for i in range(5):
if old.find(vowel)!= -1:
list2.append(old.find(vowel))
list2.sort()
x = list2
y = list2
new = old[:x] + old + old + old + old
return new
>>> change('apple')
'eppla'
>>> change('machin')
'michan'
>>> change('abca')
>>> change('abicod') def vowels(n):
list1 = []
string = ''
for i in n:
if i in ['a', 'e', 'i', 'o', 'u']:
list1.append(i)
if len(set(list1)) == 2 and len(list1) == 2:
list1.reverse()
for i in n:
if i not in ['a', 'e', 'i', 'o', 'u']:
string += i
else:
for each in list1:
if each not in string:
string += each
break
return string
else:
return None
temp = input('请输入有两个不同元音的单词:')
X = vowels(temp)
print(X) list1 = ['a','e','i','o','u']
list2 = []
def swap(str1):
"""交换字符串中的元音字母位置"""
str1 = list(str1)
for i in str1:
if i in list1:
#保存元音字母在给定字符串中的下标
sub = str1.index(i)
#储存在list2中
list2.append(sub)
#根据下标个数判断元音个数
if len(list2) == 2:
#判断两个元音字母是否相等
if str1] != str1]:
#交换元音字母位置
str1],str1] = str1],str1]
#转换成字符串
print(''.join(str1))
else:
print("None")
else:
print("None")
str1 = input("输入进行测试的字符串:")
swap(str1) while True:
temp = input('输入字符串:')
mm = str(temp)
n = 0
x = ['a', 'e', 'i', 'o', 'u']
new_mm = ''
str_a = []
p = []
ss = ''
for i in range(len(mm)):
str_a.append(mm)
if mm in x:
n += 1
p.append(i)
else:
pass
if n == 2:
ss = str_a]
str_a] = str_a]
str_a] = ss
for each in str_a:
new_mm += each
print(new_mm)
else:
print('None')
结果:
输入字符串:edit
idet
输入字符串:history
hostiry
输入字符串:hisiiii
None
输入字符串:hisdf
None
输入字符串:
def check_a(alpha):
a = ''
b = ''
output = ''
count = 0
for i in alpha:
if i in check_str:
count +=1
if count < 3:
if not len(a):
a = i
else:
b = i
#print(a,b)
if a == b:
return None #a和b一样return None
else:
return None #元音超过3个return None
if len(a) == len(b) == 1:
beta = list(alpha)
x = beta.index(a)
y = beta.index(b)
beta, beta = b, a
for j in beta:
output += j
return output
else:
return None #其他不符合情况return None
check_str = 'aeiou'
alpha = input('请输入字符串:')
print(check_a(alpha)) 本帖最后由 graceasyi 于 2018-3-16 11:36 编辑
时间有限,先写了一个,有点复杂,有空再优化。
str1 = input("请输入字符串:")
vowel=['a', 'e', 'i', 'o', 'u']
num = []
for s in vowel:
num.append(str1.count(s))
if num.count(1)==2:
v = []
for i, c in enumerate(str1):
if c in vowel:
v.append(i)
v.append(c)
tmp = list(str1)
tmp], tmp] = v, v
print(''.join(tmp))
else:
print("不符合条件") def aeiou():
#定义空列表
list1 = []
#让用户输入单词
a = input('Please input a word:')
#定义元音字符串
b = 'aeiou'
#判断输入单词中元音字母个数
for i in a:
if i in b:
list1.append(i)
else:
pass
#当输入单词仅有两个元音字母时互换两个元音字母的位置
if len(list1) == 2:
s1 = list(a)
#返回输入单词元音字母的索引值
c = a.index(list1)
d = a.index(list1)
#交换元音字母的位置
e = s1
f = s1
s1 = f
s1 = e
#赋值新的字符串并打印
new =''.join(s1)
print('New word is'+ ' ' + new)
#不为两个元音字母时打印None
else:
print('None') def exchange(st):
count = 0
vowel = ['a', 'e', 'i', 'o', 'u']
collector = []
lis =
for j in vowel:
if j in lis:
count += 1
collector.append(j)
if count >= 3 or count < 2 or len(set(collector)) == 1:
return 'None'
else:
i = lis.index(collector)
j = lis.index(collector)
lis, lis = lis, lis
st = ''.join()
return st
str1 = 'apple'
str2 = 'machin'
str3 = 'abca'
str4 = 'abicod'
print(exchange(str1))
print(exchange(str2))
print(exchange(str3))
print(exchange(str4))