Posts

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'

CS1011 Unit 4 Sample Solution

 A Sample solution: First, let me describe the definitions of "precondition" and "postcondition" based on Downy's textbook (2015). A precondition is a "requirement that should be satisfied by the caller before a function starts".  A postcondition is a "requirement that should be satisfied by the function before it ends". Listed in section 6.9 of the textbook ("Debugging"), the three possibilities to be considered if the function does not work are the following (Downy, 2015). There is something wrong with the arguments the function is getting; a precondition is violated. There is something wrong with the function; a postcondition is violated.  There is something wrong with the return value or the way it is being used. Sample 1. There is something wrong with the arguments the function is getting; a precondition is violated. In this sample, the bug occurs because a number containing a decimal point is given, even though it should origi

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_lowercase

CS1011 Unit 6 Sample Solution

 Sample Solution Describe the difference between objects and values using the terms “equivalent” and “identical”. Illustrate the difference using your own examples with Python lists and the “is” operator.   Objects are something that variables can refer to, and within this object, the object has a type and a value (Downey, 2015).  This is not very intuitive, but using the code below, I will attempt to explain the difference.   numObj_1 = 20 numObj_2 = 20 listObj_1 = [20] listObj_2 = [20] print('line1: value numObj_1 == numObj_2:',numObj_1 == numObj_2) #returns true print('line2: object numObj_1 is numObj_2:',numObj_1 is numObj_2) #returns true print() print('line3: value listObj_1 == listObj_2:',listObj_1 == listObj_2) #returns true print('line4: object listObj_1 is listObj_2:',listObj_1 is listObj_2) #returns false First, the output of the code above: line1: value numObj_1 == numObj_2: True line2: object numObj_1 is numObj_2: True line3: value listObj_1