CS1011 Unit 3 Sample Solution

 A sample solution:


Describe the difference between a chained conditional and a nested conditional. Give your own example of each. 

Both chained and nested conditionals can be used to test when there are two or more possibilities to test in a programming logic.


The first of these – chained conditional – is in a condition where exactly one branches (one of the alternative logic paths) will run within the condition body (Downey, 2015).  In the coding example below, if I pass in an gender and an age argument in to the genderAndStatus1 function, only one of the five possible branches will be evaluated based on what I pass in for gender and age arguments, and each branch condition is checked one by one, until one of the branch evaluations becomes true.


>>> def genderAndStatus1(gender, age):

if gender == 'male' and age >=18:

print("Male Adult")

elif gender == 'male' and age <18:

print("Male Minor")

elif gender == 'female' and age >= 18:

print("Female Adult")

elif gender == 'female' and age < 18:

print("Female Minor")

else:

print("Other Status")



Conversely, a nested conditional can be created to check the gender and status like the above, but in this case of nested conditional statements, the one conditional statement can reside within another conditional statement.  The example function genderAndStatus2 below accomplishes the same logic as above, but with the initial condition has 3 branches (checking age), then within the two of those branches (where age is a valid number) has three additional branches of its own to check the gender.


>>> def genderAndStatus2(gender, age):

if age >= 18:

if gender == 'male':

print("Male Adult")

elif gender == 'female':

print("Female Adult")

else:

print("Other Status")

elif age <18:

if gender == 'male':

print("Male Minor")

elif gender == 'female':

print("Female Minor")

else:

print("Other Status")

else:

print("Other Status")



Calling the two examples with same arguments confirms that they are accomplishing the same task, but in a different manner (chained vs nested).


Sample outputs for genderAndStatus1:


>>> genderAndStatus1('male', 19)

Male Adult

>>> genderAndStatus1('male', 17)

Male Minor

>>> genderAndStatus1('female', 30)

Female Adult

>>> genderAndStatus1('female', 15)

Female Minor

Sample outputs for genderAndStatus2:


>>> genderAndStatus2('male', 19)

Male Adult

>>> genderAndStatus2('male', 17)

Male Minor

>>> genderAndStatus2('female', 30)

Female Adult

>>> genderAndStatus2('female', 15)

Female Minor


Deeply nested conditionals can become difficult to read. Describe a strategy for avoiding nested conditionals.

Multiple nested conditionals can be difficult to follow if the programmer must read through many iterations of branches to follow the logic.  One can combine multiple nested conditions into single conditional. 


One short example code would be as per below.  The two functions below check the same thing, which is to check to see if the argument passed into the function is a whole number (if it’s an integer >=0).


The numCheckParam1 function is written using a nested conditional, where it first checks to see if the passed in argument is an integer, then if that’s true, a second condition checks to see if the number is greater than or equal to zero.


def numCheckParam1(param):

    if param % 1 == 0: #if true this is an integer (digit after decimal point is zero)

        if param >= 0:

            print("This is a whole number (integer >=0)")

The numCheckParam2 function accomplishes the same task, but does this in a single condition.


def numCheckParam2(param):

    if param %1 == 0 and param >= 0:

        print("This is a whole number (integer >=0)")

Calling the two functions with the same sample argument show that they have the same output.



>>> numCheckParam1(5)

This is a whole number (integer >=0)

>>> numCheckParam2(5)

This is a whole number (integer >=0)


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 5 Sample Solution

CS1011 Unit 6 Sample Solution