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: |

Unlimited members in Telegram group now

Telegram has made it easier for its users to communicate, as it has introduced a feature that allows more than 200,000 users in a group chat. However, if the users in a group chat move past 200,000, it changes into "Broadcast Group", but the feature comes with a restriction. Groups with close to 200k members can be converted to a Broadcast Group that allows unlimited members. Only admins can post in Broadcast Groups, but everyone can read along and participate in group Voice Chats," Telegram added.

What Is Bitcoin?

Bitcoin is a decentralized digital currency that you can buy, sell and exchange directly, without an intermediary like a bank. Bitcoin’s creator, Satoshi Nakamoto, originally described the need for “an electronic payment system based on cryptographic proof instead of trust.” Each and every Bitcoin transaction that’s ever been made exists on a public ledger accessible to everyone, making transactions hard to reverse and difficult to fake. That’s by design: Core to their decentralized nature, Bitcoins aren’t backed by the government or any issuing institution, and there’s nothing to guarantee their value besides the proof baked in the heart of the system. “The reason why it’s worth money is simply because we, as people, decided it has value—same as gold,” says Anton Mozgovoy, co-founder & CEO of digital financial service company Holyheld.

Rust from vn


Telegram Rust
FROM USA