CS1011 Unit 5 Sample Solution

 Sample Solution:


Question 1


def any_lowercase1(s):

    for c in s:

        if c.islower():

            return True

        else:

            return False

This function only checks the first letter.  In this case, once it checks the first character in the string whether this first character evalues to true or false in the .islower() method, it will come across a 'return' statement in either branch, so it will never make it to the second character in the string.  As an example, if I pass in an argument with string 'Ab', the function will evaluate the first character 'A' as not lowercase, and return false, and never get to evaluate the second character 'b' where it would have returned true if it had evaluated this as intended.




So in the below example we see that passing in string 'Ab' will show false, since it never got to check the second lowercase character 'b'. 


>>> any_lowercase1('Ab')

False



Question 2


def any_lowercase2(s):

    for c in s:

        if 'c'.islower():

            return 'True'

        else:

            return 'False'

In this case, we are invoking the islower() method NOT on the argument string s that was passed in to the functino lowercase2, but on a static string 'c'.  As the char 'c' is already lowercase, all we're doing by invoking the is lower() method on 'c' is validating this, so no matter what type of string we pass in to the any_lowercase2 function, it will always return 'True'. 




In this example, I've passed in the entire upper case alphabet, but it still returns true.


>>> any_lowercase2('ABCDEFGHIJKLMNOPRSTUVWXYZ')

'True'



Question 3


def any_lowercase3(s):

    for c in s:

        flag = c.islower()

    return flag

This function is essentially is the opposite case of example number 1.  This function essentially only validates the last character of the string is lowecase or not.  This happens as the function iterates through the string characters, it simply reassigns the  flag variable within the for loop with the last instance (character within the s argument) was a lower case or not.   And once a new value is assigned to flag variable, we lose all information about the previous state (Downey, 2015).  




In the example below, if I pass in a string with preceding with all lower characters, but if the last character is a capital letter, it will return false, as the last character was a not a lowercase.


>>> any_lowercase3('aaaaaX')

False



Question 4


def any_lowercase4(s):

    flag = False

    for c in s:

        flag = flag or c.islower()

    return flag

This function will return work as intended, and will return any strings that include at least one lower case letter.  It accomplishes this by assuming that the default state is false (flag = false), but as it iterates through the string, if one character has a lowercase (after being evaluted with .islower() method, it flag variable will always become true until the very end of the iteration.  This happens because the logical operator "or" guarantees this because until the end of for loop iteration of s, it will always evaluate as true.  The possible evaluations from the time that .islower() first becomes a true statement be <false or true> or <true or true> or <true or false> which all evaluates to true.




Question 5


def any_lowercase5(s):

    for c in s:

        if not c.islower():

            return False

    return True

This function is almost the reverse of the original intended function.  In this case, the function will traverse through the entire string s UNTIL it sees any characters that are not lowercase (e.g. capital letters, numbers, special characters) and once it finds that, it will return a False.


Hence in this case, even if we pass in a string that's full of lowercase letters, but just one character is not a lowercase, it will return a false.  For example:


>>> any_lowercase5('aaaaaaaaXa')

False


References


Downey, Allen. (2015). Think Python: How to Think Like a Computer Scientist. Needham, MA: Green Tea Press

Comments

Popular posts from this blog

CS1011 Unit 6 Sample Solution

CS1011 Unit 3 Sample Solution