Basic Python Syntax Week3

docx

School

Grand Rapids Community College *

*We aren’t endorsed by this school

Course

266

Subject

Computer Science

Date

Dec 6, 2023

Type

docx

Pages

4

Uploaded by ConstableWildcatMaster401

Report
Congratulations! You passed! Grade received 71.42% Latest Submission Grade 71.43% To pass 70% or higher Go to next item 1. Question 1 Which of the following are Boolean values in Python? 1 / 1 point "True" "True" True True Correct Correct. 0 0 False False Correct Correct. 2. Question 2 Consider the Boolean expression not (p or not q) not (p or not q) . Give the four following values in order, separated only by spaces: the value of the expression when p p is True True , and q q is True True , the value of the expression when p p is True True , and q q is False False , the value of the expression when p p is False False , and q q is True True , the value of the expression when p p is False False , and q q is False False , Remember, each of the four results you provide should be True True or False False with the proper capitalization. 1 / 1 point False False True False Correct 3. Question 3 Given the following initialization: 1 2 bool1 = True bool2 = False Which of the expressions below evaluate to True True ? 1 / 1 point bool1 != bool2 bool1 != bool2 Correct
bool1 == bool2 bool1 == bool2 not (bool1 == bool2) not (bool1 == bool2) Correct not bool1 not bool1 4. Question 4 Two expressions are logically equivalent if they have the same value for all possible values of the variables that comprise the expression. Given two numbers num1 num1 and num2 num2 , which one of the expressions below is logically equivalent to the following arithmetic comparison: 1 num1 >= num2 0 / 1 point 1 not (num1 < num2) 1 not (num1 <= num2) 1 num2 < num1 1 (num1 > num2) and (num1 != num2) Incorrect Incorrect. What if num1 == num2 num1 == num2 ? 5. Question 5 An if if statement can have how many else else parts? 0 / 1 point Unlimited, i.e., 0 or more 1 0 Incorrect Incorrect. Note that there are several variants of this question so read the question carefully on each attempt. 6. Question 6 In Python, conditional statements may be nested. Consider the following function that takes two Boolean values as input and returns a Boolean value. 1 2 3 4 5 6 7 8
9 10 11 12 13 14 def nand(bool1, bool2): """ Take two Boolean values bool1 and bool2 and return the specified Boolean values """ if bool1: if bool2: return False else : return True else : return True Which Boolean expression below is logically equivalent to the function call nand(bool1, bool2) nand(bool1, bool2) where bool1 bool1 and bool2 bool2 are Boolean variables? 1 / 1 point not (bool1 or bool2) not (bool1 or bool2) (bool1 or bool2) (bool1 or bool2) (bool1 and bool2) (bool1 and bool2) not (bool1 and bool2) not (bool1 and bool2) Correct Correct. The function name nand nand should have been a give away since it is short for "not and". 7. Question 7 The Collatz conjecture is an example of a simple computational process whose behavior is so unpredictable that the world's best mathematicians still don't understand it. Consider the simple function ( ) � � f ( n ) (as defined in the Wikipedia page above) that takes an integer n and divides it by two if n is even and multiplies n by 33 and then adds one to the result if n is odd. The conjecture involves studying the value of expressions of the form ( ( (... ( ( ))))) � � � � � � f ( f ( f (... f ( f ( n ))))) as the number of calls to the function f increases. The conjecture is that, for any non-negative integer n , repeated application of f to n yields a sequence of integers that always includes 11 . Your task for this question is to implement the Collatz function f in Python. The key to your implementation is to build a test that determines whether n is even or odd by checking whether the remainder when n is divided by 22 is either zero or one. Hint : You can compute this remainder in Python using the remainder opertor % % via the expression n % 2 n % 2 . Note you will also need to use integer division // // when computing f .
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Once you have implemented f , test the your implementation on the expression ( ( ( ( ( ( (674))))))) � � � � � � � f ( f ( f ( f ( f ( f ( f (674))))))) . This expression should evaluate to 190190 . Finally, compute the value of the expression ( ( ( ( ( ( ( ( ( ( ( ( ( (1071)))))))))))))) � � � � � � � � � � � � � � f ( f ( f ( f ( f ( f ( f ( f ( f ( f ( f ( f ( f ( f (1071 )))))))))))))) and enter the result below as an integer. Remember to use copy and paste when moving the expressions above into your Python environment. Never try to retype expressions by hand. 1 / 1 point 3053 Correct