From 894d06ee0f0db0fb6b87f96a886692bdf22a1f7e Mon Sep 17 00:00:00 2001 From: elies Date: Sun, 4 Aug 2024 13:15:45 +0200 Subject: [PATCH 1/6] init repo --- software/32.Zig/gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 software/32.Zig/gitkeep diff --git a/software/32.Zig/gitkeep b/software/32.Zig/gitkeep new file mode 100644 index 00000000..e69de29b From 022b1180697359bc7ec9183cc85fd5f2f5299640 Mon Sep 17 00:00:00 2001 From: elies Date: Mon, 5 Aug 2024 18:15:49 +0200 Subject: [PATCH 2/6] Add(./) --- software/32.Zig/README.md | 131 ++++++++++++++++++++++++++++++++++++++ software/32.Zig/SETUP.md | 70 ++++++++++++++++++++ software/32.Zig/TODO.md | 56 ++++++++++++++++ 3 files changed, 257 insertions(+) create mode 100644 software/32.Zig/README.md create mode 100644 software/32.Zig/SETUP.md create mode 100644 software/32.Zig/TODO.md diff --git a/software/32.Zig/README.md b/software/32.Zig/README.md new file mode 100644 index 00000000..6889dfa8 --- /dev/null +++ b/software/32.Zig/README.md @@ -0,0 +1,131 @@ +# Workshop 13 - Introduction to Zig + +Welcome to this introductory workshop on Zig! Zig is a modern programming language that emphasizes robustness, performance and clarity. Today, you'll learn how to install Zig, create your first project, and discover the language's key concepts by creating a palindrome detection program. + +## Prerequisites + +- Basic programming skills (C, C++, or any other language) +- A computer with Internet access + +## Workshop objectives + +- Install Zig on your machine +- Initialize a Zig project +- Understand the basics of the Zig language +- Create a palindrome detection program + +## Step 0 - SETUP + +All the required information to install dependencies can be found in [SETUP.md](./SETUP.md). + +> 💡 We recommend you to follow the [Getting started](https://ziglang.org/learn/getting-started/) for this workshop. + +### Step 1 - Hello World! in Zig + +> ❗ We strongly advise you to use the resources given for this exercise. + +For the first exercise, we simply ask you to write `Hello world!` in your terminal when you run your program. + +To do this, create a file `main.zig` in a folder called `src`. + + +> 💡 Zig file has `zig` extension. + +> 💡 Now, that you have created a file `main.zig`, you can use other files. To use them in your `main.zig` you have to integrate the module ([read more](https://stackoverflow.com/questions/71186556/how-do-i-include-one-zig-file-from-another-zig-file)) + +#### Resources + + - [Build System](https://ziglang.org/learn/build-system/) + - [Doc.Zig](https://ziglang.org/documentation/master/) + +### Step 2 - Palindrome? + +> ❗ We strongly advise you to use the resources given for this exercise. + +For the second exercise, you have to create a function that takes as parameter a string `word`. + +Create a file `palindrome.zig` for this new function. + +This function must return true if the word given in parameter is a palindrome and false in the opposite case. + +#### Resources + - [What is a palindrome ?](https://www.wikiwand.com/en/Palindrome) + - [New function](https://ziglang.org/documentation/master/#Functions) + - [The types in Zig](https://ziglang.org/documentation/master/#Primitive-Types) + - [Control_Structures](https://zig.guide/language-basics/while-loops) + +### Step 3 - Fibonacci sequence + +> ❗ We strongly advise you to use the resources given for this exercise. + +Create a file `fibonacci.zig` for this new function. + +For the third exercise, create a function that takes one parameter: +- A number `max` with type `i32` that represent the number of element to compute. + +You must now display the sequence of Fibonacci from the number of starts to the maximum value. + +Here is a small example of the beginning of the Fibonacci sequence: + +```shell +0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ... +``` + +#### Resources + - [Fibonacci number](https://www.wikiwand.com/en/Fibonacci_number) + - [Match](https://ziglang.org/documentation/master/#Standard-Library-Math-Functions) + - [Vector](https://ziglang.org/documentation/master/#Vectors) + + +### Step 4 - A simple and useful `Calculator`! + +> ❗ We strongly advise you to use the resources given for this exercise. + +Let's go further now! + +The objective of this fifth exercise is to create a simplifying calculator. + +Create a file `calculator.zig` for this new function. + +To do this, you have to make a calculator that can do: +- `Addition` +- `Multiplication` +- `Division` +- `Subtraction` + +between two values that must be retrieved one after the other from the user's input. + +> 💡 Pay attention to your error handling ! + +Display the result of the calculation in your terminal. + +#### Resources + - [Errors](https://ziglang.org/documentation/master/#Errors) + - [Memory](https://zig.guide/standard-library/allocators) + - [ArrayList](https://zig.guide/standard-library/arraylist) + +### Step 5 - Simple Todo List in Zig + +#### Objective + +Create a command-line todo list application where you can: + + - Add a new todo + - Delete a todo by ID + - View all todos + +Each todo has: + + - id (auto-incremented) + - name + - description + +### Step 5 - Use your C code + +Zig provides the command zig translate-c for automatic translation from C source code + +#### Resources + - [Translate-C](https://zig.guide/working-with-c/translate-c) + - [cImport](https://zig.guide/working-with-c/c-import) + - [LinkLibC](https://zig.guide/working-with-c/linking-libc) + diff --git a/software/32.Zig/SETUP.md b/software/32.Zig/SETUP.md new file mode 100644 index 00000000..b286bd6c --- /dev/null +++ b/software/32.Zig/SETUP.md @@ -0,0 +1,70 @@ +# Installing Zig + +### For Fedora + +Since most students use Fedora, installing Zig is very simple. Just open a terminal and run the following command: + +```sh +sudo dnf install zig +``` + +### For other operating systems + +- Visit the official Zig website: ziglang.org. + +### to check installation + +```sh + zig version +``` + +## Initializing a Zig Project + +### Step 1: Create a new project + +To create a new Zig project, open a terminal and navigate to the directory where you wish to create your project. Then run the following command: + +```sh +zig init +``` + +This command will create a basic project structure with the necessary files. + +### Step 2: Explore the project structure + +Once the project has been created, you'll see the following structure: + +```css +. +├── build.zig +├── build.zig.zon +└── src + ├── main.zig + └── root.zig +``` + +- build.zig: This file is a build script for your Zig project. +- src/main.zig: This is the entry point for your Zig program. +- src/root.zig: This is a sub-file of example (don't worry if you don't have it). + +### Step 3: Compile and run the project + +To compile your project, run the following command in your project directory: + +```sh +zig build +``` + +After compilation, a binary will be generated in the zig-out/bin folder (it may be called something different depending on the name of your folder). To run this binary, use the following command: + +```sh +./zig-out/bin/main +``` + +You should see the messages: + "All your codebase are belong to us. + Run `zig build test` to run the tests." are displayed, indicating that your project has been correctly configured and compiled. + +### Next step + +Now that your basic project is ready, let's explore the basic concepts of the Zig language and develop a palindrome detection program. diff --git a/software/32.Zig/TODO.md b/software/32.Zig/TODO.md new file mode 100644 index 00000000..e4aac804 --- /dev/null +++ b/software/32.Zig/TODO.md @@ -0,0 +1,56 @@ +# Step 3 - Simple Todo List in Zig + +## Objective + +Create a command-line todo list application where you can: + + - Add a new todo + - Delete a todo by ID + - View all todos + +Each todo has: + + - id (auto-incremented) + - name + - description + +## Examples + +### User Interaction + + ./zig-out/bin/main + + Choose an action: + 1. Add Todo + 2. Delete Todo + 3. View Todos + 4. Exit + > 1 + Enter name: Buy groceries + Enter description: Milk, eggs, and bread + +### Output + + Todo added successfully. + Choose an action: + 1. Add Todo + 2. Delete Todo + 3. View Todos + 4. Exit + > 3 + ID: 1, Name: Buy groceries, Description: Milk, eggs, and bread + Choose an action: + 1. Add Todo + 2. Delete Todo + 3. View Todos + 4. Exit + > 4 + + > 💡 Pay attention to your error handling ! + +## Resources + +> 💡 To easily test your functions during this workshop remember to check the tools mentioned above + +- [New function](https://ziglang.org/documentation/master/#Functions) +- [The types in Zig](https://ziglang.org/documentation/master/#Primitive-Types) From a5a1d8165578894a581ed034dae22d04780c5010 Mon Sep 17 00:00:00 2001 From: elies Date: Wed, 7 Aug 2024 17:48:16 +0200 Subject: [PATCH 3/6] mod(zig): moddified readme for compliance with official template --- software/32.Zig/README.md | 232 +++++++++++++++++++++++++++++--------- software/32.Zig/SETUP.md | 2 +- software/32.Zig/TODO.md | 56 --------- 3 files changed, 182 insertions(+), 108 deletions(-) delete mode 100644 software/32.Zig/TODO.md diff --git a/software/32.Zig/README.md b/software/32.Zig/README.md index 6889dfa8..b3d10107 100644 --- a/software/32.Zig/README.md +++ b/software/32.Zig/README.md @@ -1,18 +1,18 @@ -# Workshop 13 - Introduction to Zig +# Workshop 32 - Introduction to Zig -Welcome to this introductory workshop on Zig! Zig is a modern programming language that emphasizes robustness, performance and clarity. Today, you'll learn how to install Zig, create your first project, and discover the language's key concepts by creating a palindrome detection program. +Welcome to this introductory workshop on Zig! Zig is a modern programming language that emphasizes robustness, performance and clarity. Today, you'll learn : +✔️ how to install Zig +✔️ create your first project +✔️ discover the language's key concepts by creating a few projects -## Prerequisites +## Introduction -- Basic programming skills (C, C++, or any other language) -- A computer with Internet access +Zig is a general-purpose programming language focused on robustness, performance, and simplicity. It offers manual memory management, safety features, built-in cross-compilation, and seamless C interoperability, making it ideal for system programming and high-performance applications. -## Workshop objectives +### Prerequisites -- Install Zig on your machine -- Initialize a Zig project -- Understand the basics of the Zig language -- Create a palindrome detection program +- Basic programming skills (C, C++, or any other language) +- A computer with Internet access ## Step 0 - SETUP @@ -20,93 +20,167 @@ All the required information to install dependencies can be found in [SETUP.md]( > 💡 We recommend you to follow the [Getting started](https://ziglang.org/learn/getting-started/) for this workshop. -### Step 1 - Hello World! in Zig +## Step 1 - Hello World! in Zig -> ❗ We strongly advise you to use the resources given for this exercise. +> ❗ We strongly advise you to use the documentation provided for this exercise. + +📑 Description: For the first exercise, we simply ask you to write `Hello world!` in your terminal when you run your program. -To do this, create a file `main.zig` in a folder called `src`. +📌 Tasks: +create a file `main.zig` in a folder called `src` with your logic to print the "hello world" -> 💡 Zig file has `zig` extension. +📚 Documentation: +> 💡 Zig file has `zig` extension. > 💡 Now, that you have created a file `main.zig`, you can use other files. To use them in your `main.zig` you have to integrate the module ([read more](https://stackoverflow.com/questions/71186556/how-do-i-include-one-zig-file-from-another-zig-file)) -#### Resources +- [Build System](https://ziglang.org/learn/build-system/) +- [Doc.Zig](https://ziglang.org/documentation/master/) + +✔️ Validation: + + you should see the following : - - [Build System](https://ziglang.org/learn/build-system/) - - [Doc.Zig](https://ziglang.org/documentation/master/) + ```sh + Hello, World! + ``` -### Step 2 - Palindrome? +## Step 2 - Palindrome? > ❗ We strongly advise you to use the resources given for this exercise. +📑 Description: + For the second exercise, you have to create a function that takes as parameter a string `word`. +📌 Tasks: + Create a file `palindrome.zig` for this new function. This function must return true if the word given in parameter is a palindrome and false in the opposite case. -#### Resources - - [What is a palindrome ?](https://www.wikiwand.com/en/Palindrome) - - [New function](https://ziglang.org/documentation/master/#Functions) - - [The types in Zig](https://ziglang.org/documentation/master/#Primitive-Types) - - [Control_Structures](https://zig.guide/language-basics/while-loops) +📚 Documentation: + + -[What is a palindrome ?](https://www.wikiwand.com/en/Palindrome) + -[New function](https://ziglang.org/documentation/master/#Functions) + -[The types in Zig](https://ziglang.org/documentation/master/#Primitive-Types) + -[Control_Structures](https://zig.guide/language-basics/while-loops) -### Step 3 - Fibonacci sequence + ✔️ Validation: + + When you compile and run palindrome.zig, the output should be: + + ```sh + madam is palindrome: true + ``` + or + + ```sh + hello is palindrome: false + ``` + +## Step 3 - Fibonacci sequence > ❗ We strongly advise you to use the resources given for this exercise. -Create a file `fibonacci.zig` for this new function. +📑 Description: + +For the third exercise, you need to create a function that generates and displays the Fibonacci sequence up to a specified number of elements. + +📌 Tasks: + +- Create a file `fibonacci.zig` for this new function. For the third exercise, create a function that takes one parameter: + - A number `max` with type `i32` that represent the number of element to compute. -You must now display the sequence of Fibonacci from the number of starts to the maximum value. +- You must now display the sequence of Fibonacci from the number of starts to the `max` value. Here is a small example of the beginning of the Fibonacci sequence: -```shell -0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ... -``` + ```shell + 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ... + ``` + +📚 Documentation: + + -[Fibonacci number](https://www.wikiwand.com/en/Fibonacci_number) + -[Match](https://ziglang.org/documentation/master/#Standard-Library-Math-Functions) + -[Vector](https://ziglang.org/documentation/master/#Vectors) -#### Resources - - [Fibonacci number](https://www.wikiwand.com/en/Fibonacci_number) - - [Match](https://ziglang.org/documentation/master/#Standard-Library-Math-Functions) - - [Vector](https://ziglang.org/documentation/master/#Vectors) + ✔️ Validation: + Given the Fibonacci sequence (see previous example), if max is 5, the function should output: -### Step 4 - A simple and useful `Calculator`! + ```sh + 0, 1, 1, 2, 3 + ``` + +## Step 4 - A simple and useful `Calculator` > ❗ We strongly advise you to use the resources given for this exercise. +📑 Description: + Let's go further now! The objective of this fifth exercise is to create a simplifying calculator. -Create a file `calculator.zig` for this new function. +📌 Tasks: + +- Create a file `calculator.zig` for this new function. To do this, you have to make a calculator that can do: + - `Addition` - `Multiplication` - `Division` - `Subtraction` -between two values that must be retrieved one after the other from the user's input. +- between two values that must be retrieved one after the other from the user's input. > 💡 Pay attention to your error handling ! -Display the result of the calculation in your terminal. +- Display the result of the calculation in your terminal. + +📚 Documentation: + -[Errors](https://ziglang.org/documentation/master/#Errors) + -[Memory](https://zig.guide/standard-library/allocators) + -[ArrayList](https://zig.guide/standard-library/arraylist) + + ✔️ Validation: + + You should get something like this : + + ```sh + First number: 5 + Operation: + + Second number: 3 + 8 + ``` + + Here's a typical error handeling example : + + ```sh + First number: abc + Operation: + + Second number: 3 + Expected Output: Error message indicating invalid input. + ``` -#### Resources - - [Errors](https://ziglang.org/documentation/master/#Errors) - - [Memory](https://zig.guide/standard-library/allocators) - - [ArrayList](https://zig.guide/standard-library/arraylist) +## Bonuses - TO DO List and C Code translation -### Step 5 - Simple Todo List in Zig +### TO DO List -#### Objective +📑 Description: + +Now that you're used to the basics, you will do a little project to apply what you've learnt so far. + +📌 Tasks: Create a command-line todo list application where you can: @@ -114,18 +188,74 @@ Create a command-line todo list application where you can: - Delete a todo by ID - View all todos -Each todo has: +Each todo should have at least three fields: - id (auto-incremented) - name - description -### Step 5 - Use your C code +> 💡 To easily test your functions during this workshop remember to check the testing tools mentioned above + +📚 Documentation: + +- [New function](https://ziglang.org/documentation/master/#Functions) +- [The types in Zig](https://ziglang.org/documentation/master/#Primitive-Types) + +✔️ Validation: + + ```sh + ./zig-out/bin/main + + Choose an action: + 1. Add Todo + 2. Delete Todo + 3. View Todos + 4. Exit + > 1 + Enter name: Buy groceries + Enter description: Milk, eggs, and bread + Todo added successfully. + ``` + + ```sh + Choose an action: + 5. Add Todo + 6. Delete Todo + 7. View Todos + 8. Exit + > 3 + ID: 1, Name: Buy groceries, Description: Milk, eggs, and bread + Choose an action: + 9. Add Todo + 10. Delete Todo + 11. View Todos + 12. Exit + > 4 + ``` + + > 💡 Pay attention to your error handling ! + +### Use The C Translation tool + +📑 Description: + +Zig provides the command zig translate-c for automatic translation from C source code. You can try to read the documentation and have fun with the tool. + +📚 Documentation: + + -[Translate-C](https://zig.guide/working-with-c/translate-c) + -[cImport](https://zig.guide/working-with-c/c-import) + -[LinkLibC](https://zig.guide/working-with-c/linking-libc) + +## Conclusion + +Well done ! You've accomplished a lot with the Zig Workshop, and there is so much more to discover. Refer to the official documentation to deep-dive into it. + +Hope you enjoyed the workshop! + +## Authors -Zig provides the command zig translate-c for automatic translation from C source code +- Elie STROUN +- Pierre LISSOPE -#### Resources - - [Translate-C](https://zig.guide/working-with-c/translate-c) - - [cImport](https://zig.guide/working-with-c/c-import) - - [LinkLibC](https://zig.guide/working-with-c/linking-libc) - +> 🚀 Follow us on our different social networks, and put a star 🌟 on `PoC's` repositories. \ No newline at end of file diff --git a/software/32.Zig/SETUP.md b/software/32.Zig/SETUP.md index b286bd6c..3003b235 100644 --- a/software/32.Zig/SETUP.md +++ b/software/32.Zig/SETUP.md @@ -1,6 +1,6 @@ # Installing Zig -### For Fedora +## For Fedora Since most students use Fedora, installing Zig is very simple. Just open a terminal and run the following command: diff --git a/software/32.Zig/TODO.md b/software/32.Zig/TODO.md deleted file mode 100644 index e4aac804..00000000 --- a/software/32.Zig/TODO.md +++ /dev/null @@ -1,56 +0,0 @@ -# Step 3 - Simple Todo List in Zig - -## Objective - -Create a command-line todo list application where you can: - - - Add a new todo - - Delete a todo by ID - - View all todos - -Each todo has: - - - id (auto-incremented) - - name - - description - -## Examples - -### User Interaction - - ./zig-out/bin/main - - Choose an action: - 1. Add Todo - 2. Delete Todo - 3. View Todos - 4. Exit - > 1 - Enter name: Buy groceries - Enter description: Milk, eggs, and bread - -### Output - - Todo added successfully. - Choose an action: - 1. Add Todo - 2. Delete Todo - 3. View Todos - 4. Exit - > 3 - ID: 1, Name: Buy groceries, Description: Milk, eggs, and bread - Choose an action: - 1. Add Todo - 2. Delete Todo - 3. View Todos - 4. Exit - > 4 - - > 💡 Pay attention to your error handling ! - -## Resources - -> 💡 To easily test your functions during this workshop remember to check the tools mentioned above - -- [New function](https://ziglang.org/documentation/master/#Functions) -- [The types in Zig](https://ziglang.org/documentation/master/#Primitive-Types) From d142e80fa4d016a66dc2f2b3789b9a8734afdd94 Mon Sep 17 00:00:00 2001 From: elies Date: Mon, 12 Aug 2024 18:24:36 +0200 Subject: [PATCH 4/6] mod(zig): README.md followed pull request advices --- software/32.Zig/README.md | 40 ++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/software/32.Zig/README.md b/software/32.Zig/README.md index b3d10107..ccbaae7f 100644 --- a/software/32.Zig/README.md +++ b/software/32.Zig/README.md @@ -1,19 +1,14 @@ # Workshop 32 - Introduction to Zig Welcome to this introductory workshop on Zig! Zig is a modern programming language that emphasizes robustness, performance and clarity. Today, you'll learn : -✔️ how to install Zig -✔️ create your first project -✔️ discover the language's key concepts by creating a few projects +✔️ How to install Zig +✔️ Create your first project +✔️ Discover the language's key concepts by creating a few projects ## Introduction Zig is a general-purpose programming language focused on robustness, performance, and simplicity. It offers manual memory management, safety features, built-in cross-compilation, and seamless C interoperability, making it ideal for system programming and high-performance applications. -### Prerequisites - -- Basic programming skills (C, C++, or any other language) -- A computer with Internet access - ## Step 0 - SETUP All the required information to install dependencies can be found in [SETUP.md](./SETUP.md). @@ -30,12 +25,14 @@ For the first exercise, we simply ask you to write `Hello world!` in your termin 📌 Tasks: -create a file `main.zig` in a folder called `src` with your logic to print the "hello world" +Create a file `main.zig` in a folder called `src` with your logic to print the "hello world" + +src +└── main.zig 📚 Documentation: -> 💡 Zig file has `zig` extension. -> 💡 Now, that you have created a file `main.zig`, you can use other files. To use them in your `main.zig` you have to integrate the module ([read more](https://stackoverflow.com/questions/71186556/how-do-i-include-one-zig-file-from-another-zig-file)) +> 💡 Now, that you have created a file `main.zig`, you can use other zig files. To use them in your `main.zig` you have to integrate the module ([read more](https://stackoverflow.com/questions/71186556/how-do-i-include-one-zig-file-from-another-zig-file)) - [Build System](https://ziglang.org/learn/build-system/) - [Doc.Zig](https://ziglang.org/documentation/master/) @@ -50,8 +47,6 @@ create a file `main.zig` in a folder called `src` with your logic to print the " ## Step 2 - Palindrome? -> ❗ We strongly advise you to use the resources given for this exercise. - 📑 Description: For the second exercise, you have to create a function that takes as parameter a string `word`. @@ -71,6 +66,21 @@ This function must return true if the word given in parameter is a palindrome an ✔️ Validation: + Here's a main example : + + pub fn main() void { + const stdout = std.io.getStdOut().writer(); + + const test_word1 = "madam"; + const test_word2 = "hello"; + + const result1 = is_palindrome(test_word1); + const result2 = is_palindrome(test_word2); + + stdout.print("{} is palindrome: {}\n", .{test_word1, result1}) catch {}; + stdout.print("{} is palindrome: {}\n", .{test_word2, result2}) catch {}; +} + When you compile and run palindrome.zig, the output should be: ```sh @@ -84,8 +94,6 @@ This function must return true if the word given in parameter is a palindrome an ## Step 3 - Fibonacci sequence -> ❗ We strongly advise you to use the resources given for this exercise. - 📑 Description: For the third exercise, you need to create a function that generates and displays the Fibonacci sequence up to a specified number of elements. @@ -122,8 +130,6 @@ Here is a small example of the beginning of the Fibonacci sequence: ## Step 4 - A simple and useful `Calculator` -> ❗ We strongly advise you to use the resources given for this exercise. - 📑 Description: Let's go further now! From 4105443b864245ab2e3e78ed6fa099dcd894f277 Mon Sep 17 00:00:00 2001 From: elies Date: Mon, 26 Aug 2024 14:29:44 +0200 Subject: [PATCH 5/6] mod(zig): updated workshop --- software/32.Zig/README.md | 137 +++++++++++++++++--------------------- software/32.Zig/SETUP.md | 2 +- 2 files changed, 63 insertions(+), 76 deletions(-) diff --git a/software/32.Zig/README.md b/software/32.Zig/README.md index ccbaae7f..e1189dca 100644 --- a/software/32.Zig/README.md +++ b/software/32.Zig/README.md @@ -21,40 +21,56 @@ All the required information to install dependencies can be found in [SETUP.md]( 📑 Description: -For the first exercise, we simply ask you to write `Hello world!` in your terminal when you run your program. +For the first exercise, we simply ask you to print `Hello world!` in your terminal when you run your program. 📌 Tasks: Create a file `main.zig` in a folder called `src` with your logic to print the "hello world" +It should look like this 🔽 + +```sh src └── main.zig +``` 📚 Documentation: -> 💡 Now, that you have created a file `main.zig`, you can use other zig files. To use them in your `main.zig` you have to integrate the module ([read more](https://stackoverflow.com/questions/71186556/how-do-i-include-one-zig-file-from-another-zig-file)) +💡 After creating your main.zig file, you can include and use logic from other Zig files in your project. To do this, you need to integrate the modules from those files into your main.zig. You can achieve this by using the @import statement to include and access the functionality defined in different Zig files. +For more details on including Zig files, check out this guide : - [Build System](https://ziglang.org/learn/build-system/) - [Doc.Zig](https://ziglang.org/documentation/master/) ✔️ Validation: - you should see the following : +```sh +zig build-exe src/main.zig +./main +``` + +you should see the following : - ```sh - Hello, World! - ``` +```sh +Hello, World! +``` ## Step 2 - Palindrome? 📑 Description: -For the second exercise, you have to create a function that takes as parameter a string `word`. +For the second exercise, you have to create a function that takes as parameter a string named `word`. 📌 Tasks: Create a file `palindrome.zig` for this new function. +```sh +src +└── main.zig +└── palindrome.zig +``` + This function must return true if the word given in parameter is a palindrome and false in the opposite case. 📚 Documentation: @@ -66,80 +82,51 @@ This function must return true if the word given in parameter is a palindrome an ✔️ Validation: - Here's a main example : +Here's a main example : - pub fn main() void { +```zig +pub fn main() void { const stdout = std.io.getStdOut().writer(); - const test_word1 = "madam"; const test_word2 = "hello"; - const result1 = is_palindrome(test_word1); const result2 = is_palindrome(test_word2); - stdout.print("{} is palindrome: {}\n", .{test_word1, result1}) catch {}; stdout.print("{} is palindrome: {}\n", .{test_word2, result2}) catch {}; } +``` When you compile and run palindrome.zig, the output should be: - ```sh - madam is palindrome: true - ``` - or - - ```sh - hello is palindrome: false - ``` - -## Step 3 - Fibonacci sequence - -📑 Description: - -For the third exercise, you need to create a function that generates and displays the Fibonacci sequence up to a specified number of elements. - -📌 Tasks: - -- Create a file `fibonacci.zig` for this new function. +```sh +madam is palindrome: true +``` -For the third exercise, create a function that takes one parameter: +or -- A number `max` with type `i32` that represent the number of element to compute. +```sh +hello is palindrome: false +``` -- You must now display the sequence of Fibonacci from the number of starts to the `max` value. - -Here is a small example of the beginning of the Fibonacci sequence: - - ```shell - 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ... - ``` - -📚 Documentation: - - -[Fibonacci number](https://www.wikiwand.com/en/Fibonacci_number) - -[Match](https://ziglang.org/documentation/master/#Standard-Library-Math-Functions) - -[Vector](https://ziglang.org/documentation/master/#Vectors) - - ✔️ Validation: - - Given the Fibonacci sequence (see previous example), if max is 5, the function should output: - - ```sh - 0, 1, 1, 2, 3 - ``` - -## Step 4 - A simple and useful `Calculator` +## Step 3 - A simple and useful `Calculator` 📑 Description: Let's go further now! -The objective of this fifth exercise is to create a simplifying calculator. +The objective of this fifth exercise is to create a simplified calculator in the terimnal. +For this step you will create a interactive calculator that will take a first number, then the operation and finally the second number and once you have all the information, compute all that and print the result. 📌 Tasks: - Create a file `calculator.zig` for this new function. +```sh +src +└── main.zig +└── calculator.zig +``` + To do this, you have to make a calculator that can do: - `Addition` @@ -152,7 +139,7 @@ To do this, you have to make a calculator that can do: > 💡 Pay attention to your error handling ! - Display the result of the calculation in your terminal. - +For this step you will create a interactive calculator that will take a first number, then the operation and finally the second number and once you have all the information, compute all that and print the result. 📚 Documentation: -[Errors](https://ziglang.org/documentation/master/#Errors) -[Memory](https://zig.guide/standard-library/allocators) @@ -160,23 +147,23 @@ To do this, you have to make a calculator that can do: ✔️ Validation: - You should get something like this : +You should get something like this : - ```sh +```sh First number: 5 Operation: + Second number: 3 8 - ``` +``` - Here's a typical error handeling example : +Here's a typical error handeling example : - ```sh +```sh First number: abc Operation: + Second number: 3 Expected Output: Error message indicating invalid input. - ``` +``` ## Bonuses - TO DO List and C Code translation @@ -190,15 +177,15 @@ Now that you're used to the basics, you will do a little project to apply what y Create a command-line todo list application where you can: - - Add a new todo - - Delete a todo by ID - - View all todos +- Add a new todo +- Delete a todo by ID +- View all todos Each todo should have at least three fields: - - id (auto-incremented) - - name - - description +- id (auto-incremented) which will be an integer +- name which will be a string +- description which will also be a string > 💡 To easily test your functions during this workshop remember to check the testing tools mentioned above @@ -209,7 +196,7 @@ Each todo should have at least three fields: ✔️ Validation: - ```sh +```sh ./zig-out/bin/main Choose an action: @@ -221,9 +208,9 @@ Each todo should have at least three fields: Enter name: Buy groceries Enter description: Milk, eggs, and bread Todo added successfully. - ``` +``` - ```sh +```sh Choose an action: 5. Add Todo 6. Delete Todo @@ -237,9 +224,9 @@ Each todo should have at least three fields: 11. View Todos 12. Exit > 4 - ``` +``` - > 💡 Pay attention to your error handling ! +> 💡 Pay attention to your error handling ! ### Use The C Translation tool @@ -264,4 +251,4 @@ Hope you enjoyed the workshop! - Elie STROUN - Pierre LISSOPE -> 🚀 Follow us on our different social networks, and put a star 🌟 on `PoC's` repositories. \ No newline at end of file +> 🚀 Follow us on our different social networks, and put a star 🌟 on `PoC's` repositories. diff --git a/software/32.Zig/SETUP.md b/software/32.Zig/SETUP.md index 3003b235..f0ab03bb 100644 --- a/software/32.Zig/SETUP.md +++ b/software/32.Zig/SETUP.md @@ -34,7 +34,7 @@ This command will create a basic project structure with the necessary files. Once the project has been created, you'll see the following structure: -```css +```sh . ├── build.zig ├── build.zig.zon From e47e71a9addf854d619cc4d7152ebfea48fa7d28 Mon Sep 17 00:00:00 2001 From: elies Date: Mon, 26 Aug 2024 14:36:41 +0200 Subject: [PATCH 6/6] mod(zig): added the Pascal's Triangle Line exercice --- software/32.Zig/README.md | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/software/32.Zig/README.md b/software/32.Zig/README.md index e1189dca..2a6f0ff4 100644 --- a/software/32.Zig/README.md +++ b/software/32.Zig/README.md @@ -165,6 +165,56 @@ Here's a typical error handeling example : Expected Output: Error message indicating invalid input. ``` +## Step 4 - Pascal's Triangle Line + +📑 Description: + +Let's dive into a more advanced topic! + +The objective of this exercise is to generate the n-th line of Pascal's Triangle. Pascal's Triangle is a triangular array of binomial coefficients. For this step, you will create a function that takes an int32 parameter n and returns a list of numbers corresponding to the n-th line of the triangle. + +📌 Tasks: + +Create a file pascals_triangle.zig for this new function. + +Your project structure should look like this 🔽 + +```sh + src + └── main.zig + └── pascals_triangle.zig +``` + +Implement the function to compute the n-th line of Pascal's Triangle. You will need to: + Create a function that computes the binomial coefficients for the given line index n. + Use an efficient method to calculate these coefficients. + Return a list of coefficients that represents the n-th line of Pascal's Triangle. + +Test your implementation by adding a main function that prints out the line of Pascal's Triangle for a given n. + +📚 Documentation: + +Factorials : +Binomial Coefficient: +Array Manipulation + +✔️ Validation: + +To validate your exercise, compile and run your program. You should see output similar to the following: + +```sh +zig build-exe src/pascals_triangle.zig +./pascals_triangle +``` + +Expected output: + +```sh +Line 5 of Pascal's Triangle: 1, 5, 10, 10, 5, 1 +``` + +Ensure your program correctly calculates and displays the n-th line of Pascal's Triangle. Check different values of n to verify that the output is accurate. + ## Bonuses - TO DO List and C Code translation ### TO DO List