Skip to content

Instantly share code, notes, and snippets.

View ricky-lim's full-sized avatar

Ricky Lim ricky-lim

View GitHub Profile
@ricky-lim
ricky-lim / main.rs
Created February 28, 2023 20:11
exponentiate a list of numbers
fn main() {
let mut v = vec![1, 2, 3, 4, 5];
let exp = 2;
for num in &mut v {
*num = i32::pow(*num, exp);
}
println!("{:?}", v);
}
@ricky-lim
ricky-lim / maxProduct.java
Created March 31, 2022 18:24
Find max products
import java.util.Arrays;
class Scratch {
public static int maxProduct(int[] numbers) {
int length = numbers.length;
if (length < 2) {
throw new IllegalArgumentException(String.format("Require at least 2 numbers. But given: %s", Arrays.toString(numbers)));
}
int min1 = numbers[0];
@ricky-lim
ricky-lim / countVowelAndConsonant.java
Created March 30, 2022 11:48
Count vowels and consonants
import java.util.List;
import java.util.stream.Collectors;
class Scratch {
public static void countVowelAndConsonant(String s) {
String VOWELS = "aeiouy";
String normalized = s.toLowerCase().trim();
List<Integer> letters = normalized.chars()
@ricky-lim
ricky-lim / palindromChecker.java
Last active March 30, 2022 11:16
Palindrom checker
import java.util.stream.IntStream;
class Scratch {
public static boolean palindromeChecker(String s) {
String normalizedString = s.trim().toLowerCase();
return IntStream.range(0, normalizedString.length() / 2)
.allMatch(i ->
normalizedString.charAt(i) ==
normalizedString.charAt(normalizedString.length() - i - 1));
@ricky-lim
ricky-lim / autoreload-ipython.sh
Created January 10, 2021 08:52
Add autoreload to ipython config
# Create config file
$ ipython profile create
$ vim <ipython_config_file>
c.InteractiveShellApp.extensions = ['autoreload']
.InteractiveShellApp.exec_lines = ['%autoreload 2']
@ricky-lim
ricky-lim / generate_bins.py
Created January 7, 2021 18:21
generate binning with a size, that accumulates to the count
#!/usr/bin/env python
import math
def generate_bins(size: int, count: int):
"""
>>> list(generate_bins(size=100, count=300))
[100, 100, 100]
>>> list(generate_bins(size=100, count=301))
[100, 100, 100, 1]
@ricky-lim
ricky-lim / club_member.py
Last active December 19, 2020 09:39
club-member
import pytest
from sqlalchemy import (
create_engine,
Column,
Integer,
String,
Table,
ForeignKey,
PrimaryKeyConstraint,
)
@ricky-lim
ricky-lim / vuetify-template-form.py
Created December 2, 2020 13:08
template form for vuetify
import ipyvuetify as v
import traitlets
import json
class MyForm(v.VuetifyTemplate):
email = traitlets.Unicode('').tag(sync=True)
password = traitlets.Unicode('').tag(sync=True)
isValid = traitlets.Bool(False).tag(sync=True)
result = traitlets.Dict({}).tag(sync=True)
template = traitlets.Unicode('''
@ricky-lim
ricky-lim / many-to-many.py
Created November 28, 2020 03:34
Questions
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import sessionmaker, relationship, backref
engine = sqlalchemy.create_engine('sqlite:///tmp.db')
Base = declarative_base()
class Answer(Base):
@ricky-lim
ricky-lim / example-video-audio-youtube-dl.txt
Last active January 10, 2021 22:58
Combine m4a and mp4 from youtube
pipx install tubex
tubex --outdir foo download-mp4 https://www.youtube.com/watch?v=LABGimhsEys