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.
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...
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...
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...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Write a Python 3 program called “parse.py” using the template for a Python program that we...
Write a Python 3 program called “parse.py” using the template for a Python program that we covered in this module. Note: Use this mod7.txt input file. Name your output file “output.txt”. Build your program using a main function and at least one other function. Give your input and output file names as command line arguments. Your program will read the input file, and will output the following information to the output file as well as printing it to the screen:...
**please write code with function definition taking in input and use given variable names** for e.g....
**please write code with function definition taking in input and use given variable names** for e.g. List matchNames(List inputNames, List secRecords) Java or Python Please Note:    * The function is expected to return a STRING_ARRAY.      * The function accepts following parameters:      *  1. STRING_ARRAY inputNames      *  2. STRING_ARRAY secRecords      */ Problem Statement Introduction Imagine you are helping the Security Exchange Commission (SEC) respond to anonymous tips. One of the biggest problems the team faces is handling the transcription of the companies reported...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT