-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab-11-true.py
208 lines (180 loc) · 5.76 KB
/
lab-11-true.py
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
##def shortest_name(names_list):
## """ (list of strings) -> str
## Returns the shortest name within a given list
## """
## return ""
##
##def shortest_name(names_list):
## """ (list of strings) -> str
## Returns the shortest name within a given list
## """
## for name in names_list:
## ...
## return ...
def shortest_name(names_list):
""" (list of strings) -> str
Returns the shortest name within a given list
"""
name = names_list[0]
for sth in names_list:
if (len(sth) < len(name)):
return sth
return name
print("This is a test for function shortest_name:")
print("Joe" == shortest_name(["Joe", "Pama"]))
print("Joe" == shortest_name(["Pama", "Joe"]))
print("An" == shortest_name(["Pama", "An", "Joe"]))
print("Ba" == shortest_name(["Ba", "An", "Cu"]))
##def is_letter(x):
## """ str -> bool
## Returns True when the given argument is a letter, otherwise returns False
## """
## return False
##
##def is_letter(x):
## """ str -> bool
## Returns True when the given argument is a letter, otherwise returns False
## """
## return ... x
alph_1 = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"]
alph_2 = ["n", "o","p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
alph_3 = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"]
alph_4 = ["N", "O","P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
alphabet = alph_1 + alph_2 + alph_3 + alph_4
def is_letter(x):
""" str -> bool
Returns True when the given argument is a letter, otherwise returns False
"""
return (x in alphabet)
print("This is a test for function is_letter:")
print(False == is_letter(98))
print(False == is_letter("1"))
print(True == is_letter("y"))
##def word_count(string):
## """ str -> int
## Takes a string and returns the number of words in that string
##
## NOTE: Words may be separated by multiple spaces.
## But the string is guaranteed not to start or end with a space.
## """
## return 0
##
##def word_count(string):
## """ str -> int
## Takes a string and returns the number of words in that string
##
## NOTE: Words may be separated by multiple spaces.
## But the string is guaranteed not to start or end with a space.
## """
## count = 0
## prev = " "
## for var in string:
## ...
## return ...
def word_count(string):
""" str -> int
Takes a string and returns the number of words in that string
NOTE: Words may be separated by multiple spaces.
But the string is guaranteed not to start or end with a space.
"""
count = 0
prev = " "
for var in string:
if (is_letter(var) and (prev == " ")):
count = count + 1
prev = var
return count
print("This is a test for function word_count:")
print(5 == word_count("this is a sample string")) #expect 5
print(3 == word_count("never give up"))
##def LCM(a, b):
## """ (int, int) -> int
## Returns the least common multiple of a given pair of integers
## """
## return 0
##
##def LCM(a, b):
## """ (int, int) -> int
## Returns the least common multiple of a given pair of integers
## """
## result = (a * b)
## for val in range(a, (a * b)):
## ....
## return ...
def LCM(a, b):
""" (int, int) -> int
Returns the least common multiple of a given pair of integers
"""
result = (a * b)
for val in range(a, (a * b)):
if ((val % a == 0) or (val % b == 0)) and (val <= result):
result = val
return result
print("This is a test for function LCM:")
print(2 == LCM(2, 1))
print(4 == LCM(4, 2))
print(5 == LCM(5, 5))
print(0 == LCM(9, 0))
print(72 == LCM(72, 8))
print(LCM(4, 6))
##def is_smallest_to_highest(nums_list):
## """ (list of numbers) -> bool
## Determines if a list of numbers is sorted from
## smallest to largest
## """
## return False
##
##def is_smallest_to_highest(nums_list):
## """ (list of numbers) -> bool
## Determines if a list of numbers is sorted from
## smallest to largest
## """
## for num in nums_list:
## ...
## return ...
def is_smallest_to_highest(nums_list):
""" (list of numbers) -> bool
Determines if a list of numbers is sorted from
smallest to largest
"""
for num in nums_list:
for sth in range(1, len(nums_list) + 1):
if (num < nums_list[sth]):
return True
return False
print("This is a test for function is_smallest_to_highest:")
print(True == is_smallest_to_highest([1, 2, 3]))
print(True == is_smallest_to_highest([1, 2, 4]))
print(False == is_smallest_to_highest([2, 2, 1]))
print(False == is_smallest_to_highest([6, 2, 3, 67]))
print(True == is_smallest_to_highest([6, 8, 67, 67]))
print(False == is_smallest_to_highest([9, 8, 1, 56]))
##def first_n_cubes(n):
## """ int -> (list of integers)
##
## Creates a function that creates a list of the first n cubes
## given a positive integer n
## """
## result []
##
##def first_n_cubes(n):
## """ int -> (list of integers)
##
## Creates a function that creates a list of the first n cubes
## given a positive integer n
## """
## output = []
## for i in range(1, (n + 1)):
## ...
## return ...
def first_n_cubes(n):
""" int -> (list of integers)
Creates a function that creates a list of the first n cubes
given a positive integer n
"""
output = []
for i in range(1, (n + 1)):
output = output + [i ** 3]
return output
print("This is a test for function first_n_cubes:")
print([1, 8, 27, 64] == first_n_cubes(4))