-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython1.py
More file actions
317 lines (208 loc) · 7.96 KB
/
python1.py
File metadata and controls
317 lines (208 loc) · 7.96 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# INSTRUCTIONS
# In case it is not clear, the Question appears first, then examples, then any hints and finally the function that you need to complete appears underneath:
# <QUESTION>
# <EXAMPLES>
# <HINT>
# You are NOT allowed access to the internet for this assessment, instead you should use the DOCUMENTATION that comes bundled with your Python installation. You should already be comfortable accessing this documentation, but to summarise:
# Access Python from you CLI
# Type help() or for example help(str)
# <QUESTION 1>
# Define a function that can accept two strings as input and returns the string with maximum length to the console.
# If two strings have the same length, then the function should return both strings separated by a " ".
# In this case, the strings should be returned in the same order in which they were given.
# <EXAMPLES>
# one("hi","hello") → "hello"
# one("three", "two") → "three"
# one("three", "hello") → "three hello"
# <HINT>
# What was the name of the function we have seen to check the length of a container? Use your CLI to access the Python documentation and get help(len).
def one(input1, input2):
if len(input1) == len(input2):
return (input1+" "+input2)
elif len(input1)>len(input2):
return input1
else:
return input2
result = one("hello","hello")
print(result)
# <QUESTION 2>
# Return the string that is between the first and last appearance of "bert" in the given string
# Return the empty string "" if there is not 2 occurances of "bert"
# IGNORE CASE
# <EXAMPLES>
# two("bertclivebert") → "clive"
# two("xxbertfridgebertyy") → "fridge"
# two("xxBertfridgebERtyy") → "fridge"
# two("xxbertyy") → ""
# two("xxbeRTyy") → ""
# <HINT>
# What was the name of the function we have seen to seperate a String? How can we make a string all upper or lower case?
# Use your CLI to access the Python documentation and get help manipulating strings - help(str).
def two(input1):
return ""
# <QUESTION 3>
# given a number
# if this number is divisible by 3 return "fizz"
# if this number is divisible by 5 return "buzz"
# if this number is divisible by both 3 and 5 return "fizzbuzz"
# if this number is not divisible by 3 or 5 return "null"
# <EXAMPLES>
# three(3) → "fizz"
# three(10) → "buzz"
# three(15) → "fizzbuzz"
# three(8) → "null"
# <HINT>
# No Hints for this question
def three(arg1):
if arg1 == 0 or arg1 == "":
return "null"
if arg1 % 3 == 0 and (arg1 % 5 == 0):
return "fizzbuzz"
elif arg1 % 5 == 0:
return "buzz"
elif (arg1 % 3 == 0):
return "fizz"
else:
return "null"
'''
result = three(15)
print(result)
'''
# <QUESTION 4>
# Given a string seperate the string into the individual numbers present, then add each digit of each number to get a final value for each number
# String example = "55 72 86"
# "55" will = the integer 10
# "72" will = the integer 9
# "86" will = the integer 14
# You then need to return the highest value, in the example above this would be 14.
# <EXAMPLES>
# four("55 72 86") → 14
# four("15 72 80 164") → 11
# four("555 72 86 45 10") → 15
# <HINT>
# help(int) for working with numbers and help(str) for working with Strings.
def four(arg1):
list = []
work = arg1.split()
for i in range(len(work)):
ones = str(work[i])
one = int(ones[0]) + int(ones[1])
list.append(one)
return max(list)
# <QUESTION 5>
# Given a large string that represents a csv, the structure of each record will be as follows:
# owner,nameOfFile,encrypted?,fileSize
# "Bert,helloWorld.py,True,1447,Bert,strings.py,False,1318,Jeff,dice.py,False,1445"
# For each record, if it is not encrypted, return the names of the owners of the files in a String Array.
# Do not include duplicate names.
# If all records are encrypted, return an empty Array.
# <EXAMPLES>
# five("Jeff,random.py,False,1445") → ["Jeff"]
# five("Bert,numberGen.py,True,1447,Bert,integers.py,True,1318,Jeff,floats.py,False,1445") → ["Jeff"]
# five("Bert,boolean.py,False,1447,Bert,conditions.py,False,1318,Jeff,loops.py,False,1445") → ["Bert","Jeff"]
# five("Bert,prime.py,True,1447,Bert,ISBN.py,False,1318,Jeff,OOP.py,False,1445") → ["Bert","Jeff"]
# <HINT>
# Dont't forget, False is a String, not a Boolean value in the Tests above.
# help(str) and help(list), you might also need to use a function that can create a list of numbers for you, try help(range).
def five(input1):
return []
# <QUESTION 6>
# There is a well known mnemonic which goes "I before E, except after C", which is used to determine which order "ei" or "ie" should be in a word.
# Write a function which returns the boolean True if a string follows the mnemonic, and returns the boolean False if not.
# <EXAMPLES>
# six("ceiling") → True
# six("believe") → True
# six("glacier") → False
# six("height") → False
# <HINT>
# Step through the logic here in order to solve the problem, you may find help(range) helpful.
def six(input1):
for i in range(len(input1)):
if input1[i] == "c" and input1[i+1] == "e" and input1[i+2] == "i":
return True
elif input1[i] == "c" and input1[i+1] == "i" and input1[i+2] == "e":
return True
elif input1[i] == "i" and input1[i+1] == "e":
return True
else:
return False
# <QUESTION 7>
# Write a function which returns the integer number of vowels in a given string.
# You should ignore case.
# <EXAMPLES>
# seven("Hello") → 2
# seven("hEelLoooO") → 6
# <HINTS>
# How do we ignore case in a String? help(str) may offer some insight.
def seven(input1):
count = 0
lower = input1.lower()
for i in range(len(lower)):
if lower[i] == 'a' or lower[i] == 'e' or lower[i] == 'i' or lower[i] == 'o' or lower[i] == 'u':
count = count + 1
return count
# <QUESTION 8>
# Write a function which takes an input (between 1 and 10 inclusive) and multiplies it by all the numbers before it.
# eg If the input is 4, the function calculates 4x3x2x1 = 24
# <EXAMPLES>
# eight(1) → 1
# eight(4) → 24
# eight(8) → 40320
# <HINT>
# You may need to create a list of numbers from 0 to i, take a look at help(range).
def eight(input1):
fact = 1
if input1 >= 1 and input1 <= 10:
for i in range(1,input1+1):
fact *= i
return fact
# <QUESTION 9>
# Given a string and a char, returns the position in the String where the char first appears.
# Ensure the positions are numbered correctly, please refer to the examples for guidance.
# DO NOT ignore case
# IGNORE whitespace
# If the char does not occur, return the number -1.
# <EXAMPLES>
# nine("This is a Sentence","s") → 4
# nine("This is a Sentence","S") → 8
# nine("Fridge for sale","z") → -1
# <HINT>
# Take a look at the documentation for Strings, List and range.
def nine(inputString, char):
new = inputString.replace(" ","")
result = new.find(char)
if result == 0:
return -1
elif result > 0:
return result + 1
else:
return -1
return result
result = nine("Fridge for sale","z")
print(result)
# <QUESTION 10>
# Given a string, int and a char, return a boolean value if the 'nth'
# (represented by the int provided) char of the String supplied is the same as the char supplied.
# The int provided will NOT always be less than than the length of the String.
# IGNORE case and Whitespace.
# <EXAMPLES>
# ten("The",2,'h') → True
# ten("AAbb",1,'b') → False
# ten("Hi-There",10,'e') → False
# <HINT>
# How do we find the length of a container, take a look at help(len), you will also need to look at help(str) for String manipulation.
def ten(string, int, char):
lo = string.lower()
lc = char.lower()
if len(lo) == 0:
return False
elif len(lo) == 1 and lo[0] == lc:
return True
elif len(lo) < int:
return False
elif lo[int-1] == char:
return True
else:
return False
result = ten("T",2,'T')
print(result)