Code Monkey home page Code Monkey logo

algorithms's Issues

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"]

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.

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

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)

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.