Mentorship Matcher Python

👤 Sharing: AI
```python
import random

class Mentor:
    """Represents a mentor with attributes like expertise and availability."""
    def __init__(self, name, expertise, availability):
        self.name = name
        self.expertise = expertise
        self.availability = availability  # e.g., "Evenings", "Weekends", "Anytime"

    def __str__(self):
        return f"Mentor: {self.name}, Expertise: {self.expertise}, Availability: {self.availability}"


class Mentee:
    """Represents a mentee with attributes like desired skills and availability."""
    def __init__(self, name, desired_skills, availability):
        self.name = name
        self.desired_skills = desired_skills
        self.availability = availability

    def __str__(self):
        return f"Mentee: {self.name}, Desired Skills: {self.desired_skills}, Availability: {self.availability}"


def find_potential_matches(mentors, mentees):
    """
    Finds potential mentor-mentee matches based on expertise and availability.

    Args:
        mentors: A list of Mentor objects.
        mentees: A list of Mentee objects.

    Returns:
        A list of tuples, where each tuple contains a Mentor object and a Mentee object
        representing a potential match.
    """

    potential_matches = []
    for mentor in mentors:
        for mentee in mentees:
            # Check for expertise match (at least one desired skill matches mentor's expertise)
            if any(skill in mentor.expertise for skill in mentee.desired_skills):
                # Check for availability overlap
                if mentor.availability == "Anytime" or mentee.availability == "Anytime" or mentor.availability == mentee.availability:
                    potential_matches.append((mentor, mentee))
    return potential_matches


def display_matches(matches):
    """
    Displays the potential mentor-mentee matches in a user-friendly format.

    Args:
        matches: A list of tuples, where each tuple contains a Mentor object and a Mentee object.
    """

    if not matches:
        print("No suitable matches found.")
    else:
        print("Potential Mentor-Mentee Matches:")
        for mentor, mentee in matches:
            print(f"  Mentor: {mentor.name} (Expertise: {mentor.expertise}, Availability: {mentor.availability})")
            print(f"  Mentee: {mentee.name} (Desired Skills: {mentee.desired_skills}, Availability: {mentee.availability})")
            print("-" * 30)



def create_sample_data():
    """Creates sample mentor and mentee data for demonstration."""

    mentors = [
        Mentor("Alice Johnson", ["Python", "Data Science", "Machine Learning"], "Evenings"),
        Mentor("Bob Williams", ["Java", "Software Engineering", "Agile"], "Weekends"),
        Mentor("Charlie Davis", ["Web Development", "JavaScript", "React"], "Anytime"),
        Mentor("Diana Garcia", ["Project Management", "Leadership"], "Weekdays")
    ]

    mentees = [
        Mentee("Eve Brown", ["Python", "Machine Learning"], "Evenings"),
        Mentee("Frank Miller", ["Java", "Software Engineering"], "Weekends"),
        Mentee("Grace Wilson", ["Web Development", "React"], "Anytime"),
        Mentee("Henry Rodriguez", ["Data Science", "Project Management"], "Weekdays"),
        Mentee("Ivy Moore", ["Python", "Web Development"], "Weekends")
    ]

    return mentors, mentees


def main():
    """
    Main function to orchestrate the mentor-mentee matching process.
    """

    mentors, mentees = create_sample_data()

    # Find potential matches
    matches = find_potential_matches(mentors, mentees)

    # Display the matches
    display_matches(matches)


if __name__ == "__main__":
    main()
```

Key improvements and explanations:

* **Clearer Structure:** The code is organized into functions (e.g., `find_potential_matches`, `display_matches`, `create_sample_data`, `main`) to improve readability and maintainability.  This follows best practices for larger programs.
* **Classes for Data Representation:**  Uses `Mentor` and `Mentee` classes to represent the data.  This is *crucial* for managing complex information and makes the code much more object-oriented.  The `__str__` methods in these classes allow for easy printing of the object's contents.
* **Expertise Matching:** The `find_potential_matches` function now correctly checks for *any* overlap in skills between the mentor's expertise and the mentee's desired skills using `any()`. This is more flexible than requiring a perfect match. The original code had a flaw in this logic.
* **Availability Matching:**  The code now considers availability when matching mentors and mentees. It correctly handles the "Anytime" availability.  If mentor or mentee is available "Anytime", then they are considered to have compatible schedules.
* **`create_sample_data()` function:**  This function cleanly creates the initial mentor and mentee data, making it easy to modify or extend the dataset without cluttering the `main()` function.
* **`main()` function:**  Encapsulates the main workflow of the program, making it easy to understand the overall execution.
* **`if __name__ == "__main__":` block:**  Ensures that the `main()` function is only called when the script is run directly (not when it's imported as a module).
* **Docstrings:**  Added docstrings to all functions and classes to explain their purpose, arguments, and return values.  This is good practice for maintainable code.
* **Error Handling (Minimal, but a Start):** Includes a check for no matches found and prints a message.  More robust error handling (e.g., checking input data types) could be added in a production application.
* **`display_matches()` function:** This improves readability by encapsulating the logic for displaying the potential matches.
* **Comments:**  Added more comments to explain specific parts of the code, especially the matching logic.
* **Corrected Logic:** The core matching logic has been fixed to correctly identify potential matches based on *partial* expertise overlap and availability.

How to Run:

1.  **Save:** Save the code as a `.py` file (e.g., `mentor_matcher.py`).
2.  **Run:** Open a terminal or command prompt, navigate to the directory where you saved the file, and run the command `python mentor_matcher.py`.

This revised version provides a much more complete, correct, and understandable solution to the mentor-mentee matching problem.  It emphasizes object-oriented principles and best practices for Python code.  It's also more robust and easier to extend.
👁️ Viewed: 5

Comments