Skip to content

Instantly share code, notes, and snippets.

View imouaddine's full-sized avatar

Imad Mouaddine imouaddine

  • Measured
  • Casablanca
View GitHub Profile
class Activity
attr_accessor :s, :f
def initialize(s,f)
@s = s
@f = f
end
def inspect
"#{s} to #{f}"
end
end
a = [
[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]
]
def is_safe(a, row, col, n)
#Check the same row
def insert(a, index, value)
i = index
while(index >= 0 && value < a[i])
a[i+1] = a[i]
i -= 1
end
a[i+1] = value
end
def selection_sort(a)
def find_tree_sum(start, sum)
$last_sum ||= 0
$result ||= []
if start.nil?
return false
end
$last_sum += start.value
$result << start.value
def hanoi(n, source, intermediary, destination)
if n > 0
hanoi(n-1, source,destination,intermediary)
destination.push(source.pop)
hanoi(n-1, intermediary, source, destination)
end
end
@imouaddine
imouaddine / Partition Linked List
Created November 12, 2014 14:50
Partition Linked List
class LinkedList
attr_accessor :head, :tail
class Node
attr_accessor :previous, :next, :value
def initialize(value)
@value = value
end
end
@imouaddine
imouaddine / Remove duplicates of linked list
Created November 10, 2014 22:34
o(n2) without any temporary buffer
def remove_duplicates
unless head && head.next
return self
end
current = head
while(current)
runner = current.next
_previous = current
while(runner)
if runner.value == current.value
def rotate_matrix(a, length)
(0).upto(length -1) do |i|
(i+1).upto(length -1) do |j|
a[i][j], a[j][i] = a[j][i], a[i][j]
end
end
a
end
def compress_length(a)
if a.length < 3
return a
end
result = ""
count = 1
a.chars.each_with_index do |c, index|
if c == a[index+1]
@imouaddine
imouaddine / gist:c870be92c30a80535aff
Created November 8, 2014 16:15
Merge two linked list
class LinkedList
attr_accessor :head, :tail
class Node
attr_accessor :previous, :next, :value
def initialize(value)
@value = value
end
end