๊ด€๋ฆฌ ๋ฉ”๋‰ด

IT’s Portfolio

[Rust] Start Rust (Day 15) - Rust Example Script 3 [Get Avg & Mid & Mode] ๋ณธ๋ฌธ

Development Study/Rust

[Rust] Start Rust (Day 15) - Rust Example Script 3 [Get Avg & Mid & Mode]

f1r3_r41n 2023. 2. 25. 12:41
728x90
๋ฐ˜์‘ํ˜•

๐Ÿฆ€ Rust Day 15

๐Ÿณ๏ธ Rust Example Script 3 - Get Avg & Mid & Mode

1๏ธโƒฃ Description

  • ์ •์ˆ˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ฒกํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‰๊ท ๊ฐ’, ์ค‘๊ฐ„๊ฐ’, ์ตœ๋นˆ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ

2๏ธโƒฃ how it works

  • i32 ํƒ€์ž…์˜ ์ •์ˆ˜ ๋ฐฐ์—ด์ด ์ฃผ์–ด์ง
  • ํ•ด๋‹น ๋ฐฐ์—ด์˜ ํ‰๊ท ๊ฐ’, ์ค‘๊ฐ„๊ฐ’, ์ตœ๋นˆ๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅ

3๏ธโƒฃ Code

// src/main.rs
mod changer;
use crate::changer::operations;

fn main() {
    let nums: Vec<i32> = operations::make_integers();
    let mode = operations::get_mode(&nums);

    println!(
        "ํ‰๊ท ๊ฐ’ : {}",
        match operations::get_avg(&nums) {
            Some(avg) => avg.to_string(),
            None => "๊ฐ’์ด ์—†์Œ".to_string(),
        }
    );

    println!(
        "์ค‘๊ฐ„๊ฐ’ : {}",
        match operations::get_mid(&nums) {
            Some(mid) => mid.to_string(),
            None => "๊ฐ’์ด ์—†์Œ".to_string(),
        }
    );

    println!(
        "์ตœ๋นˆ๊ฐ’ : {}", {
            if mode.len() == 0 {
                "๊ฐ’์ด ์—†์Œ".to_string()
            } else {
                let mut s = String::new();
                for i in mode {
                    s.push_str(&i.to_string());
                    s.push(' ');
                }
                s
            }
        }
    );
}
// src/changer.rs
pub mod operations {
    pub use std::collections::HashMap;

    // vec! ๋งคํฌ๋กœ๋ฅผ ์“ฐ๊ณ  ์‹ถ์ง€ ์•Š์€ ๋งˆ์Œ์— ์ž‘์„ฑํ•œ ํ•จ์ˆ˜
    pub fn make_integers() -> Vec<i32> {
        // ๋ฐฐ์šด ๊ฒƒ์€ ์จ๋จน์œผ๋ผ ์žˆ๋Š” ๊ฒƒ.
        let mut n: Vec<i32> = Vec::new();
        let mut nums: [i32; 17] = [20, 17, 114, 24, 14, 170, 141, 67, 20, 4, 78, 404, 19, 67, 221, 67, 20];

        // ์—ญ์ฐธ์กฐ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ nums์˜ ์š”์†Œ๋“ค์— 1์„ ๋”ํ•œ ํ•œ ๊ฐ’์„ ๋ฒกํ„ฐ์— push
        for i in &mut nums {
            *i += 1;
            n.push(*i);
        }

        // ๋ฒกํ„ฐ ์ •๋ ฌ
        n.sort();

        n
    }

    // ์ค‘๊ฐ„๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
    pub fn get_mid(nums: &Vec<i32>) -> Option<i32> {
        if nums.len() == 0 { return None; }

        Some(nums[nums.len()/2])
    }

    // ์ตœ๋นˆ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
    pub fn get_mode(nums: &Vec<i32>) -> Vec<i32> {
        if nums.len() == 0 { return Vec::new(); }
        // nums์˜ ์š”์†Œ๋“ค์˜ ๋นˆ๋„์ˆ˜๋ฅผ ๋งค์น˜ํ•˜๋Š” HashMap ์ธ์Šคํ„ด์Šค
        let mut map = HashMap::new();
        // ์ตœ๋นˆ๊ฐ’์„ ๊ณจ๋ผ๋‚ด ์ €์žฅํ•  ์šฉ๋„์ธ HashMap ์ธ์Šคํ„ด์Šค
        let mut result: HashMap<i32, i32> = HashMap::new();
        // for ๋ฃจํ”„์—์„œ ํ˜„์žฌ ํ‚ค์— ์—ฐ๊ฒฐ๋œ ๊ฐ’๊ณผ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด ํ‚ค ๊ฐ’์„ ์ €์žฅํ•  ์ž„์‹œ์ ์ธ ๋ณ€์ˆ˜
        let mut tmp = 0;
        // tmp์˜ ๊ฐ’๊ณผ ๋น„๊ต ํ›„ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ ํ‚ค์˜ ๊ฐ’์„ ์ €์žฅํ•  ์ž„์‹œ์ ์ธ ๋ณ€์ˆ˜
        let mut tmp2 = 0;

        // nums ์š”์†Œ๋“ค์— ๋Œ€ํ•œ ๋นˆ๋„์ˆ˜๋ฅผ HashMap ์ธ์Šคํ„ด์Šค์— ์ €์žฅ
        for i in nums {
            // entry() => ๊ฐ’์˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” Entry ์—ด๊ฑฐ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•จ
            // or_insert() => Entry ์—ด๊ฑฐ์ž์— ์„ ์–ธ๋œ ๋ฉ”์„œ๋“œ
                // ํ‚ค ์กด์žฌ ์‹œ ํ•ด๋‹น ํ‚ค์— ์—ฐ๊ฒฐ๋œ ๊ฐ’์— ๋Œ€ํ•œ ๊ฐ€๋ณ€ ์ฐธ์กฐ๋กœ ๋ฐ˜ํ™˜ํ•จ
                // ํ‚ค ์กด์žฌํ•˜์ง€ ์•Š์„ ์‹œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•œ ํ‚ค์— ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ถ”๊ฐ€ํ•œ ํ›„ ํ•ด๋‹น ๊ฐ’์— ๋Œ€ํ•œ ๊ฐ€๋ณ€ ์ฐธ์กฐ๋ฅผ ๋ฐ˜ํ™˜ํ•จ
            let count = map.entry(i).or_insert(0);
            *count += 1;
        }

        // map์˜ ์š”์†Œ๋“ค์„ ํ•œ ๋ฒˆ์”ฉ ๋Œ๋ฉฐ ๋น„๊ตํ•˜๋Š” for ๋ฃจํ”„
        for (key, value) in map {
            // ํ•ด๋‹น ์ปฌ๋ ‰์…˜์ด ๋น„์–ด์žˆ๋Š”์ง€๋ฅผ bool ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” is_empty ๋ฉ”์„œ๋“œ
            // ๋•Œ๋ฌธ์— match ํ‘œํ˜„์‹ ์‚ฌ์šฉ
            match result.is_empty() {
                // result๊ฐ€ ๋น„์–ด์žˆ์„ ๊ฒฝ์šฐ ํ˜„์žฌ ํ‚ค์™€ ๊ฐ’์„ insert ํ›„ ํฌ์ธํ„ฐ ์—ญํ• ์„ ํ•˜๋Š” tmp์— ํ‚ค๋ฅผ ์ €์žฅ
                true => {
                    result.insert(*key, value);
                    tmp = *key;
                },
                // result๊ฐ€ ๋น„์–ด์žˆ์ง€ ์•Š์„ ๊ฒฝ์šฐ tmp์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ํ‚ค๋ฅผ ์ด์šฉํ•ด ๊ฐ’ ํ˜ธ์ถœ ๋ฐ ๋น„๊ต
                false => {
                    // HashMap์˜ get ๋ฉ”์„œ๋“œ๋Š” Option ์—ด๊ฑฐ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•จ
                    // ๋•Œ๋ฌธ์— match ํ‘œํ˜„์‹ ์‚ฌ์šฉ
                    match result.get(&tmp) {
                        // tmp์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ํ‚ค๋กœ ๊ฐ’์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค๋ฉด tmp2์—๋Š” tmp์— ์ƒ์‘ํ•˜๋Š” ๊ฐ’์„ ์ €์žฅ
                        Some(n) => { tmp2 = *n; },
                        None => { },
                    }

                    // ๋งŒ์•ฝ ํ˜„์žฌ value๊ฐ€ tmp2์™€ ๊ฐ™๋‹ค๋ฉด result์— ์ถ”๊ฐ€ํ•œ ํ›„ ํฌ์ธํ„ฐ๋ฅผ ๋‹ค์Œ์œผ๋กœ ํ‚ค๋กœ ์˜ฎ๊ฒจ์คŒ
                    // value๊ฐ€ tmp2๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ์ตœ๋นˆ๊ฐ’์ด ๋  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์•„๋ฌด ์ž‘์—…๋„ ํ•˜์ง€ ์•Š๊ณ  ๋„˜์–ด๊ฐ
                    // value๊ฐ€ tmp2๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๊ทธ ์ „๊นŒ์ง€ ์ €์žฅํ–ˆ๋˜ ๋‹ค๋ฅธ ๊ฐ’๋“ค๋„ ํ˜„์žฌ value๋ณด๋‹ค ํฌ์ง€ ์•Š์„ ๊ฒƒ์ด๋ฏ€๋กœ
                    // HashMap์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” clear ๋ฉ”์„œ๋“œ๋ฅผ ์ˆ˜ํ–‰ ํ›„ ํ•ด๋‹น ํ‚ค์™€ ๊ฐ’์„ ์ถ”๊ฐ€ํ•จ
                    // tmp2๋Š” ์ œ์ผ ํฐ ๊ฐ’๋งŒ ์ €์žฅํ•˜๋ฏ€๋กœ ํ•ด๋‹น ๊ฐ’์œผ๋กœ ์ˆ˜์ •ํ•จ
                    if value == tmp2 {
                        result.insert(*key, value);
                        tmp = *key;
                    } else if value < tmp2 {
                        continue;
                    } else {
                        result.clear();
                        result.insert(*key, value);
                        tmp2 = value;
                    }
                },
            }
        }
        // ์šฐ๋ฆฌ๋Š” ์ตœ๋นˆ๊ฐ’์ด ํ•„์š”ํ•˜์ง€ ์–ผ๋งˆ๋‚˜ ๋‚˜์™”๋Š”์ง€๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ into_keys ๋ฉ”์„œ๋“œ๋กœ ํ‚ค๋งŒ ์ถ”์ถœ
        // ์ถ”์ถœ ํ›„ collect ๋ฉ”์„œ๋“œ๋กœ ๋ฌถ์–ด์คŒ
        result.into_keys().collect()
    }

    // ํ‰๊ท ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
    pub fn get_avg(nums: &Vec<i32>) -> Option<f64> {
        if nums.len() == 0 { return None; }
        // ์ „์ฒด ํ•ฉ์„ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜
        let mut tmp: i32 = 0;

        for i in nums {
            tmp += i;
        }

        Some(tmp as f64 / nums.len() as f64)
    }
}

4๏ธโƒฃ Result

ํ‰๊ท ๊ฐ’ : 87.29411764705883
์ค‘๊ฐ„๊ฐ’ : 68
์ตœ๋นˆ๊ฐ’ : 21 68
728x90
๋ฐ˜์‘ํ˜•
Comments