none
How can you do the corret implementation using this pattern? RRS feed

  • Question

  • 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);
        }
    }

    Sunday, August 23, 2015 10:35 PM

Answers

  • To give some code, I would make a dictionary and this recursion. Handing the member in via parameter makes this a stactic functin (candidate).

    //GetFriends recursive Function
    getFriends(Member currentMember, Dictionary<Friend, int> knownFriendsDegree, int currentDegree, int DegreesLeft){
      foreach(Member possibleFriend in currentMember.Friends){
        if(knownFriends.ContainsKey(possibleFriend){
          //that friend is already in list.
          //Set the degree to currentDegree or the curernt value, wichever is lower
        }
        else{
          knownFriends.Add(possibleFriend, currentDegree);
    
          if(DegreesLeft > 0)
            getFriends(possibleFriend, knownFreidsnDegree, currentDegree+1, DegreesLeft-1);
        }
      }
    }
    Keep in mind however that in 99% of all cases this is much better solved in the Database via SQL. Not sure how do that kind of uniquiness there, however.
    Monday, August 24, 2015 1:01 PM

All replies

  • What pattern is it supposed to be?
    Sunday, August 23, 2015 11:34 PM
  • To solve your fundamental issue you need to understand most of the design patterns. I do have a collection that with most of the patterns and videos showing you about patterns. https://docs.com/ming-man/9755/design-patterns chanmm

    chanmm

    • Proposed as answer by chanmmMVP Monday, August 24, 2015 12:37 AM
    Monday, August 24, 2015 12:36 AM
  • Just the implementation. :)

    i did using composite pattern but i just wa'nna see some ideias about this implemantion.

    Monday, August 24, 2015 12:38 AM
  • Just the implementation. :)

    i did using composite pattern but i just wa'nna see some ideias about this implemantion.

    http://www.dofactory.com/net/composite-design-pattern


    Monday, August 24, 2015 12:56 AM
  • You can use this:

    public static List<Member> GetFriendsOfDegree(Member member, int degree) {

    List<Member> result = member.Friends.ToList();

    for(int i = 0; i<degree;i++) result = member.Friends.ToList();

    return result; }


    Monday, August 24, 2015 6:27 AM
  • In the DB, this would be a classical N:M relationship between members.

    I would propably solve this via some form of recursion (call "get Friends" for X itterations).

    Another way would be to look at this like a tree structure.

    The big issue here is that you might end up in a Diamond problem case. (A knows B and C. B and C both know D). So you have to make certain that member is not already in the list/already on the list with a closer friend relationship.
    Propably I would hand the list of "known friends" down the recursion ,togethere with name and degress left.

    Monday, August 24, 2015 12:28 PM
  • To give some code, I would make a dictionary and this recursion. Handing the member in via parameter makes this a stactic functin (candidate).

    //GetFriends recursive Function
    getFriends(Member currentMember, Dictionary<Friend, int> knownFriendsDegree, int currentDegree, int DegreesLeft){
      foreach(Member possibleFriend in currentMember.Friends){
        if(knownFriends.ContainsKey(possibleFriend){
          //that friend is already in list.
          //Set the degree to currentDegree or the curernt value, wichever is lower
        }
        else{
          knownFriends.Add(possibleFriend, currentDegree);
    
          if(DegreesLeft > 0)
            getFriends(possibleFriend, knownFreidsnDegree, currentDegree+1, DegreesLeft-1);
        }
      }
    }
    Keep in mind however that in 99% of all cases this is much better solved in the Database via SQL. Not sure how do that kind of uniquiness there, however.
    Monday, August 24, 2015 1:01 PM
  • I did yesterday using  dictionary and this recursion. but it's good to see some ideias for a certain problems.

    Thank you guys

    Monday, August 24, 2015 6:13 PM
  • I did yesterday using  dictionary and this recursion. but it's good to see some ideias for a certain problems.

    Thank you guys

    Hi yduretti

    What's the problem now? Currently I marked Christopher84's last response as an answer, you can unmark if no help. Thanks for your effort.

    Have a nice day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, August 25, 2015 6:38 AM
    Moderator