Add old works for Algorithms and Data Structeres
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/target
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "exam"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "exam"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
@@ -0,0 +1,7 @@
|
||||
# Exam
|
||||
|
||||
> Algorithm for replacing elements of a one-dimensional list. Implement in pseudocode or your favorite programming language.
|
||||
|
||||
Cooked this bad boy during the exam.
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
fn replace<T: Clone + PartialEq>(list: &mut [T], find: &T, with: &T, all: bool) {
|
||||
for element in list.iter_mut() {
|
||||
if element == find {
|
||||
*element = with.clone();
|
||||
if !all {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut list: Vec<_> = (0..10).map(|x| x % 7).collect();
|
||||
println!("{list:?}");
|
||||
|
||||
let find = 2;
|
||||
let with = 42;
|
||||
|
||||
replace(&mut list, &find, &with, false);
|
||||
println!("{list:?}");
|
||||
|
||||
replace(&mut list, &find, &with, true);
|
||||
println!("{list:?}");
|
||||
|
||||
let mut list = [1, 2, 3, 4, 1, 3, 1, 0, 4];
|
||||
replace(&mut list, &1, &9, true);
|
||||
println!("{list:?}");
|
||||
}
|
||||
Reference in New Issue
Block a user