Iguales al siguiente en Python

This commit is contained in:
daviddoji committed 2026-05-08 16:59:29 +02:00
1 parent 5af87fb76f
commit 1069987fb4
3 files changed
+35

No files matched your search

+16
View 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)) # []
Executable
BIN
View File
Binary file not shown.
+19
View 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)); // []
}