Hack # 1

Topic 3.12 (3.A):

  1. Define procedure and parameter in your own words
  • parameters are the variable that you use in code (like name or age) and procedures are kind of a way of telling the program what to do (multiply() or print()).
  1. Paste a screenshot of completion of the quiz screenshot
  2. Define Return Values and Output Parameters in your own words
  • a return value basically returns whatever the program is running, which means that it keeps the program running. Output parameters are, I think, the values that the program exits, or outputs. So whatever is printed.
  1. Code a procedure that finds the square root of any given number. (make sure to call and return the function)
x = 64

def sqrt(x):
    value = x**0.5
    return value

answer = sqrt(x)
print(answer)
8.0

Hack #2

Topic 3.13 (3.B):

  1. Explain, in your own words, why abstracting away your program logic into separate, modular functions is effective
  • it makes it more user friendly. Since abstraction hides particular details of how a code works, it makes it both manages complexity and make it more easier for people to see and use.
  1. Create a procedure that uses other sub-procedures (other functions) within it and explain why the abstraction was needed (conciseness, shared behavior, etc.)
import math

values = [4, 9, 16, 25, 36, 49]

squareroots = [math.sqrt(number) for number in values]

print("The Original Values:\n", values)
print("The Square Root Values:\n", squareroots)
The Original Values:
 [4, 9, 16, 25, 36, 49]
The Square Root Values:
 [2.0, 3.0, 4.0, 5.0, 6.0, 7.0]

The abstraction above is needed, more like prefered, because it makes it easier to take the square root of a bunch of numbers at once, instead of having to type in the square root function for each number. It then becomes really repetative and boring and makes life harder, whereas if we wanted to add a new number to take the square root of, we just add it to the list.

  1. Add another layer of abstraction to the word counter program (HINT: create a function that can count the number of words starting with ANY character in a given string -- how can we leverage parameters for this?)

I tried adding a list at the very end. But in this case, it wasn't very efficient but gets the job done.

# is a separate element in the list
def split_string(s):
    # use the split() method to split the string into a list of words
    words = s.split(" ")

	# initialize a new list to hold all non-empty strings
    new_words = []
    for word in words:
        if word != "":
            # add all non-empty substrings of `words` to `new_words`
            new_words.append(word)
    
    return words

# this function takes a list of words as input and returns the number of words
# that start with the given letter (case-insensitive)
def count_words_starting_with_letter(words, letter):
    count = 0
    
    # loop through the list of words and check if each word starts with the given letter
    for word in words:
        # use the lower() method to make the comparison case-insensitive
        if word.lower().startswith(letter):
            count += 1
    
    return count

# this function takes a string as input and returns the number of words that start with 'a'
def count_words_starting_with_a_in_string(s):
    # use the split_string() function to split the input string into a list of words
    words = split_string(s)
    
    # use the count_words_starting_with_letter() function to count the number of words
    # that start with 'a' in the list of words
    count = count_words_starting_with_letter(words, "a")
    
    return count

# see above
def count_words_starting_with_d_in_string(s):
    words = split_string(s)
    count = count_words_starting_with_letter(words, "d")
    return count

def count_words_starting_with_c_in_string(s):
    words = split_string(s)
    count = count_words_starting_with_letter(words, "c")
    return count

# example usage:
s = "   This is  a  test  string! Don't you think this is cool? "
a_count = count_words_starting_with_a_in_string(s)
d_count = count_words_starting_with_d_in_string(s)
c_count = count_words_starting_with_c_in_string(s)
count_letter = [a_count, d_count, c_count]
print("Words starting with a:", a_count)
print("Words starting with c:", c_count)
Words starting with a: 1
Words starting with c: 1

Hack #3

Topic 3.13 (3.C):

  1. Define procedure names and arguments in your own words.
  • A procedure name is a that a function has or a procedure has and arguments are information provided to a function
  1. Code some procedures that use arguments and parameters with Javascript and HTML (make sure they are interactive on your hacks page, allowing the user to input numbers and click a button to produce an output)
    • Add two numbers
    • Subtract two numbers
    • Multiply two numbers
    • Divide two numbers

<!-- function is called here -->
<button id="enter" onclick="print(a,b)">Multiply Button</button> 
<p id="result"></p>
<!-- javascript -->
<script>
    function print(a,b) {
        document.getElementById("result").innerHTML = a * b // math
    }
    // variables are defined
    var a = 7
    var b = 10
</script>

I figured out how to do all of the math functions, but it wouldn't let me do all four of them at the same time. :(