-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek 3
111 lines (88 loc) · 4.05 KB
/
week 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
Question 1
Which of the following are Boolean values in Python?
1
True
False
true
Ans:-
True && False
Question 2
Consider the Boolean expression not (p or not q). Give the four following values in order, separated only by spaces:
the value of the expression when p is True, and q is True,
the value of the expression when p is True, and q is False,
the value of the expression when p is False, and q is True,
the value of the expression when p is False, and q is False,
Remember, each of the four results you provide should be True or False with the proper capitalization.
Ans:-
False False True False
Question 3
Given the following initialization:
bool1 = True
bool2 = False
Which of the expressions below evaluate to True?
bool1 != bool2
bool1 == bool2
not (bool1 == bool2)
not bool1
Ans:-
bool1 != bool2 && not (bool1 == bool2)
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 and num2, which one of the expressions below is logically equivalent to the following arithmetic comparison:
num1 >= num2
not (num1 <= num2)
not (num1 <= num2)
num2 < num1
(num1 > num2) and (num1 != num2)
Ans:-
not (num1 <= num2)
Question 5
An if statement can have at most how many elif parts?
0
Unlimited, i.e., 0 or more
1
Ans:-
Unlimited, i.e., 0 or more
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.
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
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.
1011121314
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) where bool1 and bool2 are Boolean variables?
not (bool1 and bool2)
(bool1 or bool2)
(bool1 and bool2)
not (bool1 or bool2)
Ans:-
not (bool1 and bool2)
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)f(n) (as defined in the Wikipedia page above) that takes an integer nn and divides it by two if nn is even and multiplies nn by 33 and then adds one to the result if nn is odd. The conjecture involves studying the value of expressions of the form f(f(f(...f(f(n)))))f(f(f(...f(f(n))))) as the number of calls to the function ff increases. The conjecture is that, for any non-negative integer nn, repeated application of ff to nn yields a sequence of integers that always includes 11.
Your task for this question is to implement the Collatz function ff in Python. The key to your implementation is to build a test that determines whether nn is even or odd by checking whether the remainder when nn 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. Note you will also need to use integer division // when computing ff.
Once you have implemented ff, test the your implementation on the expression f(f(f(f(f(f(f(674)))))))f(f(f(f(f(f(f(674))))))). This expression should evaluate to 190190. Finally, compute the value of the expression f(f(f(f(f(f(f(f(f(f(f(f(f(f(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
Ans:-
3053