Skip to main content

Python Hacker Rank question and answer -- String 01

Python - String Operations - 1

 

Define a function `stringoperation` that takes four parameters, namely 'fn', 'ln', 'para' as a string, and 'number' as an integer. The function definition code stub is given in the editor. Generate the print statements based on the condition given as follows:

  • First print: concat 'fn', 'number' times the number of newline character and 'ln'
  • Second print: 'concat' 'fn' , tab space and 'ln'
  • Third print: Prints the 'number' multiples of the string 'fn'
  • Fourth print: Prints the 'para' string using f-string as follows:
    • The sentence is {para}

 

Input Format for Custom Testing

 

The input from stdin will be processed as follows and passed to the function:

 

  • The first line contains a string 'fn', the first name.
  • The second line contains a string 'ln', the last name.
  • The third line contains a string which contains a paragraph of words.
  • The fourth line contains an integer 'number'.

 

Sample Case 0

Sample Input

STDIN              Function
-----              --------
Abigail           →  fn = Abigail
joseph            →  ln = joseph
I am a creator    →  para = I am a creator
3                 →  number = 3

 

 

Sample Output

Abigail


joseph
Abigail	joseph
AbigailAbigailAbigail
The sentence is I am a creator

 

Answer :

#!/bin/python3

import math
import os
import random
import re
import sys



#
# Complete the 'strng' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. STRING fn
#  2. STRING ln
#  3. STRING para
#  4. INTEGER number
#

def stringoperation(fn, ln, para, number):
    # Write your code here
    print(fn +'\n'*number+ln)
    print(fn+'\t'+ln)
    print(fn*number)
    print(f'The sentence is {para}')

if __name__ == '__main__':

    fn = input()

    ln = input()

    para = input()

    no = int(input().strip())

    stringoperation(fn, ln, para, no)

Comments

Post a Comment

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

Python Question And Answers  Question : Python - Get Additional Info   Define a function called ` docstring `  which takes one parameter .  The  parameter 'function-name' is a  Python  built-in function  name.   The function definition code stub is given in the editor. Generate a  Docstring of the input function  based on the condition given below:   The Docstring must give the maximum information about the function.     Constraints Input must be a string. Input Format Format for Custom Testing   In the input, any python built-in function is   given as a string.     Sample Case 0   Sample Input STDIN      Function -----      -------- print   → functionname = print Sample Output: Help on built-in function print in module builtins: print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream...