Hackerrank列出了问题〜标准测试用例可行,但其他的则不可行

时间:2019-04-26 05:03:14

标签: python-3.x 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 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]

非常感谢您的帮助!

11 个答案:

答案 0 :(得分:1)

if __name__ == '__main__':
    N = int(input())
    m=list()
    for i in range(N):
       method,*l=input().split()
       k=list(map(int,l))
       if len(k)==2:
          q=[k[0]]
          w=[k[1]]
       elif len(k)==1:
          q=[k[0]]
       if method =='insert':
          m.insert(q[0],w[0])
       elif method == 'append':
          m.append(q[0])
       elif  method == 'remove':
          m.remove(q[0])
       elif method =='print':
          print(m)
       elif method == 'reverse':
          m.reverse()
       elif method =='pop':
          m.pop()
       elif method == 'sort':
          m.sort()

答案 1 :(得分:1)

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())

答案 2 :(得分:1)

这是一个没有“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)

答案 3 :(得分:0)

您的问题是,当要附加的数字超过一位时,您的附加代码存在错误。在您的代码中

inserts = list(map(int, [s for s in item.split()][1]))
my_list.append(inserts[0])

例如,如果“ item”命令为“ append 12”,则[s for s in item.split()][1]将是字符串“ 12”,因此list(map(int, [s for s in item.split()][1]))会将字符串中的每个字符映射为一个整数,从而使您[ 1,2]而不是[12],因此my_list.append(inserts[0])将附加数字1而不是12。因此,请解决此问题,然后您将解决问题。

答案 4 :(得分:0)

字符串切片不是一个好主意。另外,您需要获得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()

答案 5 :(得分:0)

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()              
                
    

答案 6 :(得分:0)

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])

答案 7 :(得分: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()*
        

答案 8 :(得分:0)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
    <meta name="generator" content="Hugo 0.80.0">
    <title>Detailed View</title>

    <link rel="canonical" href="">

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet"
          integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.4.0/font/bootstrap-icons.css">

    <!-- Favicons -->
    <link rel="apple-touch-icon" href="https://getbootstrap.com/docs/5.0/assets/img/favicons/apple-touch-icon.png"
          sizes="180x180">
    <link rel="icon" href="https://getbootstrap.com/docs/5.0/assets/img/favicons/favicon-32x32.png" sizes="32x32"
          type="image/png">
    <link rel="icon" href="https://getbootstrap.com/docs/5.0/assets/img/favicons/favicon-16x16.png" sizes="16x16"
          type="image/png">
    <link rel="manifest" href="https://getbootstrap.com/docs/5.0/assets/img/favicons/manifest.json">
    <link rel="mask-icon" href="https://getbootstrap.com/docs/5.0/assets/img/favicons/safari-pinned-tab.svg"
          color="#7952b3">
    <link rel="icon" href="https://getbootstrap.com/docs/5.0/assets/img/favicons/favicon.ico">
    <meta name="theme-color" content="#7952b3">

    <!-- Custom styles for this template -->
    <link href="../css/t02-stockDetailView_style.css" rel="stylesheet">

</head>
<body>

<main class="container">
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="#">Home</a></li>
            <li class="breadcrumb-item active" aria-current="page">Library</li>
        </ol>
    </nav>


    <div class="text-center py-5 px-3">

        <div class="row">

        <div class="col-xs-12 col-sm-6 col-md-6 px-0">
                    <div class="well well-sm">
                        <div class="row">
                            <div class="col-sm-6 col-md-4">
                                <img src="http://placehold.it/380x500" alt="" class="img-thumbnail"/>
                            </div>
                            <div class="col-sm-6 col-md-8">
                                <h4>
                                    Bhaumik Patel</h4>
                                <p>CEO of Apple Inc.</p>
                                <small><cite title="San Francisco, USA">San Francisco, USA <i
                                        class="glyphicon glyphicon-map-marker">
                                </i></cite></small>
                                <p>
                                    <i class="bi bi-envelope"></i> email@example.com
                                    <br/>
                                    <i class="bi bi-globe"></i><a href="http://www.jquery2dotnet.com"> www.jquery2dotnet.com</a>
                                    <br/>
                                    <i class="bi bi-gift"></i> June 02, 1988</p>
                            </div>
                        </div>
                    </div>
                </div>
                </div>

        <div class="row mt-3">

            <ul class="nav nav-pills nav-fill" id="pills-tab" role="tablist">
                <li class="nav-item">
                    <a class="nav-link active" id="pills-home-tab" data-bs-toggle="pill" href="#pills-home" role="tab" aria-controls="pills-home" aria-selected="true">Home</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" id="pills-profile-tab" data-bs-toggle="pill" href="#pills-profile" role="tab" aria-controls="pills-profile" aria-selected="false">Profile</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" id="pills-contact-tab" data-bs-toggle="pill" href="#pills-contact" role="tab" aria-controls="pills-contact" aria-selected="false">Contact</a>
                </li>
            </ul>
            <div class="tab-content" id="pills-tabContent">
                <div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">test</div>
                <div class="tab-pane fade" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab">...</div>
                <div class="tab-pane fade" id="pills-contact" role="tabpanel" aria-labelledby="pills-contact-tab">test2</div>
            </div>

        </div>


    </div>

</main><!-- /.container -->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>

</body>
</html>

答案 9 :(得分:0)

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

答案 10 :(得分:0)

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