<jupyter_start><jupyter_text>Rustで学ぶ伝統的なアルゴリズム フィボナッチ数列- **フィボナッチ数列**:    - 直前2つの整数の合計値を出力する数列    - 出力値: 1, 1, 2, 3, 5, 8, 13, 21, ...<jupyter_code>/// フィボナッチ数列の n 番目 (0から始まる) の数値を返す関数
fn fibonacci(n: u32) -> u32 {
    if n < 2 {
        1
    } else {
        fibonacci(n - 1) + fibonacci(n - 2)
    }
}

// 0 - 7 番目までのフィボナッチ数列を表示
for i in 0..8 {
    println!("{}", fibonacci(i));
}<jupyter_output>1
1
2
3
5
8
13
21<jupyter_text>FizzBuzzFizzBuzz問題```1から100までの整数を順に出力するプログラムを作成せよただし、3の倍数のときは数値の代わりに「Fizz」を出力せよ5の倍数の倍数のときは数値の代わりに「Buzz」を出力せよ3と5の両方の倍数の場合は「Fizz」「Buzz」の代わりに「FizzBuzz」を出力せよ```<jupyter_code>use std::io::{stdout, Write};

fn fizzbuzz() {
    for i in 1..=100 {
        match i {
            x if x % 15 == 0 => println!("FizzBuzz"),
            x if x % 5  == 0 => println!("Buzz"),
            x if x % 3  == 0 => println!("Fizz"),
            _ => println!("{}", i)
        }
    }
    stdout().flush().unwrap();
}

fizzbuzz();<jupyter_output>1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz<jupyter_text>テスト上記のプログラムだけではアルゴリズムが正常なのかどうか判断できないそのため、1から100までのFizzBuzz問題の解答を配列として用意しておき、アルゴリズムの出力値と比較するようにする<jupyter_code>// 1から100までのFizzBuzz問題の解答
// 普通に記述すると array<&str> になってしまうため array::iter().map().collect() で Vec<String> に変換している
let fizzbuzz100_answer: Vec<String> = [
    "1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz",
    "11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz",
    "Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz",
    "31","32","Fizz","34","Buzz","Fizz","37","38","Fizz","Buzz",
    "41","Fizz","43","44","FizzBuzz","46","47","Fizz","49","Buzz",
    "Fizz","52","53","Fizz","Buzz","56","Fizz","58","59","FizzBuzz",
    "61","62","Fizz","64","Buzz","Fizz","67","68","Fizz","Buzz",
    "71","Fizz","73","74","FizzBuzz","76","77","Fizz","79","Buzz",
    "Fizz","82","83","Fizz","Buzz","86","Fizz","88","89","FizzBuzz",
    "91","92","Fizz","94","Buzz","Fizz","97","98","Fizz","Buzz"
].iter().map(|e| e.to_string()).collect();

/// 1からnまでFizzBuzz問題を解き Vec<String> 型で結果を返す関数
fn fizzbuzz(n: u32) -> Vec<String> {
    (1..=n).map(|i| match (i % 3, i % 5) {
        (0, 0) => "FizzBuzz".to_string(),
        (_, 0) => "Buzz".to_string(),
        (0, _) => "Fizz".to_string(),
        (_, _) => i.to_string()
    }).collect::<Vec<String>>()
}

// test: 解答とアルゴリズムの出力値を比較
// => 問題なければ何も出力されない
assert_eq!(&fizzbuzz100_answer, &fizzbuzz(100));
/// ex. 間違っているアルゴリズムの場合
fn fizzbuzz(n: u32) -> Vec<String> {
    (1..=n).map(|i| match i % 3 {
        0 => "FizzBuzz".to_string(),
        _ => i.to_string()
    }).collect::<Vec<String>>()
}

// test: 解答とアルゴリズムの出力値を比較
// => 問題なければ何も出力されない
assert_eq!(&fizzbuzz100_answer, &fizzbuzz(100));<jupyter_output>thread '<unnamed>' panicked at 'assertion failed: `(left == right)`
  left: `["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz", "16", "17", "Fizz", "19", "Buzz", "Fizz", "22", "23", "Fizz", "Buzz", "26", "Fizz", "28", "29", "FizzBuzz", "31", "32", "Fizz", "34", "Buzz", "Fizz", "37", "38", "Fizz", "Buzz", "41", "Fizz", "43", "44", "FizzBuzz", "46", "47", "Fizz", "49", "Buzz", "Fizz", "52", "53", "Fizz", "Buzz", "56", "Fizz", "58", "59", "FizzBuzz", "61", "62", "Fizz", "64", "Buzz", "Fizz", "67", "68", "Fizz", "Buzz", "71", "Fizz", "73", "74", "FizzBuzz", "76", "77", "Fizz", "79", "Buzz", "Fizz", "82", "83", "Fizz", "Buzz", "86", "Fizz", "88", "89", "FizzBuzz", "91", "92", "Fizz", "94", "Buzz", "Fizz", "97", "98", "Fizz", "Buzz"]`,
 right: `["1", "2", "FizzBuzz", "4", "5", "FizzBuzz", "7", "8", "FizzBuzz", "10", "11", "FizzBuzz", "13", "14", "FizzBuzz", "16", "17", "FizzBuzz", "19", "20", "FizzBuzz", "22", "23", "FizzBuzz", "25", "26",[...]
{'fraction_non_alphanumeric': 0.2651906519065191, 'fraction_numerical': 0.10430504305043051, 'mean_word_length': 4.457718120805369, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '476586', 'license': 'MIT', 'n_tokens_mistral': 2372, 'n_tokens_neox': 2170, 'n_words': 384, 'path': '02_samples/02_algorithm.ipynb', 'repo_name': 'amenoyoya/rust-tuto', 'size': '4065'}