This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Server2 is a minimal "echo" and counter server. | |
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| "net/http" | |
| "sync" | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |