Skip to content

Commit

Permalink
start of a simple shell sort fucntion
Browse files Browse the repository at this point in the history
  • Loading branch information
hearues-zueke-github committed Apr 8, 2023
1 parent 6f4f8a0 commit 1a7c5a8
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 5 deletions.
10 changes: 7 additions & 3 deletions genetic_algorithm/tic_tac_toe/tic_tac_toe_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@ def play_the_games(
# save_moves=False,
# )

l_moves, move_nr, player_nr_won = play_one_game(tictactoe_board=tictactoe_board, d_row_nn=d_row_nn, save_moves=False)
l_moves, move_nr, player_nr_won = play_one_game(
tictactoe_board=tictactoe_board,
d_row_nn=d_row_nn,
save_moves=False,
)

if player_nr_won != 0:
nr_won = d_row_nn[player_nr_won]['nr']
Expand Down Expand Up @@ -289,7 +293,8 @@ def crossover_and_mutate(arr_bw_dst, arr_bw_src_1, arr_bw_src_2, mix_rate, rando
n_player = 2

elite_games = 3
take_best_games = 10
take_best_games = 5
amount_nr = 40
max_game_nr = 60

mix_rate = 0.65
Expand Down Expand Up @@ -323,7 +328,6 @@ def crossover_and_mutate(arr_bw_dst, arr_bw_src_1, arr_bw_src_2, mix_rate, rando

l_df_stats = []

amount_nr = 30
for player_nr, d_data in d_player_nr_to_d_data.items():
for nr in range(0, amount_nr):
d_data["nr"].append(nr)
Expand Down
55 changes: 55 additions & 0 deletions rust/shell_sort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// fn f(a: i8) -> i8 {
// return a + 1;
// }

// pub struct Row {
// a: i8,
// b: String,
// }

// impl Row {
// fn print_a(&self) -> () {
// println!("a: {}, yes", self.a);
// }
// }

// impl std::fmt::Display for Row {
// fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
// write!(f, "(value a: {}, value b: {})", self.a, self.b)
// }
// }

// impl std::fmt::Display for Vec {
// fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
// write!(f, "(value a: {}, value b: {})", self.a, self.b)
// }
// }

fn main() {
let mut a: Vec<i64> = vec![];
a.push(1);
print!("a: {:?}\n", a);

// let mut a = [1, 2, 3];
// let b = 4;
// println!("Hello World! b: {}", b);
// for (i, v) in a.iter_mut().enumerate() {
// println!("i: {}, v: {}", i, v);
// }
// let s = format!{"{:?}", a};
// println!("s: {}", s);

// let c = 6;
// let d = f(c);
// println!("d: {}", d);

// let d = Row {
// a: 48,
// b: String::from("Test"),
// };

// println!("d: {}", d);

// d.print_a();
// d.print_a();
}
2 changes: 2 additions & 0 deletions rust/shell_sort/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/*
Cargo.lock
9 changes: 9 additions & 0 deletions rust/shell_sort/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "shell_sort"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
oorandom = "11.1.3"
53 changes: 53 additions & 0 deletions rust/shell_sort/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use oorandom;

fn check_vec_sorted(v: &Vec<i64>) -> bool {
if v.len() < 2 {
return true;
}

for i in 0..v.len()-1 {
// println!("i: {}", i);
if v[i] > v[i + 1] {
return false;
}
}

return true;
}

fn sort_vec_bubble(v: &mut Vec<i64>) {
if v.len() < 2 {
return;
}

for i in (0..v.len()).rev() {
for j in 0..i {
// print!("j: {}", j);
if v[j] > v[j + 1] {
// print!(", swap!");
(v[j], v[j + 1]) = (v[j + 1], v[j])
}
// println!("");
}
}
}

fn main() {
let mut a: Vec<i64> = Vec::new();
let mut rnd = oorandom::Rand64::new(0x1234);

for _ in 0..10 {
a.push(rnd.rand_i64());
}
println!("a: {:?}", a);

println!("a.len(): {}", a.len());

let mut b = a.clone();
println!("b.len(): {}", b.len());
println!("b: {:?}", b);
println!("b is sorted? {}", check_vec_sorted(&b));
sort_vec_bubble(&mut b);
println!("b: {:?}", b);
println!("b is sorted? {}", check_vec_sorted(&b));
}
92 changes: 92 additions & 0 deletions sequence_generators/prime_generators/gcd_prime_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#! /usr/bin/env -S /usr/bin/time /usr/bin/python3.11 -i

# -*- coding: utf-8 -*-

# Some other needed imports
import datetime
import dill
import gzip
import itertools
import os
import pdb
import re
import sys
import time
import traceback

import numpy as np # need installation from pip
import pandas as pd # need installation from pip
import multiprocessing as mp

import matplotlib.pyplot as plt # need installation from pip

from collections import defaultdict
from copy import deepcopy, copy
from dotmap import DotMap # need installation from pip
from functools import reduce
from hashlib import sha256
from io import BytesIO
from math import gcd
from memory_tempfile import MemoryTempfile # need installation from pip
from shutil import copyfile
from pprint import pprint
from typing import List, Set, Tuple, Dict, Union, Any
from PIL import Image

CURRENT_WORKING_DIR = os.getcwd()
PATH_ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
HOME_DIR = os.path.expanduser("~")
TEMP_DIR = MemoryTempfile().gettempdir()
PYTHON_PROGRAMS_DIR = os.path.join(HOME_DIR, 'git/python_programs')

# set the relative/absolute path where the utils_load_module.py file is placed!
sys.path.append(PYTHON_PROGRAMS_DIR)
from utils_load_module import load_module_dynamically

var_glob = globals()
load_module_dynamically(**dict(var_glob=var_glob, name='utils', path=os.path.join(PYTHON_PROGRAMS_DIR, "utils.py")))
load_module_dynamically(**dict(var_glob=var_glob, name='utils_multiprocessing_manager', path=os.path.join(PYTHON_PROGRAMS_DIR, "utils_multiprocessing_manager.py")))

mkdirs = utils.mkdirs
MultiprocessingManager = utils_multiprocessing_manager.MultiprocessingManager

OBJS_DIR_PATH = os.path.join(PATH_ROOT_DIR, 'objs')
mkdirs(OBJS_DIR_PATH)

PLOTS_DIR_PATH = os.path.join(PATH_ROOT_DIR, 'plots')
mkdirs(PLOTS_DIR_PATH)

if __name__ == '__main__':
print("Hello World!")

for v_start in range(1, 100):
# v_start = 6
l_v = []
l_gcd = []
v = v_start
l_v.append(v)
for n in range(2, 1000000):
if n % 1000000 == 0:
print(f"n: {n}")
v_gcd = gcd(n, v)
v = v + v_gcd

l_gcd.append(v_gcd)
l_v.append(v)

arr_gcd = np.array(l_gcd)

arr_idx = np.where(arr_gcd > 1)[0]
arr_gcd_gt_1 = arr_gcd[arr_idx]

arr_idx_n = arr_idx + 2

arr_n_gcd = np.vstack((arr_idx_n, arr_gcd_gt_1)).T
# print(f"arr_n_gcd:\n{arr_n_gcd}")

print(f"v_start: {v_start}")
print(f"arr_n_gcd.shape[0]: {arr_n_gcd.shape[0]}")
if arr_n_gcd.shape[0] > 0:
max_v_gcd = np.max(arr_n_gcd[:, 1])
print(f"max_v_gcd: {max_v_gcd}")
print("")
4 changes: 2 additions & 2 deletions vlang/test_yaml.v
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import yaml
import yaml
import os

fn main() {
Expand All @@ -8,5 +8,5 @@ fn main() {
panic('error reading file $filename')
return
}
println("data:\n${data}")
print("data:\n${data}")
}

0 comments on commit 1a7c5a8

Please sign in to comment.