Skip to content

Instantly share code, notes, and snippets.

View wengkhing's full-sized avatar

Weng Khing, Wong wengkhing

View GitHub Profile
@wengkhing
wengkhing / .zshrc
Created November 23, 2023 10:25
Function to add to .zshrc for switching AWS profiles
function saws() {
AWS_CONFIG_FILE="$HOME/.aws/config"
if [ ! -f "$AWS_CONFIG_FILE" ]; then
echo "AWS config file not found: $AWS_CONFIG_FILE"
return 1
fi
local profile="$1"
@wengkhing
wengkhing / App.ts
Last active May 9, 2023 01:34
Global API state management using axios wrapper and redux
import { useState, useEffect, FunctionComponent } from 'react';
import { Page, GlobalLoader, SkeletonLoader, ProjectCard, RequestError } from './component';
import useAxios from '../helper/api';
const Dashboard: FunctionComponent = () => {
const [projects, setProject] = useState();
const axios = useAxios();
const getProjects = () =>
axios.get('/projects/', {
def benchmark
start_time = Time.now
yield
end_time = Time.now
end_time - start_time
end
# Be careful, pasting this into IRB will take a long time to print.
# It's a loooong string. :)
# long_string = "apple"*100000000
def destroy_message(string)
#TODO: return the part a string without the message
string.sub(/(.*:).*/,"\\1")
end
def destroy_message!(string)
#TODO: remove the message from string destructively!
string.sub!(/(.*:).*/,"\\1")
end
def to_pig_latin(word)
word.sub(/([^aeiou]+)([aeiou]*\S*)/, "\\2\\1ay")
end
def to_pig_latin_sentence(sentence)
words = sentence.split(" ")
count = 0
words.map! do |word|
count += 1 unless to_pig_latin(word) == word
to_pig_latin(word)
# Determine whether a string contains a Social Security number.
def has_ssn?(string)
!!(/.*\d{3}-\d{2}-\d{4}/ =~ string)
end
puts "has_ssn? returns true if it has what looks like a SSN"
puts has_ssn?("please don't share this: 234-60-1422") == true
puts "has_ssn? returns false if it doesn't have a SSN"
puts has_ssn?("please confirm your identity: XXX-XX-1422") == false
def shuffle(array)
# Implement the shuffle method
for i in 0..(array.length - 1)
j = rand(i)
array[j], array[i] = array[i], array[j]
end
array
end
# TODO: Print the elements at indices 1, 3, 5, 7, etc. on separate lines.
# You should make use of Enumerable#each_with_index
def print_odd_indexed_integers(array)
array.each_with_index do |value, index|
puts value if index % 2 != 0
end
end
# TODO: Return the odd numbers from a list of integers.
# You should make use of Enumerable#select