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

चंद्रयान-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 ...
 THE  An avid hiker keeps meticulous records of their hikes. During the last hike that took exactly     steps, for every step it was noted if it was an   uphill ,   , or a   downhill ,     step. Hikes always start and end at sea level, and each step up or down represents a     unit change in altitude. We define the following terms: A  mountain  is a sequence of consecutive steps  above  sea level, starting with a step  up  from sea level and ending with a step  down  to sea level. A  valley  is a sequence of consecutive steps  below  sea level, starting with a step  down  from sea level and ending with a step  up  to sea level. Given the sequence of  up  and  down  steps during a hike, find and print the number of  valleys  walked through. Example   The hiker first enters a valley   units deep. Then they climb...