鱼C论坛

 找回密码
 立即注册
123
返回列表 发新帖
楼主: zltzlt

[已解决]Python:每日一题 357

[复制链接]
 楼主| 发表于 2020-3-27 17:49:10 | 显示全部楼层
黎影 发表于 2020-3-23 13:08
def huiwen(list1):
    list2 = []
    for i in range(len(list1)):

效率有待提升
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-27 17:49:50 | 显示全部楼层
旅途Z 发表于 2020-3-23 13:08
def is_palindromic(str1, str2):
    determine = str1 + str2
    if determine == determine[::-1]: ...

效率有待提升
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-27 17:50:20 | 显示全部楼层
mdphd 发表于 2020-3-23 13:10
先发一个暴力法,再看看能不能优化

效率有待提升
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-27 17:51:13 | 显示全部楼层
whosyourdaddy 发表于 2020-3-23 21:06
def func357(m):
    result = []
    n = len(m)

效率有待提升
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-27 17:52:46 | 显示全部楼层
阴阳神万物主 发表于 2020-3-24 02:58
难度评级:简单
要素分析:枚举 字符串
感受:由于输入数据无规律,双 for 是一种必然(不然就是递归), ...

效率有待提升
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-27 17:54:14 | 显示全部楼层

效率有待提升
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-27 18:46:22 | 显示全部楼层
已改
  1. def fun357(lst):
  2.     if not len(lst):return 0
  3.     result=[]
  4.     for i in range(len(lst)):
  5.         for j in range(len(lst)):
  6.             if i!=j and (lst[j][::-1].startswith(lst[i]) or not lst[j]):
  7.                 result.append([i,j])
  8.     return result
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-27 18:49:35 | 显示全部楼层

解答错误

输入:
  1. ['a', 'abc', 'aba', '']
复制代码

输出:
  1. [[0, 2], [0, 3], [1, 3], [2, 3], [3, 0], [3, 1], [3, 2]]
复制代码

预期结果:
  1. [[0, 3], [3, 0], [2, 3], [3, 2]]
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-27 19:21:16 | 显示全部楼层
换了种思路
  1. def fun357(lst):
  2.     if not len(lst):return 0
  3.     result=[]
  4.     for i in range(len(lst)):
  5.         for j in range(i+1,len(lst)):
  6.             a=lst[i]+lst[j]
  7.             b=lst[j]+lst[i]
  8.             if not len(a)//2 or a[:len(a)//2]==a[-1:int(-len(a)/2)-1:-1]:
  9.                 result.append([i,j])
  10.             if not len(b)//2 or b[:len(b)//2]==b[-1:int(-len(b)/2)-1:-1]:
  11.                 result.append([j,i])
  12.     return result
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-27 21:17:01 | 显示全部楼层
本帖最后由 入土 于 2020-3-27 21:18 编辑

顺序不对不知道行不行


  1. def x(word):
  2.     list = []
  3.     list1 = []
  4.     list2 = []
  5.     len_word = len(word)
  6.     for i in range(len_word):
  7.         for d in range(len_word):
  8.             if i != d:
  9.                 new_word = word[i]+word[d]
  10.                 list.append(new_word)
  11.    
  12.     for each in list:
  13.         leng = len(each)
  14.         n = 0
  15.         while n < leng / 2:
  16.             if each[n] != each[leng-1-n]:
  17.                 break
  18.             else:
  19.                 n += 1
  20.         if n >= leng/2:
  21.             print(each)
  22.             list1.append(each)
  23.    
  24.     for i in range(len_word):
  25.         for d in range(len_word):
  26.             if i != d:
  27.                 new_word = word[i]+word[d]
  28.                 if new_word in list1:
  29.                     p = [i,d]
  30.                     list2.append(p)
  31.     print(list2)
  32.    


  33. m = ["abcd", "dcba", "lls", "s", "sssll"]
  34. x(m)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-28 13:22:06 | 显示全部楼层

输入大数据超时
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-29 12:05:56 | 显示全部楼层
def huiwen(word):
    a=word
    b=[]
    for i in a:
        b.append(i)
    c=b.copy()
    b.reverse()
    if b==c:
        return True

def findindex(word):
    a=''
    b=word
    l=len(b)
    c=[]
    d=[]
    for i in range(l):
        for j in range(l):
            a=''
            a=b[i]+b[j]
            if huiwen(a) and i!=j:
                c.append([i,j])
                d.append(a)
    print(c)
    print(d)
word=["abcd", "dcba", "lls", "s", "sssll"]
findindex(word)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-29 15:07:55 | 显示全部楼层

我会继续努力的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-1 17:35:11 | 显示全部楼层
本帖最后由 十月故里 于 2020-4-1 17:42 编辑
  1. [code]def huiwen(i,j):
  2.     a=list(''.join([i,j]))
  3.     b=a[:]
  4.     a.reverse()
  5.     if a==b:
  6.         return [i,j]

  7. def main(list1):
  8.     result=[]
  9.     result2=[]
  10.     for i in list1:
  11.         for j in list1:
  12.             if i!=j:
  13.                 res=huiwen(i,j)
  14.                 if res:
  15.                     result.append((list1.index(res[0]),list1.index(res[1])))
  16.                     result2.append(''.join(res))
  17.     return result,result2

  18. if __name__=='__main__':
  19.     list1=["abcd","dcba","lls","s","sssll"]
  20.     a=main(list1)
  21.     print('可以拼出回文串的是',a[0])
  22.     print('拼成的回文串有',a[1])
复制代码
[/code]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-19 11:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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