Skip to content

Commit 00bcc74

Browse files
authored
finished setting up page for 2521-fundamentals workshop (#248)
1 parent 8968a41 commit 00bcc74

File tree

6 files changed

+316
-4
lines changed

6 files changed

+316
-4
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
title: Break, Continue, and Ternary
3+
date: 2023-09-27
4+
desc: None of this code makes any sense to me! Help!
5+
tags:
6+
- 'COMP2521'
7+
- 'Exam Revision'
8+
author: CSESoc Education Team
9+
coverPhoto: '/images/generic/alexandru-acea-XEB8y0nRRP4-unsplash.jpg'
10+
---
11+
12+
# The break statement
13+
14+
Break statements work by telling your loop to immediately stop. It will not execute the remainder of the current iteration, and will just immediately jump to the end of the loop.
15+
16+
```c
17+
while (1) {
18+
if (1) {
19+
break;
20+
}
21+
printf("this will not be printed!");
22+
}
23+
```
24+
25+
Oookay, but that sounds pretty useless... When would I ever want this!
26+
Sometimes you want to check conditions before running an iteration, but it would become clunky top have it as a condition (think flag variables). In this case a `break` statement is great!
27+
28+
Consider the following clunky yucky code:
29+
30+
```c
31+
int index = 0;
32+
int found = 0;
33+
while (index < SIZE && found = 0) {
34+
if (array[index] == something) {
35+
found = 1;
36+
}
37+
index++;
38+
}
39+
index--;
40+
```
41+
42+
This can be simplified to:
43+
44+
```c
45+
int index = 0;
46+
while (index < SIZE) {
47+
if (array[index] == something) {
48+
break;
49+
}
50+
index++;
51+
}
52+
```
53+
54+
Nice, this makes things a lot tidier!
55+
In general, I like to use break as a "catcher", whether it be to catch errors before they happen, or otherwise catch some condition to end my loop!
56+
57+
# The continue statement
58+
59+
Continue statements tell a loop to stop executing the rest of the code for that iteration, and jump straight back to the top!
60+
61+
```c
62+
for (int i = 0; i < 20; ++i) {
63+
if (i % 2 == 0) {
64+
continue; // skip to the next iteration if the number is even
65+
}
66+
printf("%d\n", i); // will not print even numbers, since it will not get to this point
67+
}
68+
```
69+
70+
You might be thinking "this seems a lot more niche than `break`"...
71+
And you're right! Usually `continue` just lets us avoid one layer of if-nesting, but if you spot a place where you want to go straight to the next iteration without ending the loop entirely, maybe this would be a option to consider!
72+
73+
In general, I don't use continue basically ever unless I get the lightbulb moment where the situation exactly matches one where I want to go straight to the next iteration.
74+
75+
# The ternary operator
76+
77+
This one is luckily quite simple, though daunting at first! Let's take a look at the general format of a ternary operator!
78+
79+
```c
80+
variable = condition ? value_if_true : value_if_false;
81+
```
82+
83+
This is exactly equivalent to:
84+
85+
```c
86+
if (condition) {
87+
variable = value_if_true;
88+
} else {
89+
variable = value_if_false;
90+
}
91+
```
92+
93+
Though note that a ternary doesn't necessary have to bind its value to a variable - you can just return it or otherwise use it if you want!
94+
95+
This is thankfully a pretty simple one - any time you want to set a value based on whether something is true or not, you can just use a ternary! **You should, however, think about whether it actually makes your code more readable.**

data/workshops/2521-funda-23T3.mdx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: CSESoc COMP2521 Fundamentals workshop
3+
desc: Resources to give you a solid foundation for algorithms later in the course 😄
4+
course: COMP2521 Fundamentals
5+
offering: 23T3
6+
---
7+
8+
# CSESoc COMP2521 Fundamentals workshop
9+
10+
This is a set of practical programming problems designed to help you develope a solid foundation for algorithms later in the course.
11+
12+
To start, log in to cse servers via ssh and create a new folder:
13+
14+
```bash:~
15+
$ mkdir 2521-fundamentals
16+
$ cd 2521-fundamentals
17+
```
18+
19+
<br />
20+
21+
## Miscellaneous resources
22+
23+
<br />
24+
25+
### [Abstract Data Types - The Why and How]
26+
27+
[slides](https://docs.google.com/presentation/d/1wZ6S2RVDqLpSOQOlAyEl0lNuVgo39vG4VSE3BWUG0ww/edit?usp=sharing)
28+
29+
<figure class="video_container">
30+
<iframe
31+
src="https://docs.google.com/presentation/d/e/2PACX-1vQyClrhYOlgJ2duoeYd2eBVn21P4-lZ53pR10yWVk-Ou8XbtIeMpL2lKLAdZTIeikowLHdfrLSRoYs1/embed?start=false&loop=false&delayms=3000"
32+
frameborder="0"
33+
width="960"
34+
height="569"
35+
allowfullscreen="true"
36+
mozallowfullscreen="true"
37+
webkitallowfullscreen="true"></iframe>
38+
</figure>
39+
40+
<br />
41+
42+
### [Time complexity analysis and Recursion]
43+
44+
<span> Recording </span>
45+
46+
### [How to use Break, Continue, and Ternary]
47+
48+
Access the article [here](/articles/break-continue-ternary)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
title: Palindrome
3+
desc: Determine if a line is a palindrome or not **without using loops**.
4+
class: COMP2521-funda
5+
difficulty: 2
6+
---
7+
8+
# Palindrome?
9+
10+
Create a new file called 'palindrome.c' and copy the starter code [here](https://github.com/Allynixtor/comp2521-fundamentals-23T3/blob/main/problems/palindrome/palindrome.c) into it.
11+
12+
An palindrome is a string which contains the same characters going forward and backwards.
13+
14+
Implement the `is_palindromic` function in `is_palindromic.c` to return true if a given input line is a palindrome, and false if it is not.
15+
**But we have a problem, our computer is a tsundere and doesn't want you to use any loops at all!**
16+
17+
So, we must implement `is_palindromic` without any loops. Though you are free to make any extra functions you want (provided they also do not contain loops).
18+
19+
The output from your program should look **exactly** like this:
20+
21+
```bash:~/2521-fundamentals/palindrome
22+
$ gcc is_palindromic.c -o is_palindromic
23+
$ ./is_palindromic
24+
Enter a line: racecar
25+
The line is a palindrome!
26+
```
27+
28+
```bash:~/2521-fundamentals/palindrome
29+
$ ./is_palindromic
30+
Enter a line:
31+
The line is a palindrome!
32+
```
33+
34+
```bash:~/2521-fundamentals/palindrome
35+
$ ./is_palindromic
36+
Enter a line: racecars
37+
The line is not a palindrome!
38+
```
39+
40+
```bash:~/2521-fundamentals/palindrome
41+
$ ./is_palindromic
42+
Enter a line: maam
43+
The line is a palindrome!
44+
```
45+
46+
```bash:~/2521-fundamentals/palindrome
47+
$ ./is_palindromic
48+
Enter a line: eeteee
49+
The line is not a palindrome!
50+
```
51+
52+
## Assumptions/Restrictions/Clarifications
53+
54+
- The main function handles replacing the newline with a null-terminator
55+
- The input line string will be of length $N$ ($0 ≤ N ≤ 1024$)
56+
- You are allowed to make other functions, but **must not use loops**.
57+
58+
## Hints
59+
60+
<details>
61+
<summary> Hint 1 </summary>
62+
<br />
63+
if a string is a single character then it is always a palindrome, and you can
64+
easily figure out if a two-character string is a palindrome with a single
65+
comparison
66+
</details>
67+
68+
<details>
69+
<summary> Hint 2 </summary>
70+
<br />
71+
if a string is a palindrome, what can we say about the substring that excludes
72+
the left-most and right-most letter?
73+
</details>
74+
75+
<details>
76+
<summary> Hint 3 </summary>
77+
<br />
78+
for a string to be a palindrome, the left-most and right-most letter must be
79+
the same, and the substring excluding those two letters must be palindromic
80+
</details>
81+
## Solution
82+
83+
You can view the solution code to this problem [here](https://github.com/Allynixtor/comp2521-fundamentals-23T3/blob/main/problems/palindrome/solution.c).
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
title: Poggers
3+
desc: Determine if a list is poggers or not **without using loops**.
4+
class: COMP2521-funda
5+
difficulty: 2
6+
---
7+
8+
# Poggers?
9+
10+
Create a new file called 'poggers.c' and copy the starter code [here](https://github.com/Allynixtor/comp2521-fundamentals-23T3/blob/main/problems/poggers/poggers.c) into it.
11+
12+
An list is poggers if every node matches the following criteria:
13+
14+
- If the node is even, then the next node must be of strictly greater value.
15+
- If the node is odd, then the next node must be of strictly lesser value.
16+
17+
Implement the `listIsPoggers` function in `listIsPoggers.c` to return true if the input list is poggers, and false otherwise.
18+
**But we have a problem, our computer is a tsundere and doesn't want you to use any loops at all!**
19+
20+
So, we must implement `listIsPoggers` without any loops. Though you are free to make any extra functions you want (provided they also do not contain loops). For this exercise though, extra functions should not be necessary!
21+
22+
The output from your program should look **exactly** like this:
23+
24+
```bash:~/2521-fundamentals/poggers
25+
$ gcc listIsPoggers.c -o listIsPoggers
26+
$ ./listIsPoggers
27+
Enter list size: 5
28+
Enter list values: 3 2 3 2 3
29+
List: [3, 2, 3, 2, 3]
30+
POGGERS POGU POG
31+
```
32+
33+
```bash:~/2521-fundamentals/poggers
34+
$ ./listIsPoggers
35+
Enter list size: 0
36+
List: []
37+
POGGERS POGU POG
38+
```
39+
40+
```bash:~/2521-fundamentals/poggers
41+
$ ./listIsPoggers
42+
Enter list size: 5
43+
Enter list values: 3 2 3 2 1
44+
List: [3, 2, 3, 2, 1]
45+
NOT POG
46+
```
47+
48+
```bash:~/2521-fundamentals/poggers
49+
$ ./listIsPoggers
50+
Enter list size: 9
51+
Enter list values: 2 4 8 9 3 -5 -6 -4 0
52+
List: [2, 4, 8, 9, 3, -5, -6, -4, 0]
53+
POGGERS POGU POG
54+
```
55+
56+
## Assumptions/Restrictions/Clarifications
57+
58+
- You are allowed to make other functions, but **must not use any loops**.
59+
60+
## Hints
61+
62+
<details>
63+
<summary> Hint 1 </summary>
64+
<br />
65+
We may trivially say that an empty list or a list with a singular node is
66+
poggers.
67+
</details>
68+
69+
<details>
70+
<summary> Hint 2 </summary>
71+
<br />
72+
Assuming the sublist from list->next onwards is poggers, what can we say about
73+
the requirement(s) for our list onwards to be poggers?
74+
</details>
75+
76+
<details>
77+
<summary> Hint 3 </summary>
78+
<br />
79+
If the sublist from list->next onwards is poggers, then we know that we only
80+
need to check conditions between list->value and list->next->value.
81+
</details>
82+
83+
## Solution
84+
85+
You can view the solution code to this problem [here](https://github.com/Allynixtor/comp2521-fundamentals-23T3/blob/main/problems/poggers/solution.c).

pages/articles.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ const Articles: NextPage = ({ articles, allTags, courseOfferingContent, workshop
138138

139139
{/* Uncomment once we have content for workshops */}
140140

141-
{/* <Flex
141+
<Flex
142142
as="main"
143143
css={{
144144
flexDirection: 'column',
@@ -157,7 +157,7 @@ const Articles: NextPage = ({ articles, allTags, courseOfferingContent, workshop
157157
Explore the many workshops our Education Team has curated to become big brain.
158158
</Text>
159159
<WorkshopsContainerHomePage allWorkshopsOffering={workshopOfferingContent} />
160-
</Flex> */}
160+
</Flex>
161161

162162
<Text
163163
size="headline"

pages/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const Home: NextPage = ({ articles, courseOfferingContent, workshopOfferingConte
9191
</Flex>
9292
{/* Uncomment once we have content for workshops */}
9393

94-
{/* <Flex
94+
<Flex
9595
as="main"
9696
css={{
9797
flexDirection: 'column',
@@ -110,7 +110,8 @@ const Home: NextPage = ({ articles, courseOfferingContent, workshopOfferingConte
110110
Explore the many workshops our Education Team has curated to become big brain.
111111
</Text>
112112
<WorkshopsContainerHomePage allWorkshopsOffering={workshopOfferingContent} />
113-
</Flex> */}
113+
</Flex>
114+
114115
<Flex
115116
as="main"
116117
css={{

0 commit comments

Comments
 (0)