-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathless1and2
575 lines (455 loc) · 12.2 KB
/
less1and2
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# Udacity Introduction to Computer Science Class
# Notes and snippets - all in Python
#Fall 2016
#####################################
#Lesson One
# Variables Quiz
# Given the variables defined here, write Python
# code that prints out the distance, in meters,
# that light travels in one processor cycle.
# speed_of_light in meters per second
# cycles_per_second is 2.7 GHz
speed_of_light = 299792458.0
cycles_per_second = 2700000000.0
print speed_of_light / cycles_per_second
########################################
#Spirit Age Quiz
# Write python code that defines the variable
# age to be your age in years, and then prints
# out the number of days you have been alive.
age = 45
print (age / 4) + (age * 365)
#####################################
#Quiz Capital Udacity
# Write Python code that prints out Udacity (with a capital U),
# given the definition of s below.
s = 'audacity'
print str.capitalize(s[1:8])
#print str.upper (s [1:2])
#print str.lower (s[2:8 ])
#######################################
#Quiz Extracting Links
# Write Python code that initializes the variable
# start_link to be the value of the position
# where the first '<a href=' occurs in a page.
page = '''<div id="top_bin"> <div id="top_content" class="width960">
<div class="udacity float-left"> <a href="/">'''
start_link = page.find
######################################
#Lesson One Final Quiz
# Write Python code that assigns to the
# variable url a string that is the value
# of the first URL that appears in a link
# tag in the string page.
# Your code should print http://udacity.com
# Make sure that if page were changed to
#page = '<a href="http://udacity.com">Hello world</a>'
# that your code still assigns the same value to the variable 'url',
# and therefore still prints the same thing.
# page = contents of a web page
page =('<div id="top_bin"><div id="top_content" class="width960">'
'<div class="udacity float-left"><a href="http://udacity.com">')
start_link = page.find('<a href=')
end_link=page.find('>',start_link)-1
start_url = start_link + 9
url = page[start_url:end_link]
print url
############################################
# Lesson One - Text Replacement Quiz
###############################################
# Exercise by Websten from forums #
###############################################
# To minimize errors when writing long texts with
# complicated expressions you could replace
# the tricky parts with a marker.
# Write a program that takes a line of text and
# finds the first occurrence of a certain marker
# and replaces it with a replacement text.
# The resulting line of text should be stored in a variable.
# All input data will be given as variables.
#
# Replace the first occurrence of marker in the line below.
# There will be at least one occurrence of the marker in the
# line of text. Put the answer in the variable 'replaced'.
# Hint: You can find out the length of a string by command
# len(string). We might test your code with different markers!
# Example 1
marker = "AFK"
replacement = "away from keyboard"
line = "I will now go to sleep and be AFK until lunch time tomorrow."
# Example 2 # uncomment this to test with different input
#marker = "EY"
#replacement = "Eyjafjallajokull"
#line = "The eruption of the volcano EY in 2010 disrupted air travel in Europe for 6 days."
###
# YOUR CODE BELOW. DO NOT DELETE THIS LINE
###
replaced =
print replaced
# Example 1 output should be:
#>>> I will now go to sleep and be away from keyboard until lunch time tomorrow.
# Example 2 output should be:
#>>> The eruption of the volcano Eyjafjallajokull in 2010 disrupted air travel in Europe for 6 days.
#######################################
#Lesson One - Palindrome Problem
###############################################
# Exercise by Websten from forums #
###############################################
# A palindrome is a word or a phrase that reads
# the same backwards as forwards. Make a program
# that checks if a word is a palindrome.
# If the word is a palindrome, print 0 to the terminal,
# -1 otherwise.
# The word contains lowercase letters a-z and
# will be at least one character long.
#
### HINT! ###
# You can read a string backwards with the following syntax:
# string[::-1] - where the "-1" means one step back.
# This exercise can be solved with only unit 1 knowledge
# (no loops or conditions)
#word = "madam"
# test case 2
word = "madman" # uncomment this to test
###
# YOUR CODE HERE. DO NOT DELETE THIS LINE OR ADD A word DEFINITION BELOW IT.
###
bw_word=word[::-1]
is_palindrome =bw_word.find(word)
# TESTING
print is_palindrome
# >>> 0 # outcome if word == "madam"
# >>> -1 # outcome if word == "madman"
############################################
#Lesson Two
#Quiz - Square
# Define a procedure, square, that takes one number
# as its input, and returns the square of that
# number (result of multiplying
# the number by itself).
# To help you out, the code for sum(a,b) is below.
def sum(a,b):
c = a + b
return c
def square(n):
n2 = n*n
return n2
# To test your procedure, uncomment the print
# statement below, by removing the hash mark (#)
# at the beginning of the line.
# Do not remove the # from in front of the line
# with the arrows (>>>). Lines which begin like
# this (#>>>) are included to show the results
# you should see when run your procedure.
print square(5)
#>>> 25
######################
#Quiz Sum of Three
# Define a procedure, sum3, that takes three
# inputs, and returns the sum of the three
# input numbers.
# To help you out, the code for sum(a,b) is below.
def sum(a,b):
return a + b
def sum3(a,b,c):
return a+b+c
print sum3(1,2,3)
#>>> 6
print sum3(93,53,70)
#>>> 216
##############################
# Define a procedure, abbaize, that takes
# two strings as its inputs, and returns
# a string that is the first input,
# followed by two repetitions of the second input,
# followed by the first input.
def abbaize(a,b):
return a+b+b+a
print abbaize('a','b')
#>>> 'abba'
print abbaize('dog','cat')
#>>> 'dogcatcatdog'
###############################
# Quiz Find Second
# Define a procedure, find_second, that takes
# two strings as its inputs: a search string
# and a target string. It should return a
# number that is the position of the second
# occurrence of the target string in the
# search string.
def find_second (search,target):
first=search.find(target)
return search.find (target,first+1)
danton = "De l'audace, encore de l'audace, toujours de l'audace"
print find_second(danton, 'audace')
#>>> 25
twister = "she sells seashells by the seashore"
print find_second(twister,'she')
#>>> 13
###################
#Quiz If Statements
# Define a procedure, bigger, that takes in
# two numbers as inputs, and returns the
# greater of the two inputs.
def bigger(a,b):
if a==b:
return a
if a>b:
return a
if b>a:
return b
print bigger(2,7)
#>>> 7
print bigger(3,2)
#>>> 3
print bigger(3,3)
#>>> 3
######################
#Quiz Is Friend
# Define a procedure, is_friend, that
# takes a string as its input, and
# returns a Boolean indicating if
# the input string is the name of
# a friend. Assume I am friends with
# everyone whose name starts with D
# and no one else. You do not need to
# check for the lower case 'd'
# below code prints d as answer
#s='diane'
#f=s[0]
#print f
def is_friend(s):
friend='D'
return s[0]==friend
print is_friend('Diane')
#>>> True
print is_friend('Fred')
#>>> False
##### if syntax example below
#def bigger(a,b):
# if a==b:
# return a
# if a>b:
# return a
# if b>a:
# return b
##########################
#Quiz More Friends
# Define a procedure, is_friend, that takes
# a string as its input, and returns a
# Boolean indicating if the input string
# is the name of a friend. Assume
# I am friends with everyone whose name
# starts with either 'D' or 'N', but no one
# else. You do not need to check for
# lower case 'd' or 'n'
def is_friend(name):
friend='D'
friend2='N'
return name[0]==friend or name[0]==friend2
print is_friend('Diane')
#>>> True
print is_friend('Ned')
#>>> True
print is_friend('Moe')
#>>> False
#############################
#Quiz Print Numbers
# Define a procedure, print_numbers, that takes
# as input a positive whole number, and prints
# out all the whole numbers from 1 to the input
# number.
# Make sure your procedure prints "upwards", so
# from 1 up to the input number.
#WRONG WORKING CODE BELOW - MUST PRINT NUMBERS SMALLEST TO LARGEST
def print_numbers(a):
while a != 0:
print a
a=a-1
print_numbers(3)
#>>> 1
#>>> 2
#>>> 3
#CORRECT WORKING CODE BELOW
def print_numbers(a):
b=0
while b != a:
b=b+1
print b
#PROF ANSWER BELOW
def print_numbers(n):
i=1
while i <= n
print i
i = i + 1
print_numbers(3)
#>>> 1
#>>> 2
#>>> 3
#####################################
#Quiz Factorial ERROR A: Answer all is 1
#def factorial(n):
# i=1
# a=1
# b=1
# while i > 1:
# i=n-1
# a=n(i)
# b=a*b
# return b
#print factorial(4)
#>>> 24 err 1
#print factorial(5)
#>>> 120 err 1
#print factorial(6)
#>>> 720 err 1
#ERROR B: Answer to all is None
#def factorial(n):
# i=1
# a=1
# n=1
# while i > 1:
# i=n-1
# a=n*i
# b=a+b
# return b
#ERROR C: Answer to all is zero 0
#def factorial(n):
# i=1
# a=1
# n=1
# b=0
# while i >= 1:
# i=n-1
# a=n*i
# b=a+b
# return b
#ERROR D: Infinite loop
#def factorial(n):
# i=1
# a=1
# b=0
# while i >= 1:
# i=n-1
# a=n*i
# b=a+b
# return b
#CORRECT ANSWER! YAY!
def factorial(n):
b=1
while n >= 1:
b=b*n
n=n-1
return b
print factorial(4)
#>>> 24
print factorial(5)
#>>> 120
print factorial(6)
#>>> 720
## Break Quiz
# Use this to try out anything you like. Use print to display your answer
# when you press the "Test Run" button.
# Use the "Reset" button to reset the screen
#While <T>
# <S>
#<T>
#countdown >= 0:
#<S>
#print countdown
#countdown = countdown - 1
#while True:
# print 'Hello'
#<code>
#while True:
# print 'Hello'
# while True:
# break
# print 'Goodbye'
# break
#</code>
#which codes has same results as the following for countdown?
def countdown(T):
# while T >= 0:
# print T
# T = T - 1
#same#
# while T>=0:
# if False:
# break
# print T
# T = T-1
#diff#
# while True:
# print T
# break
#diff#
# while True:
# if T:
# break
# print T
# T=T-1
#same#
# while T:
# print T
# T=T-1
# if T:
# print T
# T=T-1
# else:
# break
#######No links quiz
def get_next_target(page):
start_link=page.find('<a href=')
if start_link == -1:
return None, 0
start_quote = page.find('"', start_link)
end_quote = page.find('"', start_quote + 1)
url = page[start_quote + 1:end_quote]
return url, end_quote
url, endpos = get_next_target('<div class="udacity float-left"><a href="http://udacity.com">')
if url:
print "Here!"
else:
print "Not Here!"
###### Print All Links
def print_all_links(page):
while True:
url, endpos = get_next_target(page)
if url:
print url
page = page[endpos:]
else:
break
print_all_links(get_page('http://xkcd.com/353'))
#####Find_last##
# Define a procedure, find_last, that takes as input
# two strings, a search string and a target string,
# and returns the last position in the search string
# where the target string appears, or -1 if there
# are no occurrences.
#
# Example: find_last('aaaa', 'a') returns 3
# Make sure your procedure has a return statement.
def find_last(s,t):
last_point = -1
while s.find(t) != -1:
find_point = s.find(t,last_point+1)
if find_point == -1:
return last_point
last_point = find_point
return last_point
print find_last('aaaa','a')
#>>> 3
print find_last('aaaaa', 'aa')
#>>> 3
print find_last('aaaa', 'b')
#>>> -1
print find_last("111111111", "1")
#>>> 8
print find_last("222222222", "")
#>>> 9
print find_last("", "3")
#>>> -1
print find_last("", "")
#>>> 0