这是给出的问题
Define a function called 'stringmethod ; which takes five parameters.
• The first parameter is para, which takes a string sentence.
• The second parameter is speciall, which takes a string of special characters.
• The third parameter is speciall, which takes a single string of special characters.
• The fourth parameter is listl, which takes a list of strings.
• The fifth parameter is strfind, which takes a string.
The function definition code stub is given in the editor. Generate print statements based on the following conditions:
• Remove all the special characters from para specified in speciall and save them in the variable wordl.
• Get the first 70 characters from words, reverse the strings, save it in variable rword2, and print it.
• Remove all the wide spaces from rword2, join the characters using the special character specified in speciall, and print the joint string.
• If every string from listl is present in para, then format the print statement as follows:
o "Every string in {listl } were present"
• else
o "Every string in {listl } were not present"
• Split every word from wordl and print the first 20 strings as a list.
• Calculate the less frequently used words whose count < 3. and print the last 20 less frequent words as a list. • Print the last index in wordl, where the substring strfind is found.
Input Format for Custom Testing
• In the first line, the string 'para' is given.
• In the second line, the string 'speciall is given.
• In the third line, the string 'special2' is given.
• The fourth line contains an integer a the size of the listl.
• In the next line, strfind is given.
下面是修改后的查询,我将整个程序放在我为查询编写的位置。
import math
import os
import random
import re
import sys
#
# Complete the 'strmethod' function below.
#
# The function accepts following parameters:
# 1. STRING para
# 2. STRING spch1
# 3. STRING spch2
# 4. LIST li1
# 5. STRING strf
#
from collections import Counter
def stringmethod(para, special1, special2, list1, strfind):
word1 = ''.join(i for i in para if not i in special1)
word2 = word1[69::-1]
print(word2)
rem=word2.replace(" ","")
sp=special2.join(rem)
print(sp)
if all(x in para for x in list1):
print("Every string in {} were present" .format(list1) )
else:
print("Every string in {} were not present " .format(list1))
sp2 = word1.split()
print(sp2[0:20])
l = list(word1.split())
sorted_items = [w for w, _ in Counter(l).most_common()]
print(sorted_items[-20:])
print(word1.rindex(strfind))
if __name__ == '__main__':
para = input()
spch1 = input()
spch2 = input()
qw1_count = int(input().strip())
qw1 = []
for _ in range(qw1_count):
qw1_item = input()
qw1.append(qw1_item)
strf = input()
stringmethod(para, spch1, spch2, qw1, strf)
以下是提供的输入
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam sem odio, varius nec aliquam nec, tempor commodo ante. Pellentesque sit amet augue vel ante dictum placerat ut ut sapien. Proin maximus eu diam in posuere. Suspendisse in lectus in lectus finibus auctor. Nam sed porttitor arcu. Vestibulum augue odio, posuere quis libero sed, pharetra sollicitudin est. Donec sit amet nunc eu nisi malesuada elementum id ut purus.Nunc sit amet % massa rhoncus, venenatis eros sit amet, ornare augue. Nunc a mi sed est tincidunt facilisis at nec diam. Donec nec ex lorem. Morbi vitae diam tincidunt, dignissim arcu ut, facilisis nisi. Maecenas non felis #ullamcorper, viverra augue id, consequat_nunc. Suspendisse potenti. Proin tempor, sapien ut ornare placerat, sapien mauris luctus sapien, eget aliquam turpis urna at quam. Sed a&eros vel@ ante vestibulum vulputate. Suspendisse vitae vulputate velit. Suspendisse! ligula nisl, semper ut sodales et, ultricies porttitor felis. Nunc ac mattis erat, aliquet pretium risus. Nullam quis congue lacus, et mollis nulla. Nunc laoreet in nisi sit amet facili*sis. Cras rutrum justo ut eros mollis volutpat. Sed quis mi nunc. Nunc sed bibendum nibh, quis bibendum tortor.
,_!@*%#$.
,
3
adipiscing
Aliquam
Suspendisse
vulputate
在我得到以下结果的地方,您可以在其中看到所有匹配项,除了 20 个不常用的单词部分。
ido mes mauqilA tile gnicsipida rutetcesnoc tema tis rolod muspi meroL
i,d,o,m,e,s,m,a,u,q,i,l,A,t,i,l,e,g,n,i,c,s,i,p,i,d,a,r,u,t,e,t,c,e,s,n,o,c,t,e,m,a,t,i,s,r,o,l,o,d,m,u,s,p,i,m,e,r,o,L
Every string in ['adipiscing', 'Aliquam', 'Suspendisse'] were present
['Lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit', 'Aliquam', 'sem', 'odio', 'varius', 'nec', 'aliquam', 'nec', 'tempor', 'commodo', 'ante', 'Pellentesque', 'sit']
['semper', 'sodales', 'ultricies', 'ac', 'mattis', 'erat', 'aliquet', 'pretium', 'risus', 'Nullam', 'congue', 'lacus', 'nulla', 'laoreet', 'Cras', 'rutrum', 'justo', 'volutpat', 'nibh', 'tortor']
851
而他们需要的输出是
['ultricies', 'ac', 'mattis', 'erat', 'aliquet', 'pretium', 'risus', 'Nullam', 'congue', 'lacus', 'mollis', 'nulla', 'laoreet', 'Cras', 'rutrum', 'justo', 'volutpat', 'bibendum', 'nibh', 'tortor']
而我得到的如下,这似乎并没有错
['semper', 'sodales', 'ultricies', 'ac', 'mattis', 'erat', 'aliquet', 'pretium', 'risus', 'Nullam', 'congue', 'lacus', 'nulla', 'laoreet', 'Cras', 'rutrum', 'justo', 'volutpat', 'nibh', 'tortor']
原文由 TheBeginner 发布,翻译遵循 CC BY-SA 4.0 许可协议
黑客排名中的问题本身是错误的尝试下面的代码它将通过所有测试用例。 hacker rank 中的原始条件小于 3 但它应该小于 13 那么只有你才能通过所有测试用例,因为 ‘ac’ 一词重复了 7 次