【Rust】HashMapのValueへのアクセス

employee[“A”].clone().pop() ではなく、要素を指定せずにvalueをpop()したい場合

use std::collections::HashMap;

fn main() {
    let mut employee: HashMap<String, Vec<String>> = HashMap::new();
    employee.insert("A".to_string(), vec!["X".to_string(), "Y".to_string(), "Z".to_string()]);
    employee.insert("B".to_string(), vec!["X".to_string(), "Y".to_string(), "Z".to_string()]);
    for (key, mut value) in employee {
        println!("{:?}", value.pop());
    }
}

for e in employee ってしてしまうと、eからvalueの取り出し方法がわからなくなる。
これに結構時間かかりました。

vectorの要素のindexを取得する方法

        println!("{:?}", value.iter().position(|n| n == "Y"));
use std::collections::HashMap;

fn main() {

   let mut employee: HashMap<String, Vec<String>> = HashMap::new();
   employee.insert("A".to_string(), vec!["X".to_string(), "Y".to_string(), "Z".to_string()]);
   employee.insert("B".to_string(), vec!["X".to_string(), "Y".to_string(), "Z".to_string()]);
   employee.insert("C".to_string(), vec!["Y".to_string(), "Z".to_string(), "X".to_string()]);

   let mut department: HashMap<String, Vec<String>> = HashMap::new();
   department.insert("X".to_string(), vec!["C".to_string(), "A".to_string(), "B".to_string()]);
   department.insert("Y".to_string(), vec!["B".to_string(), "C".to_string(), "A".to_string()]);
   department.insert("Z".to_string(), vec!["A".to_string(), "B".to_string(), "C".to_string()]);

   let res = stable_match(employee, department);
   println!("{:?}", res);
}

fn stable_match(request_dict: HashMap<String, Vec<String>>, accept_dict: HashMap<String, Vec<String>>) -> HashMap<String, String>{
   let mut match_dict: HashMap<String, String> = HashMap::new();
   let req_len = request_dict.len();

   while match_dict.len() < req_len {
      'label: for (req_name, req_value) in &request_dict {
         for value in match_dict.values() {
            if value == req_name {
               break 'label;
            }
         }
         let acc_name = req_value.pop();
         if !match_dict.contains_key(&acc_name) {
            match_dict.insert(acc_name.unwrap(), req_name.to_string());
         } else {
            let rival_name = match_dict[&acc_name];
            if accept_dict[&acc_name].iter().position(|n| n == req_name) < accept_dict[&acc_name].iter().position(|n| n == rival_name) {
               match_dict.insert(acc_name.unwrap(), req_name.to_string());
            }
         }
      }
   }
   return match_dict;
}

うーむ、上手く書けん…

【Rust】ラムダ関数とは

– ラムダ計算とは関数型言語の基盤となっている計算モデルで、数値や演算の具体的な表記ではなく、「関数そのもの」を使って計算を表現する
– Rustにおけるクロージャは、その外側の環境を捕捉した関数のこと
– クロージャの構文や機能は、その場限りの用途で何かを作るのに便利

   let sq = |n| n * n;
   println!("{}", sq(9));

81

   let i = 32;
   let v = |n| n * i;
   println!("{}", v(9));

288

なるほどね

【Rust】ルックアップテーブル【アルゴリズム】

fn main() {

   let Q = [
      100, 200, 250, 300, 360
   ];

   let R = [
      401, 302, 101, 102, 301
   ];

   let mut x = String::new();
   std::io::stdin().read_line(&mut x).expect("Failed to read line");
   let x  = x.trim_end().to_owned();
   let a = x.parse::<i32>().unwrap();

   let n = Q.len();

   while a > 0 {
      let mut i = 0;
      while i < n {
         if a < Q[i] {
            println!("整理番号{}番の方は{}会場です。", a, R[i]);
            i = n;
         } else {
            i += 1;
         }

      }
   }
}

整理番号100番の方は302会場です。

### 選択ソート

fn main() {

   let A = [
      84, 121, 43, 93, 140, 83, 14, 93, 181, 58
   ];

   let n = A.len();
   seq_sort(n, A);
}

fn seq_sort(n: usize, mut A: [i32; 10]) {
   let mut i = 0;
   let mut B: [i32; 10] = [0; 10];
   let mut k;
   for i in 0..n {
      
      let mut a = 999;
      let mut j = 0;
      for j in j..n {
         if a > A[j] {
            a = A[j];
            k = j;
         }
      }
      A[k] = 999;
      B[i] = a;
   }
   
}

【Rust】バイナリーサーチ【アルゴリズム】

fn main() {

   let mut commodity: [String; 8] = [
      "ジュース".to_string(),
      "紅茶".to_string(),
      "アイスティ".to_string(),
      "コーヒー".to_string(),
      "アイスコーヒー".to_string(),
      "ショートケーキ".to_string(),
      "チーズケーキ".to_string(),
      "トースト".to_string(),
   ];

   let mut price: [i32; 8] = [
      280, 300, 320, 350, 370, 550, 600, 650
   ];

   let mut x = String::new();
   std::io::stdin().read_line(&mut x).expect("Failed to read line");
   let x  = x.trim_end().to_owned();
   let a = x.parse::<i32>().unwrap();

   let n = commodity.len() as i32;
   bin_search(a, n, price);
   // seq_search();
}

fn bin_search(a: i32, n: i32, price: [i32; 8]) {
   let mut p = -1;
   let mut l = 0;
   let mut h = n - 1;

   while (l <= h && p == -1) {
      if (a > price[((l+h) / 2) as usize]) {
         l = (l + h) / 2 + 1;
      } else {
         if (a == price[((l+h)/2) as usize]) {
            p = (l +h) / 2;
         } else {
            h = (l + h) / 2 - 1;
         }
      }
   }
   println!("{}", p);
}

Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/basic`
300
1

【Rust】素数: エラトスネテスのふるい【アルゴリズム】

fn main() {

   let mut s: [i32; 101] = [1; 101];

   let mut i = 2;
   while i <= 100 {
      s[i] = 1;
      i += 1;
   }

   i = 2;
   while i * i <= 100 {
      let mut j = 2;

      while i * j <= 100 && s[i]!= 0 {
         s[i*j] = 0;
         j += 1;
      }
      i += 1;
   }

   i = 2;
   while i < 100 {
      if s[i] == 1 {
         println!("{}", i);
      }
      i += 1;
   }
}

2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

紀元前の話

【Rust】素数【アルゴリズム】

fn main() {

   let mut s: [i32; 100] = [0; 100];

   let mut i = 1;
   s[0] = 2;

   let mut n = 3;
   let mut j = 0;
   while n <= 100 {
      let quotient = n / s[j];
      let reminder = n % s[j];

      if reminder == 0 {
         n = n + 2;
         j = 1;
      } else {
         if quotient >= s[j] {
            j += 1;
         } else {
            s[j] = n;
            i += 1;
            j = 1;
            n = n + 2;
         }
      }
   }

   let mut i = 0;
   while s[i] != 0 {
      println!("{}", s[i]);
      i += 1;
   }
}

Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.40s
Running `target/debug/basic`
thread ‘main’ panicked at src/main.rs:11:22:
attempt to divide by zero
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

整数を”0″で割るとundefinedでエラーになる。。。
うーん どう表現したら良いんやろうか

【Rust】二次元配列【アルゴリズム】

配列の個数がわかっている場合、[[i32; 5]; 6]というような書き方をする。
わかっていない場合はvectorにすれば良い。

fn main() {

   let score:[[i32; 5]; 6] = [
      [65, 71, 74, 82, 97],
      [84, 83, 73, 64, 65],
      [74, 74, 78, 80, 78],
      [90, 90, 90, 90, 90],
      [87, 78, 85, 60, 88],
      [93, 95, 94, 92, 95],
   ];
   println!("番号 国語 数学 理科 社会 英語 合計 平均");

   let mut i = 0;
   while i < 5 {
      let (mut t, mut j) = (0, 0);
      print!("{} ", i);
      while (j <= 4) {
         print!("{} ", score[i][j]);
         t = t + score[i][j];
         j += 1;
      }

      let av = t / j as i32;
      println!("{} {}", t, av);
      i += 1;
   }
}

番号 国語 数学 理科 社会 英語 合計 平均
0 65 71 74 82 97 389 77
1 84 83 73 64 65 369 73
2 74 74 78 80 78 384 76
3 90 90 90 90 90 450 90
4 87 78 85 60 88 398 79

なるほどー

【Rust】配列【アルゴリズム】

fn main() {

   let mut a;
   let raikyaku: [i32; 7] = [521, 195, 221, 329, 352, 399, 450];

   let mut x = String::new();
   std::io::stdin().read_line(&mut x).expect("Failed to read line");
   let y  = x.trim_end().to_owned();
   a = y.parse::<usize>().unwrap();
   while a != 9 {
      if (a < 0 || a > 6) {
         println!("error");
      } else {
         println!("{}人", raikyaku[a]);
      }
      let mut x = String::new();
      std::io::stdin().read_line(&mut x).expect("Failed to read line");
      x = x.trim_end().to_owned();
      a = x.parse::<usize>().unwrap();
   }
}
fn main() {

   let raikyaku: [i32; 7] = [521, 195, 221, 329, 352, 399, 450];
   let (mut t, mut i) = (0, 0);

   while i < 7 {
      t = t + raikyaku[i];
      i += 1;
   }

   let av = t / i as i32;
   println!("合計: {}", t);
   println!("平均: {}", av);
}

合計: 2467
平均: 352

【Rust】OpCodeのバイトコード(u8)へのシリアライズ•デシリアライズ

バイトコードに変換するのはあくまでOpCode, Argであって、OpCodeと引数を元に処理するプログラム自体はバイトコードには変換しない。Instruction自体は別に持っておく。

use std::io::{BufReader, BufWriter, Read, Write};

#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum OpCode {
    LoadLiteral,
    Add,
}

impl From<u8> for OpCode {
    #[allow(non_upper_case_globals)]
    fn from(o: u8) -> Self {
      const LoadLiteral: u8 = OpCode::LoadLiteral as u8;
      const Add: u8 = OpCode::Add as u8;
  
      match o {
        LoadLiteral => OpCode::LoadLiteral,
        Add => OpCode::Add,
        _ => panic!("Opcode \"{:02X}\" unrecognized!", o),
      }
    }
  }

#[derive(Debug, Clone, Copy)]
#[repr(C)]
struct Instruction {
    op: OpCode,
    arg0: u8,
}

impl Instruction {
    fn new(op: OpCode, arg0: u8) -> Self {
        Self { op, arg0 }
    }

    fn serialize(
        &self,
        writer: &mut impl Write,
    ) -> Result<(), std::io::Error> {
        writer.write_all(&[self.op as u8, self.arg0])?;
        Ok(())
    }
}

fn write_program(file: &str) {
    let instructions = [
        Instruction::new(OpCode::LoadLiteral, 42),
        Instruction::new(OpCode::LoadLiteral, 36),
        Instruction::new(OpCode::Add, 0),
    ];

    if let Ok(writer) = std::fs::File::create(file) {
        let mut writer = BufWriter::new(writer);
        for instruction in &instructions {
            instruction.serialize(&mut writer).unwrap();
        }
        println!("Written {} instructions", instructions.len());
    }
}

fn main() {
    write_program("bytecode.bin");
}

fn interpret(instructions: &[Instruction]) -> Option<i64> {
    let mut stack = vec![];

    for instruction in instructions {
        match instruction.op {
          OpCode::LoadLiteral => {
            stack.push(instruction.arg0 as i64);
          }
          OpCode::Add => {
            let rhs = stack.pop().expect("Stack underflow");
            let lhs = stack.pop().expect("Stack underflow");
            stack.push(lhs + rhs);
          }
        }
      }
    stack.pop()
}

なるほど、データの流れは理解できました。

### デシリアライズ

use std::io::{BufReader, BufWriter, Read, Write};

#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum OpCode {
    LoadLiteral,
    Add,
}

impl From<u8> for OpCode {
    #[allow(non_upper_case_globals)]
    fn from(o: u8) -> Self {
      const LoadLiteral: u8 = OpCode::LoadLiteral as u8;
      const Add: u8 = OpCode::Add as u8;
  
      match o {
        LoadLiteral => OpCode::LoadLiteral,
        Add => OpCode::Add,
        _ => panic!("Opcode \"{:02X}\" unrecognized!", o),
      }
    }
  }

#[derive(Debug, Clone, Copy)]
#[repr(C)]
struct Instruction {
    op: OpCode,
    arg0: u8,
}

impl Instruction {
    fn new(op: OpCode, arg0: u8) -> Self {
        Self { op, arg0 }
    }

    fn serialize(
        &self,
        writer: &mut impl Write,
    ) -> Result<(), std::io::Error> {
        writer.write_all(&[self.op as u8, self.arg0])?;
        Ok(())
    }

    fn deserialize(
        reader: &mut impl Read,
    ) -> Result<Self, std::io::Error> {
        let mut buf = [0u8; 2];
        reader.read_exact(&mut buf)?;
        Ok(Self::new(buf[0].into(), buf[1]))
    }
}

fn write_program(file: &str) {
    let instructions = [
        Instruction::new(OpCode::LoadLiteral, 42),
        Instruction::new(OpCode::LoadLiteral, 36),
        Instruction::new(OpCode::Add, 0),
    ];

    if let Ok(writer) = std::fs::File::create(file) {
        let mut writer = BufWriter::new(writer);
        for instruction in &instructions {
            instruction.serialize(&mut writer).unwrap();
        }
        println!("Written {} instructions", instructions.len());
    }
}

fn read_program(file: &str) -> Option<Vec<Instruction>> {
    if let Ok(reader) = std::fs::File::open(file) {
        let mut reader = BufReader::new(reader);
        let mut instructions = vec![];
        while let Ok(inst) = Instruction::deserialize(&mut reader) {
            instructions.push(inst);
        }
        Some(instructions)
    } else {
        None
    }
}

fn main() {
    let mut args = std::env::args();
    args.next();
    match args.next().as_ref().map(|s| s as &str) {
        Some("w") => write_program("bytecode.bin"),
        Some("r") => {
            if let Some(instructions) = read_program("bytecode.bin") {
                let result = interpret(&instructions);
                println!("result: {result:?}");
            }
        }
        _ => println!("Please specify w or r as an argument"),
    }
    
}

fn interpret(instructions: &[Instruction]) -> Option<i64> {
    let mut stack = vec![];

    for instruction in instructions {
        match instruction.op {
          OpCode::LoadLiteral => {
            stack.push(instruction.arg0 as i64);
          }
          OpCode::Add => {
            let rhs = stack.pop().expect("Stack underflow");
            let lhs = stack.pop().expect("Stack underflow");
            stack.push(lhs + rhs);
          }
        }
      }
    stack.pop()
}

Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.61s
Running `target/debug/ruscal r`
result: Some(78)

バイトコードを読み取って、スタックを実行している。