Skip to content

Instantly share code, notes, and snippets.

View sjinno's full-sized avatar
🐽

Shohei Jinno sjinno

🐽
View GitHub Profile
const kaaten = document.createElement('div');
kaaten.style = 'display: none; position: fixed; inset: 0; background: black; z-index: 2147483647;';
document.body.append(kaaten);
let kaatenOn = false;
function orosu(e) {
if (e.ctrlKey && e.shiftKey && e.key === 'B') {
kaaten.style.display = kaatenOn ? 'none' : 'block';
@sjinno
sjinno / nth_prime.rs
Last active December 18, 2020 17:18
exercism: nth prime.
// Without using a library...
pub fn nth(n: u32) -> u32 {
let mut primes = vec![2, 3, 5, 7];
if n == 0 || n == 1 || n == 2 || n == 3 {
return primes[n as usize];
}
let mut count = 3;
let mut potential = 11;
while count != n {
@sjinno
sjinno / sublist.rs
Last active December 18, 2020 16:39
exercism: Sublist.
#[derive(Debug, PartialEq)]
pub enum Comparison {
Equal,
Sublist,
Superlist,
Unequal,
}
pub fn sublist<T: PartialEq>(_first_list: &[T], _second_list: &[T]) -> Comparison {
let len1 = _first_list.len();
type NestedVec = Vec<Elt>;
#[derive(Debug)]
enum Elt {
Num(i32),
Nest(NestedVec),
}
fn main() {
let array = vec![Elt::Num(1), Elt::Nest(vec![Elt::Num(2), Elt::Num(3)])];
@sjinno
sjinno / space_age.rs
Last active December 16, 2020 15:12
exercism challgenge: Space age.
// The code below is a stub. Just enough to satisfy the compiler.
// In order to pass the tests you can add-to or change any of this code.
#[derive(Debug)]
pub struct Duration {
seconds: f64,
}
impl From<u64> for Duration {
fn from(s: u64) -> Self {
@sjinno
sjinno / remove_specific_val_in_vec.rs
Created December 15, 2020 23:26
Find the index of the value you want to remove from a vector and remoces it.
use std::collections::HashSet;
fn main() {
// let mut hs = HashSet::new();
let hello = "hello";
let mut v: Vec<char> = hello.chars().collect();
println!("{:?}", v);
let index = v.iter().position(|x| *x == 'l').unwrap();
@sjinno
sjinno / clock.rs
Last active December 15, 2020 23:27
exercism challenge: Clock
use std::fmt;
#[derive(Debug, PartialEq)]
pub struct Clock {
hours: i32,
minutes: i32,
}
fn convert_minutes_to_time(time_in_minutes: i32) -> Clock {
let mut actual_hours = 0;
@sjinno
sjinno / anagram.rs
Last active December 16, 2020 02:02
exercism challenge: Anagram.
use std::collections::HashSet;
pub fn anagrams_for<'a>(word: &str, possible_anagrams: &[&'a str]) -> HashSet<&'a str> {
let mut output: HashSet<&'a str> = HashSet::new();
for anagram in possible_anagrams {
if check_length(word, anagram) && word.to_lowercase() != anagram.to_lowercase() {
let mut split_word: Vec<char> = word.to_lowercase().chars().collect();
for c in anagram.to_lowercase().chars() {
if split_word.iter().any(|&i| i == c) {
@sjinno
sjinno / change_val_of_specific_key.rs
Last active December 15, 2020 23:27
Playing with HashMap in rust.
use std::collections::HashMap;
fn main() {
let mut hm = HashMap::new();
hm.insert("a", 1);
hm.insert("b", 1);
hm.insert("c", 1);
println!("Before:");
for (key, val) in hm.iter() {