Question

In: Computer Science

PYTHON! Exercise 3 - Total Line length Write a python function that will return the total...

PYTHON!

Exercise 3 - Total Line length

Write a python function that will return the total length of line that passes through any number of provided points ( (x,y) ). The points should be passed as individual tuples or lists. The function should also have a parameter (True or False) to indicate whether the line should start from the origin, and that parameter should default to False. If True, the returned value should include the distance from the origin to the first point, otherwise start adding distances from the first point. So a function call could look something like this:

dist = lineLength((1,2), (2,3), (7,4), start=False)

Demonstrate it in your main program by calculating the length of line going through the following 5 points (with and without the origin option set to True):

(1,1), (-2,4), (-3,-2), (2,-1), (1,1)

PreviousNext

Solutions

Expert Solution

In this program, we have to find the length of the line connecting the given points in the argument list of the function lineLength().

To do this , we must use the argument *argv in the function. This argument will take variable length of points argument list. Remember that the user can provide as many points as they want to this function.

Then, we will define a variable named dist and set it to 0. This variable will store the lenght of the connecting line.

Then, loop through each point in the argument list, and find the distance between the current point and point in the next index. This distance will be added to the dist variable.

Finally, if start parameter is True, then add the distance between the first point and the origin to the dist variable.

dist variable will be returned from the function

In main() function, call lineLenght() with the points given in question, that is , (1,1), (-2,4), (-3,-2), (2,-1), (1,1). Once call it with start set to False, and once more with start set to True. Print both the results

program:

def lineLength(*argv,start = False):
points = argv
dist = 0
for i in range(len(points)-1):
dist_ = (points[i+1][0]-points[i][0])**2 + (points[i+1][1]-points[i][1])**2
dist_ = dist_**0.5
dist += dist_

if start:
dist_ = (points[0][0])**2 + (points[0][1])**2
dist_ = dist_**0.5
dist += dist_

return dist

def main():
dist = lineLength((1,1), (-2,4), (-3,-2), (2,-1), (1,1))
print(f"Length of line connecting given points when start is False: {dist}")
dist = lineLength((1,1), (-2,4), (-3,-2), (2,-1), (1,1),start=True)
print(f"Length of line connecting given points when start is True: {dist}")

main()

output:


Related Solutions

Write a python function that will return the total length of line that passes through any...
Write a python function that will return the total length of line that passes through any number of provided points ( (x,y) ). The points should be passed as individual tuples or lists. The function should also have a parameter (True or False) to indicate whether the line should start from the origin, and that parameter should default to False. If True, the returned value should include the distance from the origin to the first point, otherwise start adding distances...
(IN PYTHON) Write a function that accepts a line of text and a single letter as...
(IN PYTHON) Write a function that accepts a line of text and a single letter as input (case insensitive) and returns the number of times the letter is the last character of a word. Note your program should be able to handle different cases. And check if the user input is a single letter.
Write a program function code in Python that prompts the user to enter the total of...
Write a program function code in Python that prompts the user to enter the total of his/her purchase and add 5% as the VAT. The program should display the total without and with VAT. ( that mean i should ask the user to enter the number of their item " so i think that i should use range" then print the result with and without the VAT )
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the...
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the total sum of the digits in the integers from 1 to that number inclusive. b. Write a program to input an integer n and call the above function in part a if n is positive, else give ‘Value must be Positive’ message. sample: Enter a positive integer: 1000000 The sum of the digits in the number from 1 to 1000000 is 27000001 Enter a...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the given wordlist  An anagram dictionary has each word with the letters sorted alphabetically creating a “key”.
Using Python 3, define mySum function that supposed to return the sum of a list of...
Using Python 3, define mySum function that supposed to return the sum of a list of numbers (and 0 if that list is empty), but it has one or more errors in it. Write test cases to determine what errors there are.
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
Exercise 1. Rectangle, Circle and Square Write three Python classes named Rectangle constructed by a length...
Exercise 1. Rectangle, Circle and Square Write three Python classes named Rectangle constructed by a length and width, a Circle constructed by a radius and a Square constructed by a side length. Both classes should have the methods that compute: - The area - The diagonal - The perimeter Use as much abstraction as you can. At the end of the file, use those classes to calculate the perimeter of a circle with radius the half of the diagonal of...
Part 1: Write a Python function called reduceWhitespace that is given a string line and returns...
Part 1: Write a Python function called reduceWhitespace that is given a string line and returns the line with all extra whitespace characters between the words removed. For example, ‘This line has extra space characters' 'This line has extra space characters’ • Function name: reduceWhitespace • Number of parameters: one string line • Return value: one string line The main file should handle the file operations to read from a .txt file you create and call the function from the...
*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length...
*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length of all the lists. This problem can be solved two different ways. 3. Write a program that prompts the user to enter two numbers and then outputs the sum of the two numbers. 4.Write ALLODDP, a recursive function that returns T if all the numbers in a list are odd.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT