Ask Question
25 November, 02:54

Write the function setKthDigit (n, k, d) that takes three integers - - n, k, and d - - where n is a possibly-negative int, k is a non-negative int, and d is a non-negative single digit (between 0 and 9 inclusive). This function returns the number n with the kth digit replaced with d. Counting starts at 0 and goes right-to-left, so the 0th digit is the rightmost digit.

+1
Answers (1)
  1. 25 November, 06:37
    0
    Let's do this in Python, first we need to convert the number into string in order to break it into a list of character, then we can replace the kth character with d. Finally we join all characters together, convert it to integer then output it

    def setKthDigit (n, k, d):

    n_string = str (n)

    d_char = str (d)

    n_char = [c for c in n_string]

    n_char[k] = d_char

    new_n_string = ''. join (n_char)

    return int (new_n_string)
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write the function setKthDigit (n, k, d) that takes three integers - - n, k, and d - - where n is a possibly-negative int, k is a ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers