Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Useing Jython Environment for Students (JES), Version 5.0 If you dont have Version 5.0 please do not do it. The program is going to work

Useing Jython Environment for Students (JES), Version 5.0

If you dont have Version 5.0 please do not do it.

The program is going to work with a list of numbers and identify which ones are Pythagorean Triples. First, however, we need a little more information about lists than we covered in lecture.

Lists within Lists Just like how we saw that if statements may be nested inside one another, list values can themselves be lists! That probably sounds confusing, so lets look at an example.

Points = [ [ 1, 2 ], [ 47, 18 ], [19, 495], [0,0] ]

This defines a list, points, meant to contain a series of (x,y) points. Its length is 4 since it holds four values (each of the four values marked in colors). These values happen to be lists themselves since a 2-item list is a natural representation of a point. Below is some code making use of this list, where the outputs follow the ->.

printNow( len( points ) ) -> 4

printNow( points[ 0 ] ) -> [1,2]

point = points[ 1 ] printNow( point ) -> [47,18]

printNow( len( point ) ) -> 2

printNow( point[ 0 ] ) ) -> 47

printNow( point[ 1 ] ) ) -> 18

x = point[ 0 ]

y = point[ 1 ]

As you can see, Python has no trouble handling nested lists. Again, the points list holds four values, each being a list holding two values. So, the way we use them is to first access the points list using normal [ ] accessing syntax. This will give us another list that represents a particular point, which is conveniently assigned to the point variable in the examples. We can then use [ ] on point to access the two values in that list to get the x and y values.

Finding Pythagorean Triples Now we can get down to business. A Python file has been attached to this assignment giving you a start, in that it defines a list called triples whose values are lists composed of three (rather than the two above) values representing possible sides of a triangle. It is a mix of Python code and comments that serve as pseudocode as you have seen before. Your code must loop through the list of triples and find any Pythagorean Triples it contains; outputting both the triple and the location in the list it was found. So, for the list provided in the attached file, the output should be:

Triple 3, 4, 5 found at index 2

Triple 5, 12, 13 found at index 3

Incremental Development To make things a little more manageable, lets try doing this in stages. The first step will be to set up the while loop so that you simply output all the triples no matter what they are

. Triple 1, 1, 1 found at index 0

Triple 6, 7, 9.219544457292887 found at index 1

Triple 3, 4, 5 found at index 2

Triple 5, 12, 13 found at index 3

Triple 4, 5, 6.4031242374328485 found at index 4

Next, alter the program so that it does a test before printing: test the triplet to make sure it satisfies the a2 + b2 = c2 equation before printing. This should knock off the first triple from the programs output:

Triple 6, 7, 9.219544457292887 found at index 1

Triple 3, 4, 5 found at index 2

Triple 5, 12, 13 found at index 3

Triple 4, 5, 6.4031242374328485 found at index 4

Finally, add the test to make sure that all three numbers are integers since this is a requirement of a Pythagorean Triple. How do you test for an integer? There is a built-in function we can take advantage of called isinstance that returns a True or a False value. It is a function like any of the others we have seen in that it takes datain this case a number and the data typeand returns a valueTrue or Falseso you just need to call it properly. If you are still shaky on function calls you should probably review those slides now, calling functions is a vital skill in programming. Below is an example of how isinstance might be used.

if isinstance( 5, int ):

printNow( 5 is an int! ) # prints

if isinstance( 5.5, int ):

printNow( 5.5 is an int! ) # does not print

If you ran the above code in JES you would only see 5 is an int! because the second if statement will get a False from the isinstance call. Use this in combination with the test you already wrote that verifies the a2 + b2 = c2 equation. Make sure you check that all three parts of the set are integers!

Your final output for the finished program should be

Triple 3, 4, 5 found at index 2

Triple 5, 12, 13 found at index 3

You may want to play around with adding more triples that are valid and invalid as well as those that contain mixes of floats and integers. Remember, debug your programs thoroughly!

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

Recommended Textbook for

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students also viewed these Databases questions