Finish week 1 problem set and merge sort in Rust
This commit is contained in:
100000
data/week_2_assignment_2.txt
Normal file
100000
data/week_2_assignment_2.txt
Normal file
File diff suppressed because it is too large
Load Diff
50
py/week_1_set_1.py
Normal file
50
py/week_1_set_1.py
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
n = np.arange(1, 1000, 1)
|
||||||
|
|
||||||
|
# Version 1
|
||||||
|
|
||||||
|
a = 2**np.log(n)
|
||||||
|
b = 2**2**np.log(n)
|
||||||
|
c = n**(5/2)
|
||||||
|
d = 2**n**2
|
||||||
|
e = (n**2)*np.log(n)
|
||||||
|
|
||||||
|
# plt.plot(n, a, 'b', n, b, 'y') # a < b
|
||||||
|
# plt.plot(n, b, 'b', n, c, 'y') # b > c
|
||||||
|
# plt.plot(n, a, 'b', n, c, 'y') # a < c
|
||||||
|
|
||||||
|
# plt.plot(n, c, 'b', n, d, 'y') # c << d
|
||||||
|
# plt.plot(n, c, 'b', n, e, 'y') # c > e
|
||||||
|
# plt.plot(n, e, 'b', n, b, 'y') # e < b
|
||||||
|
# plt.plot(n, e, 'b', n, a, 'y') # e > a
|
||||||
|
|
||||||
|
# a < e < c < b < d
|
||||||
|
|
||||||
|
n = np.arange(1, 1000, 1)
|
||||||
|
# n = 1000
|
||||||
|
a = np.sqrt(n)
|
||||||
|
b = 10**n
|
||||||
|
c = n**1.5
|
||||||
|
d = 2**np.sqrt(np.log(n))
|
||||||
|
e = n**5/3
|
||||||
|
|
||||||
|
# print(a, b, c, d, e)
|
||||||
|
# d < a < c < e < b
|
||||||
|
# print(d < a, a < c, c < e, e < b)
|
||||||
|
|
||||||
|
plt.plot(n, a, 'b', n, d, 'y') # a > d
|
||||||
|
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
"""
|
||||||
|
1) n*log(n)
|
||||||
|
2) True
|
||||||
|
3) Yes if and Sometimes yes
|
||||||
|
4) O(nk**2)
|
||||||
|
5) :/ Tried:
|
||||||
|
|
||||||
|
ecadb
|
||||||
|
abecd
|
||||||
|
"""
|
||||||
48
src/main.rs
48
src/main.rs
@@ -1,19 +1,57 @@
|
|||||||
use std::vec::Vec;
|
|
||||||
use std::clone::Clone;
|
use std::clone::Clone;
|
||||||
|
use std::cmp::PartialOrd;
|
||||||
|
use std::marker::Copy;
|
||||||
|
use std::vec::Vec;
|
||||||
|
|
||||||
|
fn merge<T: Clone + PartialOrd + Copy>(a: Vec<T>, b: Vec<T>) -> Vec<T> {
|
||||||
|
let mut r = vec![];
|
||||||
|
let mut i = 0;
|
||||||
|
let mut j = 0;
|
||||||
|
|
||||||
fn merge_sort<T: Clone>(v: Vec<T>) -> Vec<T> {
|
while i < a.len() && j < b.len() {
|
||||||
|
if a[i] < b[j] {
|
||||||
|
r.push(a[i]);
|
||||||
|
i += 1;
|
||||||
|
} else {
|
||||||
|
r.push(b[j]);
|
||||||
|
j += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while i < a.len() {
|
||||||
|
r.push(a[i]);
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while j < b.len() {
|
||||||
|
r.push(b[j]);
|
||||||
|
j += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn merge_sort<T: Clone + PartialOrd + Copy>(v: Vec<T>) -> Vec<T> {
|
||||||
if v.len() <= 1 {
|
if v.len() <= 1 {
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
let sorted = v.to_vec();
|
let i = v.len() / 2;
|
||||||
|
let a = merge_sort((&v[..i]).to_vec());
|
||||||
|
let b = merge_sort((&v[i..]).to_vec());
|
||||||
|
let r = merge(a, b);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
return sorted;
|
fn mult_bin(a: i64, b: i64) -> i64 {
|
||||||
|
return a * b;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
let l = vec![2,1,3];
|
let l = vec![4, 2, 1, 3];
|
||||||
let l = merge_sort(l);
|
let l = merge_sort(l);
|
||||||
println!("{:?}", l);
|
println!("{:?}", l);
|
||||||
|
|
||||||
|
let r = mult_bin(42, 21);
|
||||||
|
println!("{:?}", r);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user