Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 349993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:30:35+00:00 2026-05-12T11:30:35+00:00

I’m trying to calculate the number of success cases within a recursive function in

  • 0

I’m trying to calculate the number of success cases within a recursive function in C#, but I’m astonished by the fact that my variable is shared between all the function calls!

[update 2]

More than strange this time. doing so

i = i + validTreesFun(tree.Nodes, newWords.ToList()) ;

resets i to 0

doing this

i = validTreesFun(tree.Nodes, newWords.ToList()) + i ;

gives some results (I’m not sure if it’s correct)

[updated : the full code]

public static  int validTreesFun(List<Tree<char>> nodes, List<string> words)
    {
        int i = 0;
        if (nodes == null && (words == null || words.Count == 0 || (words.Count == 1 && words.First() == "")))
            return 1;
        else
            if (nodes == null)
                return 0;

        foreach (Tree<char> tree in nodes)
        {
            var validWords = words.Where(w => w.ToCharArray()[0] == tree.Root)
                .Select(w => w);
            if (validWords.Count() == 0)
                return 0;
            else
            {
                var newWords = validWords.Select(w => join( w.ToCharArray().Skip(1).ToArray()));
                i += validTreesFun(tree.Nodes, newWords.ToList());
            }
        }
        return i;
    }

when debuging the variable i take the value 1 but it resets to 0 on the next iteration!!
despite the use of

i = i + ....

What is the problem in that piece of code?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:30:36+00:00Added an answer on May 12, 2026 at 11:30 am
    if (validWords.Count() == 0)
        return 0;
    

    Should be

    if (validWords.Count() == 0)
        continue;
    

    Also, in general, I personally think it is nicer looking to only send in one element at a time to a recursive function.

    public static  int validTreesFun(Tree<char> node, List<string> words)
    

    That way you don’t get the same kind of mistake like above. Finally, a minor note.

    w => w.ToCharArray()[0] == tree.Root
    

    can be written as

    w => w[0] = tree.Root
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.