Python 3.x Hackerrank列出了问题~标准测试用例有效,但其他测试用例无效';T
考虑一个列表(list=[])。可以执行以下命令:Python 3.x Hackerrank列出了问题~标准测试用例有效,但其他测试用例无效';T,python-3.x,list,Python 3.x,List,考虑一个列表(list=[])。可以执行以下命令: insert i e: Insert integer e at position . print: Print the list. remove e: Delete the first occurrence of integer e. append e: Insert integer e at the end of the list. sort: Sort the list. pop: Pop the last element from the
insert i e: Insert integer e at position .
print: Print the list.
remove e: Delete the first occurrence of integer e.
append e: Insert integer e at the end of the list.
sort: Sort the list.
pop: Pop the last element from the list.
reverse: Reverse the list.
初始化列表并读入的值,后跟命令行,其中每个命令都属于上面列出的类型。按顺序遍历每个命令,并在列表上执行相应的操作
样本输入:
12
insert 0 5
insert 1 10
insert 0 6
print
remove 6
append 9
append 1
sort
print
pop
reverse
print
我的代码:
import sys
if __name__ == '__main__':
N = int(input())
my_list = []
inputs = []
for line in sys.stdin:
inputs.append(line)
for item in inputs:
if item[0:5] == 'print':
print(my_list)
elif item[0:2] == 'in':
inserts = [s for s in item.split()][1:3]
inserts = list(map(int, inserts))
my_list.insert(inserts[0], inserts[1])
elif item[0:3] == 'rem':
inserts = list(map(int, [s for s in item.split()][1]))
my_list.remove(inserts[0])
elif item[0:2] == 'ap':
inserts = list(map(int, [s for s in item.split()][1]))
my_list.append(inserts[0])
elif item[0:4] == 'sort':
my_list.sort()
elif item[0:3] == 'pop':
my_list.pop()
elif item[0:7] == 'reverse':
my_list.reverse()
我不确定为什么我的代码在提交时没有得到批准。在他们提供的这个测试用例中,我的代码通过了测试。
预期输出如下:
[6, 5, 10]
[1, 5, 9, 10]
[9, 5, 1]
非常感谢你的帮助 您的问题是,当要追加的数字超过一位时,追加代码有一个错误。在你的代码里
inserts = list(map(int, [s for s in item.split()][1]))
my_list.append(inserts[0])
例如,如果“item”命令是“append 12”,
[s代表item.split()][1]
将是字符串“12”,因此list(map(int,[s代表item.split()][1])
将该字符串中的每个字符映射为一个整数,给您[1,2]而不是[12],因此my_list.append(inserts[0])
将附加数字1而不是12。所以,解决这个问题,你就会解决你的问题。字符串切片不是一个好主意。此外,您需要获得N次用户输入。因此,您可以按如下方式编辑解决方案:
if __name__ == '__main__':
N = int(input())
the_list = list()
for _ in range(N):
query = input().split()
if query[0] == "print":
print(the_list)
elif query[0] == "insert":
the_list.insert(int(query[1]), int(query[2]))
elif query[0] == "remove":
the_list.remove(int(query[1]))
elif query[0] == "append":
the_list.append(int(query[1]))
elif query[0] == "sort":
the_list = sorted(the_list)
elif query[0] == "pop":
the_list.pop()
elif query[0] == "reverse":
the_list.reverse()
下面是一个没有“if”或“elif”语句的解决方案:
if __name__ == '__main__':
N = int(input())
commands = {
"insert": lambda x, y, z: x.insert(y, z),
"print": lambda x: print(x),
"remove": lambda x, y: x.remove(y),
"append": lambda x, y: x.append(y),
"sort": lambda x: x.sort(),
"pop": lambda x: x.pop(),
"reverse": lambda x: x.reverse(),
}
out = []
for i in range(N):
a = input()
split_a = a.split(' ')
command = split_a[0]
try:
commands[command](out, int(split_a[1]), int(split_a[2]))
except IndexError:
try:
commands[command](out, int(split_a[1]))
except IndexError:
commands[command](out)
你从
stdin
中阅读N
,但你从未使用过它。当输入流中的行数与N
不同时,这是否可能导致问题?(请注意,我还没有阅读问题的规格)老实说,我不确定这是否重要。从我看来,这不应该影响结果,对吗?第一个数字只表示可以有多少个命令。非常感谢!这解决了我的问题。我很感谢你的帮助。虽然这段代码可以解决这个问题,但如何以及为什么解决这个问题将真正有助于提高你的文章质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在您的答案中添加解释,并说明适用的限制和假设。我们为什么要这样做“query=input().split()”?我需要了解它的功能purpose@Rahul因为我们需要获得用户输入的正确格式。请记住,用户输入是一个字符串,但我们需要第一个单词作为字符串,但其他单词应为int以解决此问题。欢迎使用SO。虽然我们感谢您的回答,但如果它能在其他答案的基础上提供额外的价值,那就更好了。在这种情况下,您的答案不会提供额外的价值,因为hevalhazalkurt已经发布了该解决方案。如果之前的答案对你有帮助,你应该在你有足够的声誉后投票。好的。我会在下一篇文章中尝试这样做。请不要只发布代码作为答案,还要解释你的代码的作用以及它如何解决问题。带有解释的答案通常更有用、质量更好,并且更有可能吸引更多的投票。虽然只有代码的答案在特定情况下可能会有所帮助,但请提供详细信息,说明其原因和工作方式,以便解决类似问题。欢迎使用Stackoverflow,感谢您的帮助。也许您可以解释为什么删除if和elif语句会有所帮助?谢谢Tim。我个人尽量避免使用“if”和“elif”条件句,因为它们往往被过度使用。然而,当我别无选择时,我会使用它们。根据我的经验,在不使用“如果”和“elif”的情况下寻找解决方案有助于我以不同的方式思考问题,并学习更好的方法来解决某些问题。此外,在单元测试代码时,如果可能的话,在使用条件时可能需要处理所有条件。例如,这个问题需要7+“if”和“elif”条件。使用我的方法,我只需要在我的try excepts中测试3个案例。谢谢-您的解决方案确实让人感觉更像Python。非常有创意的想法!我发现后面的三个elif/else案例可以简化为else:getattr(lst,command[0])(*map(int,command[1:])
欢迎使用SO。完全不建议使用代码式答案。请在您的问题中添加相关细节,以便其他人也能理解您的答案。如果您添加一条有用的注释,说明您为使代码正常工作所做的工作,这将有所帮助。仅仅是代码并不能真正帮助这个人理解他们做错了什么。
def execute(lst, cmd, *args):
if cmd == 'insert':
lst.insert(int(args[0]), int(args[1]))
elif cmd == 'print':
print(lst)
elif cmd == 'remove':
lst.remove(int(args[0]))
elif cmd == 'append':
lst.append(int(args[0]))
elif cmd == 'sort':
lst.sort()
elif cmd == 'reverse':
lst.reverse()
elif cmd == 'pop':
lst.pop()
else:
print("Command not recognized!")
lst = []
for _ in range(int(input())):
execute(lst, *input().split())
if __name__ == '__main__':
N = int(input())
reqarr = []
for i in range(0,N):
inplist = (input().split())
if(inplist[0] == "insert" ):
reqarr.insert(int(inplist[1]),int(inplist[2]))
elif(inplist[0] == "print"):
print(reqarr)
elif(inplist[0] == "remove"):
reqarr.remove(int(inplist[1]))
elif(inplist[0] == "append"):
reqarr.append(int(inplist[1]))
elif(inplist[0]=="sort"):
reqarr.sort()
elif(inplist[0]=="reverse"):
reqarr.reverse()
elif(inplist[0] == "pop"):
reqarr.pop()
def operation(last_value1,operation_name,*number):
list_number = last_value1
if operation_name.lower() == 'insert':
list_number.insert(int(number[0]),int(number[1]))
elif operation_name.lower() == 'remove':
list_number.remove(int(number[0]))
elif operation_name.lower() == 'append':
list_number.append(int(number[0]))
elif operation_name.lower() == 'sort':
list_number = sorted(list_number)
elif operation_name.lower() == 'print':
print(list_number)
elif operation_name.lower() == 'pop':
list_number.pop()
elif operation_name.lower() == 'reverse':
list_number= list_number[::-1]
return list_number
N = int(input())
last_value = []
for count in range(0,N):
command_input = input("Please enter the command with number seperated by space")
command_input = command_input.split()
if len(command_input) == 3:
last_value = operation(last_value,command_input[0],command_input[1],command_input[2])
elif len(command_input) == 2:
last_value= operation(last_value,command_input[0],command_input[1])
else:
last_value = operation(last_value,command_input[0])
*if __name__ == '__main__':
N = int(input())
the_list = list()
for cmd in range(N):
input_cmd = input().split()
if input_cmd[0] == 'print':
print(the_list)
elif input_cmd[0] == 'insert':
the_list.insert(int(input_cmd[1]), int(input_cmd[2]))
elif input_cmd[0] == 'remove':
the_list.remove(int(input_cmd[1]))
elif input_cmd[0] == 'append':
the_list.append(int(input_cmd[1]))
elif input_cmd[0] == 'sort':
the_list = sorted(the_list)
elif input_cmd[0] == 'pop':
the_list.pop()
elif input_cmd[0] == 'reverse':
the_list.reverse()*
if __name__ == '__main__':
N = int(input())
commands = {
"insert": lambda x, y, z: x.insert(y, z),
"print": lambda x: print(x),
"remove": lambda x, y: x.remove(y),
"append": lambda x, y: x.append(y),
"sort": lambda x: x.sort(),
"pop": lambda x: x.pop(),
"reverse": lambda x: x.reverse(),
}
out = []
for i in range(N):
a = input()
split_a = a.split(' ')
command = split_a[0]
try:
commands[command](out, int(split_a[1]), int(split_a[2]))
except IndexError:
try:
commands[command](out, int(split_a[1]))
except IndexError:
commands[command](out)
N = int(input())
lst = []
for i in range(N):
command = input().split()
if command[0] == "print":
print(lst)
elif len(command) == 1:
getattr(lst, command[0])()
elif len(command) == 2:
getattr(lst, command[0])(int(command[1]))
else:
getattr(lst, command[0])(int(command[1]), int(command[2]))
if __name__ == '__main__':
N = int(input())
l=[]
for i in range(N):
s=input().split("\n")
for j in s:
m=j.split()
if m[0]=="insert":
l.insert(int(m[1]),int(m[2]))
elif m[0]=="print":
print(l)
elif m[0]=="remove":
l.remove(int(m[1]))
elif m[0]=="append":
l.append(int(m[1]))
elif m[0]=="sort":
l.sort()
elif m[0]=="pop":
l.pop()
elif m[0]=="reverse":
l.reverse()
x= int(input())
lst = []
for i in range(x):
n = input()
lst.append(n)
newlist=[]
for ele in lst:
splitted = ele.split()
if splitted[0] == "insert":
newlist.insert(int(splitted[1]),splitted[2])
elif splitted[0] == "print":
print(newlist)
elif splitted[0] == "remove":
newlist.remove(splitted[1])
elif splitted[0] == "append":
newlist.append(splitted[1])
elif splitted[0] == "sort":
newlist.sort()
elif splitted[0] == "pop":
newlist.pop()
elif splitted[0] == "reverse":
newlist.reverse()
else:
print("Command out of Scope")
### You can try this one:
if __name__ == '__main__':
n = int(input())
num_list = []
for _ in range(n):
string = input().split()
command = string[0]
args = string[1:]
if command != "print":
command += "(" + ",".join(args) + ")"
eval("num_list." + command)
else:
print(num_list)