dimanche 23 août 2015

What is the best way to implement this using some pattern in C#?

Given a data structure representing a social network, write a function that finds friends of a certain degree. Friends of the first degree are a member's immediate friends, friends of the second degree are friends of a member's friends excluding first degree friends, etc.

For example, if A is a friend with B and B is a friend with C, then GetFriendsOfDegree(A, 2) should return C since C is the only second degree friend of A (B is a first degree friend of A).

Method name: GetFriendsOfDegree

using System;
using System.Collections.Generic;

public class Member
{
    public string Email { get; private set; }

    public ICollection<Member> Friends { get; private set; }

    public Member(string email) : this(email, new List<Member>())
    {
    }

    public Member(string email, ICollection<Member> friends)
    {
        this.Email = email;
        this.Friends = friends;
    }

    public void AddFriends(ICollection<Member> friends)
    {
        foreach (Member friend in friends)
            this.Friends.Add(friend);
    }

    public void AddFriend(Member friend)
    {
        this.Friends.Add(friend);
    }
}

public class Friends
{
    public static List<Member> GetFriendsOfDegree(Member member, int degree)
    {
        throw new NotImplementedException("Waiting to be implemented.");
    }

    public static void Main(string[] args)
    {
        Member a = new Member("A");
        Member b = new Member("B");
        Member c = new Member("C");

        a.AddFriend(b);
        b.AddFriend(c);

        foreach (Member friend in GetFriendsOfDegree(a, 2))
            Console.WriteLine(friend.Email);
    }
}

Aucun commentaire:

Enregistrer un commentaire