雪人爱晒大太阳
发表于 2018-3-16 10:44:52
本帖最后由 雪人爱晒大太阳 于 2018-3-16 10:45 编辑
想了好久。。。感觉自己想复杂了。。
def detective_string(x):
count = {'a':0,'e':0,'i':0,'o':0,'u':0}
flag = 1
kind = []
for i in x:
if i in count:
count += 1
for key,value in count.items():
if value > 2 :
flag = 0
break
for key,value in count.items():
if value == 0:
kind.append(key)
for i in kind :
count.pop(i)
k = len(count)
if k > 2 or k <= 1:
flag = 0
return flag,count
def string_change(x,count):
index_list = []
value_list = []
for key,value in count.items():
num = x.find(key)
index_list.append(num)
value_list.append(key)
string_reverse = x[::]
t = string_reverse[::]
string_change = ''
string_reverse = string_reverse.replace(value_list,value_list[-1])
k = 0
for i in string_reverse:
if k == index_list[-1]:
i = value_list
string_change += i
else:
string_change += i
k += 1
return string_change
def main():
x = input("Please input a string:")
(flag,count) = detective_string(x)
if flag == 0:
print("None")
else:
string_reverse = string_change(x,count)
print(string_reverse)
>>> main()
Please input a string:apple
eppla
>>> main()
Please input a string:machin
michan
>>> main()
Please input a string:abca
None
>>> main()
Please input a string:abicod
None
塔利班
发表于 2018-3-16 10:54:11
import re
def alp(x):
p=''
l=re.findall(p,x)
try:
a=re.search(p,x).start()
b=re.search(p,x).start()+a+1
if len(l)==2 and len(set(l))==2:
return x[:a]+x+x+x+x
except:
pass
nononoyes
发表于 2018-3-16 11:30:53
def fun(str):
suba = 'a'
sube = 'e'
subi = 'i'
subo = 'o'
subu = 'u'
sub = 'aeiou'
counta = str.count(suba)
counte = str.count(sube)
counti = str.count(subi)
counto = str.count(subo)
countu = str.count(subu)
if((counta+counte+counti+counto+countu) ==2 and counta<=1 and counte<=1 and counti <=1 and counto<=1 and countu<=1):
index1 = -1
index2 = -1
for i in sub:
if index1 == -1:
index1 = str.find(i)
elif index2 == -1:
index2 = str.find(i)
mi = min(index1,index2)
ma = max(index1,index2)
temp = str
trailer = str if ma + 1 < len(str) else ''
str = str + str + trailer
str = str + temp + str
return str
else:
return None
print(fun('machin'))
dunhee
发表于 2018-3-16 11:31:12
def f(s):
vowel = 'aeiou'
cnt = []
for i in vowel:
cnt.append(s.count(i))
D = dict(zip(vowel, cnt))
if sum(D.values()) == 2 and max(D.values()) == 1:
swap = == 1]
L = list(s)
L)] = swap
L)] = swap
return ''.join(L)
return 'None'
print(f('apple'))
print(f('machin'))
print(f('abca'))
print(f('abicod'))
Chase_Kas
发表于 2018-3-16 12:02:26
def fun(word):
if not word.islower():
return None
vowel = 'aeiou'
serial = []
all_vowel = 0
for each in vowel:
num_vowel = word.count(each)
if num_vowel > 1:
return None
elif num_vowel == 1:
serial.append(each)
all_vowel += 1
else:
continue
if all_vowel != 2:
return None
else:
new = word.replace(serial, '+')
new = new.replace(serial, serial)
new = new.replace('+', serial)
return new
word = input('请输入单词:')
print(fun(word))
天圆突破
发表于 2018-3-16 12:23:12
本帖最后由 天圆突破 于 2018-3-17 14:58 编辑
from functools import reduce
def func(string):
aeiou = set(filter(lambda x: x if x in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] else False, string))
if len(aeiou) != 2:
return None
else:
a, b = aeiou
return reduce(lambda x,y:x+y, map(lambda x: (a if x == b else b) if x in aeiou else x,list(string)))
if __name__ == '__main__':
print(func('apple'))
print(func('machin'))
print(func('abca'))
print(func('abicod'))
强行一行:
from functools import reduce
func = lambda y:None if len(set(filter(lambda x: x if x in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] else False, y))) != 2 else reduce(lambda x,y:x+y, map(lambda x: (list(set(filter(lambda x: x if x in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] else False, y))) if x == list(set(filter(lambda x: x if x in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] else False, y))) else list(set(filter(lambda x: x if x in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] else False, y)))) if x in list(set(filter(lambda x: x if x in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] else False, y))) else x,list(y)))
tsembrace
发表于 2018-3-16 12:39:05
'''
元音字母 有:a,e,i,o,u五个, 写一个函数,交换字符串的元音字符位置。
假设,一个字符串中只有二个不同的元音字符。
二个测试用例:
输入 apple输出 eppla
输入machin输出 michan
不符合要求的字符串,输出None,比如:
输入 abca (两个元音相同) 输出 None
输入 abicod (有三个元音) 输入None
'''
#判断是否符合要求的字符串
#如果符合要求,返回元音字符索引,否则返回False
def yy_counter(strx):
teststr=['a','e','i','o','u']
counter=0
for c in teststr:
tcounter=strx.count(c)
if tcounter>=2:
return False
else:
counter=counter+tcounter
if counter>2:
return False
else:
continue
if counter==2:
lt=[]
for c in teststr:
label=strx.find(c)
if label!=-1:
lt.append(label)
else:
continue
return lt
else:
return False
#最终函数
def replace_yychar(strx):
tl=list(strx)
if not yy_counter(strx):
return 'None'
else:
jf=yy_counter(strx)
tc=tl]
tl]=tl]
tl]=tc
return ''.join(tl)
teststr=replace_yychar('ab cod')
print(teststr)
晓屁屁
发表于 2018-3-16 12:47:16
list1 = ['a','e','i','o','u']
list2 = []
def search_str(instr):
count = 0
count1 = -1
for i in instr:
if i in list1:
count += 1
list2.append(i)
if count == 2:
for x in range(count):
if list2 == list2:
print('None')
break
for x in range(len(instr)):
count1 += 1
if instr not in list1:
print(instr,end='')
else:
print(list2.pop(),end='')
else:
print('None')
temp = input('---:')
search_str(temp)
8306最硬
发表于 2018-3-16 13:12:54
def fun(s):
s, lst =list(s), ['a', 'e', 'i', 'o', 'u']
vowel =
if len( set(vowel) ) == 2:
i, j = s.index( vowel.pop() ), s.index( vowel.pop() )
s, s = s, s
return ''.join(s)
return None
grf1973
发表于 2018-3-16 14:25:33
def getnewstr(mstr):
loc=[]
k=0
for p in mstr:
if p in 'aeiou':
loc.append(k)
if len(loc)>2:
return "None"
k+=1
if len(loc)<2:
return "None"
if mstr]==mstr]:
return "None"
newstr=''
for k in range(0,len(mstr)):
if k==loc:
newstr+=mstr]
elif k==loc:
newstr+=mstr]
else:
newstr+=mstr
return newstr
mstr='case'
print("\n原字符"+mstr+"\n新字符"+getnewstr(mstr))
_小菜鸟
发表于 2018-3-16 16:56:54
#-*- coding:utf-8 -*-
vowel = ['a','e','i','o','u']
def fun(string):
i = 0
lis = []
new = ""
for word in string:
if word in vowel:
lis.append(string.index(word))
i += 1
if i<2 or i >2:
print(new + "none")
else:
temp = string]
temp_1 = string]
for st in string:
if string.index(st) == lis:
print(new + temp_1, end="")
elif string.index(st) == lis:
print(new + temp, end="")
else:
print(new + st, end="")
return new
if __name__ == '__main__':
string = "apple"
fun(string)
print()
string1 = "banana"
fun(string1)
string2 = "juice "
fun(string1)
测试结果
eppla
none
none
FTT100
发表于 2018-3-16 17:08:08
刚开始学,感觉写的比较臃肿
def test1 :
word = input('请输入一个单词:')
list1 = ['a','e','i','o','u']
list2 = list(word)
list3 = []
num = 0
for x in list1 :
nn = list2.count(x)
if nn >=2 or num >2:
print('None')
break
elif nn < 1:
print('nn=%d ,x=%s'%(nn,x))
else :
num=num+nn
list3.append(x)
print(num)
if num == 2:
x=list2.index(list3)
y=list2.index(list3)
al=list2
bl=list2
list2=bl
list2=al
str1=''.join(list2)
print(list3)
print(str1)
else :
print('None')
阿bang
发表于 2018-3-16 17:22:20
本帖最后由 阿bang 于 2018-3-16 18:46 编辑
def change_vowel(word):
count = 0
changeword =list(word)
for i in changeword:
if i in 'aeiou':
count += 1
if count == 1:
first = changeword.index(i)
continue
if i == changeword:
print 'None'
return None
if count == 2 and i != changeword:
second = changeword.index(i)
if count == 2:
temp = changeword
changeword = changeword
changeword = temp
print ''.join(changeword),
else:
print 'None'
return None
word = raw_input('Please input a string:')
change_vowel(word)
初学python,方法贼蠢.
阿bang
发表于 2018-3-16 18:18:15
本帖最后由 阿bang 于 2018-3-16 18:53 编辑
def change_vowel(word):
char_numbers = 0
for i in 'aeiou':
if word.count(i):
if word.count(i) >= 2:
print None
return
char_numbers += word.count(i)
if char_numbers == 1:
firstord = word.index(i)
elif char_numbers == 2:
secondord = word.index(i)
if char_numbers == 2:
list1 = list(word)
temp = word
list1 = list1
list1 = temp
print ''.join(list1)
return
else:
print None
return
word = raw_input('Please input a string:')
change_vowel(word)
判断的过程换了个写法,思路和判断会清晰一点,对于None的返回效率会更高一点。不会无脑暴力执行。
t6am3
发表于 2018-3-16 20:05:45
def yuan(c):
if c == 'a'or c == 'e' or c == 'i' or c == 'o' or c == 'u':
return c
else:
return 0
str1 = input()
set1 = set()
list1 = []
for eachch in str1:
if yuan(eachch):
list1.append(str1.index(eachch))
set1.add(eachch)
if len(set1) == 2:
str1 = str1[:list1] + str1]+str1+1:list1]+str1]+str1+1:]
print (str1)
else:
print ('None')
冬雪雪冬
发表于 2018-3-16 20:57:35
评分截至标记。
fkmol
发表于 2018-3-16 21:37:05
def ys(rs):
sm = 0
for i in list(rs):
if i in list(yuany):
sm += 1
return sm
def mnn(sr):
lieb = list(sr)
for n in list (yuany):
if n in sr:
m=lieb.index(n)
for n in list (yuany):
if n in sr:
g=lieb.index(n)
break
lieb,lieb=lieb,lieb
for i in lieb:
print (i,end='')
sr = input ()
yuany = 'aeio'
ms = ys(sr)
if ms == 2:
mnn(sr)
else:
print ('None')
Agoni
发表于 2018-3-16 22:08:44
本帖最后由 Agoni 于 2018-3-16 22:10 编辑
def swap(letter):
vowel = "aeiou"
pos_1 = 0 # 位置1
pos_2 = 0 # 位置2
temp = []
num = 0
for each in letter:
if each in vowel:
num += 1
temp.append(each)
# print(num)
if temp == temp or num > 2:
return None
# 查找元音字母的位置
pos_1 = letter.find(temp)
pos_2 = letter.find(temp)
# print(temp,pos_1,pos_2)
return letter[:pos_1] + temp + letter + temp + letter
print(swap('apple'),swap('machin'))
print(swap('abca'))
print(swap("abicod"))
wyf345491656
发表于 2018-3-16 22:37:59
str1 = input('请输入单词:')
vowel = 'aeiou'
i = 0
j = 0
k = 0
ch1 = []
ch2 = []
temp1 = []
temp2 = []
for each in str1:
if each in vowel:
ch1.append(each)
i += 1
if i != 2:
print('None')
elif ch1 == ch1:
print('None')
else:
ch2 = list(str1)
for each in str1:
if each in vowel:
temp1.append(ch2)
temp2.append(j)
k += 1
j += 1
ch2] = temp1
ch2] = temp1
str2 = ''.join(ch2)
print(str2)
°蓝鲤歌蓝
发表于 2018-3-16 22:53:31
def fun(eng):
List = ['a', 'e', 'i', 'o', 'u']
leng = list(eng)
count = 0
k = []
for i, each in enumerate(eng):
if each in List:
count += 1
k.append()
if count > 2:
return None
if count == 2:
leng], leng] = k, k
result = ''.join(leng)
if result != eng:
return result
print(fun('apple'))
print(fun('appile'))
print(fun('aapple'))
print(fun('abca'))
没赶上时间,不过还是放这里。