Skip to main content

HackerRank Question and Answer : Hands-on - List Operations

Basic List Operations

 

Write the function definition for the function 'List_Op' which takes 2 parameters 'Mylist' and 'Mylist2' both of which are LIST of INTEGERS

 

Do as per the following to get the expected output:

1.Print 'Mylist'

2.Print the 1st element of  'Mylist'

3.Print the Last element of  'Mylist'

4.Add '3' as the Last element of 'Mylist'

5.Change the 4th element of 'Mylist' into '60'

6.Print 'Mylist' again

7.Print from the '2nd' element to the '5th' element of 'Mylist'

8.Append 'Mylist2' to 'Mylist'

9.Print 'Mylist' again

10.Extend 'Mylist' using 'Mylist2'

11.Print 'Mylist' again

12.Remove the Last element of 'Mylist'

13.Print 'Mylist' again

14.Print 'Length' of 'Mylist'

 

Input Format for Custom Testing

 

  • The first line contains an integer n1, the size of the Mylist

            Each of the next n1 lines contains an Mylist[i] where 0 ≤ i < n1.

  • The next line contains an integer n2, the size of the Mylist2.

            Each of the next n2 lines contains an Mylist2[i] where 0 ≤ i < n2.

 

Sample Test Case 1

 

Sample Input

STDIN      Function
-----      --------
5       →  Mylist[] Size n1 = 5
1       →  Mylist[] = [ 1, 2, 3, 4, 5 ]
2
3
4
5
3       →  Mylist2[] Size n2 = 3
6       →  Mylist2[] = [6, 7, 8]
7
8

Sample Output

[1, 2, 3, 4, 5]
2
5
[1, 2, 3, 60, 5, 3]
[2, 3, 60, 5]
[1, 2, 3, 60, 5, 3, [6, 7, 8]]
[1, 2, 3, 60, 5, 3, [6, 7, 8], 6, 7, 8]
[1, 2, 3, 60, 5, 3, [6, 7, 8], 6, 7]
9

 





#!/bin/python3

import math
import os
import random
import re
import sys



#
# Complete the 'List_Op' function below.
#
# The function accepts following parameters:
#  1. LIST Mylist
#  2. LIST Mylist2
#

def List_Op(Mylist, Mylist2):
    # Write your code here
    print(Mylist)
    print(Mylist[1])
    print(Mylist[-1])
    Mylist.append(3)
    Mylist.pop(3)
    Mylist.insert(3,60)
    print(Mylist)
    print(Mylist[1:5])
    Mylist.append(Mylist2)
    print(Mylist)
    Mylist.extend(Mylist2)
    print(Mylist)
    Mylist.pop(-1)
    print(Mylist)
    print(len(Mylist))
    

if __name__ == '__main__':
    qw1_count = int(input().strip())

    qw1 = []

    for _ in range(qw1_count):
        qw1_item = int(input().strip())
        qw1.append(qw1_item)

    qw2_count = int(input().strip())

    qw2 = []

    for _ in range(qw2_count):
        qw1_item = int(input().strip())
        qw2.append(qw1_item)

    List_Op(qw1,qw2)

Comments

Popular posts from this blog

Python Hacker Rank Question and Answer -- Hands-on - Range

Python - Range1   Define a function called ` rangefunction ` which takes three parameters . The first parameter is ` startvalue ` , the next parameter is ` endvalue` and the last parameter is 'stepvalue' .   The function definition code stub is given in the editor. Generate the squares of numbers from ` startvalue ` to ` endvalue ` using range   based on condition given below:   Print the values which must be separated by tab spaces. Note: startvalue and endvalue both are inclusive in the range.    Constraints 1 ≤ startvalue Input Format for Custom Testing   In the first line  startvalue given In the second line endvalue given In the second line stepvalue given     Sample Case 0   Sample Input STDIN      Function -----      -------- 2       → startvalue = 2 9 → endvalue = 9 2 → ...

Python Hacker Rank Question and Answer -- Hands-on - Namespaces

  Python - Namespaces Write the function definition for the function  ' Assign ' to assign the different types of variables in its parameters to new variables. Parameters: 1.An INTEGER in the variable ' i ' 2.A FLOAT in the variable ' f ' 3.A STRING in the variable ' s ' 4.A BOOLEAN in the variable ' b '   New Variables to be assigned with : 1.An INTEGER in the variable ' w ' 2.A FLOAT in the variable ' x ' 3.A STRING in the variable ' y ' 4.A BOOLEAN in the variable ' z '   Assign the parameter variables  Respectively . and Print these in the following order: 1.w 2.x 3.y 4.z 5.Display all the objects defined in the current namespace by using the 'dir' function   Input Format for Custom Testing: # In the first line, value for ' i ' # In the second line, value for ' f ' # In the third line, value for ' s ' # In the fourth line, value for ' b '   Sample Test Case 1:   Sampl...

Python Hacker Rank question and answer --HandsOn -- Print

Greeting Quote Mr.Greet is the name of the digital notice board placed at the entrance of the seminar hall.Its purpose is to welcome each and every participants of the seminar by showing a welcoming quote with their name on it. It is based on the function ‘ Greet ’ which takes a single parameter ‘ Name ’ as a String are the names of the participants as Input One by one. Write the function definition for ‘ Greet ’ which will generate the welcoming quote as below : For Example,the ‘ Name ’ is “Ramakrishnan” then the welcoming quote will be : Output: Welcome Ramakrishnan. It is our pleasure inviting you. Have a wonderful day.   Note: ‘ Name ’ must be of String Data type.   Input Format for Custom Testing: It’s a single line contains a name. Sample Test Case 1:   Sample Input STDIN      Function -----      -------- Karthik → Name Sample Output Welcome Karthik. It is our pleasure inviting yo...