Module 3

Object Oriented Programming

Unit 8

Data Structures and Data Search in Practice

Learning Outcomes

  • Describe the scenarios in which each of the set operations will be applicable in relation to the driverless car scenario.
  • Implement a linear data search technique.

Pratical Activity - Recursion and Polymorphism

Review Chapter 5 and Section 17.9 of 'Think Python' to help you understand this week's concepts. You can discuss your work with your tutor and save your results in your e-portfolio.

Exercise 5.3:

If you are given three sticks, you may or may not be able to arrange them in a triangle. For example, if one of the sticks is 12 inches long and the other two are one inch long, you will not be able to get the short sticks to meet in the middle. For any three lengths, there is a simple test to see if it is possible to form a triangle:
If any of the three lengths is greater than the sum of the other two, then you cannot form a triangle. Otherwise, you can. (If the sum of two lengths equals the third, they form what is called a “degenerate” triangle.)

  1. Write a function named is_triangle that takes three integers as arguments, and that prints either “Yes” or “No”, depending on whether you can or cannot form a triangle from sticks with the given lengths.
  2. Write a function that prompts the user to input three stick lengths, converts them to integers, and uses is_triangle to check whether sticks with the given lengths can form a triangle.
Code:
                
def is_triangle(a, b, c):
    if ((a + b) > c) and ((a + c) > b) and ((b + c) > a):
        print("Yes")
    else:
        print("No")
                
                
def is_triangle_user_input():
    a = int(input("Enter the first stick's length: "))
    b = int(input("Enter the second stick's length: "))
    c = int(input("Enter the third stick's length: "))

    is_triangle(a, b, c)


# Check if triangle can be formed
is_triangle(10, 7, 5)   # Yes
is_triangle(3, 1, 2)    # No
is_triangle_user_input()                
                
                

Reflection

  • Given my limited understanding of humanoid robots at that time, I chose to focus on completing the practical activities instead of participating in Collaborative Discussion 2.

Phone

-

Address

Mumbai, India