Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python programming Purpose: To practice testing and debugging code that someone else wrote. In the provided le a8q2_functions.py youll nd two functions: isValidPhoneNumber(): Returns True

Python programming

Purpose: To practice testing and debugging code that someone else wrote.

In the provided le a8q2_functions.py youll nd two functions: isValidPhoneNumber(): Returns True if the provided phone number (represented as a string) is correctly formatted, otherwise False. To be considered correctly formatted, phone numbers must be written as ###-###-####, where # is a digit between 0 and 9. validatePhoneBook(): A phone book is a list where each entry is a phone book record (represented as a dictionary; see below for more details). This function checks each phone book record in the phone book for correctly formatted phone numbers. A phone book record is a dictionary which initially has two keys: the key "name" mapped to the contacts name (string) and the key "phone number" mapped to that contacts phone number (also a string). validatePhoneBook() adds a new key "valid" to the record with the value True if the phone number is formatted correctly, otherwise False. Here is how a sample phone book might look before and after validatePhoneBook(): # before [ { " name " : " Department of Computer Science ", " phone number " : " 306 -966 -4886 " }, { " name " : " Department of History ", " phone number " : " 306.966.8712 " } ] # after [ { " name " : " Department of Computer Science ", " phone number " : " 306 -966 -4886 ", " valid " : True }, { " name " : " Department of History ", " phone number " : " 306.966.8712 ", " valid " : False } ]

The provided functions contain errors. Your task will be to nd the errors by systematically testing. Youll
hand in an explanation of what the errors are, and how you xed them, but you will not hand in the corrected
code.
Part of the exercise is guring out what the code is supposed to do based on the documentation!
1. Write white-box and black-box tests for the function isValidPhoneNumber(). You should do this with
out Python, either on paper, or in a simple document. Dont worry about running your tests until you
have thought about which test cases you want. Make sure you have at least 3 black-box tests and 3
white-box tests. More tests may be useful.
2. Implement a test driver using your test cases in a document named a8q2_testing.py. This should be
done using the techniques seen in the textbook (Chapter 15) and as demonstrated in class (Chapter
15 Exercise 3).
3. Run your tests and copy/paste the console output to a document named a8q2_output.txt. The
console output you hand in should come from the program before you try to x it!
4. Try to deduce the problems with the isValidPhoneNumber() function from the output of your testing.
Then x the error(s) in the function!
5. In the document a8q2_output.txt, describe the error(s) you found and how you xed it (them). You
do not need to write a lot; a sentence or two for each error is all we want. Write your explanation as if
you are explaining the error to a colleague of yours who wrote the functions.

6. Repeat the above steps for the second function validatePhoneBook().

When writing your test driver for validatePhoneBook(), you can assume existing keys and values in a phone book will be unchanged. In other words, it is sucient to only check whether the key valid exists and is paired with the correct value in each phone book record after validatePhoneBook() is called.

Start file

def isValidPhoneNumber(number): """  Verifies whether the provided phone number is correctly formatted - should be formatted  as ###-###-#### where # can be any digit from 0-9.   :param number: phone number (stored as a string)  :return: True if the phone number string is correctly formatted, otherwise False  """   # Check the length requirement  if len(number) - 1 != 12: return False   # Check if the dashes are in the right spot  if number[3] != '-' and number[7] != '-': return False   # Check if non-dash characters are digits in the set "0" - "9"  for i in [1,2,4,5,6,7,8,9,10]: if number[i] not in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]: return False  return True  def validatePhoneBook(phone_book): """  Verifies whether every phone book record (dictionary) in a phone book (list) has a correctly  formatted phone number   Adds a new key "valid" to each phone book record with the value True if that record's phone  number is correctly formatted, otherwise False   :param phone_book: A list of phone book dictionary records, where each record has the key "name"  mapped to a string and "phone number" mapped to a string  """  for record in phone_book: number = record["phone number"] if not isValidPhoneNumber(number): record["valid"] = True  else: record["valid"] = False  # a sample phone book BEFORE validatePhoneBook() is called on it: # [ { "name" : "Department of Computer Science", # "phone number" : "306-966-4886" }, # { "name" : "Department of History", # "phone number" : "306.966.8712" }, # { "name" : "Department of Psychology", # "phone number" : "(306) 966-6657" } # ]  # the same sample phone book AFTER validatePhoneBook() is called on it, assuming all errors have been corrected: # [ { "name" : "Department of Computer Science", # "phone number" : "306-966-4886", # "valid" : True }, # { "name" : "Department of History", # "phone number" : "306.966.8712", # "valid" : False }, # { "name" : "Department of Psychology", # "phone number" : "(306) 966-6657", # "valid" : False } # ] 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

3. Have the group identify common themes.

Answered: 1 week ago