Python question
It is required to make a function
biggest_vert_group(vert)
that consumes vert, which is a list of list of integers containing the coordinates of the consecutive vertices of a polygon. The function should return the size of the largest group of same length edges.
This function must have a big-o runtime of O(n) where n is the length of vert
Example
biggest_vert_group([[0,0],[1,1],[0,2],[-2,3],[-1,2]]) => 3
only library allowed to be imported is math. list comprehensions, sets and/or enumerators are Forbidden from getting used. Mutating passed parameters are also restricted.
Hello dear....
Here is the perfectly working Python 3 program for the given task by following all the conditions. And the runtime of funtion is O(n) where n is the length of vert. I have also written comments for better understaning. Also attached images of output and code for comfort of reading. Hope you will like it.
same_length_edges.py
import math
#function returns the length of edge between two given vertices
def length_of_edge(a,b):
#calculate and return length of edge according to the formula
return math.sqrt( (a[0]-b[0])**2 + (a[1]-b[1])**2 )
#function returns the size of largest group of same length edges
def biggest_vert_group(vert):
#a dictionary variable to store count of same length edges
sameLengthEdgesCount = {}
#prev variable to calculate distance, initialized with last element
prev = vert[-1]
#initially taking maximum count = 1
maxCount = 1
#traverse though the given list
for i in vert:
#calculate the length of edge between current vertex and previous vertex
length = length_of_edge(prev, i)
#if the 'length' is already in sameLengthEdgesCount dictionary
if(length in sameLengthEdgesCount):
#increment count for that length
sameLengthEdgesCount[length] += 1
#if the count of that length of greater than maxCount
if(maxCount < sameLengthEdgesCount[length]):
#update maxcount
maxCount = sameLengthEdgesCount[length]
#else add an item with length as key and 1 as value
else:
sameLengthEdgesCount[length] = 1
#make current vertex as prev vertex
#so that we can use it to calculate length of the edge in next iteration
prev = i
#return max count
return maxCount
#create a list of vertices
vert = [[0,0],[1,1],[0,2],[-2,3],[-1,2]]
#call biggest_vert_group() function with above list and print the returned value
print(biggest_vert_group(vert))
Output:
Image of code:
(Note: If you lose indentations while copying the code, adjust according to the above image)
Hope you like it.
If you have any doubts ask me in comment section.
If you like my work, please give me a like and feedback. That helps me a lot. Thank you.
All the best.
Python question It is required to make a function biggest_vert_group(vert) that consumes vert, which is a...
The language is python Write the function largest_edge_group(vertices) that consumes vertices, a list of list of integer containing the coordinates of the consecutive vertices of a polygon. The function largest_edge_group returns the size of the largest group of same length edges. Your function must run in O(n), where n is the length of vertices. Example largest_edge_group([[0,0],[1,1],[0,2],[-2,3],[-1,2]]) => 3 Hint Remember, it is not possible to compare Floats for strict equality, but since the coordinates are integers, the squared distance is...
USING PYTHON 3 Write a function no_pairs(L) that consumes a list of natural numbers L and mutates it, replacing any natural numbers which appear exactly twice in the list with -1. The no_pairs function returns None. This function should run in at worst O(n log n) time. HINT: Solving this problem within the demanded runtime will likely require both O(n log n) sorting and O(log n) searching! Samples: L = [254 , 955 , 198 , 590 , 368] after...
Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elements. which are called eernents (or components). The elements of an (m × n)-dimensional matrix A are denoted as a,, where 1im and1 S, symbolically, written as, A-a(1,1) S (i.j) S(m, ). Written in the familiar notation: 01,1 am Gm,n A3×3matrix The horizontal and vertical lines of entries in a matrix are called rows and columns, respectively A matrix with the...