end of day 4 week 1
This commit is contained in:
0
progress.md
Normal file
0
progress.md
Normal file
16
week 1/day 4/exo1.ps1
Normal file
16
week 1/day 4/exo1.ps1
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
# exo 1
|
||||||
|
$number = [int](Read-Host('please enter a number'));
|
||||||
|
|
||||||
|
if(!$number || $number -isnot [int])
|
||||||
|
{
|
||||||
|
Write-Host('you should enter a number !')
|
||||||
|
exit
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($number % 2)
|
||||||
|
{
|
||||||
|
Write-Host('Odd');
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
Write-Host('Even');
|
||||||
|
}
|
||||||
13
week 1/day 4/exo2.ps1
Normal file
13
week 1/day 4/exo2.ps1
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# exo 2
|
||||||
|
# return the number of character of a string only if they're letter
|
||||||
|
|
||||||
|
$text = Read-Host('please enter a phrase')
|
||||||
|
$counter = 0
|
||||||
|
|
||||||
|
foreach ($char in $text.ToCharArray()) {
|
||||||
|
if($char -match '[A-Za-z]'){
|
||||||
|
$counter++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host("your string contain ${counter} character")
|
||||||
28
week 1/day 4/exo3.ps1
Normal file
28
week 1/day 4/exo3.ps1
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# accept the username only if
|
||||||
|
# it contain letters, the username is inferior at 20
|
||||||
|
# start with a letter, no digit, no space
|
||||||
|
|
||||||
|
$username = Read-Host("Please enter your username");
|
||||||
|
$badChar = $false;
|
||||||
|
|
||||||
|
if($username -eq ''){
|
||||||
|
Write-Host("The username can't be empty");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($char in $username.ToCharArray()){
|
||||||
|
if($char -notmatch '[A-Za-z]' -or $char -eq ' '){
|
||||||
|
$badChar = $true;
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$firstLetter = $username[0]
|
||||||
|
$validFirstLetter = $firstLetter -match '[A-Za-z]'
|
||||||
|
|
||||||
|
if(-not $badChar -and $username.Length -le 20 -and $validFirstLetter ){
|
||||||
|
Write-Host("Accepted");
|
||||||
|
}else{
|
||||||
|
Write-Host("Invalid");
|
||||||
|
|
||||||
|
}
|
||||||
24
week 1/day 4/exo4.ps1
Normal file
24
week 1/day 4/exo4.ps1
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
$users = @("admin", "root1", "John_Doe", "Alice", "Bob42")
|
||||||
|
|
||||||
|
foreach($username in $users){
|
||||||
|
|
||||||
|
$badChar = $false
|
||||||
|
|
||||||
|
foreach($char in $username.ToCharArray()){
|
||||||
|
if($char -notmatch '[A-Za-z]' -or $char -eq ' '){
|
||||||
|
$badChar = $true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$firstLetter = $username[0]
|
||||||
|
$validFirstLetter = $firstLetter -match '[A-Za-z]'
|
||||||
|
|
||||||
|
if(-not $badChar -and $username.Length -le 20 -and $validFirstLetter ){
|
||||||
|
Write-Host("$username is valid");
|
||||||
|
}else{
|
||||||
|
Write-Host("$username is Invalid");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
24
week 1/day 4/exo5.md
Normal file
24
week 1/day 4/exo5.md
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
**Task:**
|
||||||
|
|
||||||
|
* Explain what happens if the number of users doubles
|
||||||
|
* Write the runtime formula using:
|
||||||
|
|
||||||
|
* `n` = number of users
|
||||||
|
* `m` = average username length
|
||||||
|
|
||||||
|
**Expected reasoning:**
|
||||||
|
|
||||||
|
* Doubling the list doubles the execution time
|
||||||
|
* Linear runtime: `T(n, m) = n × m`
|
||||||
|
|
||||||
|
**Answer**
|
||||||
|
|
||||||
|
if the number of user double in size
|
||||||
|
|
||||||
|
$$
|
||||||
|
T(2n, m) = n /times 2 /times m
|
||||||
|
$$
|
||||||
|
|
||||||
|
so the number of execution will multiply by 2
|
||||||
|
|
||||||
|
the big O notation will be O(n⋅m)
|
||||||
137
week 1/day 4/readme.md
Normal file
137
week 1/day 4/readme.md
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
# 📘 Week 1 · Day 4 — PowerShell Exercises
|
||||||
|
|
||||||
|
This document describes **all exercises for Week 1 Day 4**, focused on **PowerShell conditionals, loops, and sysadmin-oriented input validation**.
|
||||||
|
|
||||||
|
The goal is to translate your existing algorithmic knowledge (from C, Python, SQL) into **PowerShell syntax and pipelines**, preparing you for Windows administration and automation tasks.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🎯 Learning Objectives
|
||||||
|
|
||||||
|
By completing today’s exercises, you will:
|
||||||
|
|
||||||
|
* Use `if / elseif / else` for conditional logic
|
||||||
|
* Loop over strings and collections with `foreach`
|
||||||
|
* Apply input validation logic in a Windows scripting context
|
||||||
|
* Understand early exit using `break`
|
||||||
|
* Reason about algorithm runtime (`T(n)`) in PowerShell
|
||||||
|
* Practice defensive programming for security-sensitive scripts
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🛠 Exercises
|
||||||
|
|
||||||
|
### Exercise 1 — Even / Odd Number Checker
|
||||||
|
|
||||||
|
**Task:**
|
||||||
|
|
||||||
|
* Prompt the user for a number
|
||||||
|
* Output `Even` or `Odd`
|
||||||
|
|
||||||
|
**Concepts practiced:**
|
||||||
|
|
||||||
|
* `Read-Host`
|
||||||
|
* Integer conversion
|
||||||
|
* Conditional branching (`if / else`)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Exercise 2 — Count Letters in a String
|
||||||
|
|
||||||
|
**Task:**
|
||||||
|
|
||||||
|
* Prompt the user for a string
|
||||||
|
* Count only letters (`A-Z`, `a-z`)
|
||||||
|
* Output the number of letters
|
||||||
|
|
||||||
|
**Concepts practiced:**
|
||||||
|
|
||||||
|
* `foreach` loop over string characters
|
||||||
|
* Regex matching with `-match`
|
||||||
|
* Counter variable logic
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Exercise 3 — Username Validator
|
||||||
|
|
||||||
|
**Task:**
|
||||||
|
|
||||||
|
* Prompt the user for a username
|
||||||
|
* Validate using these rules:
|
||||||
|
|
||||||
|
* Length ≤ 20
|
||||||
|
* Starts with a letter
|
||||||
|
* No digits
|
||||||
|
* No spaces
|
||||||
|
* Output `Accepted` or `Refused`
|
||||||
|
|
||||||
|
**Concepts practiced:**
|
||||||
|
|
||||||
|
* Conditional logic
|
||||||
|
* Early exit using `break`
|
||||||
|
* Character validation via regex
|
||||||
|
* Defensive programming
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Exercise 4 — Validate Multiple Usernames
|
||||||
|
|
||||||
|
**Task:**
|
||||||
|
|
||||||
|
* Given an array of usernames:
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
$users = @("admin", "root1", "John_Doe", "Alice", "Bob42")
|
||||||
|
```
|
||||||
|
|
||||||
|
* Print only valid usernames (using the same rules as Exercise 3)
|
||||||
|
|
||||||
|
**Concepts practiced:**
|
||||||
|
|
||||||
|
* Iterating over collections with `foreach`
|
||||||
|
* Reusing validation logic
|
||||||
|
* Output formatting
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Exercise 5 — Algorithm Explanation (Written)
|
||||||
|
|
||||||
|
**Task:**
|
||||||
|
|
||||||
|
* Explain what happens if the number of users doubles
|
||||||
|
* Write the runtime formula using:
|
||||||
|
|
||||||
|
* `n` = number of users
|
||||||
|
* `m` = average username length
|
||||||
|
|
||||||
|
**Expected reasoning:**
|
||||||
|
|
||||||
|
* Doubling the list doubles the execution time
|
||||||
|
* Linear runtime: `T(n, m) = n × m`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🔐 Security Perspective
|
||||||
|
|
||||||
|
PowerShell scripts are heavily used in **Windows administration and security tasks**. Today’s exercises reinforce:
|
||||||
|
|
||||||
|
* Input validation to prevent bad data
|
||||||
|
* Predictable scaling of loops
|
||||||
|
* Defensive coding practices
|
||||||
|
|
||||||
|
These habits are essential for:
|
||||||
|
|
||||||
|
* Active Directory scripting
|
||||||
|
* Log parsing and auditing
|
||||||
|
* Automating security tasks
|
||||||
|
* Incident response and analysis
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## ✅ Completion Criteria
|
||||||
|
|
||||||
|
Day 4 is complete when:
|
||||||
|
|
||||||
|
* Exercises 1–4 run correctly and produce expected outputs
|
||||||
|
* Exercise 5 demonstrates correct understanding of scaling and runtime
|
||||||
|
* Scripts are secure, readable, and reusable
|
||||||
5
week 1/day 4/test.ps1
Normal file
5
week 1/day 4/test.ps1
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# a little script to test if powershell work with my machine
|
||||||
|
|
||||||
|
Write-Host("hello from powershell");
|
||||||
|
|
||||||
|
# ! it work :)
|
||||||
Reference in New Issue
Block a user