这是给出的问题
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']
6条答案
按热度按时间inn6fuwd1#
这个问题是错误的本身在黑客等级尝试下面的代码,它将通过所有的测试用例。在黑客等级的原始条件是小于3,但它应该小于13,然后只有你可以通过所有的测试用例,因为单词'ac'是重复7次
bvuwiixz2#
一些调整
sqyvllje3#
尝试以下代码
请参考博客的完整解释:Here
qpgpyjmq4#
尝试以下代码
tzxcd3kk5#
cld4siwp6#
对我来说这下面是运行良好
定义字符串方法(帕拉、特殊1、特殊2、列表1、字符串查找):
如果名称==“主”:帕拉=输入()