Assignment 1

Quadratic Formula

Background

The quadratic formula is a useful equation for finding the roots of a quadratic equation. The quadratic equation has the form:

ax2 + bx + c = 0

where a, b, and c are constants, a is not zero, and x is unknown. The values of x that make the equation true are called the roots of the quadratic equation.

There are many ways to find the roots of a quadratic equation, but a simple way is to use the quadratic formula:

x = (-b ± √b2 - 4ac) / (2a)

Note that the symbol "±" means to add and subtract. In other words, this is actually two equations, one where you add the two terms and another where you subtract them.

Program

Write a program that:

  1. Asks the user for a, b, and c (explaining what each represents).
  2. Computes the two roots.
  3. Displays the two roots (and some descriptive text). Round the roots to two decimal places.

An example program is:

This program will find the roots of a quadratic equation of the form a*x^2 + b*x + c = 0
Please enter a: 2
Please enter b: 4
Please enter c: -8
The two roots are:
x = 1.24
x = -3.24

You may want to use a function from the math module to take the square root.

What kinds of inputs could cause your program to crash, throw an exception/error, or not behave as the user might expect? There are at least three kinds. One is almost explicitly stated in the background section and for another, consider what happens when you take the square root of a negative number. For these two, use if statements to catch the problem, print an error message, then stop the program (use the exit method from the sys module). For the third kind of problem, identify it in the assignment information sheet since we have not yet seen how to handle the problem.

Contextual Search

Python's default search functions (find and index) tell you the starting location of the item being searched for. Often, it is useful to the user to see some context as well (as most search engines do). For this program, ask the user for a string to search for and display the first occurrence with 10 characters of context (10 characters before the string is found and 10 characters after the end of the string).

You will be searching through the first chapter of Pride and Prejudice, provided for you below. You may copy and paste this directly into your program.

chapter = '''It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.
However little known the feelings or views of such a man may be on his first entering a neighbourhood, this truth is so well fixed in the minds of the surrounding families, that he is considered the rightful property of some one or other of their daughters.
"My dear Mr. Bennet," said his lady to him one day, "have you heard that Netherfield Park is let at last?"
Mr. Bennet replied that he had not.
"But it is," returned she; "for Mrs. Long has just been here, and she told me all about it."
Mr. Bennet made no answer.
"Do you not want to know who has taken it?" cried his wife impatiently.
"You want to tell me, and I have no objection to hearing it."
This was invitation enough.
"Why, my dear, you must know, Mrs. Long says that Netherfield is taken by a young man of large fortune from the north of England; that he came down on Monday in a chaise and four to see the place, and was so much delighted with it, that he agreed with Mr. Morris immediately; that he is to take possession before Michaelmas, and some of his servants are to be in the house by the end of next week."
"What is his name?"
"Bingley."
"Is he married or single?"
"Oh! Single, my dear, to be sure! A single man of large fortune; four or five thousand a year. What a fine thing for our girls!"
"How so? How can it affect them?"
"My dear Mr. Bennet," replied his wife, "how can you be so tiresome! You must know that I am thinking of his marrying one of them."
"Is that his design in settling here?"
"Design! Nonsense, how can you talk so! But it is very likely that he may fall in love with one of them, and therefore you must visit him as soon as he comes."
"I see no occasion for that. You and the girls may go, or you may send them by themselves, which perhaps will be still better, for as you are as handsome as any of them, Mr. Bingley may like you the best of the party."
"My dear, you flatter me. I certainly have had my share of beauty, but I do not pretend to be anything extraordinary now. When a woman has five grown-up daughters, she ought to give over thinking of her own beauty."
"In such cases, a woman has not often much beauty to think of."
"But, my dear, you must indeed go and see Mr. Bingley when he comes into the neighbourhood."
"It is more than I engage for, I assure you."
"But consider your daughters. Only think what an establishment it would be for one of them. Sir William and Lady Lucas are determined to go, merely on that account, for in general, you know, they visit no newcomers. Indeed you must go, for it will be impossible for us to visit him if you do not."
"You are over-scrupulous, surely. I dare say Mr. Bingley will be very glad to see you; and I will send a few lines by you to assure him of my hearty consent to his marrying whichever he chooses of the girls; though I must throw in a good word for my little Lizzy."
"I desire you will do no such thing. Lizzy is not a bit better than the others; and I am sure she is not half so handsome as Jane, nor half so good-humoured as Lydia. But you are always giving her the preference."
"They have none of them much to recommend them," replied he; "they are all silly and ignorant like other girls; but Lizzy has something more of quickness than her sisters."
"Mr. Bennet, how can you abuse your own children in such a way? You take delight in vexing me. You have no compassion for my poor nerves."
"You mistake me, my dear. I have a high respect for your nerves. They are my old friends. I have heard you mention them with consideration these last twenty years at least."
"Ah, you do not know what I suffer."
"But I hope you will get over it, and live to see many young men of four thousand a year come into the neighbourhood."
"It will be no use to us, if twenty such should come, since you will not visit them."
"Depend upon it, my dear, that when there are twenty, I will visit them all."
Mr. Bennet was so odd a mixture of quick parts, sarcastic humour, reserve, and caprice, that the experience of three-and-twenty years had been insufficient to make his wife understand his character. Her mind was less difficult to develop. She was a woman of mean understanding, little information, and uncertain temper. When she was discontented, she fancied herself nervous. The business of her life was to get her daughters married; its solace was visiting and news.'''

Program

Write a program that:

  1. Asks the user for a string to search for.
  2. Search for the string.

Here is an example of a program finding the search string:

What text would you like to search for in the first chapter of Pride and Prejudice? Liz
The text 'Liz' was found! Here it is with some context:
my little Lizzy."
"I de

Here is an example of a program not finding the search string:

What text would you like to search for in the first chapter of Pride and Prejudice? Darcy
The text 'Darcy' was not found!

Edge Cases

The program description above works in most cases, but strange things can happen when you search for something at the beginning or end of the string.

Searching for 'news.', which only appears at the very end of the string:

What text would you like to search for in the first chapter of Pride and Prejudice? news.
The text 'news.' was found! Here it is with some context:
iting and news.

Notice that only context before the search string is shown. This makes sense because there is nothing else after "news.", so Python couldn't show you anything else even though you ask it to. In other programming languages, trying to access characters after the end of the string could cause the program to access memory it shouldn't access (similar to the Heartbleed bug in the news about a year ago) or cause an exception/error to be raised. Python, with its philosophy of "do the simple thing and don't worry about the details", just ends the string where the string ends without any problems.

Since there's nothing to show after the end of the string, it's ok to show no context after the end of the string. Unfortunately, searching for 'It', which first appears at the start of the string, is more complicated:

What text would you like to search for in the first chapter of Pride and Prejudice? It
The text 'It' was found! Here it is with some context:

Notice that neither the context nor the original string are shown. The reason for this is that 'It' appears at position 0, so getting the 10 characters before that gives you a position of -10. Why doesn't Python just convert that to 0? Because negative indices have a special meaning in Python. We'll talk more about that when we get to lists. For now, if getting 10 characters before the search string gives a negative position, just use a starting position of zero. You may find the max function useful. When it is given two (or more) values, it returns the largest value.

The correct behavior for your program when finding something at the start of the string is:

What text would you like to search for in the first chapter of Pride and Prejudice? It
The text 'It' was found! Here it is with some context:
It is a trut

Additional Challenges

If you would like to challenge yourself, try one (or more) of these:

  1. Case-insensitive context search: Here, the user can enter text with any capitalization and the string is found. The result displayed should still have the original capitalization. For example: What text would you like to search for in the first chapter of Pride and Prejudice? liz
    The text 'liz' was found! Here it is with some context:
    my little Lizzy."
    "I de
  2. Word-based context: Instead of giving 10 characters of context (which often only gives partial words for context), show the word before and the word after for context. For example: What text would you like to search for in the first chapter of Pride and Prejudice? Netherfield
    The text 'Netherfield' was found! Here it is with some context:
    that Netherfield Park
    You may find the rfind method useful for part of this.

Final Notes

Submission and Grading:

Complete the Assignment Information Sheet.

Submit your final programs and assignment information sheet (zipped into one file) to CourseWeb in the Assignment 1 assignment.

The grading rubric can be found here: Rubric (doc).

The assignment is due Wednesday, June 3 by 11:59 pm. As with all programming assignments, you have unlimited uploads (before the deadline), so you may upload the assignment before the deadline. If you later decide to upload another, you may do so without penalty (as long as it's before the assignment deadline). The last submission uploaded before the deadline will be the one graded. If you would like ungraded feedback on a programming assignment, you may send an email to your TA or the instructor and ask for feedback; please send your code as well.

For more advice on submitting your assignment, see the Programming Assignments section of the Tips for Success page.