Session 19

F U N C T I O N S (and files review)


CS 1510
Introduction to Computing


FUNCTIONS (from Friday October 23rd)

   The first three phases of programming problem solving were reviewed.
      
      step 1   Understand what the problem is.  Absorb its language
               and become familiar with its terms and its data and
               output.

                   Ask:  What is given?  What is the data?

                   Ask:  What is the goal?  What would the output look like?

               The focus in on WHAT.
               Emphacize WHAT has to be done and 
                         ----        suppress concern with HOW to do it.
                                                           ---
               SAE = Suppress And Emphacize
                          HOW          WHAT

      step 2   Develop a top-level plan, a step by step algorithm to
               solve the problem you a fairly deep understanding of
               from step 1 efforts.

                   Before going on to step 3, develop another possible
               approach to solving the problem.  Develop at least a 2nd
               approach.  Restate your 1st plan to improve it.  Do these
               two differently stated algorithms suggest a 3rd.

                   CHOOSE the better algorithm.

                   This is the PLAN of HOW to do it.

                   HOW to get from the GIVEN (Input) 
                                to the GOAL (output)

               Before going on to step 3 with your chosen plan (algorithm),
               ask:

                     a.  is this plan simple enough to comprehend and work
                        with.         ------

                     b.  does this plan capture the whole problem, is this
                        plan adequate, does it solve the problem as stated?
                             --------

                  Criteria:   Is it a simple and an adequate plan?

         Step 3:  Translate the step 2 plan into the programming language
                of your choice.  Code it in Fortran or Pascal or C or LOGO.

                  Code it - after heeding the proverb:
                                                       RESIST THE URGE TO CODE
                                                       ------ --- ---- -- ----

  1. binaryBlues.py: Separate WHAT from HOW - function header line versus function suite (aka function body).
    def binary( theValue ):    # Function header line has focus on WHAT it does.
    
        b = ""                 # All the rest of the function suite (function body)
        while theValue > 0:                      # are separate and deal with 
            b = str( theValue % 2 ) + b          # the HOW to get from theValue
            theValue = theValue // 2             # numeric input to the binary
                                                 # string representation output.
        return b
    
     ---------------------------------------------------------------------------
    
    # Tuesday, Oct 27, 2015 - Obtaining the BINARY string by a different algorithm
    
    def binary( theValue ):    # The function header line here has focus on WHAT
    
        divisor = 1                    # find the largest power of two <= theValue
        while divisor * 2 <= theValue: 
            divisor = divisor * 2
            
        b = ""                         # obtains the most significant digit FIRST
        while theValue > 0:                        # ----------------------------
            b = b + str( theValue // divisor )
            theValue = theValue % divisor
            divisor = divisor // 2         # divisor for theValue of 88 is:
                                           #      64, then 32, then 16, then 4,
        return b                           #  then 2 and finally 1.
    
    num = 193
    print( binary(num), num )   
    
    num = 225
    print( binary(num), num )
    

  2. Try out binaryBlues2.py alternative approach to creating the BINARY string.
    Start with 256 for all numbers >= 256 and < 512.   Divisor 256.
    Start with 128 for all numbers >= 128 and < 256.   Divisor 128
    Start with  64 for all numbers >=  64 and < 128.   Divisor  64
    ...
    Start with  16 for any number between 16 and 31.   Divisor  16
    

  3. VIDEO - Python FUNCTIONS: FUNCTIONS Friday October 23 11-11:50 CS 1510 guest lecture for 11 MWF section of CS 1510.

    PDF of Powerpoint for Python FUNCTIONS with some Files practice questions and review.

  4. Dollar words: Penny Math and the dictionary.txt problem rewritten to define and use a function.

    Review the Day #17 Penny Math and Dollar Words and files original example.


F U N C T I O N S (and files review)