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.
👍22



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

At a time when the Indian stock market is peaking and has rallied immensely compared to global markets, there are companies that have not performed in the last 10 years. These are definitely a minor portion of the market considering there are hundreds of stocks that have turned multibagger since 2020. What went wrong with these stocks? Reasons vary from corporate governance, sectoral weakness, company specific and so on. But the more important question is, are these stocks worth buying?

How To Find Channels On Telegram?

There are multiple ways you can search for Telegram channels. One of the methods is really logical and you should all know it by now. We’re talking about using Telegram’s native search option. Make sure to download Telegram from the official website or update it to the latest version, using this link. Once you’ve installed Telegram, you can simply open the app and use the search bar. Tap on the magnifier icon and search for a channel that might interest you (e.g. Marvel comics). Even though this is the easiest method for searching Telegram channels, it isn’t the best one. This method is limited because it shows you only a couple of results per search.

Rust from id


Telegram Rust
FROM USA