Question

PYTHON Write a python function that will return the total length of line that passes through...

PYTHON

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)

Homework Answers

Answer #1

Python3 code:

# function to calculate the line length
def lineLength(*points, start = False):
    points = list(points)
    if start == True:
        points = [(0, 0)] + points
    
    # Calculate the euclidean distance between the consecutive points
    dist = 0
    for i in range(len(points) - 1):
        length = ((points[i+1][0] - points[i][0])**2 + (points[i+1][1] - points[i][1])**2 )**0.5
        dist += length
    
    return dist

# main function
def main():

    # Testing for two cases
    dist1 = lineLength((1,1), (-2,4), (-3,-2), (2,-1), (1,1), start = False)
    dist2 = lineLength((1,1), (-2,4), (-3,-2), (2,-1), (1,1), start = True)

    # Print the output
    print("Length of the line without origin:", dist1)
    print("Length of the line with origin:", dist2)

if __name__ == "__main__":
    main()

Please refer to the following picture for the source code and sample output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list strings and the second...
Write a Python function that takes two parameters: the first a list strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False.
WRITE IN PYTHON Using any large text file or any literature English book in .txt format....
WRITE IN PYTHON Using any large text file or any literature English book in .txt format. The program will read a .txt file and process the information Write a module called, “book_digest”. The module must have the following functions: ● digest_book(file_path: str) -> None ○ This function collects and stores the required information into global dictionaries, lists, and variables. The file (book) is read and parsed only one time then closed ○ This function should raise a FileNotFoundError exception if...
IN PYTHON Stretch Remember that while it’s a good idea to have one person primarily responsible...
IN PYTHON Stretch Remember that while it’s a good idea to have one person primarily responsible for typing (the driver) and the other reviewing each line of code and making suggestions (the navigator) you must switch roles after every problem. 1). List Construction Write a Python program that prompts the user to enter a sequence of lowercase words and stores in a list only those words whose first letter occurs again elsewhere in the word (e.g., "baboon"). Continue entering words...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class Zillion implements a decimal counter that allows numbers with an effectively infinite number of digits. Of course the number of digits isn’t really infinite, since it is bounded by the amount of memory in your computer, but it can be very large. 1. Examples. Here are some examples of how your class Zillion must work. I’ll first create an instance of Zillion. The string...
Using c++ You may #include only: <iostream> <cmath> “functions.h” Write the function void readForceValuesFromStdIn(double* leftTeam, double*...
Using c++ You may #include only: <iostream> <cmath> “functions.h” Write the function void readForceValuesFromStdIn(double* leftTeam, double* rightTeam, unsigned const int noParticipants) that will read the list of forces exerted by each wizard alternating by the team into the correct array of doubles (see example in problem description) Define the function bool validForces(const double* forces, unsigned const int noParticipants) using the provided code. This function will be used to ensure that the forces exerted by each team’s wizard are non-negative The...
Questions 1 through 6 work with the length of the sidereal year vs. distance from the...
Questions 1 through 6 work with the length of the sidereal year vs. distance from the sun. The table of data is shown below. Planet Distance from Sun (in millions of miles) Years (as a fraction of Earth years) ln(Dist) ln(Year) Mercury 36.19 0.2410 3.5889 -1.4229 Venus 67.63 0.6156 4.2140 -0.4851 Earth 93.50 1.0007 4.5380 0.0007 Mars 142.46 1.8821 4.9591 0.6324 Jupiter 486.46 11.8704 6.1871 2.4741 Saturn 893.38 29.4580 6.7950 3.3830 Uranus 1,794.37 84.0100 7.4924 4.4309 Neptune 2,815.19 164.7800 7.9428...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter feeds. At a high level, your program is going to perform the following: Read in two files containing twitter feeds. Merge the twitter feeds in reverse chronological order (most recent first). Write the merged feeds to an output file. Provide some basic summary information about the files. The names of the files will be passed in to your program via command line arguments. Use...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT