Question

In: Computer Science

In python, Part 1: Estimate the value of e. e is defined as  as n goes to...

In python,

Part 1: Estimate the value of e.

e is defined as  as n goes to infinity. So the larger the value of n, the closer you should get to e.

math.e is defined as 2.718281828459045

You'll write a program that uses a while loop to estimate e to within some defined error range, while having a limit on how many iterations it will attempt.

Part 2: Palindrome detection

A palindrome is a string that is the same forwards and backwards.

Madam I'm Adam
Racecar
Was it a cat I saw?

There are many sophisticated ways of detecting palindromes. Some are surprisingly simple (s == s[::-1])

You will use a for loop to iterate over the contents of the string to determine if it is a palindrome.

Beware! There are many solutions online. Don't fall prey to looking this one up. It's not as hard as it looks. Draw it out on paper first and think through how you index the string from both ends.

skeleton of code:

#
# Estimate the value of e.
#
# Write a routine that uses a while loop to estimate e.
#
# Arguments:
#       delta: How close the estimate has to come
#       n:      Maximum number of attempts
#
# Algorithm:
#       Compare math.e to your value of e and if the difference is
#       greater than delta, increment count and make a new estimate. A
#       Assuming the count variable is "i", the formula for e is:
#           e = (1 + 1/i)**i
#
#       Stop when the difference between math.e and your value is less
#       than delta or the count variable reaches n.
#
# Output:
#       Return a tuple of the delta and the number of tries you made
#
import math
def estimateE(delta, n):
    pass

#
# Return true if s is a palindrome
#
# Look at the indices of a string s = "abcdcba":
#   0, 1, 2, 3, 4, 5, 6
# -7, -6, -5, -4, -3, -2, -1
#
# To be a palindrome, s[0] == s[-1], s[1] == s[-2], etc
#
# Return True if s is a palindrome
#
# Use ONLY a for loop, len, and slicing.
#
def palindrome(s):
    pass

#
# Don't change the lines below! They run the tests for grade evaluation!
#
def main():
    print(palindrome("abcdef"))
    print(palindrome("abcdcba"))
    print(palindrome("abba"))
    print(palindrome("abccba"))
    r = estimateE(0.01, 10)
    print("{0:6.4}".format(r[0]), r[1]) # Prints 0.1371, 10
    r = estimateE(0.01, 100)
    print("{0:6.4f}".format(r[0]), r[1]) # Prints 0.0136, 100
    r = estimateE(0.01, 1000)
    print("{0:6.4f}".format(r[0]), r[1]) # Prints 0.0100, 136
  
#
# Only run testcases if this is main
#
if __name__ == "__main__":
    main()

Solutions

Expert Solution

In this program, we have two tasks:

1.) find the approximation for e

2.) check whether a string is palindrome or not

First let's write the function estimateE()

This function must estimate the value to e upto an accuracy where the error is less than delta.

The formula for e is , e = (1+1/i)**i. The accuracy increases as i increases. But i must not be more then n

Then, let's write palindrome() function

This function will check if s is a palindrome or not.

To do this , loop over the first half of s, and check if each of the character are equal to the mirror indices in the second half. If any of the character is not equal to its mirror, then return false. Otherwise, return true

program:

import math
def estimateE(delta, n):
i = 1;
eApprox = (1+1/i)**i
delta_ = abs(eApprox-math.e)
while(delta_>delta and i<=n):
i += 1
eApprox = (1+1/i)**i
delta_ = abs(eApprox-math.e)
return (delta_,i)
def palindrome(s):
for i in range(len(s)//2+1):
if s[i] != s[len(s)-i-1]:
return False

return True
def main():
print(palindrome("abcdef"))
print(palindrome("abcdcba"))
print(palindrome("abba"))
print(palindrome("abccba"))
r = estimateE(0.01, 10)
print("{0:6.4}".format(r[0]), r[1]) # Prints 0.1371, 10
r = estimateE(0.01, 100)
print("{0:6.4f}".format(r[0]), r[1]) # Prints 0.0136, 100
r = estimateE(0.01, 1000)
print("{0:6.4f}".format(r[0]), r[1]) # Prints 0.0100, 136

if __name__ == "__main__":
main()

'

output:


Related Solutions

Two coding challenges this week! Part 1: Estimate the value of e. e is defined as  as...
Two coding challenges this week! Part 1: Estimate the value of e. e is defined as  as n goes to infinity. So the larger the value of n, the closer you should get to e. math.e is defined as 2.718281828459045 You'll write a program that uses a while loop to estimate e to within some defined error range, while having a limit on how many iterations it will attempt. Part 2: Palindrome detection A palindrome is a string that is the...
Previously, we defined a point estimate as a value of a statistic that estimates the value...
Previously, we defined a point estimate as a value of a statistic that estimates the value of a parameter. For example, a sample mean would estimate a population mean. Do you think most people trust point estimates for things that need to be measured? Why or why not? Please cite examples for your reasoning. Looking back over previous examples, how might adding confidence intervals help managers accept the results?
IN PYTHON Generate valid keys (e, n) for the RSA cryptosystem.
IN PYTHON Generate valid keys (e, n) for the RSA cryptosystem.
Part 1: Encrypt the message CINEMA using RSA with n = 17 * 11 and e...
Part 1: Encrypt the message CINEMA using RSA with n = 17 * 11 and e = 13, use A =10...Z = 35, work in blocks of one letter each. Part 2: Decrypt the message 088-164-051-164-021-074 using the same parameters from part 1.
Obtain an estimate for the value of e by approximating the solution of the following initial...
Obtain an estimate for the value of e by approximating the solution of the following initial value problem at t = 1 y'=y     y0=1     Use a step size of 0.25. Apply Euler’s Method, the Midpoint Method, and the Improved Euler’s Method in order to approximate the solution to this problem. Calculate the absolute relative true percent error using seven significant figures. Calculate this error for each method and only for the last iteration.
Part 1                                          &n
Part 1                                                                                                                            Technology Inc. predicted 2017 variable and fixed costs are as follows:                                                                                                                                                                                                       Variable costs    Fixed costs                                                                                                    Manufacturing                                 480,000               315900                                                                                            Selling and Administrative             216,000               60500                                                                                              Total                                                   696,000               376,400                                                                                                                                                                                                                                 Technology Inc. produces a wide variety of computer interface devices. Per unit                                                                                                                                        manufacturing cost information about one of these products, a high-capacity flash drive is as follows:                                                                                                                                                                                                                                                                              Direct material                                                $10                                                                                                                ...
3. The Hofstadter Conway sequence is defined by a(1)=a(2)=1 and (for n>2 by) a(n)=a(a(n-1))+a(n-a(n-1)). Write a...
3. The Hofstadter Conway sequence is defined by a(1)=a(2)=1 and (for n>2 by) a(n)=a(a(n-1))+a(n-a(n-1)). Write a function to quickly compute this sequence. >>> [hc(i) for i in range(1,20)] [1, 1, 2, 2, 3, 4, 4, 4, 5, 6, 7, 7, 8, 8, 8, 8, 9, 10, 11]
For f: N x N -> N defined by f(m,n) = 2m-1(2n-1) a) Prove: f is...
For f: N x N -> N defined by f(m,n) = 2m-1(2n-1) a) Prove: f is 1-to-1 b) Prove: f is onto c) Prove {1, 2} x N is countable
2. The Fibonacci sequence is defined as f(n) = f(n - 1) + f(n - 2)...
2. The Fibonacci sequence is defined as f(n) = f(n - 1) + f(n - 2) with f(0) = 0 and f(1) = 1. Find f(54) by a program or maually. Note that this number must be positive and f(53) = 53.......73 (starting with 53 and ending with 73). I must admit that my three machines including a desktop are unable to find f(54) and they quit during computation. The answer is f(54) = 86267571272 */ The Java code: public...
Use Java for the following; Part 1 n!= n * (n –1)* (n–2)* ...* 3 *...
Use Java for the following; Part 1 n!= n * (n –1)* (n–2)* ...* 3 * 2 * 1 For example, 5! = 5 * 4 * 3 * 2 * 1 = 120 Write a function called factorial that takes as input an integer. Your function should verify that the input is positive (i.e. it is greater than 0). Then, it should compute the value of the factorial using a for loop and return the value. In main, display...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT