鱼C论坛

 找回密码
 立即注册
楼主: 冬雪雪冬

[技术交流] Python:每日一题 164

[复制链接]
发表于 2018-3-16 10:44:52 | 显示全部楼层
本帖最后由 雪人爱晒大太阳 于 2018-3-16 10:45 编辑

想了好久。。。感觉自己想复杂了。。
  1. def detective_string(x):
  2.     count = {'a':0,'e':0,'i':0,'o':0,'u':0}
  3.     flag = 1
  4.     kind = []
  5.     for i in x:
  6.         if i in count:
  7.             count[i] += 1
  8.     for key,value in count.items():
  9.         if value > 2 :
  10.             flag = 0
  11.             break
  12.     for key,value in count.items():
  13.         if value == 0:
  14.             kind.append(key)
  15.     for i in kind :
  16.         count.pop(i)
  17.     k = len(count)
  18.     if k > 2 or k <= 1:
  19.         flag = 0
  20.     return flag,count

  21. def string_change(x,count):
  22.     index_list = []
  23.     value_list = []
  24.     for key,value in count.items():
  25.         num = x.find(key)
  26.         index_list.append(num)
  27.         value_list.append(key)
  28.     string_reverse = x[::]
  29.     t = string_reverse[::]
  30.     string_change = ''
  31.     string_reverse = string_reverse.replace(value_list[0],value_list[-1])
  32.     k = 0
  33.     for i in string_reverse:
  34.         if k == index_list[-1]:
  35.             i = value_list[0]
  36.             string_change += i
  37.         else:
  38.             string_change += i
  39.         k += 1
  40.    
  41.     return string_change


  42. def main():
  43.     x = input("Please input a string:")
  44.     (flag,count) = detective_string(x)
  45.     if flag == 0:
  46.         print("None")
  47.     else:
  48.         string_reverse = string_change(x,count)
  49.         print(string_reverse)

  50. >>> main()
  51. Please input a string:apple
  52. eppla
  53. >>> main()
  54. Please input a string:machin
  55. michan
  56. >>> main()
  57. Please input a string:abca
  58. None
  59. >>> main()
  60. Please input a string:abicod
  61. None
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 10:54:11 | 显示全部楼层
  1. import re
  2. def alp(x):
  3.     p='[aeiou]'
  4.     l=re.findall(p,x)
  5.     try:
  6.         a=re.search(p,x).start()
  7.         b=re.search(p,x[a+1:]).start()+a+1
  8.         if len(l)==2 and len(set(l))==2:
  9.             return x[:a]+x[b]+x[a+1:b]+x[a]+x[b+1:]
  10.     except:
  11.         pass
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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[ma]
        trailer = str[ma+1:] if ma + 1 < len(str) else ''
        str = str[0:ma] + str[mi] + trailer
        str = str[0:mi] + temp + str[mi+ 1:]
        return str
    else:
        return None

print(fun('machin'))

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 11:31:12 | 显示全部楼层
  1. def f(s):
  2.     vowel = 'aeiou'
  3.     cnt = []
  4.     for i in vowel:
  5.         cnt.append(s.count(i))
  6.     D = dict(zip(vowel, cnt))
  7.     if sum(D.values()) == 2 and max(D.values()) == 1:
  8.         swap = [v for v in D.keys() if D[v] == 1]
  9.         L = list(s)
  10.         L[s.index(swap[0])] = swap[1]
  11.         L[s.index(swap[1])] = swap[0]
  12.         return ''.join(L)
  13.     return 'None'

  14. print(f('apple'))
  15. print(f('machin'))
  16. print(f('abca'))
  17. print(f('abicod'))
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 12:02:26 | 显示全部楼层
  1. def fun(word):
  2.    
  3.     if not word.islower():
  4.         return None

  5.     vowel = 'aeiou'
  6.     serial = []
  7.     all_vowel = 0
  8.     for each in vowel:
  9.         num_vowel = word.count(each)
  10.         if num_vowel > 1:
  11.             return None
  12.         elif num_vowel == 1:
  13.             serial.append(each)
  14.             all_vowel += 1
  15.         else:
  16.             continue

  17.     if all_vowel != 2:
  18.         return None
  19.     else:
  20.         new = word.replace(serial[0], '+')
  21.         new = new.replace(serial[1], serial[0])
  22.         new = new.replace('+', serial[1])
  23.         return new

  24. word = input('请输入单词:')
  25. print(fun(word))
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 12:23:12 | 显示全部楼层
本帖最后由 天圆突破 于 2018-3-17 14:58 编辑
  1. from functools import reduce
  2. def func(string):
  3.     aeiou = set(filter(lambda x: x if x in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] else False, string))
  4.     if len(aeiou) != 2:
  5.         return None
  6.     else:
  7.         a, b = aeiou
  8.         return reduce(lambda x,y:x+y, map(lambda x: (a if x == b else b) if x in aeiou else x,list(string)))
复制代码


  1. if __name__ == '__main__':
  2.     print(func('apple'))
  3.     print(func('machin'))
  4.     print(func('abca'))
  5.     print(func('abicod'))
复制代码



强行一行:
  1. from functools import reduce
  2. 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)))[0] if x == list(set(filter(lambda x: x if x in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] else False, y)))[1] else list(set(filter(lambda x: x if x in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] else False, y)))[1]) 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)))
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 12:39:05 | 显示全部楼层
  1. '''
  2. 元音字母 有:a,e,i,o,u五个, 写一个函数,交换字符串的元音字符位置。
  3. 假设,一个字符串中只有二个不同的元音字符。
  4. 二个测试用例:
  5. 输入 apple  输出 eppla
  6. 输入machin  输出 michan
  7. 不符合要求的字符串,输出None,比如:
  8. 输入 abca (两个元音相同) 输出 None
  9. 输入 abicod (有三个元音) 输入None
  10. '''

  11. #判断是否符合要求的字符串
  12. #如果符合要求,返回元音字符索引,否则返回False
  13. def yy_counter(strx):
  14.     teststr=['a','e','i','o','u']
  15.     counter=0
  16.     for c in teststr:
  17.         tcounter=strx.count(c)
  18.         if tcounter>=2:
  19.             return False
  20.         else:
  21.             counter=counter+tcounter
  22.             if counter>2:
  23.                 return False
  24.             else:
  25.                 continue
  26.     if counter==2:
  27.         lt=[]
  28.         for c in teststr:
  29.             label=strx.find(c)
  30.             if label!=-1:
  31.                 lt.append(label)
  32.             else:
  33.                 continue               
  34.         return lt
  35.     else:
  36.         return False

  37. #最终函数
  38. def replace_yychar(strx):
  39.     tl=list(strx)
  40.     if not yy_counter(strx):
  41.         return 'None'
  42.     else:
  43.         jf=yy_counter(strx)
  44.         tc=tl[jf[1]]
  45.         tl[jf[1]]=tl[jf[0]]
  46.         tl[jf[0]]=tc
  47.         return ''.join(tl)
  48. teststr=replace_yychar('ab    cod')
  49. print(teststr)
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 12:47:16 | 显示全部楼层
  1. list1 = ['a','e','i','o','u']
  2. list2 = []
  3. def search_str(instr):
  4.     count = 0
  5.     count1 = -1
  6.     for i in instr:
  7.         if i in list1:
  8.             count += 1
  9.             list2.append(i)
  10.     if count == 2:
  11.         for x in range(count):
  12.             if list2[x] == list2[x+1]:
  13.                 print('None')
  14.             break
  15.         for x in range(len(instr)):
  16.             count1 += 1
  17.             if instr[count1] not in list1:
  18.                 print(instr[count1],end='')
  19.             else:
  20.                 print(list2.pop(),end='')
  21.     else:
  22.         print('None')

  23. temp = input('---:')
  24. search_str(temp)
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 13:12:54 | 显示全部楼层
  1. def fun(s):
  2.     s, lst =  list(s), ['a', 'e', 'i', 'o', 'u']
  3.     vowel = [i for i in s for j in lst if i == j]

  4.     if len( set(vowel) ) == 2:
  5.         i, j = s.index( vowel.pop() ), s.index( vowel.pop() )
  6.         s[i], s[j] = s[j], s[i]
  7.         return ''.join(s)
  8.     return None
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 14:25:33 | 显示全部楼层
  1. def getnewstr(mstr):
  2.   loc=[]
  3.   k=0
  4.   for p in mstr:
  5.     if p in 'aeiou':
  6.       loc.append(k)
  7.       if len(loc)>2:
  8.         return "None"
  9.     k+=1
  10.   if len(loc)<2:
  11.     return "None"
  12.   if mstr[loc[1]]==mstr[loc[0]]:
  13.     return "None"
  14.   newstr=''
  15.   for k in range(0,len(mstr)):
  16.     if k==loc[0]:
  17.       newstr+=mstr[loc[1]]
  18.     elif k==loc[1]:
  19.       newstr+=mstr[loc[0]]
  20.     else:
  21.       newstr+=mstr[k]
  22.   return newstr

  23. mstr='case'
  24. print("\n原字符"+mstr+"\n新字符"+getnewstr(mstr))
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 16:56:54 | 显示全部楼层
  1. #-*- coding:utf-8 -*-
  2. vowel = ['a','e','i','o','u']
  3. def fun(string):
  4.     i = 0
  5.     lis = []
  6.     new = ""
  7.     for word in string:
  8.         if word in vowel:
  9.             lis.append(string.index(word))
  10.             i += 1
  11.     if i<2 or i >2:
  12.         print(new + "none")
  13.     else:
  14.         temp = string[lis[0]]
  15.         temp_1 = string[lis[1]]
  16.         for st in string:      
  17.             if string.index(st) == lis[0]:
  18.                 print(new + temp_1, end="")
  19.             elif string.index(st) == lis[1]:
  20.                 print(new + temp, end="")
  21.             else:
  22.                 print(new + st, end="")
  23.     return new      
  24. if __name__ == '__main__':
  25.     string = "apple"
  26.     fun(string)
  27.     print()
  28.     string1 = "banana"
  29.     fun(string1)
  30.     string2 = "juice "
  31.     fun(string1)
复制代码


测试结果
eppla
none
none

点评

abca出错  发表于 2018-3-17 20:57

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
冬雪雪冬 + 1 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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[0])
        y=list2.index(list3[1])
        al=list2[x]
        bl=list2[y]
        list2[x]=bl
        list2[y]=al
        str1=''.join(list2)
        print(list3)
        print(str1)
    else :
        print('None')

点评

定义函数没有括号。  发表于 2018-3-17 21:00

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
冬雪雪冬 + 1 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 17:22:20 | 显示全部楼层
本帖最后由 阿bang 于 2018-3-16 18:46 编辑
  1. def change_vowel(word):
  2.     count = 0
  3.     changeword =list(word)
  4.     for i in changeword:
  5.         if i in 'aeiou':
  6.             count += 1
  7.             if count == 1:
  8.                 first = changeword.index(i)
  9.                 continue
  10.             if i == changeword[first]:
  11.                 print 'None'
  12.                 return None
  13.             if count == 2 and i != changeword[first]:
  14.                 second = changeword.index(i)

  15.     if count == 2:
  16.         temp = changeword[first]
  17.         changeword[first] = changeword[second]
  18.         changeword[second] = temp
  19.         print ''.join(changeword),
  20.     else:
  21.         print 'None'
  22.         return None


  23. word = raw_input('Please input a string:')
  24. change_vowel(word)
复制代码


初学python,方法贼蠢.

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 18:18:15 | 显示全部楼层
本帖最后由 阿bang 于 2018-3-16 18:53 编辑
  1. def change_vowel(word):
  2.     char_numbers = 0
  3.     for i in 'aeiou':
  4.         if word.count(i):
  5.             if word.count(i) >= 2:
  6.                 print None
  7.                 return
  8.             char_numbers += word.count(i)
  9.             if char_numbers == 1:
  10.                 firstord = word.index(i)
  11.             elif char_numbers == 2:
  12.                 secondord = word.index(i)
  13.     if char_numbers == 2:
  14.         list1 = list(word)
  15.         temp = word[firstord]
  16.         list1[firstord] = list1[secondord]
  17.         list1[secondord] = temp
  18.         print ''.join(list1)
  19.         return
  20.     else:
  21.         print None
  22.         return

  23. word = raw_input('Please input a string:')
  24. change_vowel(word)
复制代码


判断的过程换了个写法,思路和判断会清晰一点,对于None的返回效率会更高一点。不会无脑暴力执行。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 20:05:45 | 显示全部楼层
  1. def yuan(c):
  2.     if c == 'a'or c == 'e' or c == 'i' or c == 'o' or c == 'u':
  3.         return c
  4.     else:
  5.         return 0
  6. str1 = input()
  7. set1 = set()
  8. list1 = []
  9. for eachch in str1:
  10.     if yuan(eachch):
  11.         list1.append(str1.index(eachch))
  12.         set1.add(eachch)
  13. if len(set1) == 2:
  14.             str1 = str1[:list1[0]] + str1[list1[1]]+str1[list1[0]+1:list1[1]]+str1[list1[0]]+str1[list1[1]+1:]
  15.             print (str1)
  16. else:
  17.     print ('None')
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-16 20:57:35 | 显示全部楼层
评分截至标记。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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[m],lieb[g]=lieb[g],lieb[m]
    for i in lieb:
        print (i,end='')
        
   
   
sr = input ()
yuany = 'aeio'
ms = ys(sr)
if ms == 2:
    mnn(sr)
else:
    print ('None')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 22:08:44 | 显示全部楼层
本帖最后由 Agoni 于 2018-3-16 22:10 编辑

  1. def swap(letter):
  2.     vowel = "aeiou"
  3.     pos_1 = 0   # 位置1
  4.     pos_2 = 0   # 位置2
  5.     temp = []
  6.     num = 0
  7.     for each in letter:
  8.         if each in vowel:
  9.             num += 1
  10.             temp.append(each)
  11. #   print(num)
  12.     if temp[0] == temp[1] or num > 2:
  13.         return None
  14.     # 查找元音字母的位置
  15.     pos_1 = letter.find(temp[0])
  16.     pos_2 = letter.find(temp[1])
  17. #    print(temp,pos_1,pos_2)
  18.    
  19.     return letter[:pos_1] + temp[1] + letter[pos_1+1:pos_2] + temp[0] + letter[pos_2:]

  20. print(swap('apple'),swap('machin'))
  21. print(swap('abca'))
  22. print(swap("abicod"))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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[0] == ch1[1]:
        print('None')
else:
    ch2 = list(str1)
    for each in str1:
        if each in vowel:
            temp1.append(ch2[j])
            temp2.append(j)
            k += 1
        j += 1
    ch2[temp2[0]] = temp1[1]
    ch2[temp2[1]] = temp1[0]
str2 = ''.join(ch2)
print(str2)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-16 22:53:31 | 显示全部楼层

  1. def fun(eng):
  2.     List = ['a', 'e', 'i', 'o', 'u']
  3.     leng = list(eng)
  4.     count = 0
  5.     k = []
  6.     for i, each in enumerate(eng):
  7.         if each in List:
  8.             count += 1
  9.             k.append([i, each])
  10.         if count > 2:
  11.             return None

  12.     if count == 2:
  13.         leng[k[0][0]], leng[k[1][0]] = k[1][1], k[0][1]
  14.         result = ''.join(leng)
  15.         if result != eng:
  16.             return result


  17. print(fun('apple'))
  18. print(fun('appile'))
  19. print(fun('aapple'))
  20. print(fun('abca'))
复制代码

没赶上时间,不过还是放这里。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 03:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表