Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
48
Task/Matrix-transposition/Rust/matrix-transposition-1.rust
Normal file
48
Task/Matrix-transposition/Rust/matrix-transposition-1.rust
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
struct Matrix {
|
||||
dat: [[i32; 3]; 3]
|
||||
}
|
||||
|
||||
|
||||
|
||||
impl Matrix {
|
||||
pub fn transpose_m(a: Matrix) -> Matrix
|
||||
{
|
||||
let mut out = Matrix {
|
||||
dat: [[0, 0, 0],
|
||||
[0, 0, 0],
|
||||
[0, 0, 0]
|
||||
]
|
||||
};
|
||||
|
||||
for i in 0..3{
|
||||
for j in 0..3{
|
||||
|
||||
out.dat[i][j] = a.dat[j][i];
|
||||
}
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
pub fn print(self)
|
||||
{
|
||||
for i in 0..3 {
|
||||
for j in 0..3 {
|
||||
print!("{} ", self.dat[i][j]);
|
||||
}
|
||||
print!("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main()
|
||||
{
|
||||
let a = Matrix {
|
||||
dat: [[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
[7, 8, 9] ]
|
||||
};
|
||||
|
||||
let c = Matrix::transpose_m(a);
|
||||
c.print();
|
||||
}
|
||||
25
Task/Matrix-transposition/Rust/matrix-transposition-2.rust
Normal file
25
Task/Matrix-transposition/Rust/matrix-transposition-2.rust
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
fn main() {
|
||||
let m = vec![vec![1, 2, 3], vec![4, 5, 6]];
|
||||
println!("Matrix:\n{}", matrix_to_string(&m));
|
||||
let t = matrix_transpose(m);
|
||||
println!("Transpose:\n{}", matrix_to_string(&t));
|
||||
}
|
||||
|
||||
fn matrix_to_string(m: &Vec<Vec<i32>>) -> String {
|
||||
m.iter().fold("".to_string(), |a, r| {
|
||||
a + &r
|
||||
.iter()
|
||||
.fold("".to_string(), |b, e| b + "\t" + &e.to_string())
|
||||
+ "\n"
|
||||
})
|
||||
}
|
||||
|
||||
fn matrix_transpose(m: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
|
||||
let mut t = vec![Vec::with_capacity(m.len()); m[0].len()];
|
||||
for r in m {
|
||||
for i in 0..r.len() {
|
||||
t[i].push(r[i]);
|
||||
}
|
||||
}
|
||||
t
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue