Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# **************************
# CS1101 - Discussion Unit 5
# **************************
# **************************
# Instructions
# 1. Check whether its argument has any lowercase letters
# 2. Describe what it actually does when called with a string argument and if it does not correctly check for lowercase letters.
# 4. Give an example argument that produces incorrect results. (SEE BOTTOM for 3 calls to all functions!)
# 5. Describe why the result is incorrect. (SEE inline comments)
# **************************
# **************************
# Decides on the first letter and breaks the for..in loop once it's True or False
# Roger == False, because of R,
# oger == True, because of o
# ogeR == True, because of o; doesn't keep looking.
def any_lowercase1(s):
for c in s:
if c.islower():
return True
else:
return False
# **************************
# 'c' is a string. It is passing a string not c the variable, which means it's always true, because 'c' is lowercase.
def any_lowercase2(s):
for c in s:
# Lowercase is true every time.
if 'c'.islower():
return 'True'
else:
return 'False'
# **************************
# A. 'flag' (inner variable) is declared inside the scope of the for..in loop and it's scope doesn't extend to the parent scope of the function.
# B. name 'flag' (outer variable) is not defined. It's irrelevant.
def any_lowercase3(s):
for c in s:
flag = c.islower()
return flag
# **************************
# Once the `flag` is found to be True it can't be reassigned. First 'truth' means all are True.
def any_lowercase4(s):
flag = False
for c in s:
flag = flag or c.islower()
return flag
# **************************
# Will check if string has an uppercase and when True, then break the for..in loop with a False.
def any_lowercase5(s):
for c in s:
# Iterates continuously inside this loop until the return False on any single uppercase
if not c.islower():
return False
# All iterations succeed without a return and then the function exits the for..in and returns True.
return True
# **************************
# **************************
# **************************
# **************************
# Program manager
# Runs all the functions with the test strings in series and prints them.
# **************************
def test_lowercase_list(funcs):
#i when, func what is called, func('xxx') results printed
for i, func in enumerate(funcs): # (Hunner, T., 2016)
# prints index number with string test, function and function call result.
print(f'{i}** Roger:', func, func('Roger'))
print(f'{i}** oger:', func, func('ogerc'))
print(f'{i}** rogeR:', func, func('rogeR'))
print('\n')
# **************************
# list of functions above
list_funcs = [any_lowercase1, any_lowercase2, any_lowercase3, any_lowercase4, any_lowercase5]
# This function iterates over all the functions from list_funcs
test_lowercase_list(list_funcs)
# **************************
# **************************
# **************************
# **************************
# OUTPUT
# **************************
# 0** Roger: <function any_lowercase1 at 0x7f983f5b91f0> False
# 0** oger: <function any_lowercase1 at 0x7f983f5b91f0> True
# 0** rogeR: <function any_lowercase1 at 0x7f983f5b91f0> True
# 1** Roger: <function any_lowercase2 at 0x7f9840921820> True
# 1** oger: <function any_lowercase2 at 0x7f9840921820> True
# 1** rogeR: <function any_lowercase2 at 0x7f9840921820> True
# 2** Roger: <function any_lowercase3 at 0x7f98409218b0> True
# 2** oger: <function any_lowercase3 at 0x7f98409218b0> True
# 2** rogeR: <function any_lowercase3 at 0x7f98409218b0> False
# 3** Roger: <function any_lowercase4 at 0x7f9840921940> True
# 3** oger: <function any_lowercase4 at 0x7f9840921940> True
# 3** rogeR: <function any_lowercase4 at 0x7f9840921940> True
# 4** Roger: <function any_lowercase5 at 0x7f98409219d0> False
# 4** oger: <function any_lowercase5 at 0x7f98409219d0> True
# 4** rogeR: <function any_lowercase5 at 0x7f98409219d0> False
# **************************
# Reference
# **************************
"""
Reference
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press.
Hunner, T. (2016). "How to loop with indexes in Python". Retrieved from https://treyhunner.com/2016/04/how-to-loop-with-indexes-in-python/
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment