Breaking News

Python - List Operations 2- HackerRank Questions and answers Course ID 55193

 

Handson - Python - List Operations 2- HackerRank Questions and answers Course ID 55193

 Python - List Operations 2

Define a function called 'tuplefunction' which takes four parameters

  • The first parameter is list1 which takes a list of integers
  • The second parameter is list2 which takes a list of strings
  • The third parameter is string1 which takes a string
  • The fourth parameter is n which takes an integer
The function definition code stub is given in the editor. Generate the print statements based on condition given below:
  • convert the lists(list1,list2) into tuples and store them as tuple1 and tuple2.
  • concatenate tuple1 and tuple2 and print the concatenated tuple.
  • print the 4th index in the concatenated tuple.
  • create a nested tuple out of the two tuples(tuple1 and tuple2).
  • print the length of the nested tuple.
  • create a tuple with repetition of the string string1, n number of times and print tuple
  • print the maximum if tuple tuple1.
Input Format for Custom Testing
  • The first line contains an integer n1, the size of the list1.
            Each of the next n1 lines contains an list1[i] where 0<=i<n1.
  • The next line contains an integer n2, the size of the list2.
            Each of the next n2 lines contains an list2[i] where 0<=i<n2
  • In the next line string1 is given
  • In the next line n is given

Sample Case 0
Sample Input

STDIN      Function
-----      --------
3       →  list1[] Size n1 = 3
1       →  list1[] = [ 1, 2, 6 ]
2
6
3       →  list2[] Size n2 = 3
text1   →  list2[] = [text1 , text2, text3 ]
text2
text3
word    →  string1 = word
3       →  n = 3

Sample Output

(1, 2, 6, 'text1', 'text2', 'text3')
text2
((1, 2, 6), ('text1', 'text2', 'text3'))
2
('word', 'word', 'word')
6  

Answer: 

#!/bin/python3

import math
import os
import random
import re
import sys


#
# Complete the 'tuplefun' function below.
#
# The function accepts following parameters:
#  1. LIST list1
#  2. LIST list2
#  3. STRING string1
#  4. INTEGER n
#

def tuplefunction(list1, list2, string1, n):
    # Write your code here
    tuple1 = tuple(list1)
    tuple2 = tuple(list2)
    c = tuple1+tuple2
    print(c)
    print(c[4])
    nest = (tuple1,tuple2)
    print(nest)
    print(len(nest))
    rep = tuple((string1,)*n)
    print(rep)
    print(max(tuple1))

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 = input()
        qw2.append(qw1_item)
        
    str1 = input()

    n = int(input().strip())

    tuplefunction(qw1,qw2,str1, n)

Click on image to open:


No comments