Mutations| Hacker Rank Solution in Python


Problem:

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

Solution:

def mutate_string(string, position, character):
    l1=list(string)
    l1[position]=character
    stri = ''.join(l1)
    return stri

if __name__ == '__main__':
    s = raw_input()
    i, c = raw_input().split()
    s_new = mutate_string(s, int(i), c)
    print s_new

Comments

Popular posts from this blog

Write a Function | Hacker Rank Solution in Python

Diagonal Difference | Hacker Rank Solution in C

Print Function | Hacker Rank Solution in Python