Telegram Group & Telegram Channel
🦀 Задача на Rust: "Найди пропавшее число"

Условие
У тебя есть Vec<String>, в котором находятся строки от "1" до "100", но одно число отсутствует.
Найди его без использования sort, HashMap, iter().sum() и т. д.
Можно использовать parse::<u32>().

Формат функции:


fn find_missing_number(data: &Vec<String>) -> u32


Решение через XOR:


fn find_missing_number(data: &Vec<String>) -> u32 {
let mut xor_full = 0;
let mut xor_data = 0;

for i in 1..=100 {
xor_full ^= i;
}

for s in data {
if let Ok(n) = s.parse::<u32>() {
xor_data ^= n;
}
}

xor_full ^ xor_data
}


Пример использования:


fn main() {
let mut data: Vec<String> = (1..=100)
.filter(|&x| x != 42)
.map(|x| x.to_string())
.collect();

use rand::seq::SliceRandom;
let mut rng = rand::thread_rng();
data.shuffle(&mut rng);

let missing = find_missing_number(&data);
println!("Пропущено: {}", missing); // Ожидается 42
}


Зависимости в `Cargo.toml`:


[dependencies]
rand = "0.8"


Объяснение:
XOR всех чисел от 1 до 100 ⊕ XOR из входных данных → пропущенное число.
Работает, потому что a ^ a = 0, 0 ^ b = b.



tg-me.com/rust_code/956
Create:
Last Update:

🦀 Задача на Rust: "Найди пропавшее число"

Условие
У тебя есть Vec<String>, в котором находятся строки от "1" до "100", но одно число отсутствует.
Найди его без использования sort, HashMap, iter().sum() и т. д.
Можно использовать parse::<u32>().

Формат функции:


fn find_missing_number(data: &Vec<String>) -> u32


Решение через XOR:


fn find_missing_number(data: &Vec<String>) -> u32 {
let mut xor_full = 0;
let mut xor_data = 0;

for i in 1..=100 {
xor_full ^= i;
}

for s in data {
if let Ok(n) = s.parse::<u32>() {
xor_data ^= n;
}
}

xor_full ^ xor_data
}


Пример использования:


fn main() {
let mut data: Vec<String> = (1..=100)
.filter(|&x| x != 42)
.map(|x| x.to_string())
.collect();

use rand::seq::SliceRandom;
let mut rng = rand::thread_rng();
data.shuffle(&mut rng);

let missing = find_missing_number(&data);
println!("Пропущено: {}", missing); // Ожидается 42
}


Зависимости в `Cargo.toml`:


[dependencies]
rand = "0.8"


Объяснение:
XOR всех чисел от 1 до 100 ⊕ XOR из входных данных → пропущенное число.
Работает, потому что a ^ a = 0, 0 ^ b = b.

BY Rust


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/rust_code/956

View MORE
Open in Telegram


Rust Telegram | DID YOU KNOW?

Date: |

What is Secret Chats of Telegram

Secret Chats are one of the service’s additional security features; it allows messages to be sent with client-to-client encryption. This setup means that, unlike regular messages, these secret messages can only be accessed from the device’s that initiated and accepted the chat. Additionally, Telegram notes that secret chats leave no trace on the company’s services and offer a self-destruct timer.

Telegram Auto-Delete Messages in Any Chat

Some messages aren’t supposed to last forever. There are some Telegram groups and conversations where it’s best if messages are automatically deleted in a day or a week. Here’s how to auto-delete messages in any Telegram chat. You can enable the auto-delete feature on a per-chat basis. It works for both one-on-one conversations and group chats. Previously, you needed to use the Secret Chat feature to automatically delete messages after a set time. At the time of writing, you can choose to automatically delete messages after a day or a week. Telegram starts the timer once they are sent, not after they are read. This won’t affect the messages that were sent before enabling the feature.

Rust from es


Telegram Rust
FROM USA