Codepath

Trilogy

TIP102 Unit 1 Session 1 Standard (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 5 mins
  • 🛠️ Topics: Functions, Conditionals

U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Q: What should the function do if the year is not 2005, 2008, or 2012?
    • A: The function should print "Christopher Nolan did not release a Batman movie in <year>.", with <year> replaced by the given year.
  • Q: What if the year is one of the specified years?

    • A: The function should print the corresponding movie title from the table.
  • The function trilogy() should accept an integer year and print the title of the Batman movie released that year based on a specified table.

    HAPPY CASE
    Input: 2008
    Output: The Dark Knight
    
    Input: 2012
    Output: The Dark Knight Rises
    
    EDGE CASE
    Input: 1998
    Output: Christopher Nolan did not release a Batman movie in 1998.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Define the function with conditional statements to check the input year.

1. Define the function `trilogy(year)`.
2. Use if-elif-else statements to check the year:
    a. If year is 2005, print "Batman Begins".
    b. If year is 2008, print "The Dark Knight".
    c. If year is 2012, print "The Dark Knight Rises".
    d. If none of the above, print "Christopher Nolan did not release a Batman movie in {year}.".

⚠️ Common Mistakes

  • Not covering all conditions.
  • Forgetting to format the output string correctly.

I-mplement

Implement the code to solve the algorithm.

def trilogy(year):
    if year == 2005:
        print("Batman Begins")
    elif year == 2008:
        print("The Dark Knight")
    elif year == 2012:
        print("The Dark Knight Rises")
    else:
        print(f"Christopher Nolan did not release a Batman movie in {year}.")
Fork me on GitHub