-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwarmup-1.py
More file actions
executable file
·134 lines (121 loc) · 3.65 KB
/
warmup-1.py
File metadata and controls
executable file
·134 lines (121 loc) · 3.65 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
# sleep_in
def sleep_in(weekday, vacation):
'''
The parameter weekday is True if it is a weekday, and the parameter vacation is True if
we are on vacation. We sleep in if it is not a weekday or we're on vacation. Return True
if we sleep in.
'''
return not weekday or vacation
# monkey_trouble
def monkey_trouble(a_smile, b_smile):
'''
We have two monkeys, a and b, and the parameters a_smile and b_smile indicate
if each is smiling. We are in trouble if they are both smiling or if neither
of them is smiling. Return True if we are in trouble.
'''
return a_smile == b_smile
# sum_double
def sum_double(a, b):
'''
Given two int values, return their sum. Unless the two values are the same, then return
double their sum.
'''
sum = a + b
if a == b:
sum = sum * 2
return sum
# sum_double solution2
def sum_double(a, b):
if a==b:
return 2*(a+b)
return a+b
# diff21
def diff21(n):
'''
Given an int n, return the absolute difference between n and 21, except return
double the absolute difference if n is over 21.
'''
if n <= 21:
return 21 - n
else:
return (n - 21)*2
# parrot_trouble
def parrot_trouble(talking, hour):
'''
We have a loud talking parrot. The "hour" parameter is the current hour time in
the range 0..23. We are in trouble if the parrot is talking and the hour is before
7 or after 20. Return True if we are in trouble.
'''
return talking and hour not in range(7,21)
# makes10
def makes10(a, b):
'''
Given 2 ints, a and b, return True if one if them is 10 or if their sum is 10.
'''
return a+b == 10 or a == 10 or b == 10
# near_hundred
def near_hundred(n):
'''
Given an int n, return True if it is within 10 of 100 or 200. Note: abs(num)
computes the absolute value of a number.
'''
return((abs(100 - n) <= 10) or (abs(200 - n) <= 10))
# pos_neg
def pos_neg(a, b, negative):
'''
Given 2 int values, return True if one is negative and one is positive.
Except if the parameter "negative" is True, then return True only if both
are negative.
'''
if negative:
return a < 0 and b < 0
return (a > 0 and b < 0) or (a < 0 and b > 0)
# not_string
def not_string(str):
'''
Given a string, return a new string where "not " has been added to the front. However,
if the string already begins with "not", return the string unchanged.
'''
if str[0:3] == 'not':
return str
return 'not ' + str
# missing_char
def missing_char(str, n):
'''
Given a non-empty string and an int n, return a new string where the char at index n
has been removed. The value of n will be a valid index of a char in the original strin
(i.e. n will be in the range 0..len(str)-1 inclusive).
'''
front = str[:n]
end = str[n+1:]
return front + end
# missing_char solution2
def missing_char(str, n):
return str[:n]+str[n+1:]
# front_back
def front_back(str):
'''
Given a string, return a new string where the first and last chars have been exchanged.
'''
if len(str) <= 1:
return(str)
else:
return(str[-1] + str[1:-1] + str[0])
#front_back solution2
if len(str) > 1:
return str[-1]+str[1:-1]+str[0]
return str
# front3
def front3(str):
'''
Given a string, we'll say that the front is the first 3 chars of the string. If the string
length is less than 3, the front is whatever is there. Return a new string which is 3 copies
of the front.
'''
if len(str) <= 3:
return(str * 3)
else:
return(str[:3] * 3)
# front3 solution2
def front3(str):
return str[0:3]*3