2020-12-09 00:00:12 +03:00
|
|
|
// Swap two elements of an array.
|
2021-03-19 18:30:24 +03:00
|
|
|
function swap(a: [u32; 2], const i: u32, const j: u32) -> [u32; 2] {
|
2021-03-18 22:19:07 +03:00
|
|
|
const t = a[i];
|
2020-12-09 00:00:12 +03:00
|
|
|
a[i] = a[j];
|
|
|
|
a[j] = t;
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
function main() {
|
2021-03-18 22:19:07 +03:00
|
|
|
let arr: [u32; 2] = [0, 1];
|
|
|
|
const expected: [u32; 2] = [1, 0];
|
2020-12-09 00:00:12 +03:00
|
|
|
|
|
|
|
// Do swap.
|
2021-03-18 22:19:07 +03:00
|
|
|
const actual = swap(arr, 0, 1);
|
2020-12-09 00:00:12 +03:00
|
|
|
|
|
|
|
// Check result.
|
|
|
|
for i in 0..2 {
|
|
|
|
console.assert(expected[i] == actual[i]);
|
|
|
|
}
|
|
|
|
}
|