Skip to content

Commit b84635d

Browse files
ClémentClément
authored andcommitted
Fixing method name in file_displayer project
1 parent 1bdb1d0 commit b84635d

4 files changed

Lines changed: 12 additions & 20 deletions

File tree

source/lectures/control/more_on_recursion.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ tags:
55

66
# More on Recursion
77

8-
The code for this lecture is available [in this archive](./code/projects/AdvancedRecursion.zip).
9-
108
## Re-Introduction
119

1210
We previously defined recursion as follows:
@@ -15,15 +13,15 @@ We previously defined recursion as follows:
1513
1614
Applied very strictly, the simplest (and most likely shortest) recursive method is the following:
1715

18-
```
16+
```{download="./code/projects/AdvancedRecursion.zip"}
1917
!include`snippetStart="// Simplest possible example of a recursive function:", snippetEnd="// Uncomment at your own risk!"` code/projects/AdvancedRecursion/AdvancedRecursion/Program.cs
2018
```
2119

2220
It is a method (`R`) that simply … calls itself. Even if this method does not "do" anything, calling it will most likely make your program crash, since `R` will keep calling itself forever: this is actually an example of an infinite loop, and the basics of the ["fork bomb" attack](https://en.wikipedia.org/wiki/Fork_bomb)^[Except that the fork bomb calls itself *twice*, and in parallel.].
2321

2422
A better definition of recursion would include something about the method eventually terminating, like the following:
2523

26-
```
24+
```{download="./code/projects/AdvancedRecursion.zip"}
2725
!include`snippetStart="// Recursive CountDown method", snippetEnd="// Usage example"` code/projects/AdvancedRecursion/AdvancedRecursion/Program.cs
2826
```
2927

@@ -37,15 +35,15 @@ But note that this method is not _always_ terminating: indeed, calling `CountDow
3735

3836
A possible way to patch this would be to have two additional method: one to count "up" to 0, and one that decides which method to call:
3937

40-
```
38+
```{download="./code/projects/AdvancedRecursion.zip"}
4139
!include`snippetStart="// Recursive CountUp method", snippetEnd="// Mutually recursive methods"` code/projects/AdvancedRecursion/AdvancedRecursion/Program.cs
4240
```
4341

4442
As we can see, `Count` itself is *not* recursive, but it calls a recursive method.
4543

4644
Finally, methods can be *mutually recursive*: a method `MyTurn` can call a `YourTurn` method that itself calls `MyTurn`. While neither method are recursive, they create a recursive situation, as exemplified below:
4745

48-
```
46+
```{download="./code/projects/AdvancedRecursion.zip"}
4947
!include`snippetStart="// Mutually recursive methods", snippetEnd="// Usage example"` code/projects/AdvancedRecursion/AdvancedRecursion/Program.cs
5048
```
5149

source/lectures/control/recursion.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ tags:
55

66
# Recursion
77

8-
The code for this lecture is available [in this archive](./code/projects/RecursionHelloWorld.zip) (first parts) and [this one](./code/projects/ListDirectoriesFileRecursively.zip) (listing files and folders recursively).
9-
108
## Introduction
119

1210
Recursion is a central notion in programming, simple to state but difficult to master: a method is *recursive* if it calls itself.
@@ -17,7 +15,7 @@ Below, we present some simple recursive programs: while some could be written wi
1715

1816
Consider the following:
1917

20-
```
18+
```{download="./code/projects/RecursionHelloWorld.zip"}
2119
!include`snippetStart="// Displays numbers from n to 1", snippetEnd="// Displays numbers from 1 to n"` code/projects/RecursionHelloWorld/RecursionHelloWorld/Program.cs
2220
```
2321

@@ -43,7 +41,7 @@ In our example, it would display "3 2 1 ".
4341

4442
*When* the function calls itself matters a lot. Indeed, consider `displayRAll`, which calls itself *before* executing the `Console.WriteLine` instruction:
4543

46-
```
44+
```{download="./code/projects/RecursionHelloWorld.zip"}
4745
!include`snippetStart="// Displays numbers from 1 to n", snippetEnd="// Recursive multiplication"` code/projects/RecursionHelloWorld/RecursionHelloWorld/Program.cs
4846
```
4947

@@ -72,7 +70,7 @@ In this example, "1 2 3 " would be displayed: the order is reversed with resp
7270

7371
`displayAll` is an example of _tail recursion_: the recursive call is the **last** statement in the method.
7472
`displayRAll` is an example of _head recursion_: the recursive call is the **first** statement in the method.
75-
They are furthormore both examples of *linear recursion*, as they call themselves only once.
73+
They are furthermore both examples of *linear recursion*, as they call themselves only once.
7674

7775
## Recursive Methods Returning a Value
7876

@@ -83,7 +81,7 @@ Recursive methods can also return a value, used by previous calls to compute som
8381
For example, consider that multiplication can be defined by addition: indeed, $x × y$ is $y + y + y + … + y$ where $y$ is summed $x$ times. Stated differently (read: recursively), $x × y$ is $y + ((x - 1) × y)$.
8482
We can implement such a program easily:
8583

86-
```
84+
```{download="./code/projects/RecursionHelloWorld.zip"}
8785
!include`snippetStart="// Recursive multiplication", snippetEnd="// Recursive factorial"` code/projects/RecursionHelloWorld/RecursionHelloWorld/Program.cs
8886
```
8987

@@ -96,7 +94,7 @@ Observe that `mult(10000000, 0)` would call `mult` 10000001 times and add 0 to i
9694
The factorial of $n$ is $n! = n × (n-1) × (n-2) × (n-3) × … × 1$.
9795
This function can easily be implemented using recursion:
9896

99-
```
97+
```{download="./code/projects/RecursionHelloWorld.zip"}
10098
!include`snippetStart="// Recursive factorial", snippetEnd="// An improvment would be to replace"` code/projects/RecursionHelloWorld/RecursionHelloWorld/Program.cs
10199
```
102100

@@ -107,12 +105,8 @@ Note that this code actually compute e.g., $5! = 5 × 4 × 3 × 2 × 1 × 1$ (wi
107105
While multiplication and factorial can be implemented without recursion, some structures makes it natural, or even required, to use recursion.
108106
Going through folders and files is an example of such situation.
109107

110-
```
108+
```{download="./code/projects/ListDirectoriesFileRecursively.zip"}
111109
!include code/projects/ListDirectoriesFileRecursively/ListDirectoriesFileRecursively/Program.cs
112110
```
113111

114-
<!--
115-
`snippetStart="// https://stackoverflow.com/a/929277", snippetEnd="// An improvment would be to replace"
116-
-->
117-
118112
Note that our previous examples were calling themselves only once per method call, but that `ListDir` calls itself as many times as there are folders in the folder currently examined.

source/projects/file_displayer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ A bonus (for more experienced programmers) asks to compute the winner of a race
2323

2424
#### In more details
2525

26-
Your goal is to design and implement a static `TextFileHelper` class containing 5 methods (the fifth one being optional):
26+
Your goal is to design and implement a static `FileDisplayer` class containing 5 methods (the fifth one being optional):
2727

2828
#. A `Display` method that takes a `string` as an argument and displays the content of the file located at the corresponding path if it exists, and an error message otherwise.
2929
#. A `DisplayN` method that takes a `string` and an `int`, and displays the line corresponding to the `int` argument from the file located at the corresponding path if it exists (with a line number before), and an error message otherwise.

source/solutions/io/files.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ tags:
4747
sw.WriteLine("Hello World!!");
4848
```
4949

50-
<details><summary>Solution</summary>This program would
50+
<details><summary>Solution</summary> This program would
5151

5252
#. Create a file located at `filePath`,
5353
#. Store in the file "Hello World!!", followed by a new line.

0 commit comments

Comments
 (0)