Iguales al siguiente en Python
This commit is contained in:
16
src/Python/001.py
Normal file
16
src/Python/001.py
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
# def same_to_next(lst: list) -> list:
|
||||||
|
# same_to_next_lst = []
|
||||||
|
# for idx, element in enumerate(lst[:-1]):
|
||||||
|
# if lst[idx] == lst[idx+1]:
|
||||||
|
# same_to_next_lst.append(element)
|
||||||
|
# return same_to_next_lst
|
||||||
|
|
||||||
|
def same_to_next(lst: list) -> list:
|
||||||
|
return [x for x, y in zip(lst, lst[1:]) if x == y]
|
||||||
|
|
||||||
|
|
||||||
|
check = [1, 2, 2, 2, 3, 3, 4]
|
||||||
|
print(same_to_next(check)) # [2, 2, 3]
|
||||||
|
|
||||||
|
check = list(range(1, 11))
|
||||||
|
print(same_to_next(check)) # []
|
||||||
BIN
src/Rust/001
Executable file
BIN
src/Rust/001
Executable file
Binary file not shown.
19
src/Rust/001.rs
Normal file
19
src/Rust/001.rs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
fn same_to_next(lst: &[i32]) -> Vec<i32> {
|
||||||
|
let mut _lst: Vec<i32> = Vec::new();
|
||||||
|
|
||||||
|
for idx in 0..(lst.len() - 1) {
|
||||||
|
if lst[idx] == lst[idx + 1] {
|
||||||
|
_lst.push(lst[idx]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return _lst
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let check = vec![1, 2, 2, 2, 3, 3, 4];
|
||||||
|
println!("{:?}", same_to_next(&check)); // [2, 2, 3]
|
||||||
|
|
||||||
|
let check: Vec<i32> = (1..=10).collect();
|
||||||
|
println!("{:?}", same_to_next(&check)); // []
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user