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.

Respuesta :

Answer:

Explanation:

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)

Function are collections of named code blocks, that are executed when called or evoked.

The setKthDigit function written in Python, where comments are used to explain each line is as follows

#This defines the function

def setKthDigit(n, k, d):

   #If the number of digits in n is greater than k

   if len(str(n)) > k:

       #This splits the number into a list

       myWord = list(str(n))

       #This reverses the list

       myWord.reverse()

       #This updates the kth digit

       myWord[k] = str(d)

       #This reverses the list

       myWord.reverse()

       #This initializes string newStr

       newStr = ''

       #This following loop creates the the number to return, as string

       for i in myWord:

           newStr+=i

   #If otherwise

   else:

       #This initializes string newStr

       newStr = ''

       #This following loop creates the the number to return, as string

       for i in range(1+k-len(str(n))):

           newStr+=str(d)

       newStr+=str(n)

   #This returns the updated number, as an integer

   return int(newStr)

Read more about functions at:

brainly.com/question/19360941