In: Computer Science
HOW TO ANSWER IN PYTHON :
PROBLEM: Given a positive integer (call it N), a position in
that integer (call it P), and a transition integer (call it D).
Transform N as follows:
If the Pth digit of N from the right is from 0 to 4, add D to
it. Replace the Pth digit by the units digit of the sum. Then,
replace all digits to the right of the Pth digit by 0.
If the Pth digit of N from the right is from 5 to 9, subtract
D from it. Replace the Pth digit by the leftmost digit of the
absolute value of the difference. Then, replace all digits to the
right of the Pth digit by 0.
nd
Example 1: N = 7145032, P = 2, D = 8 . The 2 digit from the right
is 3; add 8 to it (3+8=11),
and replace the 3 with 1 to get 7145012. Replace the digits to the
right by 0s to get 7145010. rd
Example 2: N = 1540670, P = 3, D = 54 . The 3 digit from the right
is 6; the absolute value of 6-54 is 48; replace with the 4 to get
1540470. Replace the digits to the right with 0s to get
1540400.
INPUT: There will be 5 sets of data. Each set contains 3 positive
integers: N, P, and D. N will be less than 1015 ; P and D will
be valid inputs. No input will cause an output to have a leading
digit of 0.
OUTPUT: Print the transformed number. The printed number may not
have any spaces between the digits.
ANSWER:
N=input("Enter a positive integer(N):")
P=int(input("Enter position(P):"))
D=int(input("Enter transition integer(D):"))
int_N=int(N)
ele=int(N[len(N)-P]) #for element at postion P form right
if(ele>=0 and ele<=4):
D=D+ele #if element <4 D is added
D=str(D)
t_num=N[0:len(N)-P]+D[len(D)-1] #adding number till position with
unit digit from D added with element
n_z=len(N)-len(t_num)
t_num=t_num+'0'*n_z #adding zeros to right of number
else:
D=abs(D-ele) #if element >4 D is subtracted
D=str(D)
t_num=N[0:len(N)-P]+D[0] #adding left most digit from D after
subtracting from element
n_z=len(N)-len(t_num)
t_num=t_num+'0'*n_z
print("Transformed number is:",t_num)
#for indentation please verify the below pic
OUTPUT: