Skip to content

Instantly share code, notes, and snippets.

View hirohiro2255's full-sized avatar

Hirokazu Hirono hirohiro2255

View GitHub Profile
@hirohiro2255
hirohiro2255 / main.rs
Created December 19, 2020 11:39
omikuji part 2
extern crate rand;
use rand::Rng;
fn main() {
let n = rand::thread_rng().gen_range(1, 91);
match n {
n if n >= 1 && n <= 10 => {
println!("大吉");
}
@hirohiro2255
hirohiro2255 / main.rs
Created December 19, 2020 11:29
omikuji script
extern crate rand;
use rand::Rng;
fn main() {
println!("おみくじだよ!");
let secret_num = rand::thread_rng().gen_range(0, 7);
match secret_num {
@hirohiro2255
hirohiro2255 / anagram.js
Created October 1, 2019 12:55
check if they are anagrams
function validAnagram(str1, str2) {
if (typeof str1 === 'string' && typeof str2 === 'string') {
if (str1.length !== str2.length) {
return false;
}
const counter1 = {};
const counter2 = {};
for (let ch of str1) {
counter1[ch] = (counter1[ch] || 0) + 1;
}
@hirohiro2255
hirohiro2255 / same2.js
Created October 1, 2019 12:30
Frequency counter with O(N)
function same(arr1, arr2) {
// O(N)
if (Array.isArray(arr1) && Array.isArray(arr2)) {
if (arr1.length !== arr2.length) {
return false;
}
const freqCounter1 = {};
const freqCounter2 = {};
for (let val of arr1) {
freqCounter1[val] = (freqCounter1[val] || 0) + 1;
@hirohiro2255
hirohiro2255 / same.js
Created October 1, 2019 04:58
frequency counter
function same(arr1, arr2) {
if (Array.isArray(arr1) && Array.isArray(arr2)) {
for (let i = 0; i < arr1.length; i++) {
if (!arr2.includes(arr1[i] * arr1[i])) {
return false;
}
}
return true;
}
return false;
@hirohiro2255
hirohiro2255 / reverseStr4.js
Created September 15, 2019 12:02
function to reverse string
function reverse(str) {
if (typeof str === "string") {
return str.split("").reduce((a, b) => {
return b + a;
});
}
}
@hirohiro2255
hirohiro2255 / reverseStr3.js
Created September 15, 2019 11:57
function to reverse string
function reverseStr3(str) {
let reversed = "";
for (let char of str) {
reversed = char + reversed;
}
return reversed;
}
@hirohiro2255
hirohiro2255 / reverseStr2.js
Created September 15, 2019 11:47
function to reverse string
function reverseStr2(str) {
const arr = str.split("");
for (let i = 0; i < Math.floor(str.length / 2); i++) {
let temp = arr[i];
arr[i] = arr[str.length - i];
arr[str.length - i] = temp;
}
return arr.join("");
}
@hirohiro2255
hirohiro2255 / reverseStr1.js
Created September 15, 2019 11:32
function to reverse string
function reverseStr(str) {
if (typeof str === "string") {
return str
.split("")
.reverse()
.join();
}
}
@hirohiro2255
hirohiro2255 / falsyBouncer.js
Created September 14, 2019 13:12
basic algorithm from fcc
function falsyBouncer(arr) {
if (Array.isArray(arr)) {
return arr.filter(val => {
return !!val;
});
}
}