HakerrankSolutions

Task 1:In Python, a string can be split on a delimiter.

Example:

a = "this is a string" a = a.split(" ") # a is converted to a list of strings. print a ['this', 'is', 'a', 'string'] Joining a string is simple:

a = "-".join(a) print a this-is-a-string Task You are given a string. Split the string on a " " (space) delimiter and join using a - hyphen.

Function Description

Complete the split_and_join function in the editor below.

split_and_join has the following parameters:

string line: a string of space-separated words Returns

string: the resulting string Input Format The one line contains a string consisting of space separated words.

Task 2:

We have seen that lists are mutable (they can be changed), and tuples are immutable (they cannot be changed).

Let's try to understand this with an example.

You are given an immutable string, and you want to make changes to it.

Example

string = "abracadabra" You can access an index by:

print string[5] a What if you would like to assign a value?

string[5] = 'k' Traceback (most recent call last): File "", line 1, in TypeError: 'str' object does not support item assignment How would you approach this?

One solution is to convert the string to a list and then change the value. Example

string = "abracadabra" l = list(string) l[5] = 'k' string = ''.join(l) print string abrackdabra Another approach is to slice the string and join it back. Example

string = string[:5] + "k" + string[6:] print string abrackdabra Task Read a given string, change the character at a given index and then print the modified string. Function Description

Complete the mutate_string function in the editor below.

mutate_string has the following parameters:

string string: the string to change int position: the index to insert the character at string character: the character to insert Returns

string: the altered string Input Format

The first line contains a string, . The next line contains an integer , the index location and a string , separated by a space.

Sample Input

STDIN Function


abracadabra s = 'abracadabra' 5 k position = 5, character = 'k' Sample Output

abrackdabra

Sample Input

this is a string
Sample Output

this-is-a-string

Task 3:

You are given the firstname and lastname of a person on two different lines. Your task is to read them and print the following:

Hello firstname lastname! You just delved into python.

Function Description

Complete the print_full_name function in the editor below.

print_full_name has the following parameters:

string first: the first name string last: the last name Prints

string: 'Hello ! You just delved into python' where and are replaced with and . Input Format

The first line contains the first name, and the second line contains the last name.

Constraints

The length of the first and last names are each ≤ .

Sample Input 0

Ross Taylor Sample Output 0

Hello Ross Taylor! You just delved into python. Explanation 0

The input read by the program is stored as a string data type. A string is a collection of characters.