-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
391 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
## Лекція 13. Контрольне опитування | ||
|
||
### Чи .... | ||
|
||
- так | ||
- ні | ||
- не знаю | ||
- залежить від ситуації | ||
- що таке ... | ||
|
||
### .... | ||
|
||
- так | ||
- ні | ||
- не знаю | ||
- залежить від ситуації | ||
- що таке ... |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/// if/else | ||
/// match | ||
/// for/while/loop | ||
/// continue/break | ||
fn basic_flow() { | ||
fn f(a: u32) -> f32 { | ||
todo!() | ||
} | ||
fn g(a: f32) -> bool { | ||
todo!() | ||
} | ||
fn h(a: bool) -> String { | ||
todo!() | ||
} | ||
|
||
fn app(a: u32) -> String { | ||
let b = f(a); | ||
let c = g(b); | ||
let d = h(c); | ||
d | ||
} | ||
|
||
let n: i32 = 5; | ||
|
||
if n < 0 { | ||
println!("{} is negative", n); | ||
} else { | ||
println!("{} is non-negative", n); | ||
} | ||
} | ||
|
||
#[test] | ||
fn if_basic_syntax() { | ||
let n: i32 = 5; | ||
|
||
if n < 0 { | ||
println!("{} is negative", n); | ||
} else { | ||
println!("{} is non-negative", n); | ||
} | ||
} | ||
|
||
#[test] | ||
fn if_stacked_syntax() { | ||
let n: i32 = 5; | ||
|
||
if n < 0 { | ||
println!("{} is negative", n); | ||
} else if n > 0 { | ||
println!("{} is positive", n); | ||
} else { | ||
println!("{} is zero", n); | ||
} | ||
} | ||
|
||
#[test] | ||
fn if_returns_syntax() { | ||
let n: i32 = 5; | ||
|
||
let s = if n < 0 { | ||
"negative" | ||
} else if n > 0 { | ||
"positive" | ||
} else { | ||
"zero" | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
### not, ! | ||
|
||
| a | !a | | ||
|-------|:-----:| | ||
| true | false | | ||
| false | true | | ||
|
||
### or, a || b | ||
|
||
| a | b | a\|\|b | | ||
|-------|-------|:------:| | ||
| false | false | false | | ||
| false | true | true | | ||
| true | false | true | | ||
| true | true | true | | ||
|
||
### and, a && b | ||
|
||
| a | b | a&&b | | ||
|-------|-------|:-----:| | ||
| false | false | false | | ||
| false | true | false | | ||
| true | false | false | | ||
| true | true | true | | ||
|
||
### xor, a ^ b | ||
|
||
| a | b | a^b | | ||
|-------|-------|:-----:| | ||
| false | false | false | | ||
| false | true | true | | ||
| true | false | true | | ||
| true | true | false | | ||
|
||
- (a || b) && c == a && c || b && c | ||
- a || !a = true | ||
- a && !a = false | ||
- a || true = true | ||
- a && true = a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
fn not(x: bool) -> bool { | ||
!x | ||
} | ||
|
||
fn and(x: bool, y: bool) -> bool { | ||
x && y | ||
} | ||
|
||
fn or(x: bool, y: bool) -> bool { | ||
x || y | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/// if/else | ||
/// match | ||
/// for/while/loop | ||
/// continue/break | ||
// 4h24min | ||
#[test] | ||
fn for_basic_not_including_syntax() { | ||
for n in 0..5 { | ||
println!("{}", n); | ||
} | ||
} | ||
|
||
#[test] | ||
fn for_basic_including_syntax() { | ||
for n in 0..=5 { | ||
println!("{}", n); | ||
} | ||
} | ||
|
||
fn for_infinite() { | ||
for n in 0.. { | ||
println!("{}", n); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#[test] | ||
fn while_basic_syntax() { | ||
let mut n = 1; | ||
while n < 20 { | ||
if n % 15 == 0 { | ||
println!("{}: fizzbuzz", n); | ||
} else if n % 3 == 0 { | ||
println!("{}: fizz", n); | ||
} else if n % 5 == 0 { | ||
println!("{}: buzz", n); | ||
} else { | ||
println!("{}", n); | ||
} | ||
n += 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/// infinite | ||
#[ignore] | ||
#[test] | ||
fn loop_syntax() { | ||
let mut n = 0u32; | ||
loop { | ||
n += 1; | ||
println!("{}", n); | ||
} | ||
} | ||
|
||
#[test] | ||
fn loop_syntax_with_break() { | ||
let mut n = 0u32; | ||
loop { | ||
n += 1; | ||
println!("{}", n); | ||
if n == 10 { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
#[ignore] | ||
#[test] | ||
fn loop_syntax_with_break_and_continue() { | ||
let mut n = 0u32; | ||
loop { | ||
// ... | ||
// ... | ||
n += 1; | ||
println!("{}", n); | ||
if n % 2 == 0 { | ||
continue; | ||
} | ||
// ... | ||
// ... | ||
if n == 100 { | ||
break; | ||
} | ||
// ... | ||
// ... | ||
} | ||
} | ||
|
||
#[test] | ||
fn loop_syntax_with_labels() { | ||
let mut n = 0u32; | ||
|
||
'a: loop { | ||
n += 1; | ||
|
||
println!("b"); | ||
|
||
if n == 100 { | ||
break; | ||
}; | ||
continue 'a; | ||
} | ||
} | ||
|
||
#[test] | ||
fn loop_old_usage() { | ||
let mut total = 1; | ||
|
||
loop { | ||
total *= 2; | ||
if total > 1000 { | ||
break; | ||
} | ||
} | ||
assert_eq!(1024, total); | ||
} | ||
|
||
#[test] | ||
fn loop_returns() { | ||
let mut total = 1; | ||
|
||
let outcome = loop { | ||
total *= 2; | ||
if total > 1000 { | ||
break total; | ||
} | ||
}; | ||
assert_eq!(1024, outcome); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#[test] | ||
fn break_for() { | ||
for n in 0.. { | ||
println!("{}", n); | ||
if n == 999 { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn break_while() { | ||
let mut n: i32 = 0; | ||
while true { | ||
n += 1; | ||
if n == 999 { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn break_loop() { | ||
let mut n: i32 = 0; | ||
loop { | ||
n += 1; | ||
if n == 999 { | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#[test] | ||
fn continue_syntax() { | ||
let mut n: i32 = 0; | ||
for i in 0..100 { | ||
n = n + 1; // 100 times | ||
if i % 2 == 0 { | ||
continue; | ||
} | ||
n = n + 1; // 50 times | ||
} | ||
assert_eq!(150, n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#[test] | ||
fn nested_loops() { | ||
let mut x = 0; | ||
let mut y = 0; | ||
loop { | ||
loop { | ||
x += 1; | ||
y += 1; | ||
print!("{} ", x); | ||
if x == 5 { | ||
break; | ||
} | ||
} | ||
loop { | ||
x -= 1; | ||
y += 1; | ||
print!("{} ", x); | ||
if x == 0 { | ||
break; | ||
} | ||
} | ||
if y >= 20 { | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#[test] | ||
fn pattern_match() { | ||
enum Solution { | ||
NoRoots, | ||
OneRoot(f32), | ||
TwoRoots(f32, f32), | ||
} | ||
|
||
fn solve_quadratic(a: f32, b: f32, c: f32) -> Solution { | ||
use Solution::*; | ||
|
||
let d = b.powi(2) - 4.0 * a * c; | ||
|
||
match d { | ||
d if d > 0. => { | ||
let dq = f32::sqrt(d); | ||
let a2 = a * 2.; | ||
TwoRoots((-b - dq) / a2, (-b + dq) / a2) | ||
} | ||
0. => OneRoot(-b / (2. * a)), | ||
_ => NoRoots, | ||
} | ||
} | ||
|
||
fn solve_and_report(a: f32, b: f32, c: f32) { | ||
use Solution::*; | ||
|
||
match solve_quadratic(a, b, c) { | ||
NoRoots => println!("quadratic equation has no roots"), | ||
OneRoot(x) => println!("quadratic equation has one root: {x}"), | ||
TwoRoots(x1, x2) => println!("quadratic equation has two roots: {x1}, {x2}"), | ||
} | ||
} | ||
|
||
solve_and_report(1., 2., 2.); // no roots | ||
solve_and_report(1., 2., 1.); // one root | ||
solve_and_report(1., 1., 0.); // two roots | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
|
||
mod a1if; | ||
mod a2boolean_algebra; | ||
mod a3for; | ||
mod a4while; | ||
mod a5loop; | ||
mod a6break; | ||
mod a7continue; | ||
mod a8nested; | ||
mod a9control; |
Oops, something went wrong.