λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

University of California, Berkeley/ElectricalEngineering & ComputerSciences

Functional Decomposition & Debugging

Program Decomposition

-   The process of breaking a large problem into more manageable sub-problems

: It's often easier to write 4 250-line functions than 1 1000-line function

-   Goals of Program Decomposition

: complexity management

: reusability and abstraction

The "Black Box"

-   A "black box" is opaque _ its inner workings are not revealed

: data abstraction is an example of this

: for constructors & selectors we don't care about the body of the function. We only need to know what they take as input and their intended result

-   Decomposition is a divide and conquer strategy

: from the outside, black boxes are simply defined in terms of

Function Rules in Practice

Q   what should a function's parameters be ?

Q   when is a task sufficiently complex or independent to merit being put in its own function ?

-   a function should solve one problem

-   a function should have the smallest number of parameters possible to solve its problem

-   a repeated sequence should be put in its own function

An example : Hog

-   The code for Hog was broken down into

hog.py : functions that implement the logic of the Game of Hog

hog_gui.py : a Graphical User Interface for the Game of Hog

ucb.py : utility functions for cs61a _ trace(fn) is used for debugging

dice.py : functions that simulate dice rolls

   make_fair_dice(sides) returns a fair die

   make_test_dice(*outcomes) returns a rigged die

An example : Pig Latin

-   In your new project, you will implement the function pig_latin

-   Pig Latin

: Rule One _ when a word begins with a consonant or a consonant cluster, simply take the consonant/consonant cluster and move it to the end of the word, adding the suffic 'ay' to the end of the word

: Rule Two _ when a word begins with a vowel, leave the word as it & add the suffix 'way'

Q   What is the 'problem' that Pig Latin solves ?

Q.  should Pig Latin be broken up into multiple functions ?

-   Decomposition is also relevant inside of functions

: we break each function down into smaller subproblems in order to logically implement the function

-   Pig Latin takes in a single English wor

: first, find a vowel

: next, split the word up into parts

: then, form the "Pig Latin" version of the word using the parts

Doctests : isPal

-   Palindromes ( e.g. racecar )

-   isPal ( x ) 

input _ list

output _ True of False, depending on whether x is a palindrome

-   isPalN ( n, lst )

input _ integer, list

output _ Prints "is a palindrome" or "is not a palindrome", depending on whether the first n elements of x form a plaindrome

Preventing Bugs : Assert Statements

-   I want to catch some bugs as soon as they are introduced

-   Assert statements

: Internal self-checks for your program _ they declare some condition MUST be true

-   Assert syntax

: assert < condition >, < error message >

Understanding Errors

-   Hog Project : announce_highest

( a higher-order function that returns a commentary function )

 

'University of California, Berkeley > ElectricalEngineering & ComputerSciences' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

LAB_day 5  (0) 2019.07.11
Trees  (0) 2019.07.11
LAB_day 4  (0) 2019.07.09
Sequence & Data Abstraction  (0) 2019.07.09
Week 3  (0) 2019.07.09