Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

A small recursive utility.

Earlier, I lost track of the total number of posts I had in TAFWA. For those who don't know, that's just TA's way of keeping track of helpful posts. They are stored in this format:

Code
(Amount) Month Day, Year

I wanted to add up all of the amounts, so I wrote a quick little recursive algorithm, for a little practice, I guess.

By the way, I know this isn't a beautiful algorithm, but it works and that is what matters; its an example to help people understand basic recursion.

PHP Code

<?php 
private int GetTotalNumber(string text)
  {
    
int count = 0;
    foreach (
string line in text.Split('\n'))
    {
    
count += GetNumber(line.Trim());
    }
    return 
count;
  }

  private 
int GetNumber(string line, int counter = 0, int temp = 0)
  {
    if (
line == "") return temp;
    else if (
line[counter] == '(') return GetNumber(line, counter + 1, temp);
    else if (
line[counter] == ')') return temp;
    else if (
char.IsNumber(line[counter]))
    if (
char.IsNumber(line[counter + 1]))
    {
    
string tempStr = line.Substring(counter, counter + 1);
    return 
GetNumber(line, counter + 2, temp + Convert.ToInt32(tempStr));
    }
    else
    return 
GetNumber(line, counter + 1, temp + (((char)(Convert.ToInt32(line[counter])))) - '0');    
    else
    return 
temp;
  } 

Input:
Code
(15)
(3)
(8)
(7)

Output:
Code
33

Works for anything up to:

Code
(99)

which is enough for its use.

So yeah, just a basic example. Hadn't released anything public for some time, but decided I might as well post this.
That is some good stuff Ethereal, recursive methods is something you have to work with whether you like it or not when you get further in your programming.

Here's another example I made to recursively loop through a treeviews nodes to get them all (needed this for my obfuscator project). It's kind of stupid that treeview doesn't have a property implemented to get all nodes. :l

Code
private List<TreeNode> RetrieveAllNodes(TreeView treeView)
  {
    List<TreeNode> outList = new List<TreeNode>();

    foreach (TreeNode node in treeView1.Nodes)
    GetSubNodes(node, outList);

    return outList;
  }

  private void GetSubNodes(TreeNode node, List<TreeNode> outList)
  {
    outList.Add(node);

    foreach (TreeNode _node in node.Nodes)
    GetSubNodes(_node, outList);
  }

Probably some better way to do this?
Probably coding some useless shit.
UbbeLol, magic answer is binary trees. Traversal alone saved up my cpu time by 40%+.
Use a composite design pattern.

Both combined together, produce magic of infinite amounts.
Nice share. I love recursion although it can eat up a lot of memory.
(Oct 2, 2011 05:23 AM)ubbelol Wrote:  That is some good stuff Ethereal, recursive methods is something you have to work with whether you like it or not when you get further in your programming.

Here's another example I made to recursively loop through a treeviews nodes to get them all (needed this for my obfuscator project). It's kind of stupid that treeview doesn't have a property implemented to get all nodes. :l

Code
private List<TreeNode> RetrieveAllNodes(TreeView treeView)
  {
    List<TreeNode> outList = new List<TreeNode>();

    foreach (TreeNode node in treeView1.Nodes)
    GetSubNodes(node, outList);

    return outList;
  }

  private void GetSubNodes(TreeNode node, List<TreeNode> outList)
  {
    outList.Add(node);

    foreach (TreeNode _node in node.Nodes)
    GetSubNodes(_node, outList);
  }

Probably some better way to do this?

(Oct 2, 2011 05:50 AM)Nonmod Wrote:  UbbeLol, magic answer is binary trees. Traversal alone saved up my cpu time by 40%+.
Use a composite design pattern.

Both combined together, produce magic of infinite amounts.

This.

(Oct 2, 2011 06:59 AM)KingCobraXD Wrote:  Nice share. I love recursion although it can eat up a lot of memory.

Yeah, that's why you have to be careful you don't forkbomb yourself.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by SIM-3437924Aug 15, 2020 09:57 AM 1 182 Aug 15, 2020 09:57 AM
Last Post: SIM-3437924
[Shell] by SajuukJul 15, 2020 03:48 PM 4 386 Jul 15, 2020 03:48 PM
Last Post: Sajuuk
[Shell] by HackooJul 9, 2020 02:04 PM 11 672 Jul 9, 2020 02:04 PM
Last Post: Forrest74