开发者

Implement the following method using recursion c# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

How could I change the following method to a recursive method:

    public List<ScanItem> search(List<ScanItem> allItems)
    {
        List<ScanItem> rt = new List<ScanItem>();

        foreach (ScanItem s in allItems)
        {
            if (s.IsDirectory)
            {
                rt.Add(s);



                foreach (ScanItem s1 in ((ScanDir)s).Items)
                {
                    if (s1.IsDirectory)
                    {
                        rt.Add(s1);




                        foreach (ScanItem s2 in ((ScanDir)s1).Items)
                        {
                            if (s2.IsDirectory)
                            {
                                rt.Add(s2);

                                foreach (ScanItem s3 in ((ScanDir)s2).Items)
                                {
                                    if (s3.IsDirectory)
                                    {
                                        rt.Add(s3);

                                        /// repeat until there are no more direcotries

                                    }
                                    else
                                        if (s3.Name.Contains("Foo"))
                                            rt.Add(s3);
                                }

                            }
                            else
                                if (s2.Name.Contains("Foo"))
                                    rt.Add(s2);
                        }



                    }
                    else
                        if (s1.Name.Contains("Foo"))
                            rt.Add(s1);
                }


            }
            else                
                if (s.Name.Contains("Foo"))                    
                    rt.Add(s);                    

        }

        return rt;

    }

let me show you how I built it:

Step1:

    public List<ScanItem> search(List<ScanItem> allItems)
    {
        List<ScanItem> rt = new List<ScanItem>();

        foreach (ScanItem s in allItems)
        {
            if (s.IsDirectory)
            {
                rt.Add(s);

            }
            else                
                if (s.Name.Contains("Foo"))                    
                    rt.Add(s);                    

        }

        return rt;

    }

Step2:

    public List<开发者_Python百科;ScanItem> search(List<ScanItem> allItems)
    {
        List<ScanItem> rt = new List<ScanItem>();

        foreach (ScanItem s in allItems)
        {
            if (s.IsDirectory)
            {
                rt.Add(s);



                foreach (ScanItem s1 in ((ScanDir)s).Items)
                {
                    if (s1.IsDirectory)
                    {
                        rt.Add(s1);

                    }
                    else
                        if (s1.Name.Contains("Foo"))
                            rt.Add(s1);
                }


            }
            else                
                if (s.Name.Contains("Foo"))                    
                    rt.Add(s);                    

        }

        return rt;

    }

Step 3:

    public List<ScanItem> search(List<ScanItem> allItems)
    {
        List<ScanItem> rt = new List<ScanItem>();

        foreach (ScanItem s in allItems)
        {
            if (s.IsDirectory)
            {
                rt.Add(s);



                foreach (ScanItem s1 in ((ScanDir)s).Items)
                {
                    if (s1.IsDirectory)
                    {
                        rt.Add(s1);




                        foreach (ScanItem s2 in ((ScanDir)s1).Items)
                        {
                            if (s2.IsDirectory)
                            {
                                rt.Add(s2);

                            }
                            else
                                if (s2.Name.Contains("Foo"))
                                    rt.Add(s2);
                        }



                    }
                    else
                        if (s1.Name.Contains("Foo"))
                            rt.Add(s1);
                }


            }
            else                
                if (s.Name.Contains("Foo"))                    
                    rt.Add(s);                    

        }

        return rt;

    }

step 4:

    public List<ScanItem> search(List<ScanItem> allItems)
    {
        List<ScanItem> rt = new List<ScanItem>();

        foreach (ScanItem s in allItems)
        {
            if (s.IsDirectory)
            {
                rt.Add(s);



                foreach (ScanItem s1 in ((ScanDir)s).Items)
                {
                    if (s1.IsDirectory)
                    {
                        rt.Add(s1);




                        foreach (ScanItem s2 in ((ScanDir)s1).Items)
                        {
                            if (s2.IsDirectory)
                            {
                                rt.Add(s2);

                                foreach (ScanItem s3 in ((ScanDir)s2).Items)
                                {
                                    if (s3.IsDirectory)
                                    {
                                        rt.Add(s3);

                                    }
                                    else
                                        if (s3.Name.Contains("Foo"))
                                            rt.Add(s3);
                                }

                            }
                            else
                                if (s2.Name.Contains("Foo"))
                                    rt.Add(s2);
                        }



                    }
                    else
                        if (s1.Name.Contains("Foo"))
                            rt.Add(s1);
                }


            }
            else                
                if (s.Name.Contains("Foo"))                    
                    rt.Add(s);                    

        }

        return rt;

    }

Note that I just keep copying:

                                foreach (ScanItem s3 in ((ScanDir)s2).Items)
                                {
                                    if (s3.IsDirectory)
                                    {
                                        rt.Add(s3);

                                        /// repeat until there are no more direcotries

                                    }
                                    else
                                        if (s3.Name.Contains("Foo"))
                                            rt.Add(s3);
                                }


private void SearchCore(IEnumerable<ScanItem> items, List<ScanItem> foundItems)
{
    foreach (ScanItem s in items)
    {
        if (s.IsDirectory)
        {
            foundItems.Add(s);
            searchCore (((ScanDir)s2).Items, foundItems);
        }
        else if (s.Name.Contains("Foo"))                    
                foundItems.Add(s);
    }
}

public List<ScanItem> Search(List<ScanItem> allItems)
{
    List<ScanItem> foundItems = new List<ScanItem>();
    searchCore (allItems, foundItems);
    return foundItems;
}


public IEnumerable<ScanItem> Search(IEnumerable<ScanItem> allItems, 
    string nameContains)
{
    foreach (var item in allItems)
    {
        if (item.IsDirectory)
            foreach (var child in Search(((ScanDir)item).Items, nameContains))
                yield return child;
        else if (item.Name.Contains(nameContains))
            yield return item;
    }
}


I'd arrange it like so:

public IEnumerable<ScanItem> GetItems(IEnumerable<ScanItem> allItems)
{
    foreach (var item in allItems)
    {
        if (!item.IsDirectory)
            yield return item;
        else
            foreach (var child in GetItems(item.Items))
                yield return child;
    }
}

// ...

return GetItems(allItems).Where(i => i.Name.Contains("foo"));


Linq approach with recursion:

public List<ScanItem> Search(List<ScanItem> scanItems)
{
    List<ScanItem> results = scanItems.Where( x => x.IsDirectory)
                                      .Select( x => Search(((ScanDir)x).Items))
                                      .SelectMany( s => s)
                                      .ToList();
    results.AddRange(scanItems.Where( x => !x.IsDirectory &&
                                            x.Name.Contains("Foo")));
    return results;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜