CS1101-Unit04 programming assignment

docx

School

University of the People *

*We aren’t endorsed by this school

Course

1101

Subject

Computer Science

Date

Jan 9, 2024

Type

docx

Pages

10

Uploaded by BaronEmuPerson965

Report
Part 1 In taking an incremental development approach to coding my hypotenuse function, my first step is to simply outline the function in such a way that I can test it before adding any body code. The function will take two arguments, one for each of the two shorter sides of a right triangle, which I have called a and b. Prior to defining the function further, I have instructed it to always return 0.0 in order to validate that the syntax is correct and that the code runs. This is an example of “scaffolding,” or code that is used during development but not part of the final program (Downey, 2015, p. 53.) When entering 3 and 4 as our sample arguments, the function returns 0.0 as expected, confirming that so far everything is working as it should. Next, I begin to address the calculations involved in determining the hypotenuse length based on the Pythagorean theorem, which states that c² = a² + b² (where “c” refers to the length of the hypotenuse, and “a” and “b” refer to the lengths of the two shorter sides.) Again breaking the process down into small steps, I start by assigning variables for a² (“asquared”) and b² (“bsquared”) using the exponentiation operator ** to perform the calculations. I add instructions to print “asquared is __” and “bsquared is __,” enabling me to validate the calculations.
When I call the function with my test arguments 3 and 4 once again, it returns the anticipated responses, that asquared is 9 and bsquared is 16 . Since asquared and bsquared are correct, the next logical step is adding them together. I have created variable csquared to represent c², which equals asquared + bsquared, as well as the instruction to print validation message “csquared is __.” As expected, test arguments 3 and 4 return the correct response, csquared is 25 .
To finally arrive at the length of the hypotenuse (or the value of “c” in our equation,) the only remaining task is to determine the square root of csquared (or c².) I chose to use the square root function built into Python’s math module for this. It is generally acceptable to take a “leap of faith” when utilizing Python’s built-in functions, meaning it is safe to assume they will work as intended (Downey, 2015, p. 57.) Therefore, I completed my hypotenuse function by instructing it to print the final result. We finally arrive at the correct result; as expected, the value of c (and the length of our hypotenuse, in this example,) is 5. Here are some additional examples of the hypotenuse function’s output, given different arguments:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Part 2 I would like to write a program that asks for user input text and then counts the number of vowels and consonant characters it contains. I begin by assigning variables called “text” (keyboard input the user will be prompted to enter,) “vowels,” and “consonants.” Prior to receiving any user input, vowels and consonants will both start at 0. I utilize the print function in order to validate that the user prompt is working correctly and vowels and consonants do indeed have current value of 0. Next, I decide to use a for loop, as described in ForLoop - Python Wiki (n.d.), to loop through and check each character in the text. I am using conditional statements to determine whether each character is a vowel (in which case it will add 1 to the vowels variable) or not (in which case it will add 1 to the consonants variable.)
To test my code, I decide to use test input “aaa xxx” because I already know that if the code is working as intended it should produce known output counts of 3 vowels and 3 consonants. However, this is not the output that I receive, as shown here: This minor hiccup leads me to realize that I have failed to account for non-alphabetic characters, such as numbers or punctuation (or spaces, which was my specific oversight.) I would like my program to ignore such characters. After some research, it seems the easiest way to accomplish that is by implementing Python’s built-in string method to check whether each subsequent character is alphabetic or not as it proceeds through checking them ( Built-in Types , n.d.) By nesting the vowel/consonant checking statements under the conditional statement if
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
i.isalpha(): , it will only count a character as a vowel or consonant if it first determines that the character is indeed alphabetic; otherwise, the character will be ignored. Using our test input “aaa xxx” once more, the program now produces the correct output count: Finally, I clean up the code and made it more user-friendly by providing a clearer prompt for user input and adding a brief contextual explanation to the output print statement.
Additional output examples:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
References Downey, A. B. (2015). Think Python: How to Think Like a Computer Scientist. O’Reilly Media. ForLoop - Python Wiki . (n.d.). https://wiki.python.org/moin/ForLoop Built-in types. (n.d.). Python Documentation. https://docs.python.org/3/library/stdtypes.html? highlight=isalpha#string-methods