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
| class Activity | |
| attr_accessor :s, :f | |
| def initialize(s,f) | |
| @s = s | |
| @f = f | |
| end | |
| def inspect | |
| "#{s} to #{f}" | |
| end | |
| 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 = [ | |
| [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 |
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 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) |
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 find_tree_sum(start, sum) | |
| $last_sum ||= 0 | |
| $result ||= [] | |
| if start.nil? | |
| return false | |
| end | |
| $last_sum += start.value | |
| $result << start.value |
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 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 |
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
| class LinkedList | |
| attr_accessor :head, :tail | |
| class Node | |
| attr_accessor :previous, :next, :value | |
| def initialize(value) | |
| @value = value | |
| end | |
| 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
| 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 |
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 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 |
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 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] |
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
| class LinkedList | |
| attr_accessor :head, :tail | |
| class Node | |
| attr_accessor :previous, :next, :value | |
| def initialize(value) | |
| @value = value | |
| end | |
| end |