Question: Using Python 1. A substitution cipher for the digits 0, 1, 2, 3,..., 9 substitutes each digit in 0, 1, 2, 3,..., 9 with another
Using Python
1. A substitution cipher for the digits 0, 1, 2, 3,..., 9 substitutes each digit in 0, 1, 2, 3,..., 9 with another digit in 0, 1, 2, 3,..., 9. It can be represented as a 10-digit string specifying how each digit in 0, 1, 2, 3,..., 9 is substituted. For example, the 10-digit string '3941068257' specifies a substitution cipher in which digit 0 is substituted with digit 3, 1 with 9, 2 with 4, and so on. To encrypt a nonnegative integer, substitute each of its digits with the digit specified by the encryption key. Implement function cipher() that takes as input a 10-digit string key and a digit string (i.e., the clear text to be encrypted) and returns the encryption of the clear text. >>> encrypt('3941068257', '132') '914' >>> encrypt('3941068257', '111') '999'
2. The Heron method is a method the ancient Greeks used to compute the square root of a number n. The method generates a sequence of numbers that represent better and better approximations for n. The first number in the sequence is an arbitrary guess; every other number in the sequence is obtained from the previous number prev using the formula 1 2 (prev + n prev) Write function heron() that takes as input two numbers: n and error. The function should start with an initial guess of 1.0 for n and then repeatedly generate better approximations until the difference (more precisely, the absolute value of the difference) between successive approximations is at most error. >>> heron(4.0, 0.5) 2.05 >>> heron(4.0, 0.1) 2.000609756097561
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
