Hacks

Topic 3.12 (3.A):

  1. Define procedure and parameter in your own words
  2. Paste a screenshot of completion of the quiz
  3. Define Return Values and Output Parameters in your own words
  4. Code a procedure that finds the square root of any given number. (make sure to call and return the function)

Topic 3.13 (3.B):

  1. Explain, in your own words, why abstracting away your program logic into separate, modular functions is effective
  2. Create a procedure that uses other sub-procedures (other functions) within it and explain why the abstraction was needed (conciseness, shared behavior, etc.)
  3. 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?)

Topic 3.13 (3.C):

  1. Define procedure names and arguments in your own words.
  2. 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

3.A Hacks:

  1. A procedure is the way of completing a task by code instructions that can return an output from an input/parameter. A parameter is a variable that follows a function that is used in a function to give an output without actually having an input value. They are used to write the function "blueprint".

  2. Below (not an image I completed the quiz on here)

  3. Return values are values that are returned after a return statement. They instruct the program to continue. Output parameters are used to call a function. For example, function(a,b) would be used to call the function, "function" with the parameter of a and b.

  4. Below

questionNum = 3
correct = 0
questions = [
    "What is are correct names for a procedure? \n A) Method \n B) Function \n C) Both",
    "What is a procedure? \n A) Sequencing \n B) Selection \n C) Iteration \n D) All",
    "Use this for following question: \n def inchesToFeet(lengthInches): \n\t lengthFeet = lengthInches / 12 \n\t return lengthFeet \n\n What is the procedure name, the parameter, and what the procedure returns? \n A) feetToInches, lengthInches, lengthMeters \n B) inchesToFeet, lengthInches, lengthFeet \n C) inchesToFeet, lengthFeet, lengthInches \n D) lengthInches, inchesToFeet, lengthFeet"]
answers = ["c", "d", "b"]

def qna(question, answer):
    print("Question:", question)
    response = input()
    print("Answer:", response)
    
    if response.lower() == answer:
        print("Correct :) \n")
        global correct
        correct += 1
    else:
        print("Incorrect :( \n")
for x in range(questionNum):
    qna(questions[x], answers[x])
    
print("Score:", correct, "/ 3")
Question: What is are correct names for a procedure? 
 A) Method 
 B) Function 
 C) Both
Answer: c
Correct :) 

Question: What is a procedure? 
 A) Sequencing 
 B) Selection 
 C) Iteration 
 D) All
Answer: d
Correct :) 

Question: Use this for following question: 
 def inchesToFeet(lengthInches): 
	 lengthFeet = lengthInches / 12 
	 return lengthFeet 

 What is the procedure name, the parameter, and what the procedure returns? 
 A) feetToInches, lengthInches, lengthMeters 
 B) inchesToFeet, lengthInches, lengthFeet 
 C) inchesToFeet, lengthFeet, lengthInches 
 D) lengthInches, inchesToFeet, lengthFeet
Answer: b
Correct :) 

Score: 3 / 3
import math

number = input("Input a number you would like to take the square root of")

def square_root(num):
    result = math.sqrt(num)
    return result

square_root_input = square_root(int(number)) # calculates the square root of the input (ex. 36)
squareone = square_root(9) # calculates the square root of 9
squaretwo = square_root(16) # calculated the square root of 16

print('The square root is: ', squareone)
print('The square root is: ', squaretwo)
print('The square root of ', number, " is ", square_root_input)
The square root is:  3.0
The square root is:  4.0
The square root of  36  is  6.0

3.B Hacks

  1. Abstracting your program into separate, modular functions is a way of simplifying your code so that it can be reused throughout the program without having to be rewritten. Doing this makes programs much more readble by other programers and is easier to debug than programs with duplication.

  2. below

questionnumber = 3
meals = []
questions = [
    "What did you eat for breakfast? Bacon, eggs, or cereal?",
    "What did you eat for lunch? A sandwich, mac and cheese, or a lunchable?",
    "What did you eat for dinner? Pizza, pasta, or a salad?"
]

def mealask(question):
    print("Question: ", question)
    response = input()
    print("Your response: ", response)
    meals.append(response)

for number in range(questionnumber):
    mealask(questions[number])

for i in meals:
    if i == "cereal" or i == "lunchable" or i == "pizza":
        print(i, " was not a healthy choice")
    elif i == "bacon" or i == "mac and cheese" or i == "pasta":
        print(i, " was not a bad choice")
    else:
        print(i, " was a healthy choice")
Question:  What did you eat for breakfast? Bacon, eggs, or cereal?
Your response:  bacon
Question:  What did you eat for lunch? A sandwich, mac and cheese, or a lunchable?
Your response:  lunchable
Question:  What did you eat for dinner? Pizza, pasta, or a salad?
Your response:  salad
bacon  was not a bad choice
lunchable  was not a healthy choice
salad  was a healthy choice

In the algorithim above, abstraction was needed due to shared behaior and conciseness. Without abstraction, I would have had to write all of the questions, their inputs, and the responses out individually. This saved me time and made the code more simple to follow and look at.

  1. below ^ EXTRA CREDIT: I have also decided to work extra and figure out a way to list the number of words that start with each letter in the alphebet (unless is zero words start with that letter)
import string
# this function takes a string as input and returns a list of words, where each word
# 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 

letter_tested = input("What letter would you like to count?")
def count_words_starting_with_input_in_string(s):
    words = split_string(s)
    count = count_words_starting_with_letter(words, letter_tested)
    return count

def count_words_starting_with_letter_in_string(s):
    words = split_string(s)
    count_dictionary = {}
    for c in string.ascii_lowercase:
        count_dictionary[c] = count_words_starting_with_letter(words, c)
    return count_dictionary


# 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)
letter_count = count_words_starting_with_input_in_string(s)
count_dictionary_print = count_words_starting_with_letter_in_string(s)
print("Words starting with a:", a_count)
print("Words starting with d:", d_count)
print("Words starting with", letter_tested,":", letter_count)
print("\nChecking all letters of alphabet:")
for letter in count_dictionary_print:
    if count_dictionary_print[letter] != 0:
        print("Words starting with", letter,":", count_dictionary_print[letter])
Words starting with a: 1
Words starting with d: 1
Words starting with y : 1

Checking all letters of alphabet:
Words starting with a : 1
Words starting with c : 1
Words starting with d : 1
Words starting with i : 2
Words starting with s : 1
Words starting with t : 4
Words starting with y : 1

Hacks 3.C

  1. Procedure names are names that describe a function and can be used to call a function. Procedure names make it easier for other programers to identify a function's purpose and use throughout a program. Arguments are within parameters which provide functions with information.

  2. below

a = 2

b = 5