Giter VIP home page Giter VIP logo

algorithms's Introduction

Algorithms

Algorithms playground for common questions solved in ruby syntax.
In case you want to prepare yourself for a job interview - try to solve it yourself first, then have a look here.

Why?

I interviewed with Google, Facebook, LinkedIn, Twitter and others. I also interviewed others myself. Sometimes it looks like they all ask you the same "out of the box" questions that don't really check knowledge but memorization of the same tricks. This is my way of saying - change your interview style. There are lots of smart people out there, this is not the best way to find them.

Problems

problem solution
Unique binary search trees click
House Robber click
Decode Ways click
Coin change click
Decode Strings click
Wiggle Subsequence click
Binary Tree Side View click
Unix path click
Array Product click
Top K Frequent Elements click
Sum 4 arrays click
Reverse Linked List click
Reverse String In-place click
Nested List Weight Sum II click
Symmetric Tree click
Dijkstra's shortest path between two nodes click
Kruskal's minimum spanning tree algorithm click
Find the square root of a number click
Add two binary numbers click
Binary search click
Longest increasing subsequence click
Find all permutations of array click
Finding all combinations of well-formed brackets click
Finding the powerset of a set click
Game of life click
Print all combinations of set joining click
count and say click
Telephone number to words click
Maximum contiguous subarray click
Max Stack click
Find the smallest biggest number that has the same digits click
Find the minimum insertions needed to make a word palindrome click
String matching - Knuth Morris Pratt algorithm KMP click
Balanced Parentheses click
Quicksort algorithm click
Mergesort algorithm click
Max Stocks Profit click
Countingsort algorithm click
Shellsort algorithm click
Knapsack problem click, click
Move Zeroes click
Longest common subsequence problem click , click
Monty Hall Problem click
Eucliden and Extended Eucliden algorithm click
Suggest index of a number in an array click
Range minimum query sparse table algorithm click
Insertion Sort click
Towers of Hanoi using Stack click
Tarjan's strongly connected components finder click

How?

Simply run ruby some_file.rb to execute the algorithm. At the bottom of each file there are some test samples.
example: ruby brackets_combinations.rb will print:
["((()))", "(()())", "(())()", "()(())", "()()()"].

Contribute

Did you find a bug? any way to do it better? please feel free to pull-request it :)

algorithms's People

Contributors

bryant1410 avatar carlxshen avatar christianbender avatar delta4d avatar easher avatar frontsideair avatar jumanjiman avatar kevivmatrix avatar kokopak avatar lexruee avatar naimrajib07 avatar nateberkopec avatar rauljordan avatar sagivo avatar sanchojaf avatar st0le avatar yordis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

algorithms's Issues

Let's make friends!

Hello, @sagivo.

I am one of the contributors to similar repository: EKAlgorithms - Objective-C implementations of common algorithms.

Few days ago I discovered this your repository and decided to add a link to it on EKAlgorithms main page. I suggest you doing the same. Let's make friends!

Thanks,

Stanislaw

perm() doesn't return the permutations of a string

This is what I'm seeing in your code:

def perm arr, i=0
  return arr if i == arr.size  
  (i..arr.size-1).each do |j|  
    arr[i], arr[j] = arr[j], arr[i]    
    perm arr, i+1    
    arr[i], arr[j] = arr[j], arr[i]    
  end    
end  
p perm 'ABC'
#=> 0..2

You can use this instead if you want to list all permutations of a string:

class String
  def perms
    self.chars.permutation.map{|x| x.join}
  end
end
"ABC".perms
#=> ["ABC", "ACB", "BAC", "BCA", "CAB", "CBA"]

should be @parent[y] = rx

@parent[rx] += @parent[ry]

and my suggestion:

#!/usr/bin/env ruby

# DisjointSet - Union-Find Data Structure
#   - root(x): find the leader of the set where x in
#   - connected?(x, y): check if x and y are in the same set
#   - union(x, y): union x and y if x and y which are not in the same set

# here the index of array is the data and rank weight
# the value of the array is the parent data

class DisjointSet
  def initialize(n)
    @parent = Array.new(n, -1)
  end

  def root(x)
    @parent[x] < 0 ? x : root(@parent[x])
  end

  def union(x, y)
    rx = root(x)
    ry = root(y)
    if rx < ry
      @parent[rx] = ry
      return true
    elsif rx > ry
      @parent[ry] = rx
      return true
    else
      return false
    end
  end
end

Edge = Struct.new(:from, :to, :weight) do
  def <=> rhs
    self.weight <=> rhs.weight
  end
end


# minimum spanning tree
# the label of the vertex is between 0 and n - 1
def kruskal(n, edges)
  ret = []
  dsu = DisjointSet.new(n)
  edges.sort.each{ |e| ret << e if dsu.union(e.from, e.to) }
  ret
end

# test
edges = [
    Edge.new(0, 1, 1), Edge.new(1, 2, 2), Edge.new(2, 3, 9),
    Edge.new(3, 0, 7), Edge.new(0, 2, 2), Edge.new(1, 3, 6)
]

p kruskal(4, edges)

More questions?

How do you decide on new questions? Can we contribute with questions?
Or you can put up questions in a to-do list in readme and we can contribute.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.