mirror of
https://github.com/LukasKalbertodt/programmieren-in-rust.git
synced 2024-11-18 02:48:58 +01:00
Add solution for sheet2
This commit is contained in:
parent
671ec22f10
commit
95de881390
62
aufgaben/sheet2/sol1/prime.rs
Executable file
62
aufgaben/sheet2/sol1/prime.rs
Executable file
@ -0,0 +1,62 @@
|
||||
//! Aufgabe 1.2: Primzahltest
|
||||
|
||||
fn main() {
|
||||
for i in 1..20 {
|
||||
print!("{}", i);
|
||||
if is_prime(i) {
|
||||
print!("*");
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
}
|
||||
|
||||
/// Tests if the given number `n` is prime.
|
||||
fn is_prime(n: u64) -> bool {
|
||||
// Note: there are nicer *and* faster ways to do a primality test. But that
|
||||
// was not necessarily the point of this task.
|
||||
|
||||
// 1 is special and yes: 1 is not a prime!
|
||||
if n <= 1 {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Iterator through all possible divisors
|
||||
for d in 2..n {
|
||||
if n % d == 0 {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn small_primes() {
|
||||
assert!(is_prime(2));
|
||||
assert!(is_prime(3));
|
||||
assert!(is_prime(5));
|
||||
assert!(is_prime(7));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn small_composites() {
|
||||
assert!(!is_prime(1));
|
||||
assert!(!is_prime(4));
|
||||
assert!(!is_prime(6));
|
||||
assert!(!is_prime(8));
|
||||
assert!(!is_prime(9));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn large_primes() {
|
||||
assert!(is_prime(1_300_769));
|
||||
assert!(is_prime(1_300_297));
|
||||
assert!(is_prime(7_367_287));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn large_composites() {
|
||||
assert!(!is_prime(908_209));
|
||||
assert!(!is_prime(3_073_009));
|
||||
assert!(!is_prime(4_897_369));
|
||||
}
|
42
aufgaben/sheet2/sol2/sort.rs
Executable file
42
aufgaben/sheet2/sol2/sort.rs
Executable file
@ -0,0 +1,42 @@
|
||||
fn main() {
|
||||
let mut arr = [61, 21, 27, 79, 57, 60, 46, 42, 27, 92, 66, 26];
|
||||
|
||||
sort(&mut arr);
|
||||
println!("{:?}", arr);
|
||||
}
|
||||
|
||||
fn sort(arr: &mut [u64]) {
|
||||
for start in 0..arr.len() {
|
||||
let mut min_idx = start;
|
||||
|
||||
for i in start..arr.len() {
|
||||
if arr[i] < arr[min_idx] {
|
||||
min_idx = i;
|
||||
}
|
||||
}
|
||||
|
||||
// This is not a nice way to swap those elements. We should use
|
||||
// `std::mem::swap()` or even better: `[T]::swap()`.
|
||||
let tmp = arr[min_idx];
|
||||
arr[min_idx] = arr[start];
|
||||
arr[start] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn sort_array() {
|
||||
let mut arr = [
|
||||
61, 21, 27, 79, 57, 60, 46, 92, 66, 26, 37, 15, 29, 70, 30, 55, 62, 81,
|
||||
84, 35, 34, 52, 98, 50, 39, 42, 41, 24, 28, 64, 95, 47, 43, 23, 14, 71,
|
||||
78, 86, 51, 20, 9, 1, 18, 17, 94, 33, 3, 91, 65, 2, 38, 59, 96, 8, 83,
|
||||
19, 90, 63, 16, 58, 68, 48
|
||||
];
|
||||
sort(&mut arr);
|
||||
assert_eq!(&arr as &[u64], &[
|
||||
1u64, 2, 3, 8, 9, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 26, 27, 28, 29,
|
||||
30, 33, 34, 35, 37, 38, 39, 41, 42, 43, 46, 47, 48, 50, 51, 52, 55, 57,
|
||||
58, 59, 60, 61, 62, 63, 64, 65, 66, 68, 70, 71, 78, 79, 81, 83, 84, 86,
|
||||
90, 91, 92, 94, 95, 96, 98,
|
||||
] as &[u64]);
|
||||
}
|
19
aufgaben/sheet2/sol3/count.rs
Executable file
19
aufgaben/sheet2/sol3/count.rs
Executable file
@ -0,0 +1,19 @@
|
||||
//! Aufgabe 2.3
|
||||
|
||||
fn main() {
|
||||
let s = "The fat cat had a friend! ♥";
|
||||
let count = count(s, 'a');
|
||||
println!("{}", count);
|
||||
}
|
||||
|
||||
|
||||
fn count(haystack: &str, needle: char) -> u64 {
|
||||
let mut count = 0;
|
||||
for c in haystack.chars() {
|
||||
if c == needle {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
count
|
||||
}
|
Loading…
Reference in New Issue
Block a user