Skip to content

Commit

Permalink
Notes on recursion.
Browse files Browse the repository at this point in the history
  • Loading branch information
aubertc committed Nov 5, 2024
1 parent bd0802c commit 4228f13
Show file tree
Hide file tree
Showing 6 changed files with 397 additions and 83 deletions.
261 changes: 261 additions & 0 deletions source/code/projects/AdvancedRecursion/AdvancedRecursion/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
using System;
using System.Collections.Generic;

class Program
{
static void Main(string[] args)
{
// Simplest possible example of a recursive function:
// Warning: dangerous function!
void R()
{
R();
}
// Uncomment at your own risk!
// R();

// Recursive CountDown method
void CountDown(int n)
{
if (n == 0)
{
Console.WriteLine($"{n}: Blast off!");
}
else
{
Console.Write($"{n}…");
CountDown(n - 1);
}
}
// Usage example
CountDown(10);

// Recursive CountUp method:
void CountUp(int n)
{
if (n == 0)
{
Console.WriteLine($"{n}: Blast off!");
}
else
{
Console.Write($"{n}…");
CountUp(n + 1); // <- Only change.
}
}
void Count(int n)
{
if (n < 0)
CountUp(n);
else
CountDown(n);
}
Count(10);
Count(-10);

// Mutually recursive methods
void MyTurn(int n)
{
if (n < 0)
{
Console.WriteLine("The Game is over.");
}
else
{
Console.WriteLine("It's my turn");
n--;
if (n < 0)
{
Console.WriteLine("The Game is over.");
}
else
{
YourTurn(n);
}
}
}
void YourTurn(int n)
{
Console.WriteLine("It's your turn.");
MyTurn(n);
}
// Usage example:
MyTurn(4);

// Sorted method helper
bool SortedH(int[] aP, int currentIndex)
{
if (aP.Length == currentIndex + 1)
return true;
else if (aP[currentIndex] > aP[currentIndex + 1])
return false;
else
return SortedH(aP, currentIndex + 1);
}
// Sorted method
bool Sorted(int[] aP)
{
if (aP == null)
return false;
else
return SortedH(aP, 0);
}
// Examples

int[] arrayS = { 1, 2, 4, 10, 13, 17 };
int[] arrayNS = { 12, 98, 120, 15 };
int[] arrayExample =
{
1,
10,
12,
129,
190,
220,
230,
310,
320,
340,
400,
460,
};

Console.WriteLine("Array sorted: " + Sorted(arrayS));
Console.WriteLine("Array sorted: " + Sorted(arrayNS));
Console.WriteLine(
"Array sorted: " + Sorted(arrayExample)
);

// Binary Search helper
bool BinFindH(int[] aP, int start, int end, int target)
{
int mid = (start + end) / 2;
if (start > end)
{
return false;
}
else
{
if (target == aP[mid])
{
return true;
}
else if (target > aP[mid])
{
return BinFindH(aP, mid + 1, end, target);
}
else
{
return BinFindH(aP, start, mid - 1, target);
}
}
}
// Binary search
bool BinFind(int[] aP, int target)
{
return BinFindH(aP, 0, aP.Length - 1, target);
}

// Examples
Console.Write(
"BinFind: " + BinFind(arrayExample, 10) + "\n"
);
Console.Write(
"BinFind: " + BinFind(arrayExample, 460) + "\n"
);
Console.Write(
"BinFind: " + BinFind(arrayExample, 440) + "\n"
);
Console.Write(
"BinFind: " + BinFind(arrayExample, 480) + "\n"
);

// Recursive way of displaying lists
string DisplayH(
string retString,
List<string> listP,
int indexP
)
{
if (listP.Count == indexP + 1)
{
return retString + listP[indexP] + ".\n";
}
else
{
retString += listP[indexP] + " -> ";
return DisplayH(retString, listP, indexP + 1);
}
}

string Display(List<string> ListP)
{
string retString = "";
return DisplayH(retString, ListP, 0);
}
// Examples
List<string> operatingSystems = new List<string>
{
"Unix",
"Windows",
"iOS",
"OS X",
"Android",
};
Console.WriteLine(Display(operatingSystems));

// Using reference and recursion to display lists
void DisplayRef(ref string descP, List<string> listP)
{
if (listP == null || listP.Count == 0)
{
descP += ".\n";
}
else if (listP.Count == 1)
{
descP += listP[0] + ".\n";
}
else
{
descP += listP[0] + " -> ";
listP.RemoveAt(0);
DisplayRef(ref descP, listP);
}
}
// Example.
string desc = "";
// Note that we create a copy of the list.
List<string> listCopy = new List<string>(
operatingSystems
);
// We now call the method with the copy of the list.
DisplayRef(ref desc, listCopy);
Console.WriteLine(desc);
}
}


/*
// https://gist.github.com/SauliusSun/9572698
class RecursionExample
{
static void Main()
{
string output = Display("", items, 0);
// Result: Node1 <- Node2 <- Node3 <-.
Console.Write(output);
Console.Write(items[0]);
}
}
*/
70 changes: 0 additions & 70 deletions source/code/projects/ListRecursion/ListRecursion/Program.cs

This file was deleted.

14 changes: 7 additions & 7 deletions source/code/projects/Polymorphism1/Polymorphism1/Class.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
class Class1
{
private string attribute1;
private string attribute1;

public void SetAttribute1(string aP)
{
attribute1 = aP;
}
public void SetAttribute1(string aP)
{
attribute1 = aP;
}

public string Property1 { get; set; }
public string Property1 { get; set; }
}

class Class2 : Class1
{
public string Property2 { get; set; }
public string Property2 { get; set; }
}
12 changes: 6 additions & 6 deletions source/code/projects/Polymorphism3/Polymorphism3/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ public virtual string Test()

class Class2 : Class1
{
public override sealed string Test()
public sealed override string Test()
{
return "Class2";
}
}

class Class3: Class2
class Class3 : Class2
{
public override string Test()
{
return "Class 3";
}
public override string Test()
{
return "Class 3";
}
}
Loading

0 comments on commit 4228f13

Please sign in to comment.