Question

A triangle is expressed by tuples of three positive numbers, which are the length of sides...

A triangle is expressed by tuples of three positive numbers, which are the length of sides of the triangle. Two triangles a1 and a2 are similar if a2 can be generated by rotating and reflecting triangle a1. For example, t1 = (7, 10, 13) is similar to t2 = (7, 13, 10) since a2 can be obtained by rotating and reflecting t1. (7, 10, 13) rotate −−−→ (10, 13, 7) reflect −−−−→ (7, 13, 10) On the other hand, t1 = (7, 10,7) is not similar to t2 = (7, 10, 10). Two triangles are said to be the same kind if they are similar. In Python, a triangle will be represented as a 3-tuple of positive integers. Your task is to write a function, which returns the number of different kinds of triangles in a given list.

Caution:

• Your code must be written in Python 3

• The function must have average-case runtime of O(n). You can assume Simple Uniform Random Hashing.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code is implemented in python3

The function have average-case runtime of O(n). because we just traverse list only once

Two triangles a1 and a2 are similar if a2 can be generated by rotating and reflecting triangle a1

so just sort the tuple we get triangle order which can be used to identify similar triagle

There may be different techniques but i used above technique

def distinct_triangles(ListTuples):
   d={}
   for i in ListTuples:
       d[tuple(sorted(i))]=1
   return len(d)
def main():
   print("Enter number of trianlges in list")
   n=int(input())
   print("Enter triangles in list")
   ListTuples=[]
   for i in range(n):
       line=input()
       ListTuples.append(tuple(line.split(',')))
   print("the number of different kinds of triangles in a given list:",distinct_triangles(ListTuples))
if __name__ == "__main__":
main()

Add a comment
Know the answer?
Add Answer to:
A triangle is expressed by tuples of three positive numbers, which are the length of sides...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose we have an array that contains tuples. These tuple contains three positive numbers. Implement an...

    Suppose we have an array that contains tuples. These tuple contains three positive numbers. Implement an algorithm that counts how many distinct tuple that an array has(contains same number in same order). ex) [(1, 2, 1), (2, 2, 2), (3, 8, 3), (1, 2, 1), (3, 4, 3)] gives 4. 1 The algorithm should be implemented in Python3. 2 The function must have average-case runtime of O(n). You can assume Simple Uniform Random Hashing. 3 Python built-in dictionary cannot be...

  • Please answer all the blanks (volume if H2 and everything in analysis). TIA! Data 5 1...

    Please answer all the blanks (volume if H2 and everything in analysis). TIA! Data 5 1 oong 0.00 10.5ml 2 o.olag 0.00 11.0 Trial 3 o.org 0.00 12.00 o Daag o.albg 0.00 10.0 ml 11.5ml Mass of Mg (g) Initial volume of Syringe (mL) Final volume of Syringe (mL) Volume of H (mL) Barometric pressure (torr) Ambient temperature (°C) Vapor pressure of H2O (torr) 779.314har 23. Oi 21.0 forr TA.314tar 23.0c 179.3 14ton 23.0¢ 779.314 ton 23.0c 779.31472 23.0c 21.0...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT