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
| # Best O(1) | |
| # Worst O(long n) | |
| arr = [1,2,3,4,5,6,7,10] | |
| def bs (list, elem) | |
| low = 0 | |
| high = list.length | |
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
| arr = [9,81,-9,3,0,12,-10] | |
| def find_min(list) | |
| min = list[0] | |
| index = 0 | |
| list.each_with_index do |val, i| | |
| if val < min | |
| min = val | |
| index = 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
| require "serverspec" | |
| require "docker" | |
| package = ['rake', 'pg'] | |
| describe "Dockerfile" do | |
| before(:all) do | |
| @image = Docker::Image.build_from_dir('.', {'dockerfile'=>'Dockerfile'}) | |
| @image.tag(repo: 'image-test', tag: 'latest'). |
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
| const BOM = '\ufffe' //LE. for BE '\ufeff' | |
| func createFile(name string) error { | |
| var bytes [2]byte | |
| data := `test string UTF-8` | |
| file, err := os.Create(name) | |
| if err != nil { | |
| fmt.Errorf("Can't open file. %v", err) | |
| return err |
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
| =begin | |
| Worst time: O(n2) if choose bad pivot element | |
| Approximate time: O(n log n) | |
| =end | |
| def quicksort array | |
| if array.length < 2 | |
| array | |
| else | |
| pivot = array[rand(array.length)] |
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(|V|+|E|) | |
| graph = Hash.new | |
| graph["you"] = ["alice", "bob", "claire"] | |
| graph["bob"] = ["anuj", "peggy"] | |
| graph["alice"] = ["peggy"] | |
| graph["claire"] = ["thom", "jonny"] | |
| graph["anuj"] = [] | |
| graph["peggy"] = [] | |
| graph["thom"] = [] |
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
| graph = {} | |
| graph['start'] = Hash.new | |
| graph['start']['a'] = 6 | |
| graph['start']['b'] = 2 | |
| graph['a'] = Hash.new | |
| graph['a']['fin'] = 1 | |
| graph['b'] = Hash.new | |
| graph['b']['a'] = 3 | |
| graph['b']['fin'] = 5 |
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
| fib = 9 | |
| def BinetFib number | |
| phi = ((1 + Math.sqrt(5)) / 2) #or 1.6180339887 | |
| divid = phi**number | |
| divider = Math.sqrt(5) | |
| (divid / divider).round | |
| end | |
| puts BinetFib(fib); |
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 sum_digits digit | |
| if digit < 10 | |
| digit | |
| else | |
| (digit % 10) + (digit / 10).to_f | |
| end | |
| end | |
| def validate number | |
| number = number.to_s.reverse.to_i |
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
| # Set cover problem | |
| states = ['mt', 'wa', 'or', 'id', 'nv', 'ut', 'ca', 'az'].uniq | |
| station = {} | |
| station["kone"] = ['id', 'nv', 'ut'].uniq | |
| station["ktwo"] = ['mt', 'wa', 'id'].uniq | |
| station["kthree"] = ['or', 'nv', 'ca'].uniq | |
| station["kfour"] = ['nv', 'ut'].uniq | |
| station["kfive"] = ['ca', 'az'].uniq |