0% found this document useful (0 votes)
4 views

ComputerSysAndProgramming_9

Uploaded by

raniaalfiky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

ComputerSysAndProgramming_9

Uploaded by

raniaalfiky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

Cairo University

Faculty of Graduate Studies for Statistical Research


Department of Computer and Information Sciences

Computer Sys. And Programming


Lec. 9 outline: You’ll find all the information you need here on Why
Design?, Design Strategies, Drivers and Stubs, Skeletal Design,
Recursive Design, Higher-order functions, Why Use
Parameters?,Default and Optional Parameters, Keyword Parameters,
The map function, Mappers, Using lambda , ….and ….
Tarek Aly
01126182476
http://41.32.221.109:8080/DrTarekAly.htm
E-mail: tarekmmmmt@{pg.cu.edu.eg; egyptpost.org; gmail.com; yahoo.com}
Contents
 Why Design?
 Design Strategies
 Drivers and Stubs
 Skeletal Design
 Recursive Design
 Higher-order functions
 Why Use Parameters?
 Default and Optional Parameters
 Keyword Parameters
 The map function
 Mappers
 Using lambda for

10/2/2023 Computer Sys. And Programming


Why Design?
 As problems become more complex, so do their
solutions

 There is more to programming than just


hacking out code

 We can decompose a complex problem into


simpler subproblems and solve each of these

 Divide up the work and assign responsibilities


to individual actors (functions)

10/2/2023 Computer Sys. And Programming


Top-Down Design
In top-down design, we decompose a complex problem into
simpler subproblems, and solve these with different functions.

Function-1

Function-2 Function-3

Function-4 Function-5 Function-6 Function-7

10/2/2023 Computer Sys. And Programming


Example: The doctor Program
Each function has its own responsibilities; in a well-designed
program, no function does too much

main

input reply

random.choice changePerson

10/2/2023 Computer Sys. And Programming


Example: The doctor Program

Functions communicate via arguments and returned values

main

string
reply
string
string
changePerson
string

10/2/2023 Computer Sys. And Programming


Example: The doctor Program
Functions also go to shared data pools for information
Data Pool
hedges
qualifiers
replacements
string string
main

string string
reply
string
string
changePerson
string

10/2/2023 Computer Sys. And Programming


Example: The
main Sentence Program
string

sentence
string string

nounPhrase verbPhrase
string string

nounPhrase prepositionalPhrase
string

nounPhrase

articles nouns verbs prepositions


Data Pool

10/2/2023 Computer Sys. And Programming


Design Strategies: Top Down
 Start with the main function and pretend that
the functions that it calls are already defined

 Work your way down by defining those


functions, etc.

 Cannot test anything until they’re all finished

10/2/2023 Computer Sys. And Programming


Drivers and Stubs
 Start with the main function and pretend that
the functions that it calls are already defined

 Define these functions using simple headers


and almost no code
 If a function returns a value, return a reasonable
default (0 or empty string)
 If a function does not return a value, return None

 The main function is known as a driver, and the


other functions are called stubs

10/2/2023 Computer Sys. And Programming


Skeletal Design
 Drivers/stubs allow you to sketch out the
structure of a program in terms of cooperating
functions without finishing everything at once

 Set up their communication links, which are


the arguments and return values

 Run the program to check these before filling


in the details

10/2/2023 Computer Sys. And Programming


Design Strategies: Bottom Up
 Start with simple functions at the bottom of
the chart and work your way up

 Each new function can be tested as soon as it’s


defined

 Easier to get the little pieces of a program up


and running, then integrate them into a more
complete solution

10/2/2023 Computer Sys. And Programming


Good Design?
 Do you divide up the work so that each function does one
coherent thing?

 Do the functions communicate via arguments and return


values rather than a common pool of data?

 When a common pool of data seems necessary, do you confine


access to just a few functions?

 Do you name the functions and arguments to reflect their


purpose in the program?

 Do you document your design?????

10/2/2023 Computer Sys. And Programming


Recursive Design
As a special case of top-down design, we decompose a
problem into smaller subproblems that have the same form,
and solve these with the same function.

Function-1

Function-1 Function-1

10/2/2023 Computer Sys. And Programming


Recursive Design Cont
As a special case, we decompose a problem into smaller
subproblems that have the same form, and solve these
with the same function.

sentence = nounPhrase verbPhrase [ “and” sentence ]


sentence

nounPhrase verbPhrase sentence

10/2/2023 Computer Sys. And Programming


Example: Get a Valid Integer
 The function getValidInteger prompts
the user for an integer and inputs it

 The integer is returned if it is within the


specified range

 Otherwise, the function displays an error


message and calls getValidInteger to
try again

10/2/2023 Computer Sys. And Programming


Define a Recursive Function
def getValidInteger(prompt, low, high):
number = int(input(prompt))
if number >= low and number <= high:
return number
else:
print('ERROR: Input number is out of range')
return getValidInteger(prompt, low, high)

A recursive function calls itself

There will be 0 or more recursive calls of this function

A recursive process is similar to an iterative process (the


same thing is done repeatedly, 0 or more times)
10/2/2023 Computer Sys. And Programming
The Base Case
def getValidInteger(prompt, low, high):
number = int(input(prompt))
if number >= low and number <= high:
return number
else:
print('ERROR: Input number is out of range')
return getValidInteger(prompt, low, high)

A recursive process stops when a base case is reached

In this function, a valid input number is simply returned

10/2/2023 Computer Sys. And Programming


The Recursive Step
def getValidInteger(prompt, low, high):
number = int(input(prompt))
if number >= low and number <= high:
return number
else:
print('ERROR: Input number is out of range')
return getValidInteger(prompt, low, high)

Otherwise, a recursive step drives the recursion forward,


until a base case is reached

10/2/2023 Computer Sys. And Programming


Computing Factorial (!)
 4! = 4 * 3 * 2 * 1 = 24

 N! = N * (N - 1) * (N - 2) * … * 1

 Recursive definition of factorial:


 N! = 1, when N = 1
 N! = N * (N - 1)!, otherwise

10/2/2023 Computer Sys. And Programming


Define factorial Recursively
# N! = 1, when N = 1
# N! = N * (N - 1)!, otherwise

def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)

What is the base case?

What is the recursive step?

Does the recursive step advance the process toward the


base case?
10/2/2023 Computer Sys. And Programming
Tracing factorial
# N! = 1, when N = 1
# N! = N * (N - 1)!, otherwise

def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
>>> factorial(4) # With a trace of the process
n = 4
n = 3
n = 2
n = 1
factorial(1) = 1
factorial(2) = 2
factorial(3) = 6
factorial(4) = 24
10/2/2023 Computer Sys. And Programming
Gathering File System Stats
 Count the files
 Get the size of a directory (number of
bytes) D

D F F F D F

F F F D F

F F
10/2/2023 Computer Sys. And Programming
Modules os and os.path
os.getcwd()

os.listdir(dirname)

os.chdir(dirname)

os.path.isfile(name)

os.path.isdir(name)

os.path.getsize(filename)

Define functions:

countFiles(dirname)

getSize(dirname)
10/2/2023 Computer Sys. And Programming
Counting the Files
 Use a recursive strategy

 Sum all of the items in the current


directory

 If the item is a file, add 1

 Otherwise, the item is a directory, so


add the count obtained from a recursive
call of the function
10/2/2023 Computer Sys. And Programming
Define and Use countFiles
import os
import os.path

def countFiles(dirname):
"""Counts the files in a directory and its subdirectories."""
count = 0
listOfItems = os.listdir(dirname)
for item in listOfItems:
if os.path.isfile(item):
count += 1 # It’s a file
else:
os.chdir(item) # It’s a directory
count += countFiles(os.getcwd())
os.chdir("..")
return count

countFiles(os.getcwd())
10/2/2023 Computer Sys. And Programming
Higher-order functions

10/2/2023 Computer Sys. And Programming


Arguments and Return Values
 A function can receive data
(arguments) from its caller

 A function can return a single value to


its caller

y = math.sqrt(x)

10/2/2023 Computer Sys. And Programming


Why Use Parameters?
 Parameters allow a function to be used with different
data in different parts of a program

 The general method or algorithm is the same, but the


arguments vary with the situation

>>> math.sqrt(2)
1.4142135623730951

>>> math.sqrt(16)
4.0

>>>

10/2/2023 Computer Sys. And Programming


Default and Optional
Parameters
One or more parameters can have default values, so
the caller can omit some arguments
>>> round(3.1416) # Default precision is 0
3

>>> round(3.1416, 3) # Override the default


3.142

>>> list(range(5)) # Default lower bound is 0, and


[0,1,2,3,4] # default step value is 1

>>> list(range(1, 5)) # Override the default lower bound


[1,2,3,4]

>>> list(range(1, 5, 2)) # Override lower bound and step


[1,3]
10/2/2023 Computer Sys. And Programming
Keyword Parameters
Optional parameters can also be filled using keywords

>>> lyst = [4, 2, 6, 3] # Items in random order

>>> lyst.sort() # Default sort of list

>>> lyst # Ascending order


[2,3,4,6]

>>> lyst = [4, 2, 6, 3] # Reset to original order

>>> lyst.sort(reverse = True) # Override default sort order

>>> lyst # Descending order


[6,4,3,2]
10/2/2023 Computer Sys. And Programming
Convert Based Numbers to ints
 Write a general function that expects a string
representation of a number and its base (an
int) as arguments

 The function returns the integer represented

>>> convert('10', 2) # 102 = 2


2
>>> convert('10', 10) # 1010 = 10
10
>>> convert('10', 16) # 1016 = 16
16
>>> convert('100', 2) # 1002 = 4
4
10/2/2023 Computer Sys. And Programming
Implementation
def convert(digits, base):
"""Returns the integer represented by the digits
in the given base."""
intValue = 0
expo = len(digits - 1)
for ch in digits:
ch = string.upper(ch)
intvalue += hexdigits[ch] * base ** expo
expo -= 1
return intValue

10/2/2023 Computer Sys. And Programming


Default and Optional
Parameters
One or more parameters can have default values, so
the caller can omit some arguments

>>> convert('111', 2)
7
>>> convert('111', 10)
111
>>> convert('111', 16)
273
>>> convert('111') # Same result as the previous line
273

The caller can treat base16 as the standard base in


this system or use other bases by mentioning them
10/2/2023 Computer Sys. And Programming
Implementation
def convert(digits, base = 16):
"""Returns the integer represented by the digits
in the given base, with 16 as the default base."""
intValue = 0
expo = len(digits – 1)
for ch in digits:
ch = string.upper(ch)
intvalue += hexdigits[ch] * base ** expo
expo -= 1
return intValue

10/2/2023 Computer Sys. And Programming


Some Syntax Rules
 The required arguments used in a
function call must match the required
parameters named in the definition, by
position
<function name>(<required args>, <optional args>):

 The programmer should list the required


parameters first (to the left) in the
function’s definition
def <function name>(<required params>, <default params>):

10/2/2023 Computer Sys. And Programming


Some Syntax Rules Cont.
 A required parameter is just a name

 A default parameter looks like an


assignment statement

def <function name>(<name>,…, <name> = <expression>,…):

10/2/2023 Computer Sys. And Programming


Ways to Call a Function
def convert(digits, base = 16):

>>> convert('111')
273
>>> convert('111', 16)
273

>>> convert('111', base = 16)


273
>>> convert(digits = '111', base = 16)
273
>>> convert(base = 16, digits = '111')
273

Order is important, unless you use all the keywords


10/2/2023 Computer Sys. And Programming
Processing Lists
listOfStrings = []
for number in listOfNumbers:
listOfStrings.append(str(number))

listOfAbsolutes = []
for number in listOfNumbers:
listOfAbsolutes.append(abs(number))

listOfRoots = []
for number in listOfNumbers:
listOfRoots.append(math.sqrt(number))

Each pattern applies a function to each value in a list


to produce a list of results
10/2/2023 Computer Sys. And Programming
Generalize to a Map

def map(aFunction, listOfArgs):


listOfResults = []
for item in listOfArgs:
listOfResults.append(aFunction(item))
return listOfResults

Each pattern applies a function to each value in a list


to produce a list of results

10/2/2023 Computer Sys. And Programming


The map function
listOfStrings = list(map(str, listOfNumbers))

listOfAbsolutes = list(map(abs, listOfNumbers))

listOfRoots = list(map(math.sqrt, listOfNumbers))

map takes a function and a list as arguments, and returns


an iterable object on the results

Then run list to build a list from this object

10/2/2023 Computer Sys. And Programming


Functions and Data
 In Python, functions are also first-class
data objects

 Functions can be stored in data


structures (lists, dictionaries, etc.)

 Functions can be passed as arguments


to other functions and returned as the
values of other functions

10/2/2023 Computer Sys. And Programming


Higher-Order Functions
 A higher-order function can receive
another function as an argument

 The higher-order function then


applies the argument function in
some manner

 HOFs are a powerful way of


simplifying code
10/2/2023 Computer Sys. And Programming
Mappers
 Sometimes we want to transform a
list of data into a list of results

 Such a transformation is called a


mapping

 Build and return a list that contains


the results of applying a function to
each of the elements in another list
10/2/2023 Computer Sys. And Programming
Example: A List of Square
Roots
oldlist = [2, 3, 4]

newlist = []

for n in oldlist:
newlist.append(math.sqrt(n))

# Do something with newlist

10/2/2023 Computer Sys. And Programming


Example: A List of Square
Roots Cont.
oldlist = [2, 3, 4]

newlist = []

for n in oldlist:
newlist.append(math.sqrt(n))

This type of operation is so common that Python


includes a special function called map to simplify it:
oldlist = [2, 3, 4]

newlist = list(map(math.sqrt, oldlist))


Note that map does not return a list, but we can run
list to get one from it
10/2/2023 Computer Sys. And Programming
Syntax of map

oldlist = [2, 3, 4]

newlist = list(map(math.sqrt, oldlist))

map(<a function>, <a list of arguments>)

A function

A list Another list


map list

10/2/2023 Computer Sys. And Programming


Using map to Simplify Code
fileName = input("Enter the file name: ")
inputFile = open(fileName, "r")

numberList = []
for word in inputFile:
numberList.append(int(word))

if len(numberList) > 0:
print("The number of numbers is", len(numberList))
print("The sum total is", sum(numberList))
print("The average is",
sum(numberList) / len(numberList)
print("The maximum is", max(numberList))
print("The minimum is", min(numberList))
else:
print("The file is empty.")
10/2/2023 Computer Sys. And Programming
Using map to Simplify Code
fileName = input("Enter the file name: ")
inputFile = open(fileName, "r")

numberList = list(map(int, inputFile.read().split()))

if len(numberList) > 0:
print("The number of numbers is", len(numberList))
print("The sum total is", sum(numberList))
print("The average is",
sum(numberList) / len(numberList)
print("The maximum is", max(numberList))
print("The minimum is", min(numberList))
else:
print("The file is empty.")

10/2/2023 Computer Sys. And Programming


Using map
def cube(n):
return n ** 3

oldlist = [2, 3, 4]

newlist = list(map(cube, oldlist))

print(newlist) # Displays [8, 27, 64]

Define the function to use in the mapping, and then map


it onto a list

10/2/2023 Computer Sys. And Programming


Using map Cont.
oldlist = [2.17, 3.46, 4.54]

newlist = list(map(round, oldlist))

print(newlist) # Displays [2, 3, 5]

How could we round to 1 place of precision?

10/2/2023 Computer Sys. And Programming


Using map Cont.
oldlist = [2.17, 3.46, 4.54]

newlist = list(map(round, oldlist, [1, 1, 1]))

print(newlist) # Displays [2.2, 3.5, 4.5]

The figures of precision for round are taken from the


second list argument to map

map(<a function of 2 args>, <list1>, <list2>)

10/2/2023 Computer Sys. And Programming


Using map Cont.
def roundto1place(n): return round(n, 1)

oldlist = [2.17, 3.46, 4.54]

newlist = list(map(roundto1place, oldlist))

print(newlist) # Displays [2.2, 3.5, 4.5]

Alternatively, we could define a new function that


expects one argument and rounds it to 1 place of
precision

10/2/2023 Computer Sys. And Programming


Using lambda for
an Anonymous Function

oldlist = [2.17, 3.46, 4.54]

newlist = list(map(lambda n: round(n, 1), oldlist))

print(newlist) # Displays [2.2, 3.5, 4.5]

lambda creates a function “on the fly,” just for


temporary use

map(lambda <params>: <expression>, <a list of arguments>)

10/2/2023 Computer Sys. And Programming


Simplifying changePerson
def changePerson(sentence):
oldlist = sentence.split()
newlist = []
for word in oldlist:
newlist.append(replacements.get(word, word))
return " ".join(newlist)

Builds a list of the results of applying the method get


to the words in a list

10/2/2023 Computer Sys. And Programming


Simplifying changePerson Cont.
def changePerson(sentence):
oldlist = sentence.split()
newlist = map(lambda word: replacements.get(word, word),
oldlist)
return " ".join(newlist)

Builds a list of the results of applying the method get


to the words in a list

Note that join can work directly with the result of


map, which need not be converted to a list

10/2/2023 Computer Sys. And Programming


Simplifying changePerson Cont.
def changePerson(sentence):
newlist = map(lambda word: replacements.get(word, word),
sentence.split())
return " ".join(newlist)

Much of data processing is simply transforming data


structures into other data structures

10/2/2023 Computer Sys. And Programming


Simplifying changePerson Cont.
def changePerson(sentence):
return " ".join(map(lambda word: replacements.get(word,
word),
sentence.split()))

Much of data processing is simply transforming


collections of data values

10/2/2023 Computer Sys. And Programming


Example: Obtain a List of
Inputs
names = inputList("Enter a name")
ints = inputList("Enter an integer", int)
floats = inputList("Enter a float", float)

def inputList(prompt, convert = str):


"""Returns a list of input values, using the
string prompt and the convert function."""
results = []
while True:
data = input(prompt + " or return to quit: ")
if data == "":
return results
results.append(convert(data))
10/2/2023 Computer Sys. And Programming
Filters
 Sometimes we want to transform a
list by removing elements that do not
pass a test

 Such a transformation is called a filter

 A filter builds the list of elements that


cause a Boolean function to return
True
10/2/2023 Computer Sys. And Programming
Example: A List of Even Numbers
oldlist = <get a list of numbers from somewhere>
newlist = []
for n in oldlist:
if n % 2 == 0:
newlist.append(n)

This type of operation is so common that Python


includes a special function named filter to
simplify it:
oldlist = <get a list of numbers from somewhere>
newlist = list(filter(lambda n: n % 2 == 0, oldlist))

A Boolean function

A list Another list


filter list

10/2/2023 Computer Sys. And Programming


Example: A List of File Names
import os, os.path

lyst = os.listdir(os.getcwd())
filelist = []
for name in lyst:
if os.path.isfile(name):
filelist.append(name)

import os, os.path

filelist = list(filter(os.path.isfile,
os.listdir(os.getcwd())))

A Boolean function

A list Another list


filter list

10/2/2023 Computer Sys. And Programming


Generalize in a New Function
import os, os.path

def getNames(test, path):


return list(filter(test, os.listdir(path)))

filelist = getNames(os.path.isfile, os.getcwd())

dirlist = getNames(os.path.isdir, os.getcwd())

A Boolean function

A list Another list


filter list

10/2/2023 Computer Sys. And Programming


Reducers
 Sometimes we want to use the contents of a
list to compute a single value

 Such a transformation is called a reducer

 A reducer applies a function to pairs of


elements to produce a value

 After each application, the value becomes an


argument for the next application

10/2/2023 Computer Sys. And Programming


Example: Products
oldlist = <get a list of numbers from somewhere>
total = 1
for n in oldlist:
total *= n

This type of operation is so common that Python A list


includes a special function called reduce to
simplify it: A function
reduce
oldlist = <get a list of numbers from somewhere>

from functools import reduce


A value
product = reduce(lambda x, y: x * y, oldlist)

10/2/2023 Computer Sys. And Programming


Thank You

Let's get started!

2023-12-21 Computer Sys. And Programming

You might also like