ooxx7788 发表于 2017-4-26 22:12:33

Python: 每日一题 34

给你一个字符串组成列表strarr,给你一个整数k。{啊,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 ""。

请将以下代码存为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)


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


**** Hidden Message *****

lovesword 发表于 2017-4-27 09:08:53

def get_longest(list_,k):
    list_.sort(cmp=None, key=lambda x:len(x), reverse=True)
    if len(list_) == 0 or k > len(list_) or k <= 0 :
      return ""
    return ''.join(list_)
list_= ["zone", "abigail", "theta", "form", "libe", "zas"]
k=2
print get_longest(list_,k)

技术部-李宁 发表于 2017-4-27 10:36:02

没懂k是什么意思……

ooxx7788 发表于 2017-4-27 13:04:37

技术部-李宁 发表于 2017-4-27 10:36
没懂k是什么意思……

k就是最后的字符是由几个strarr里面的元素组成的

jerryxjr1220 发表于 2017-4-27 13:39:04

def get_longest(lst,k):
nlst =
xlst = ) for i in range(len(lst)-k)]
maxi = xlst.index(max(xlst))
return ''.join(lst)

余欲渔 发表于 2017-4-28 20:58:58

def lj(strarr,k):
    b=sorted(strarr,key=lambda x:len(x),reverse=True)
    n=len(b)
    if n==0 or k>n or k<=0:
      return ''
    return ''.join(b for i in range(k))

a=["itvayloxrp","wkppqsztdkmvcuwvereiupccauycnjutlv","vweqilsfytihvrzlaodfixoyxvyuyvgpck"]
print(lj(a,2))

ooxx7788 发表于 2017-4-28 21:07:38

余欲渔 发表于 2017-4-28 20:58


大佬刷题的速度超快啊!

kyf84202 发表于 2017-4-29 00:08:17

看看

余欲渔 发表于 2017-4-29 23:41:01

ooxx7788 发表于 2017-4-28 21:07
大佬刷题的速度超快啊!

前几天没时间,昨天恶补了一下

willLin 发表于 2017-5-12 14:58:50

看看

solomonxian 发表于 2017-6-21 20:40:52

不知道我的理解对不对,题目没给出字符串一样长时怎么取,是我干了你随意吗{:10_250:}
# 排序出字符串长度,再按次数取出,有同样长度的字符串可能按Unicode码排列
def longest_consec(a,k):
    """传入字符串列表 a,要求字符数列 k,返回k个字符的最长组合"""
    if len(a)==0 or k>len(a) or k<=0:
      return ""
    else:
      s = sorted(a, key=lambda x: len(x), reverse=True)
      return "".join( for i in range(k)])

gausser 发表于 2017-9-24 16:50:41


k就是列表strarr中k个元素组成的最长的字符串?但是连续怎么理解?题目没看懂。。。

gausser 发表于 2017-9-24 18:05:40

def longest_consec(str_arr, k):
    long_str = ''
    if len(str_arr) == 0 or k <= 0 or k > len(str_arr):
      return ''
   
    while k > 0:
      tmp_str = str_arr
      
      # Find the longest str in str_arr
      for each in str_arr:
            if len(each) > len(tmp_str):
                tmp_str = each
      # Remove the same element in str_arr
      for i in str_arr:
            if i == tmp_str:
                str_arr.remove(i)

      long_str = long_str + tmp_str
      k = k - 1
    print '******the answer is: *******', long_str

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

本帖最后由 shigure_takimi 于 2017-12-3 18:13 编辑

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(strarr, k):
    if len(strarr)==0 or k>len(strarr) or k<=0:
      return ''
    else:
      lengthOfEachStr =
      maxLength = 0
      start = 0
      for i in range(len(strarr)):
            length = sum(lengthOfEachStr)
            if length > maxLength:
                maxLength = length
                start = i
      return ''.join(strarr)


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), "")
      


>>> ================================ RESTART ================================
>>>
Success!
Success!
Success!
Success!
Success!
Success!
Success!
Success!
Success!

1071006772 发表于 2017-12-5 12:12:22

发现一道题目做不来

大头目 发表于 2018-4-16 15:10:22

{:10_266:}

小庆子 发表于 2018-5-17 19:54:48

def longest_consec(listr,n):
    n_list = []
    for i in range(len(listr)):
      temp = ),listr]
      n_list.append(temp)
      temp = []
    n_list.sort(reverse=True)
    result = ('')
    time = 0
    while time != n :
      result += str(n_list)
      time += 1
    print(result)
      

longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 2)

chinayyyy 发表于 2018-5-17 20:48:16

{:10_266:}

萧丹夜 发表于 2018-5-26 22:33:03

本帖最后由 萧丹夜 于 2018-5-26 22:42 编辑

貌似已经出来了
## 代码
import test

def longest_consec(x,k):
    result = ''
    y = []
    z = []
    n = len(x)
    if n == 0 or n < k or k <= 0:
      return ""
    else:
      for i in range(n):
            y.append(len(x))
      i = 0
      while i < k:
            sub = y.index(max(y))
            if x not in z:
                # print(x,end = '')
                result += x
                z.append(x)
                y = 0
                i += 1
            else:
                y = 0

    return result

#### 测试
test.assert_equals(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas"], 2), "abigailtheta")
test.assert_equals(longest_consec(["ejjjjmmtthh", "zxxuueeg", "aanlljrrrxx", "dqqqaaabbb", "oocccffuucccjjjkkkjyyyeehh"], 1), "oocccffuucccjjjkkkjyyyeehh")
test.assert_equals(longest_consec([], 3), "")
test.assert_equals(longest_consec(["itvayloxrp","wkppqsztdkmvcuwvereiupccauycnjutlv","vweqilsfytihvrzlaodfixoyxvyuyvgpck"], 2), "wkppqsztdkmvcuwvereiupccauycnjutlvvweqilsfytihvrzlaodfixoyxvyuyvgpck")
test.assert_equals(longest_consec(["wlwsasphmxx","owiaxujylentrklctozmymu","wpgozvxxiu"], 2), "wlwsasphmxxowiaxujylentrklctozmymu")
test.assert_equals(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas"], -2), "")
test.assert_equals(longest_consec(["it","wkppv","ixoyx", "3452", "zzzzzzzzzzzz"], 3), "ixoyx3452zzzzzzzzzzzz")
test.assert_equals(longest_consec(["it","wkppv","ixoyx", "3452", "zzzzzzzzzzzz"], 15), "")
test.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!

小强工作室 发表于 2018-7-18 14:42:15

看不懂题目,好闹心
页: [1] 2
查看完整版本: Python: 每日一题 34