Exercise: Create Function with Parameters in Python


We've written this code:

print("My name is Andy")
print("I code in Python")
print("I have a dog")

print("My name is Mike")
print("I code in Java")
print("I have a cat")

print("My name is Sarah")
print("I code in JavaScript")
print("I have a parrot")

Write the same code in a better way by creating and calling a function with parameters print_messages()

Understanding the Problem

The core challenge here is to avoid repetitive code by using a function with parameters. This makes the code more modular, readable, and easier to maintain. Functions are a fundamental concept in programming that allow us to encapsulate logic and reuse it with different inputs.

Approach

To solve this problem, we need to create a function named print_messages() that takes three parameters: name, language, and pet. This function will then print the messages using these parameters.

Here is a step-by-step approach:

  1. Create a function named print_messages().
  2. Define three parameters for the function: name, language, and pet.
  3. Inside the function, use the print() function to print the messages, incorporating the parameters.
  4. Call the function with the appropriate arguments for each set of messages.

Algorithm

Here is a step-by-step breakdown of the algorithm:

  1. Define the function print_messages(name, language, pet).
  2. Inside the function, print the following messages:
    • "My name is {name}"
    • "I code in {language}"
    • "I have a {pet}"
  3. Call the function with the arguments for Andy, Mike, and Sarah.

Code Implementation

def print_messages(name, language, pet):
    # Print the name
    print(f"My name is {name}")
    # Print the programming language
    print(f"I code in {language}")
    # Print the pet
    print(f"I have a {pet}")

# Call the function with different sets of arguments
print_messages("Andy", "Python", "dog")
print_messages("Mike", "Java", "cat")
print_messages("Sarah", "JavaScript", "parrot")

Complexity Analysis

The time complexity of this solution is O(1) because the function performs a constant amount of work regardless of the input size. The space complexity is also O(1) as we are not using any additional data structures that grow with the input size.

Edge Cases

Potential edge cases include:

  • Empty strings for any of the parameters.
  • Very long strings for the parameters.

Our function handles these cases gracefully as it simply prints whatever is passed to it.

Testing

To test the solution comprehensively, we can use the following test cases:

  • Normal cases with typical names, languages, and pets.
  • Edge cases with empty strings.
  • Cases with very long strings.

Example test cases:

# Normal cases
print_messages("Andy", "Python", "dog")
print_messages("Mike", "Java", "cat")
print_messages("Sarah", "JavaScript", "parrot")

# Edge cases
print_messages("", "", "")
print_messages("A" * 1000, "Python" * 100, "dog" * 50)

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

  • Identify repetitive code and think about how to encapsulate it in a function.
  • Think about the parameters that the function needs to perform its task.
  • Ensure that the function is reusable and can handle different inputs.

Practice by solving similar problems and studying different algorithms to improve your problem-solving skills.

Conclusion

In this exercise, we learned how to refactor repetitive code by using a function with parameters. This approach makes the code more modular, readable, and maintainable. Understanding and applying such techniques is crucial for writing efficient and clean code.

Additional Resources

For further reading and practice, consider the following resources: