-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogic-1.py
More file actions
223 lines (149 loc) · 5.05 KB
/
Logic-1.py
File metadata and controls
223 lines (149 loc) · 5.05 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 13 14:33:05 2019
@author: YaoJunyan
"""
'''
cigar_party
When squirrels get together for a party, they like to have cigars. A squirrel party is successful when the number of cigars is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of cigars. Return True if the party with the given values is successful, or False otherwise.
cigar_party(30, False) → False
cigar_party(50, False) → True
cigar_party(70, True) → True
'''
def cigar_party(cigars, is_weekend):
if is_weekend:
if cigars < 40:
return False
else:
return True
else:
if cigars >=40 and cigars <=60:
return True
else:
return False
'''
date_fashion
You and your date are trying to get a table at a restaurant. The parameter "you" is the stylishness of your clothes, in the range 0..10, and "date" is the stylishness of your date's clothes. The result getting the table is encoded as an int value with 0=no, 1=maybe, 2=yes. If either of you is very stylish, 8 or more, then the result is 2 (yes). With the exception that if either of you has style of 2 or less, then the result is 0 (no). Otherwise the result is 1 (maybe).
date_fashion(5, 10) → 2
date_fashion(5, 2) → 0
date_fashion(5, 5) → 1
'''
def date_fashion(you, date):
if you <= 2 or date <=2:
return 0
elif you >=8 or date >= 8:
return 2
else:
return 1
'''
squirrel_play
The squirrels in Palo Alto spend most of the day playing. In particular, they play if the temperature is between 60 and 90 (inclusive). Unless it is summer, then the upper limit is 100 instead of 90. Given an int temperature and a boolean is_summer, return True if the squirrels play and False otherwise.
squirrel_play(70, False) → True
squirrel_play(95, False) → False
squirrel_play(95, True) → True
'''
def squirrel_play(temp, is_summer):
if is_summer:
if temp<=100 and temp >=60:
return True
else:
return False
else:
if temp<=90 and temp >=60:
return True
else:
return False
'''
caught_speeding
You are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday -- on that day, your speed can be 5 higher in all cases.
caught_speeding(60, False) → 0
caught_speeding(65, False) → 1
caught_speeding(65, True) → 0
'''
def caught_speeding(speed, is_birthday):
if is_birthday:
if speed >=86:
return 2
elif speed >=66:
return 1
else:
return 0
else:
if speed >=81:
return 2
elif speed >=61:
return 1
else:
return 0
'''
sorta_sum
Given 2 ints, a and b, return their sum. However, sums in the range 10..19 inclusive, are forbidden, so in that case just return 20.
sorta_sum(3, 4) → 7
sorta_sum(9, 4) → 20
sorta_sum(10, 11) → 21
'''
def sorta_sum(a, b):
if a+b in range(10,20):
return 20
else:
return a+b
'''
alarm_clock
Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a boolean indicating if we are on vacation, return a string of the form "7:00" indicating when the alarm clock should ring. Weekdays, the alarm should be "7:00" and on the weekend it should be "10:00". Unless we are on vacation -- then on weekdays it should be "10:00" and weekends it should be "off".
alarm_clock(1, False) → '7:00'
alarm_clock(5, False) → '7:00'
alarm_clock(0, False) → '10:00'
'''
def alarm_clock(day, vacation):
if vacation:
if day in range(1,6):
return '10:00'
else:
return 'off'
else:
if day in range(1,6):
return '7:00'
else:
return '10:00'
'''
love6
The number 6 is a truly great number. Given two int values, a and b, return True if either one is 6. Or if their sum or difference is 6. Note: the function abs(num) computes the absolute value of a number.
love6(6, 4) → True
love6(4, 5) → False
love6(1, 5) → True
'''
def love6(a, b):
if a==6 or b==6 or (a+b)==6 or abs(a-b)==6:
return True
else:
return False
'''
in1to10
Given a number n, return True if n is in the range 1..10, inclusive. Unless outside_mode is True, in which case return True if the number is less or equal to 1, or greater or equal to 10.
in1to10(5, False) → True
in1to10(11, False) → False
in1to10(11, True) → True
'''
def in1to10(n, outside_mode):
if outside_mode:
if n<=1 or n>=10:
return True
else:
return False
else:
if n in range(1,11):
return True
else:
return False
'''near_ten
Given a non-negative number "num", return True if num is within 2 of a multiple of 10. Note: (a % b) is the remainder of dividing a by b, so (7 % 5) is 2.
near_ten(12) → True
near_ten(17) → False
near_ten(19) → True
'''
def near_ten(num):
if (num%10) in [0,1,2,8,9]:
return True
else:
return False