Skip to content
Snippets Groups Projects
Commit 96bd549e authored by Eric Tilli's avatar Eric Tilli
Browse files

initial commit

parents
No related branches found
No related tags found
No related merge requests found
File added
class Array:
def err(self,idx):
raise ValueError(f"Taulukossa ei ole negatiivisia indeksejä ({idx})")
def __init__(self,size):
self.content = [None]*size
self.size = size
def __getitem__(self, idx):
if idx < 0:
self.err(idx)
return self.content[idx]
def __setitem__(self,idx,item):
if idx < 0:
self.err(idx)
self.content[idx] = item
def __repr__(self):
return self.content.__repr__()
def __str__(self):
return self.content.__str__()
def __len__(self):
return len(self.content)
if __name__ == "__main__":
x = Array(10)
print(x)
x[0] = 8
print(x)
x[9] = 10
print(x)
x[-1] = 45
print(x)
ex2.pdf 0 → 100644
File added
ex2.tex 0 → 100644
\input{"/home/sabo/lib/tex/preamble.tex"}
\begin{document}
\begin{IEEEeqnarray*}{rCl}
& T_A(n) & = 30n + 600 \\
& T_B(n) & = 50n^2 + n + 5 \\
& T_C(n) & = log_2(n) + 4000 \\
& T_D(n) & = 2n^3 + 100 \\
& T_A(10) & = 900\\
& T_B(10) & = \text{5 015}\\
& T_C(10) & \approx \text{4 003} \\
& T_D(10) & = \text{2 100}\\
& T_A(100) & = \text{3 600}\\
& T_B(100) & = \text{500 105}\\
& T_C(100) & \approx \text{4 006} \\
& T_D(100) & = \text{2 000 100}\\
& T_A(1000) & = \text{30 600}\\
& T_B(1000) & = \text{50 001 005}\\
& T_C(1000) & \approx \text{4 010} \\
& T_D(1000) & = \text{2 000 000 100}\\
\end{IEEEeqnarray*}
Olkoon siis \(<\) kyseinen relaatio.
\[A < D < C < B,\text{ kun } n = 10 \]
\[A < C < B < D,\text{ kun }n = 100 \]
\[C < A < B < D,\text{ kun }n = 1000 \]
\(D\) kasvaa selvästi nopeiten (derivaatat)
\begin{IEEEeqnarray*}{rCl}
&& 2n^3 + 100 - 30n - 600 = 0 \\
&& \implies n \approx 7 \\
&& 2n^3 + 100 - 50n^2 -n -5 = 0 \\
&& \implies n \approx -1.35 \text{ tai } 1.4 \text{ tai } 25 \\
&& 2n^3 + 100 - log_2(n) - 4000 = 0 \\
&& \implies n \approx 13 \\
\end{IEEEeqnarray*}
\[7_A < 13_C < 25_B \implies \text{\(D\) ohittaa \(B\):n kun \(n = 25\)}\]
\end{document}
import random
from customarray import Array
# testiluokka, joita voi lisätä mappiin.
class Opiskelija:
def __init__(self, opnro, nimi):
self.opnro = opnro
self.nimi = nimi
def __str__(self):
return f"Opiskelija({self.opnro}, {self.nimi})"
def __repr__(self):
return self.__str__()
# Varsinainen hashmap. Alempana käyttöesimerkki.
class HashMap:
def __init__(self, load_factor = 0.75, size = 8):
self.load_factor = load_factor
self.table = Array(size)
self.count = 0
def __str__(self):
return str(self.table)
def get_index(self, key):
A = 0.61
return int(len(self.table)*((A*key)% 1))
# return hash(key)%len(self.table)
def _extend_table(self, key, value):
new = Array(int(self.table.size * 2))
old = self.table
self.table = new
self.count = 0
for bucket in old:
if bucket:
for key, value in bucket:
self._append_table(key, value)
def _append_table(self, key, value):
index = self.get_index(key)
bucket = self.table[index]
if bucket == None:
self.table[index] = [(key,value)]
else:
bucket.append((key,value))
self.count += 1
# Muokkaa Put-metodia niin, että se kasvattaa varsinaista taulua.
def put(self, key, value):
if self.count > self.load_factor * self.table.size:
self._extend_table(key, value)
else:
self._append_table(key, value)
def print_buckets(self):
for bucket in self.table:
print(bucket)
# TESTIKOODIA!
opiskelijat = HashMap()
print(opiskelijat)
for i in range(5): # MUOKKAA LUKUA JA VARMISTA ETTÄ LISÄYS TOIMII!
x = Opiskelija(random.randint(100000,10000000),"NIMETÖN")
opiskelijat.put(x.opnro, x)
opiskelijat.print_buckets()
quicksort 0 → 100755
File added
File added
const std = @import("std");
pub fn swap(arr: anytype, a: usize, b: usize) void {
comptime {
const ArrTypeInfo = @typeInfo(@TypeOf(arr));
switch (ArrTypeInfo) {
.Pointer => {},
else => @compileLog("Expected a slice type", ArrTypeInfo),
}
}
const t = arr[a];
arr[a] = arr[b];
arr[b] = t;
}
pub fn quicksort_partition(comptime T: type, arr: []T, cmp: fn (a: T, b: T) bool) usize {
const pivot: T = arr[arr.len - 1];
var i: usize = 0; // seuraavan siirron indeksi
for (0..arr.len - 1) |j| {
if (cmp(arr[j], pivot)) {
swap(arr, j, i);
i += 1; // vaihdetaan paikkoja ja nostetaan indeksiä
}
}
swap(arr, arr.len - 1, i); // vaihdetaan pivotin paikka suurempien ja pienempien väliin
return i;
}
pub fn quicksort(comptime T: type, arr: []T, original: []T, cmp: fn (a: T, b: T) bool) void {
std.debug.print("\nTilanne: \n", .{});
print_arr(original);
std.debug.print("\n", .{});
std.debug.print("kutsuttiin seuraavalla: ", .{});
print_arr(arr);
const pivot: usize = quicksort_partition(T, arr, cmp);
std.debug.print("kutsutun muutos: ", .{});
print_arr(arr);
if (pivot == 0) {
std.debug.print("uusi pivot: {} (ei muutosta)\n", .{pivot});
} else {
std.debug.print("uusi pivot: {}\n", .{pivot});
}
if (1 < pivot) {
std.debug.print("kutsutaan vasemmalla: ", .{});
print_arr(arr[0..pivot]);
}
if (pivot + 1 < arr.len) {
std.debug.print("kutsutaan oikealla: ", .{});
print_arr(arr[pivot + 1 .. arr.len]);
}
std.debug.print("\n", .{});
if (1 < pivot) {
quicksort(T, arr[0..pivot], original[0..], cmp);
}
if (pivot + 1 < arr.len) {
quicksort(T, arr[pivot + 1 .. arr.len], original[0..], cmp);
}
}
pub fn print_arr(arr: anytype) void {
std.debug.print("[", .{});
for (arr, 1..) |e, i| {
if (i != arr.len) {
std.debug.print("{}, ", .{e});
} else {
std.debug.print("{} ]\n", .{e});
}
}
}
pub fn leq(a: usize, b: usize) bool {
return a <= b;
}
pub fn main() !void {
var arr = [_]usize{ 5, 7, 3, 9, 2, 0, 19, 2, 17, 10 };
std.debug.print("Alkuperäinen: \n", .{});
print_arr(arr);
std.debug.print("\n", .{});
quicksort(usize, arr[0..], arr[0..], leq);
}
Alkuperäinen:
[5, 7, 3, 9, 2, 0, 19, 2, 17, 10 ]
Tilanne:
[5, 7, 3, 9, 2, 0, 19, 2, 17, 10 ]
kutsuttiin seuraavalla: [5, 7, 3, 9, 2, 0, 19, 2, 17, 10 ]
kutsutun muutos: [5, 7, 3, 9, 2, 0, 2, 10, 17, 19 ]
uusi pivot: 7
kutsutaan vasemmalla: [5, 7, 3, 9, 2, 0, 2 ]
kutsutaan oikealla: [17, 19 ]
Tilanne:
[5, 7, 3, 9, 2, 0, 2, 10, 17, 19 ]
kutsuttiin seuraavalla: [5, 7, 3, 9, 2, 0, 2 ]
kutsutun muutos: [2, 0, 2, 9, 5, 7, 3 ]
uusi pivot: 2
kutsutaan vasemmalla: [2, 0 ]
kutsutaan oikealla: [9, 5, 7, 3 ]
Tilanne:
[2, 0, 2, 9, 5, 7, 3, 10, 17, 19 ]
kutsuttiin seuraavalla: [2, 0 ]
kutsutun muutos: [0, 2 ]
uusi pivot: 0 (ei muutosta)
kutsutaan oikealla: [2 ]
Tilanne:
[0, 2, 2, 9, 5, 7, 3, 10, 17, 19 ]
kutsuttiin seuraavalla: [2 ]
kutsutun muutos: [2 ]
uusi pivot: 0 (ei muutosta)
Tilanne:
[0, 2, 2, 9, 5, 7, 3, 10, 17, 19 ]
kutsuttiin seuraavalla: [9, 5, 7, 3 ]
kutsutun muutos: [3, 5, 7, 9 ]
uusi pivot: 0 (ei muutosta)
kutsutaan oikealla: [5, 7, 9 ]
Tilanne:
[0, 2, 2, 3, 5, 7, 9, 10, 17, 19 ]
kutsuttiin seuraavalla: [5, 7, 9 ]
kutsutun muutos: [5, 7, 9 ]
uusi pivot: 2
kutsutaan vasemmalla: [5, 7 ]
Tilanne:
[0, 2, 2, 3, 5, 7, 9, 10, 17, 19 ]
kutsuttiin seuraavalla: [5, 7 ]
kutsutun muutos: [5, 7 ]
uusi pivot: 1
Tilanne:
[0, 2, 2, 3, 5, 7, 9, 10, 17, 19 ]
kutsuttiin seuraavalla: [17, 19 ]
kutsutun muutos: [17, 19 ]
uusi pivot: 1
#!/usr/bin/env python3
import sys
def print_arr(arr):
for i in arr:
print(i, end=' ')
print()
def attach(old, syms, length):
new = []
if len(old) == 0:
for c in syms:
new.append(c)
else:
for i in old:
for c in syms:
new.append(i + c)
if len(new[0]) < length:
new = attach(new, syms, length)
return new
if __name__ == "__main__":
syms = sys.argv[1]
length = int(sys.argv[2])
assert(len(syms) > 0)
assert(length > 0)
val = attach([], syms, length)
assert(len(val) == len(syms)**length)
print_arr(val)
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment