Question

In: Computer Science

HOW TO ANSWER IN PYTHON : PROBLEM: Given a positive integer (call it ​N), a position​...

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.

Solutions

Expert Solution

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:


Related Solutions

Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
Given a positive integer k and an array A[1..n] that contains the quiz scores of n...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n students in ascending order, design a divide and conquer algorithm to efficiently count the number of students that have quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. Let group i be the set of students with quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. The counting result should be stored in G[1..k],...
In Python, write a program to get a positive integer n from keyboard first (your program...
In Python, write a program to get a positive integer n from keyboard first (your program must be able to check the validation of n being positive), and then get n integers from keyboard and store these n integers in a list. Do some processing, and print out average of the elements in the list at end of your program. For example, suppose user enters 3 first (n = 3), which means we get another three integers from the user....
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer value n and returns an approximation of e as (1 + 1/n)^n I am not even sure what the question is asking me to do but I think it is asking me to code that function. Does anyone know how to do this?
For a given positive integer n, output the first n primes. Example: n=3, output: 2,3,5; n=7,...
For a given positive integer n, output the first n primes. Example: n=3, output: 2,3,5; n=7, output: 2,3,5,7,11,13,17. In Java please
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether we can use numbers from c[0...n − 1] to make a sum of v, allowing any particular number in the list to be used multiple times. Or, mathematically, this means that there exists non-negative integer coefficients, x0, x1, ..., xn−1, such that v = x0c[0] + x1c[1] + ...xn−1c[n − 1]. For example, given c[0...3] = {2, 4, 6, 10}, and v = 17,...
Let n be a positive integer. Prove that if n is composite, then n has a...
Let n be a positive integer. Prove that if n is composite, then n has a prime factor less than or equal to sqrt(n) . (Hint: first show that n has a factor less than or equal to sqrt(n) )
Statement: For a given integer N, print all the squares of positive integers where the square...
Statement: For a given integer N, print all the squares of positive integers where the square is less than or equal to N, in ascending order. Programming Tasks: Prompt the user to input the value of N Output to the screen all squares of positive integers <= N Tests: Item Test 1 Test 2 Test 3 Inputs: 50 9 100 Outputs: 1 4 9 16 25 36 49 1 4 9 1 4 9 16 25 36 49 64 81...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
Prove or disprove that 3|(n 3 − n) for every positive integer n.
Prove or disprove that 3|(n 3 − n) for every positive integer n.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT