CSc 250 Lab 4

In this lab, you will be tasked with writing several functions that have parameters and returns, and then calling them a few times to test their behavior.

Problem 1

Create a new program called pythagorean.py. In this program file, write one function named pythagorean. This function will compute the length of the hypotenuse of a right triangle. The function will accept two parameters (the length of the two sides of a right triangle adjacent to the 90-degree angle) and it will return the length of the hypotenuse. Once you write the function, test it to ensure it works properly. These tests:
print(pythagorean(1, 2))
print(pythagorean(5, 10))
print(pythagorean(12, 4))
Should produce this output:
2.23606797749979
11.180339887498949
12.649110640673518
Note that you might want to import the math module, and use math.sqrt() to compute a square root.

Problem 2

Create a code file named password.py. In this file, you will write a function named validate_password. This function will accept a string as a parameter, and ensure that it meets the minimum password requirements. The password requirements are:
  • Be all digits and alphabetical characters
  • Must contain at least one digit
  • must contain at least one letter
  • must be at least 9 characters
  • must not begin with the letter z
For example, these tests:
print(validate_password('hello'))
print(validate_password('ziThere8787'))
print(validate_password('HiThere8787'))
print(validate_password('1234567890'))
print(validate_password('foreverAndADay'))
print(validate_password('1a2b3c4d5e'))
Should produce:
False
False
True
False
False
True

Problem 3

Create a program file named extract.py. In this file, write a function named first_middle_last. This function will take a single string as input. It will "extract" the first, middle, and last character from the string, combine them into a new string, and then return the new string. This test case:
print(first_middle_last('abcde'))
print(first_middle_last('12345557890'))
print(first_middle_last('CSC250'))
print(first_middle_last('W   I   N'))
print(first_middle_last('this is a significantly longer string than the rest of them'))
Should produce this output:
ace
150
C20
WIN
trm

You must turn in at least 2 of the problems on D2L to get credit for this.

Solutions: