找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1810|回复: 6
收起左侧

[Microsoft] MS昂塞

[复制链接]

1161

主题

184

精华

3664

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3664
发表于 2-23-2017 03:56 PM | 显示全部楼层 |阅读模式

亲!马上注册或者登录会查看更多内容!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 Sophia 于 2-24-2017 10:18 AM 编辑
0 L: E! g5 {' A7 o! a- b
, E6 o. B$ B3 [% r$ s* j* ]+ t: E! [Write a program in that determines the members and parents of nested groups without using recursion.$ ?: h" Y! ~1 H; L( l3 W: {# U) Q4 n
& `% D2 `9 j% v. v  ^
These are the requirements.' f5 T5 H9 X% q8 Q* Z) [0 q& A
" z' c* Y" K, h
1. A group can have 0 or more members.& q' w: d' Y( Y6 w5 I# G
2. A group can be member of one or more groups) m: `3 B/ `$ e' `3 i6 ~* {
3. A group can be member of itself.' A; @. ]4 e5 L! C
4. If there is a path from a group either directly or through multiple hops, then that user is considered as member of the group.* u! T- v! s& @& [. `0 l
5. A group can have users or groups as members
* x4 @4 h$ }5 S/ E4 s9 V. N' |5 ^
+ ]" v: y. Y6 ?6 r5 j1 pEX: Input looks like this</p><p><code><pre class="language-java">var groupMembers = new Dictionary<string, HashSet<string>>1 u" e- J9 P* }: M, u6 Q  S, s" z
            {
9 L# B5 X) f$ U# w9 f- a                {  "G4", new HashSet<string> { "U1","G1"} },/ ~& [9 l+ |7 |. M, q  A" ~
                {  "G1", new HashSet<string> { "G2","G3","G6"} },/ j( t3 m; v. v/ T. c
                {  "G3", new HashSet<string> { "G3","G5"} },
+ G2 ^; H" [6 \' L                {  "G2", new HashSet<string> { "G4","U2"} },
" {1 g! L: i$ {2 }3 }3 l                {  "G5", new HashSet<string> { "U2","G6"} },
2 v& E4 A3 a, V% r                {  "G6", new HashSet<string> { "U3"} },
. O7 Z" M( ?/ \5 f% @& M            };
; {( z+ f% `# N# SSignature of function is:( K( k' A  S7 `
private List<MyGroup> FindMembers(Dictionary<string, HashSet<string>> groupMembers)6 X) l9 U- R. O- D5 |
You need to make sure that you take care of cycles in the graph and not go into infinite recursion.3 q1 w0 \7 e9 D4 O; g, B
% W) |- b. t1 r' Q( u! n. w, s
Output should look like a list of groups where a group is as follows.
3 `9 A6 X! Y' X5 P3 Q8 r# f$ P1 A6 I1 T8 ^
private class MyGroup
8 U$ |' J! s# j    {
/ ?4 j9 j4 s* C8 W8 c3 k0 ?6 q; }/ y        public string Identity { get; set; }
! ]! E) f: ?1 {; e( o: C: r! l: [- Q+ ^( B
        public Dictionary<string, MyGroup> MemberOf { get; set; }1 m4 A* z' u0 l- d) a; \

2 L% U4 Y) W3 z        public Dictionary<string, MyGroup> Members { get; set; }" g( Q) u9 ^  W' d" j2 }( t1 t/ z
% N3 _! L$ k0 y/ e  U
        public HashSet<string> Users { get; set; }
; @9 {# D9 n  [+ s$ l7 _* b; Z& c7 W" n# m
        public MyGroup(string name)  k6 f  B3 U. P9 s2 {, c! h$ @- z! g
        {
. v) c3 X( c9 k            this.Identity = name;
: H2 q0 ]/ G; t- K" L2 S( W            this.MemberOf = new Dictionary<string, MyGroup>();% T* Y3 E+ A7 S$ q
            this.Members = new Dictionary<string, MyGroup>();/ w  q2 _) c' P  M' _
            this.Users = new HashSet<string>();
  M" l% I+ b4 P        }( {# w, v" W& F# _. X
}Each group object should contain all the groups it's a memberOf (directly or indirectly), all the groups that are it's members (directly or indirectly) and all users that are it's members.

1178

主题

174

精华

3584

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3584
发表于 2-23-2017 03:56 PM | 显示全部楼层

This sounds like a good plan. Do you have pseudo code for this approach? Would love to see how the algorithm looks in real code

回复 支持 反对

使用道具 举报

1157

主题

163

精华

3497

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3497
发表于 2-23-2017 03:56 PM | 显示全部楼层

working code?

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表