Functional Decomposition

In this lab, you’ll practice breaking down problems into smaller, more manageable chunks using functions.

Summary (Notes)

Programming Language Syntax

String Literals

The Print Statement

print <expression>
print ('Hello World!')

Function Declaration Statements

def <function name>():
    <statement1>
    <statement2>
    ...
def print_hello():
    print ('Hello World!')

Function Call Statements

<function name>()
print_hello()

Python Programs

<function declaration>

<function declaration>

...

def main():
    <body of main>

main()

The Song of Love

Even with our extremely limited Python skill set we are still able to exercise some of the fundamentals of computational thinking. One of those fundamentals we’ll explore in this homework is the ability to identify structure within a problem that we’re trying to solve and then eliminate redundancy in the code we make to solve that problem.

Problem

A cumulative song song is song whose verses build upon each other. Classic examples of cumulative songs include “There was an Old Lady that Swallowed a Fly” and “The Twelve Days of Christmas”. For this lab, you will be reproducing the lyrics of a more contemporary cumulative song: the “Song of Love” from the Broadway musical comedy Once Upon a Mattress.

For reference, here’s the Song of Love performed by the Bakersfield Theater of Bakersfield, California:

I like you, Fred, I like you!
You're just saying those words to be kind.
No, I mean it.  I like... I mean I love you, Fred!
He is out of his medieval mind!
I'm perfectly sane and sound!  I never felt better in my life!
Everybody... everybody, everybody!  Come on!  And meet my incipient wife!

I'm in love with a girl named Fred.
My reasons must be clear.
When she shows you all how strong she is you'll stand right up and cheer!
With a "F" and a "R" and an "E" and a "D"
And a "F-R-E-D" Fred!  Yeah!

I'm in love with a girl named Fred.
She drinks just like a lord!
So sing a merry drinking song and let the wine be poured!
Fill the bowl to overflowing.  Raise the goblet high!
With a "F" and a "R" and an "E" and a "D"
And a "F-R-E-D" Fred!  Yeah!

I'm in love with a girl named Fred.
She sings just like a bird!
You'll be left completely speechless when her gentle voice is heard!
La la la la, la la la la, la la la la la!
Fill the bowl to overflowing.  Raise the goblet high!
With a "F" and a "R" and an "E" and a "D"
And a "F-R-E-D" Fred!  Yeah!

I'm in love with a girl named Fred.
She wrestles like a Greek!
You will clap your hands in wonder at her fabulous technique!
Clap clap, clap clap, clap clap clap clap, clap, clap clap!
La la la la, la la la la, la la la la la!
Fill the bowl to overflowing.  Raise the goblet high!
With a "F" and a "R" and an "E" and a "D"
And a "F-R-E-D" Fred!  Yeah!

I'm in love with a girl named Fred.
She dances with such grace!
You are bound to sing her praises 'til you're purple in the face!
Bravo!  Bravo!  Bravissimo bravo!  Bravissimo!
Clap clap, clap clap, clap clap clap clap, clap, clap clap!
La la la la, la la la la, la la la la la!
Fill the bowl to overflowing.  Raise the goblet high!
With a "F" and a "R" and an "E" and a "D"
And a "F-R-E-D" Fred!  Yeah!

I'm in love with a girl named Fred.
She's musical to boot!
She will set your feet a-tapping when she plays upon her lute!
Strum strum, strum strum, strum strum strum strum strum, strum.
Bravo!  Bravo!  Bravissimo bravo!  Bravissimo!
Clap clap, clap clap, clap clap clap clap, clap, clap clap!
La la la la, la la la la, la la la la la!
Fill the bowl to overflowing.  Raise the goblet high!
With a "F" and a "R" and an "E" and a "D"
And a "F-R-E-D" Fred!  Yeah!

I'm in love with a girl named Fred.
A clever, clownish wit!
When she does her funny pantomime your sides are sure to split!
Ha ha ha ha, ho ho ho ho, ha ha ha ha ho!
Strum strum, strum strum, strum strum strum strum strum, strum.
Bravo!  Bravo!  Bravissimo bravo!  Bravissimo!
Clap clap, clap clap, clap clap clap clap, clap, clap clap!
La la la la, la la la la, la la la la la!
Fill the bowl to overflowing.  Raise the goblet high!
With a "F" and a "R" and an "E" and a "D"
And a "F-R-E-D" Fred!  Yeah!

I'm in love with a girl.
He's in love with a girl named "F-R-E-D" Fred!

Your goal is to write a program to reproduce exactly the lyrics to the Song of Love as presented above. You should preserve capitalization, punctuation, and spacing. As a rule of thumb, a computer will do exactly what you tell it and no better (or worse), so you will need to be diligent about checking the output of your program to ensure that it matches the format above.

Your program should exist in a Python file called songoflove.py with a main function and appropriate helper functions that main calls.

Style

One easy way to do this program is to copy-paste the above lyrics into your Python file and wrap it in print calls. However, this defeats the purpose of the assignment. Instead you should use functions to both capture the structure of the song and eliminate redundancy of your code.

Capturing structure

As much as possible, your programs should capture the inherent structure of the problem at hand. In this case, your program should reflect the fact that a song can be decomposed into a series of verses. For example, you should have a function for each verse that prints the contents of that verse to the console.

Eliminating redundancy

If you inspect the lyrics, you’ll quickly notice that several lines are repeated, for example, the line I'm in love with a girl named Fred appears in every verse beyond the first and last. While we could write one print for each time we print this line to the console, imagine if we made a typo transcribing the line. We would need to visit each of the print to ensure that we didn’t make the same mistake at each, a code maintenance disaster even by our simple standards!

However, simply factoring out that line into its own method is not enough. You must identify whole groups of lines that are repeated in the song and use functions to eliminate those redundancies. Keep in mind that functions can call other functions. So you’ll need to take a step back and figure out how reduce redundancy not only in groups of repeated print statements but also groups of repeated functions calls as well.

Diffing Output

To ensure that your output exactly matches the text above, you should use a diffing tool. A diffing tool compares two bodies of text and reports any differences it discovers between them. For this homework, you can use diffchecker to compare the output of your program with the text above.