Question

In: Computer Science

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 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)

Solutions

Expert Solution

import math

def findSlope(A,B):
  slopeVal = (B[1]-A[1])/B[0]-A[0]
  return slopeVal

def findSum(distance): # sum of all linesegments
  sumval = 0
  for k in distance:
    sumval += distance[k]
  return sumval

def findDist(point1,point2): # distance between 2 points
  return (math.sqrt((point1[0]-point2[0])**2 + (point1[1]-point2[1])**2))

def linelength (tupleList,start):
  distance = {}
  if start:
    for point in tupleList:
      slopeVal = findSlope((0,0),point)
      if slopeVal in distance.keys(): # if there are no points in the given list with calc slope add it in available slopes
        distance[slopeVal] = max(distance[slopeVal],findDist((0,0),point))
      else: # if multiple points are with same slope then pick the largest line segment that passes through point and lies in the given list
        distance[slopeVal] = findDist((0,0),point)
    return findSum(distance)
  else:
    for i in range(1,len(tupleList)):
      slopeVal = findSlope(tupleList[0],tupleList[i])
      if slopeVal in distance.keys(): # if there are no points in the given list with calc slope add it in available slopes
        distance[slopeVal] = max(distance[slopeVal],findDist(tupleList[0],tupleList[i]))
      else: # if multiple points are with same slope then pick the largest line segment that passes through point and lies in the given list
        distance[slopeVal] = findDist(tupleList[0],tupleList[i])
    return findSum(distance)
  return 0

def main():
  tupleList = [(1,1), (-2,4), (-3,-2), (2,-1), (1,1)]
  dist = linelength(tupleList,True)
  print(dist)
main()

Comment in case of any doubts, Please upvote


Related Solutions

IN PYTHON Write a function called check_inventory(inventory, low) that passes a dictionary as inventory and low...
IN PYTHON Write a function called check_inventory(inventory, low) that passes a dictionary as inventory and low as an integer, and returns a sorted list of items that are below an inventory level that is given by thelowinteger parameter. The function should work without the low parameter supplied - in which case, you should assume low is 5. You do not have to worry about reading and writing to a file, that code is provided, and you don’t have to change...
(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.
Find the line that passes through the point (0, 1) and through the point of intersection...
Find the line that passes through the point (0, 1) and through the point of intersection of the two lines  and
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”.
*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.
Python pls 1. The function only allow having one return statement. Any other statements are not...
Python pls 1. The function only allow having one return statement. Any other statements are not allowed. You only have to have exactly one return statement. (ONE STATEMENT ALLOWED) For example the answer should be def hello(thatman): return thatman * thatman Create a function search_hobby. This function returns a set of hobby names and people's excitement level. input database = {'Dio': {'Lottery': 2, 'Chess': 4, 'Game': 3},'Baily': {'Game': 2, 'Tube': 1, 'Chess': 3}, 'Chico': {'Punch': 2, 'Chess': 5}, 'Aaron': {'Chess':...
Python pls 1. The function only allow having one return statement. Any other statements are not...
Python pls 1. The function only allow having one return statement. Any other statements are not allowed. You only have to have exactly one return statement. For example the answer should be def hello(thatman): return thatman * thatman 1. Create a function search_wholo function. This function returns a list of 2 tuples, which people and their excitement skills, and sorted by decreasing excitement level(highest excitement skill first). If two people have the same excitement level, they should appear alphabetically. The...
Python pls 1. The function only allow having one return statement. Any other statements are not...
Python pls 1. The function only allow having one return statement. Any other statements are not allowed. You only have to have exactly one return statement. For example the answer should be def hello(thatman): return thatman * thatman Create a function search_hobby. This function returns a set of hobby names and people's excitement level. input database = {'Dio': {'Lottery': 2, 'Chess': 4, 'Game': 3},'Baily': {'Game': 2, 'Tube': 1, 'Chess': 3}, 'Chico': {'Punch': 2, 'Chess': 5}, 'Aaron': {'Chess': 4, 'Tube': 2,...
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value...
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value pairs of the dictionary as tuples (key, value), reverse sorted by value (highest first) and where multiple keys with the same value appear alphabetically (lowest first).
how to write a code with python function trying to find minimum difference between any two...
how to write a code with python function trying to find minimum difference between any two elements in a list
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT