Skip to main content

 Hackerank quetion...

We have see  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 its.

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 "<stdin>", line 1, in <module>
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.

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

Output Format
Using any of the methods explained above, replace the character at index  with character .

Sample Input

abracadabra
5 k

Sample Output

abrackdabra

..phython..3

def mulatble(string, position, character):
    l = list(string)
    l[position] = character;
    string = ''.join(l);
    return string
if __name__=='__main__':
    string = input()
    position, character = input().split()
    new = mulatble(string, int(position), character)
    print(new)


sd

Comments

Popular posts from this blog

hackerrank

AAlex works at a clothing store. There is a large pile of socks that must be paired by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. For example, there are   socks with colors  . There is one pair of color   and one of color  . There are three odd socks left, one of each color. The number of pairs is  . .AFunction Description Complete the  sockMerchant  function in the editor below. It must return an integer representing the number of matching pairs of socks that are available. asockMerchant has the following parameter(s): n : the number of socks in the pile ar : the colors of each sock .Input Format The first line contains an integer  , the number of socks represented in  . The second line contains   space-separated integers describing the colors   of the socks in the pile. Constraints  where  .Output Fo...

चंद्रयान-2,

Chandrayan-2: चांद के और करीब पहुंचा चंद्रयान-2, जानिए-           पता लगाएगा गुरुवार की रात उपग्रह को पृथ्वी की अगली कक्षा में प्रवेश कराया गया। 29 जुलाई को तीसरी बार चंद्रयान-2 की कक्षा को बदला जाएगा।   बेंगलुरु, प्रेट्र।  Chandrayaan-2  उपग्रह गुरुवार की रात सफलतापूर्वक चंद्रमा की तरफ और एक कदम बढ़ गया। अंतरिक्षयान पर लगे प्रणोदन प्रणाली के जरिए गुरुवार-शुक्रवार की रात एक बजकर आठ मिनट पर दूसरी बार उपग्रह की कक्षा को बदलते हुए उसे सफलतापूर्वक पृथ्वी की अगली कक्षा में प्रवेश कराया गया। भारतीय अंतरिक्ष अनुसंधान संगठन (इसरो) ने बताया कि कक्षा बदलने की यह प्रक्रिया लगभग 15 मिनट तक चली। की ऊंचाई पर जीएसएलवी-मैक 3 रॉकेट से अलग हो गया था और चक्कर लगा रहा था। बुधवार 24 जुलाई को पहली बार उपग्रह को पृथ्वी की अगली कक्षा में भेजा गया था। शुक्रवार को दूसरी बार उसके कक्षा में बदलाव किया गया। छह अगस्त तक अभी और दो बार चंद्रयान-2 की कक्षा (ऑर्बिट) को बदला जाएगा। चंद्रयान-2 पृथ्वी की कक्षा से 14 अगस्त को चंद...
  following version of Bubble Sort: for ( int i = 0 ; i < n ; i ++) { for ( int j = 0 ; j < n - 1 ; j ++) { // Swap adjacent elements if they are in decreasing order if ( a [ j ] > a [ j + 1 ]) { swap ( a [ j ], a [ j + 1 ]); } } } Given an array of integers, sort the array in ascending order using the  Bubble Sort  algorithm above. Once sorted, print the following three lines: Array is sorted in numSwaps swaps. , where   is the number of swaps that took place. First Element: firstElement , where   is the  first  element in the sorted array. Last Element: lastElement , where   is the  last  element in the sorted array. Hint:  To complete this challenge, you must add a variable that keeps a running tally of  all  swaps that occur during execution. Example swap a 0 [6,4,1] 1 [4,6,1] 2 ...