Ask 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 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. 9 February, 05:46
    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?
Find 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 the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers