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

60.1K

Verified Solution

Question

Programming

In python,

Part 1: Estimate the value of e.

e is defined as  as n goes to infinity. So the largerthe 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 towithin some defined error range, while having a limit on how manyiterations it will attempt.

Part 2: Palindrome detection

A palindrome is a string that is the same forwards andbackwards.

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

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

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

Beware! There are many solutions online. Don't fall prey tolooking this one up. It's not as hard as it looks. Draw it out onpaper first and think through how you index the string from bothends.

skeleton of code:

#
# Estimate the value of e.
#
# Write a routine that uses a while loop to estimate e.
#
# Arguments:
#       delta: How close the estimatehas to come
#      n:      Maximum number of attempts
#
# Algorithm:
#       Compare math.e to your valueof e and if the difference is
#       greater than delta, incrementcount and make a new estimate. A
#       Assuming the count variableis \"i\", the formula for e is:
#           e =(1 + 1/i)**i
#
#       Stop when the differencebetween math.e and your value is less
#       than delta or the countvariable reaches n.
#
# Output:
#       Return a tuple of the deltaand 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 gradeevaluation!
#
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]) # Prints0.1371, 10
    r = estimateE(0.01, 100)
    print(\"{0:6.4f}\".format(r[0]), r[1]) # Prints0.0136, 100
    r = estimateE(0.01, 1000)
    print(\"{0:6.4f}\".format(r[0]), r[1]) # Prints0.0100, 136
  
#
# Only run testcases if this is main
#
if __name__ == \"__main__\":
    main()

Answer & Explanation Solved by verified expert
4.0 Ratings (472 Votes)
In this program we have two tasks1 find the approximation for e2 check whether a string is palindrome or notFirst lets write the function    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students