In: Computer Science
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()
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: