475123763 发表于 2018-7-28 23:03:31

jerryxjr1220 发表于 2017-4-27 13:39
def get_longest(lst,k):
nlst =
xlst =

xlst = ) for i in range(len(lst)-k)]
maxi = xlst.index(max(xlst))
大哥能把这个按照初学者能看懂的方式写下吗

475123763 发表于 2018-7-28 23:21:08

str = "Office workers who would normally step into a pub or gym to cope with the"
lst = str.split(" ")
nlst =
t=max(nlst)
d=nlst.index(t)
c = lst
del lst
nlst =
t=max(nlst)
d=nlst.index(t)
f=lst
print(c+f)
初学者的献丑

475123763 发表于 2018-7-29 22:45:39

g=['Office', 'workers', 'who','Office', 'woeqrkers', 'who', 'would', 'normally', 'step', 'into', 'a']

k=3
m=[]
def compare(list,k):
i = 0
while i<k:
      nlst =
      t = max(nlst)
      d = nlst.index(t)
      m.insert(0,list)
      del list
      i+=1
print(''.join(m))
compare(g,k)

Akihisa 发表于 2018-8-18 11:30:54

test.assert_equals(longest_consec(["it","wkppv","ixoyx", "3452", "zzzzzzzzzzzz"], 3), "ixoyx3452zzzzzzzzzzzz")
为什么我的结果和楼主测试结果不一样
>>> longest_consec(["it","wkppv","ixoyx", "3452", "zzzzzzzzzzzz"], 3)
'zzzzzzzzzzzzwkppvixoyx'

Akihisa 发表于 2018-8-18 11:31:33

def longest_consec(strarr,k):
    n = len(strarr)
    if n == 0 or k > n or k <= 0:
      return "无法执行"
    else:
      strarr.sort(key =lambda a:len(a),reverse = True)
      str1 =''
      for i in range(k):
            str1 += strarr
      return str1

jrro452 发表于 2018-9-11 17:33:21

from heapq import nlargest
def longest_consec(lists,num):
    count_list = []
    dict_list = []
    result_list = []
    sorted_list = []
    fr = ''
    joint = ''
    if len(lists) == 0 or num>len(lists) or num<=0:
      return ""
    else:
      for each in lists:
            count_list.append(len(each))
      dict_list = zip(count_list,lists)
      result_list = nlargest(num,dict_list)
      for each in result_list:
            sorted_list.append(each)
      fr =joint.join(sorted_list)
    return fr
做的好复杂,有2到结果和题主结果不一样。不知道是什么情况。

算幽幽 发表于 2018-9-28 21:32:24

本帖最后由 算幽幽 于 2018-9-28 21:41 编辑

I Iove Fishc C{:5_105:}

def longest_consec(list1, n):
    if n==0 or len(list1)<n or len(list1)<=0:
      return ''
    i = 0
    list_len = []
    while i + n <= len(list1):
      result = ""
      for m in range(i, i + n):
            result += list1
      else:
            list_len.append(result)
      i+=1
    return sorted(list_len,key=lambda s:len(s))[-1]


double-lee 发表于 2018-12-12 12:56:27

看看...

3333332 发表于 2019-2-28 10:08:22

谢谢

北城以北灬 发表于 2019-3-19 10:35:32

shigure_takimi 发表于 2017-12-3 14:00


倒数第3个?结果是这样的?为什么?

捞鬼 发表于 2019-4-15 20:48:10

yu123py 发表于 2019-4-29 15:13:15

貌似方法不太对:
def longest_consec(list, k):
    if len(list) == 0 or k > len(list) or k <= 0:
      return ''
    list.sort(reverse = True, key = len)
    s = ''
    for i in range(k):
      s += list
    return s

克里斯保罗 发表于 2019-9-4 19:45:05

没看懂 = =

Jung 发表于 2019-12-5 08:48:41

import time
def fun32():
    Tiangan = {3:"甲",4:"乙",5:"丙",6:"丁",7:"戊",8:"己",9:"庚",0:"辛",1:"壬",2:"癸"}
    DiZhi = {3:"子",4:"丑",5:"寅",6:"卯",7:"辰",8:"巳",9:"午",10:"未",11:"申",0:"酉",1:"戌",2:"亥"}
    ShengXiao = {"子":"鼠","丑":"牛","寅":"虎","卯":"兔","辰":"龙","巳":"蛇","午":"马","未":"羊","申":"猴","酉":"鸡","戌":"狗","亥":"猪"}
    Jiazibiao = {1:"甲子", 11:"甲戌", 21:"甲申",31:"甲午",41:"甲辰",51:"甲寅",
               2:"乙丑",12:"乙亥",22:"乙酉",32:"乙未",42:"乙巳",52:"乙卯",
               3:"丙寅",13:"丙子",23:"丙戌",33:"丙申",43:"丙午",53:"丙辰",
               4:"丁卯",14:"丁丑",24:"丁亥",34:"丁酉",44:"丁未",54:"丁巳",
               5:"戊辰",15:"戊寅",25:"戊子",35:"戊戌",45:"戊申",55:"戊午",
               6:"己巳",16:"己卯",26:"己丑",36:"己亥",46:"己酉",56:"己未",
               7:"庚午",17:"庚辰",27:"庚寅",37:"庚子",47:"庚戌",57:"庚申",
               8:"辛未",18:"辛巳",28:"辛卯",38:"辛丑",48:"辛亥",58:"辛酉",
               9:"壬申",19:"壬午",29:"壬辰",39:"壬寅",49:"壬子",59:"壬戌",
               10:"癸酉",20:"癸未",30:"癸巳",40:"癸卯",50:"癸丑",60:"癸亥"}
    localtime = time.asctime( time.localtime(time.time()))
    LCurrentYear=localtime.split(" ")
    CurrentYear=LCurrentYear
    Year = str(input("请输入年份:"))
    Tian = Tiangan)]
    Di = DiZhi
    ShuXiang = ShengXiao
    print("公元%s是黄历的%s年。属相是%s" % (Year,Tian+Di,ShuXiang))
    GanZi = input("请输入干子:")
    if GanZi in Jiazibiao.values():
      for key in Jiazibiao.keys():
            if Jiazibiao== GanZi:
                GanZiindex = key
                break
      
    else:
      print("输入干子不对")
      
    Year = CurrentYear
    Tian = Tiangan)]
    Di = DiZhi
    CurrentYearGanZi=Tian+Di
    for key1 in Jiazibiao.keys():
      if Jiazibiao== CurrentYearGanZi:
            CurrentYearGanZiindex = key1
            break
    if CurrentYearGanZi==GanZi:
       print("上一个%s年是%d.下一个%s年时%d.本年%d也是%s年" % (GanZi,int(Year)-60,GanZi,int(Year)+60,int(CurrentYear),GanZi))
    else:
         print("上一个%s年是%d.下一个%s年时%d." % (GanZi,int(Year)-(CurrentYearGanZiindex-GanZiindex),GanZi,int(Year)-(CurrentYearGanZiindex-GanZiindex)+60))

Jung 发表于 2019-12-5 09:05:25

Jung 发表于 2019-12-5 08:48
import time
def fun32():
    Tiangan = {3:"甲",4:"乙",5:"丙",6:"丁",7:"戊",8:"己",9:"庚",0:"辛",1: ...

发错地方了。

holiday_python 发表于 2020-4-20 08:41:39

def longest_consec(strarr,k):
    n = len(strarr)
    chr_len = {}
    if n ==0 or k > n or k <= 0:
      return ''
    else:
      for idx in range(1,len(strarr)+1):
            length = len(strarr)
            chr_len] = length
      for k in chr_len.keys():
            if chr_len
            #用字典排序


            return ''.join(strarr[-2:])

if __name__ == '__main__':
    strarr = list(input('请输入字符串元素(用逗号分隔): ').split(','))
    k = int(input('请输入需要取出的字符串个数: '))
    print(longest_consec(strarr, k))

nononoyes 发表于 2020-6-11 15:43:41

666

nononoyes 发表于 2020-6-11 15:50:23

'''

给你一个字符串组成列表strarr,给你一个整数k。
你的任务是找出由k个字符组成的最长的连续的字符串组合。
例子:
longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 2) --> "abigailtheta"
n是列表的长度,如果if n = 0 or k > n or k <= 0 return ""
'''

'''
解题思路:
1.新建一个列表存放原字符串列表中每个字符的长度
2.通过max函数和index函数找到最长字符串的索引值
3.同时删除两个列表最长字符串的索引值再次循环找到最长字符串的索引值
'''
#请将以下代码存为test.py用于检验答案。
def assert_equals(func, target, *args):
    if func == target:
      print('Success!')
    else:
      print('Fail!{0} not equals {1}'.format(func, target))
      print(*args)


def longest_consec(list,k):
    #1.新建一个列表存放原字符串列表中每个字符的长度
    listSort = []
    str=""
    if len(list) ==0 or k > len(list) or k <=0:
      return ""
    else:
      for i in list:
            #1.新建一个列表存放原字符串列表中每个字符的长度
            listSort.append(len(i))
      for j in range(k):
            #通过max函数和index函数找到最长字符串的索引值
            index = listSort.index(max(listSort))
            str += list
            #同时删除两个列表最长字符串的索引值对应的字符
            listSort.remove(max(listSort))
            list.remove(list)
    return str


assert_equals(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas"], 2), "abigailtheta")
assert_equals(longest_consec(["ejjjjmmtthh", "zxxuueeg", "aanlljrrrxx", "dqqqaaabbb", "oocccffuucccjjjkkkjyyyeehh"], 1), "oocccffuucccjjjkkkjyyyeehh")
assert_equals(longest_consec([], 3), "")
assert_equals(longest_consec(["itvayloxrp","wkppqsztdkmvcuwvereiupccauycnjutlv","vweqilsfytihvrzlaodfixoyxvyuyvgpck"], 2), "wkppqsztdkmvcuwvereiupccauycnjutlvvweqilsfytihvrzlaodfixoyxvyuyvgpck")
assert_equals(longest_consec(["wlwsasphmxx","owiaxujylentrklctozmymu","wpgozvxxiu"], 2), "wlwsasphmxxowiaxujylentrklctozmymu")
assert_equals(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas"], -2), "")
assert_equals(longest_consec(["it","wkppv","ixoyx", "3452", "zzzzzzzzzzzz"], 3), "ixoyx3452zzzzzzzzzzzz")
assert_equals(longest_consec(["it","wkppv","ixoyx", "3452", "zzzzzzzzzzzz"], 15), "")
assert_equals(longest_consec(["it","wkppv","ixoyx", "3452", "zzzzzzzzzzzz"], 0), "")


Success!
Success!
Success!
Success!
Fail!owiaxujylentrklctozmymuwlwsasphmxx not equals wlwsasphmxxowiaxujylentrklctozmymu

Success!
Fail!zzzzzzzzzzzzwkppvixoyx not equals ixoyx3452zzzzzzzzzzzz

Success!
Success!

wwwwwise 发表于 2021-3-2 17:10:23

1
页: 1 [2]
查看完整版本: Python: 每日一题 34