Skip to content

Instantly share code, notes, and snippets.

View rayning0's full-sized avatar
🤪

Raymond Gan rayning0

🤪
View GitHub Profile
// https://code.tutsplus.com/articles/data-structures-with-javascript-singly-linked-list-and-doubly-linked-list--cms-23392
// http://blog.benoitvallon.com/data-structures-in-javascript/the-singly-linked-list-data-structure/
node = {
value: 5,
next: null
}
function List(){
this.head = null;
def total_tax_amount_from_db
total_amount = invoice_custom_vendor_items.where(is_taxable: true)
.map(&:total_amount)
.sum
tax = Tax.new(zipcode: shipping_zip, revenue: total_amount).get_tax
if tax['ItemMessages'].empty?
taxes = tax['GroupList'][0]
self.shipping_state = taxes['StateCode']
require 'concurrent'
pool = Concurrent::FixedThreadPool.new(20, idletime: nil)
start = Time.now
100.times do |i|
puts "task #{i}"
end
time1 = Time.now - start
puts "#{time1} secs"
func TestEnergyPrices(t *testing.T) {
var jsonStr = []byte(`{"starttime":"201506031105","endtime":"201506031200"}`)
r, _ := http.NewRequest("POST", "/prices", bytes.NewBuffer(jsonStr))
w := httptest.NewRecorder()
fmt.Println(bytes.NewBuffer(jsonStr))
beego.BeeApp.Handlers.ServeHTTP(w, r)
beego.Trace("testing", "TestEnergyPrices", "Code[%d]\n%s", w.Code, w.Body.String())
Convey("Subject: Gets Average Energy Prices\n", t, func() {
// Server2 is a minimal "echo" and counter server.
package main
import (
"fmt"
"log"
"net/http"
"sync"
)
# O(N * (N + M))
# 100% correct, 40% performance
def slow_solution(m, a)
n = a.size
return 1 if n == 1
distinct = 0
n.times do |back|
(back..n - 1).each do |front|
if a[back..front] == a[back..front].uniq
distinct += 1
# Both these solutions got 100% on Codility
def binary(m)
bin = ''
begin
bin.insert(0, (m % 2).to_s)
m /= 2
end until m == 0
bin
end
@rayning0
rayning0 / flags.rb
Last active December 1, 2017 04:21
a = [1, 3, 2]
# Goal: returns the maximum number of flags that can be set on the peaks of the array
def solution(a)
# 1. find all peaks
peaks = []
(1..a.size - 2).each do |i|
if a[i] > a[i - 1] && a[i] > a[i + 1]
peaks << i
end
# count number of divisors of n
# O(n)
def slow_divisors(n)
divisors = 0
(1..n).each do |num|
if n % num == 0
divisors += 1
puts "num: #{num}"
end
A = [4, 6, 6, 6, 6, 8, 8]
n = 7
size =
value = 6
k = 5
A[k] = 8
# def hash_leader(n)
# freq = Hash.new(0)