首页 > 解决方案 > 另一个 IDE 中 HackerRank 中的副本输入(sublime text 或 spyder)

问题描述

我正在尝试将 Hacker Rank 问题的输入复制到另一个 IDE。见鬼,它甚至不能在 HankerRank 本身工作。 https://www.hackerrank.com/challenges/python-lists/problem

请通过回答以下问题帮助我:

  1. 如何访问 HackerRank 平台中的每一行
  2. 如何在 IDE 中复制输入,例如 sublime text 或 spyder
  3. 是不是我的代码不好?

这是输入:

12
insert 0 5
insert 1 10
insert 0 6
print
remove 6
append 9
append 1
sort
print
pop
reverse
print

这里有两个挑战。如何逐行迭代我使用了以下代码:

test = []
n = int(raw_input())
for _ in range(0,int(raw_input())):
    input = raw_input.split(" ")
    if len(input) == 3:
        eval("test.{0}({1},{2})".format(input[0],input[1],input[2]))
        print(test)
    elif len(line2) == 2:
        eval("test.{0}({1})".format(input[0],input[1]))
        print(test)
    elif len(line2) == 1:
        eval("{1}(test)".format(input[0](test)))
        print(test)

print(test)

这里的目标是逐行迭代并将每一行作为命令执行。

Consider a list (list = []). You can perform the following commands:

insert i e: Insert integer  at position .
print: Print the list.
remove e: Delete the first occurrence of integer .
append e: Insert integer  at the end of the list.
sort: Sort the list.
pop: Pop the last element from the list.
reverse: Reverse the list.
Initialize your list and read in the value of  followed by  lines of commands where each command will be of the  types listed above. Iterate through each command in order and perform the corresponding operation on your list.

标签: python-3.x

解决方案


这是一个关于列表的非常好的问题。
可以用这种方法解决。

N = int(input())
result = []
for n in range(N):
    x = input().split(" ")
    command = x[0]
    if command == 'append':
        result.append(int(x[1]))
    if command == 'print':
        print(result)
    if command == 'insert':
        result.insert(int(x[1]), int(x[2]))
    if command == 'reverse':
        result = result[::-1]
    if command == 'pop':
        result.pop()
    if command == 'sort':
        result = sorted(result)
    if command == 'remove':
        result.remove(int(x[1]))

回答您的问题
1-在接受输入时考虑值并相应地执行功能。
2-我给你的代码可以在任何地方工作(sublime、spyder 等)。
3-我有一些想法。

  • 你正在为'N'输入输入,这就是为什么你在'for'循环中再次输入输入的操作数。你可以在那里使用'N'并避免混淆。
  • 您可以编写提供的关键字,例如“追加”、“打印”等,而不是检查输入的长度,并将它们比较哪一个是正确的,然后采取相应的行动。
  • 'line2' 没有在你使用'input' 作为变量名的'if' 条件中的任何地方和相同位置定义。

我试图用最简单的词来解释它。
让我知道它是否有帮助。


推荐阅读