Escolar Documentos
Profissional Documentos
Cultura Documentos
s1
elif t2:
s2
elif t3:
s3
else
s4
# A stimulation of a do loop
while True:
if not t:
break
"""
not in python !
and && or ||
blanky('') - false
blanky('prince') - false
"""
# Version 1
def blanky(s):
j=0
while(j<len(s)):
return True
else:
j+=1
return false
blanky('s')
def blanky(s):
for x in s:
if x == ' ':
return True
return False
def blanky(s):
if len(s) == 0:
return false
return True
else:
return blanky(s[1:])
# DAY 3 PYTHON
"""
Python sequences, tuple, list, dictionary (Examples if time) One more blanky example...
# naive way
def blanky(s):
if ' ' in s:
return True
else:
return false
Tuple ordered sequence of 0 or more objects, not necessarily all of the same type. Objects = elements
of the tuple
e's are expressions (e0, e1, e2,..., en) returns a tuple whose elements are the e's
A tuple doesn't specify type for instance (3, "cheers", 4, "python", (1, 3))
Tuples can have 0 elements, however if we want a tuple with one element needs a trailing comma (1,)
Q[0][2]
(1,2) + ("a", "b", "c") = (1, 2, "a", "b", "c") -- This does not change the original tuples
s = (,)
while J < 3
s= s + (J,)
J+= 1
s = (0, 1,2)
Lists are mutable and they use square parenthesis. No extra comman needed to show its a list
Dictionaries (mutable):
9/15
Try and catcg statements in python which allow you to process exceptions!
"""
def blanky(s):
# The in operator takes any sequence (string etc) if blank return true else false
try:
doSomething() #raise E, v
except: # Note that here you should be able to specify what exception to negate, without passing any
specification this will catch all statements
else: # in this instance python could also use this syntax in line with a try statement
# An example
class Person:
self.name = name
self.age = age
def doSomethingWithPerson():
name = readName()
age = readAge()
j =1
while j <=100:
try:
print message
pass
j +=1
# finally in a try statement will always execute the try except clause! Think of this as a clean up
# ALSO NOTE THAT YOU CAN USE RAISE WITHOUT WRITING A TRY STATEMENT! - It just reminds you of
your last excpetion...
""" ****************************************
INHERITANCE
Classes form a hierarchy we can make new classes by adding things to old ones.
Examples:
"""
# INHERITANCE SINGLE
class Person:
self.__name = name
self.__age = age
class Wizard(Person):
# super()
self.__wand = wand
def getWand(self):
return self.__wand
class Muggle(Person):
self.__salary = salary
def getSalary(self):
return self.__salary
# MULTIPLE INHERITANCE
class Scary:
self.__size = size
class Alive(Scary):
self.__howAlive = howAlive
class Dead(Scary):
self.__howDead = howDead
# CODE HERE
"""
Higher-order functions
examples
Map
Filter
Reduce
However, this is reducing nowadays because it will eventually get bound by the laws
Quantam mechanics claim you will not be able to build very small transistors.
Possibly fail in 2025...
One box with many small chips inside of it and connect it in parallel. Massively
parallel computers
Memory (p1, p2) If we would like for them in parallel. To do this, maybe get rid
of variables?
F(g1(), g2(), g3()) - run g1, g2, g3 in parallel and then feed the result to F.
"""
"""
Higher-order functions - functiosn taht take other functions as arguments and return other functions
as values (BOTH).
"""
# Takes a predicate - a function that tests if its argument has some properties (T/F)
for s in s:
if p(e):
return True
return False
def blanky(s):
def eq(k):
# F = eq(1)
# F(1) - True
# F(0) - True
# F - function of 1-ary
# map(F, (f1, f2, f3, f4)) (Any sequence - list, string, tuple)
"""
if len(s) == 0:
return []
else:
"""
"""
filter(p, (e1,e2,e3,e4))
# list of elements (e1, e2, e3, e4) etc that make p True
# example
filter(odd, (1,2,3,4,5,6))
"""
Functional programming -
Filter, Reduce
example:
"""
if len(s) == 0:
return []
elif p(s[0]):
else:
# Reduce - the more complicated one! (Notice some other programming languages have this same
function but called ~ fold)
# F - function (2-ary)
def reduce(F, (e0, e1, e2), I):
# Will return F(F(F(I, e0), e1), e2) Returns a list of nested calls to F associate to the left (leftReduce)!
# Indexing in python s=(e0, e1, e2, e3, e4, e5) from 0 to len(s) -1,
if len(s) == 0:
return I
else:
# Note element 0 in F is actually the recursion call and the 1 element is the last element
# reduce(add, (1,2,3),0):
# add(reduce(add, (1,2),0),3)
#add(add(add(0, 1),2),3)
#((0+1)+2)+3 = 6
#howMany = len(oneName)
#average = sumOflengths/howMany
##class Time:
## self.__hour = hour
## self.__minute = minute
## self.__seconds = seconds
##
## def getHour(self):
## return __self.hour
## def getMinute(self):
## return __self.minute
## def getSecond(self):
## return __self.seconds
## print ("ddfdf")
##
##time.printTime()
def divide(X,Y):
return X / Y
def rightReduce(F,S,I):
if len(S) == 0:
return I
else:
def reduce(F,S,I):
if len(S) == 0:
return I
else:
print(rightReduce(divide,(16,4,2),32))
print(reduce(divide,(16,4,2),32))