Skip to content

Commit abc2120

Browse files
committedJan 24, 2021
Finished chapter 5
1 parent eee8655 commit abc2120

File tree

10 files changed

+277
-0
lines changed

10 files changed

+277
-0
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*/*/target
2+
*/*/.vscode

‎chapter_5/problem_1/Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎chapter_5/problem_1/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "problem_1"
3+
version = "0.1.0"
4+
authors = ["Yohei Osawa <yohei.osawa.318.niko8@gmail.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
input = {path = "../../library/input"}

‎chapter_5/problem_1/src/main.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use input::{read_number, read_numline};
2+
3+
fn main() {
4+
let _: usize = read_number();
5+
let mut n_vec: Vec<i32> = read_numline();
6+
let _: usize = read_number();
7+
let s_vec: Vec<i32> = read_numline();
8+
let mut s_count_found_in_nvec = 0;
9+
for s in s_vec {
10+
let mut counter: usize = 0;
11+
let key = s;
12+
n_vec.push(s);
13+
while n_vec[counter] != key {
14+
counter += 1;
15+
}
16+
n_vec.pop();
17+
if counter != n_vec.len() {
18+
s_count_found_in_nvec += 1;
19+
}
20+
}
21+
println!("found item count is {}", s_count_found_in_nvec);
22+
}

‎chapter_5/problem_2/Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎chapter_5/problem_2/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "problem_2"
3+
version = "0.1.0"
4+
authors = ["Yohei Osawa <yohei.osawa.318.niko8@gmail.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
input = {path = "../../library/input"}

‎chapter_5/problem_2/src/main.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use input::{read_number, read_numline};
2+
3+
fn binary_search(vec: &Vec<i32>, num: i32) -> bool {
4+
let mut is_found = false;
5+
let mut left = 0;
6+
let mut right = vec.len();
7+
8+
while left < right {
9+
let mid = (left + right) / 2;
10+
if vec[mid] == num {
11+
is_found = true;
12+
break;
13+
} else if num < vec[mid] {
14+
right = mid;
15+
} else {
16+
left = mid + 1;
17+
}
18+
}
19+
is_found
20+
}
21+
22+
fn main() {
23+
let _: usize = read_number();
24+
let n_vec: Vec<i32> = read_numline();
25+
let _: usize = read_number();
26+
let s_vec: Vec<i32> = read_numline();
27+
let mut found_num = 0;
28+
for s in s_vec {
29+
let is_found = binary_search(&n_vec, s);
30+
if is_found {
31+
found_num += 1;
32+
}
33+
}
34+
println!("found count is {}", found_num);
35+
}

‎chapter_5/problem_3/Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎chapter_5/problem_3/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "problem_3"
3+
version = "0.1.0"
4+
authors = ["Yohei Osawa <yohei.osawa.318.niko8@gmail.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
input = {path = "../../library/input"}

‎chapter_5/problem_3/src/main.rs

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// https://hnw.hatenablog.com/entry/2021/01/10/162018
2+
// を参考に2パターンの連想配列クラスを作成する
3+
4+
struct Pair1 {
5+
key: String,
6+
value : i32,
7+
}
8+
9+
struct Hash1 {
10+
table: [Vec<Pair1>; Hash1::TABLESIZE],
11+
}
12+
13+
impl Hash1 {
14+
const TABLESIZE: usize = 10;
15+
16+
fn new() -> Self{
17+
Hash1{
18+
// とりあえずこれで
19+
table: Default::default(),
20+
}
21+
}
22+
23+
fn make_int_key(&self, key: &String) -> usize {
24+
let mut int_key: usize = 0;
25+
for c in key.chars() {
26+
int_key += c as usize;
27+
}
28+
int_key
29+
}
30+
31+
fn hash_func (&self, int_key: usize) -> usize {
32+
int_key % Self::TABLESIZE
33+
}
34+
35+
36+
fn insert(&mut self, key: String, value: i32) {
37+
let int_key = self.make_int_key(&key);
38+
let insert_index = self.hash_func(int_key);
39+
for pair in self.table[insert_index].iter_mut() {
40+
if pair.key == key {
41+
pair.value = value;
42+
}
43+
}
44+
self.table[insert_index].push(Pair1{key: key, value: value})
45+
}
46+
47+
fn search(&self, key: String) -> Option<i32> {
48+
let int_key = self.make_int_key(&key);
49+
let insert_index = self.hash_func(int_key);
50+
for pair in &self.table[insert_index] {
51+
if pair.key == key {
52+
return Some(pair.value)
53+
}
54+
}
55+
None
56+
}
57+
}
58+
59+
struct Pair2 {
60+
key: String,
61+
value : i32,
62+
next: i32,
63+
}
64+
65+
struct Hash2 {
66+
vec_index_for_table_index: usize,
67+
table: [i32; Hash2::TABLESIZE],
68+
data: Vec<Pair2>,
69+
}
70+
71+
impl Hash2 {
72+
const TABLESIZE: usize = 10;
73+
74+
fn new() -> Self{
75+
Hash2{
76+
vec_index_for_table_index: 0,
77+
table:[-1; Hash2::TABLESIZE],
78+
data: Vec::<Pair2>::new(),
79+
}
80+
}
81+
82+
fn make_int_key(&self, key: &String) -> usize {
83+
let mut int_key: usize = 0;
84+
for c in key.chars() {
85+
int_key += c as usize;
86+
}
87+
int_key
88+
}
89+
90+
fn hash_func (&self, int_key: usize) -> usize {
91+
int_key % Self::TABLESIZE
92+
93+
}
94+
95+
fn insert(&mut self, key: String, value: i32) {
96+
let int_key = self.make_int_key(&key);
97+
let insert_index = self.hash_func(int_key);
98+
if self.table[insert_index] == -1 {
99+
self.table[insert_index] = self.data.len() as i32;
100+
let insert_data = Pair2{key: key, value:value, next: -1, };
101+
self.data.push(insert_data);
102+
self.vec_index_for_table_index += 1;
103+
} else {
104+
let mut prev_index = self.table[insert_index] as usize;
105+
loop {
106+
if self.data[prev_index].key == key {
107+
self.data[prev_index].value = value;
108+
return;
109+
}
110+
if self.data[prev_index].next == -1 {
111+
break;
112+
} else {
113+
prev_index = self.data[prev_index].next as usize;
114+
}
115+
}
116+
self.data[prev_index].next = self.data.len() as i32;
117+
let insert_data = Pair2{key: key, value:value, next: -1, };
118+
self.data.push(insert_data);
119+
}
120+
}
121+
122+
fn search(&self, key: String) -> Option<i32> {
123+
let int_key = self.make_int_key(&key);
124+
let insert_index = self.hash_func(int_key);
125+
if self.table[insert_index] == -1 {
126+
return None
127+
}
128+
let mut search_index = self.table[insert_index];
129+
while search_index != -1 {
130+
if self.data[search_index as usize].key == key {
131+
return Some(self.data[search_index as usize].value)
132+
}
133+
search_index = self.data[search_index as usize].next;
134+
}
135+
None
136+
}
137+
138+
}
139+
140+
fn main() {
141+
let mut hash1 = Hash1::new();
142+
hash1.insert("Me".to_string(), 10);
143+
hash1.insert("Me".to_string(), 20);
144+
hash1.insert("You".to_string(), 15);
145+
println!("value is {:?} ", hash1.search("Me".to_string()));
146+
println!("value is {:?} ", hash1.search("You".to_string()));
147+
let mut hash2 = Hash2::new();
148+
hash2.insert("Me".to_string(), 10);
149+
hash2.insert("Me".to_string(), 20);
150+
hash2.insert("You".to_string(), 15);
151+
println!("value is {:?} ", hash2.search("Me".to_string()));
152+
println!("value is {:?} ", hash2.search("You".to_string()));
153+
}

0 commit comments

Comments
 (0)
Please sign in to comment.