Skip to content

Commit c824d53

Browse files
committed
Updated project
1 parent f190be9 commit c824d53

32 files changed

+522
-1
lines changed

Chap_1_Arrays_and_Strings/.DS_Store

6 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def is_unique(string)
2+
unique = ""
3+
string.chars do |char|
4+
if unique.include?(char) == false
5+
unique << char
6+
end
7+
end
8+
string.length == unique.length ? true : false
9+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def check_permutation(s1="", s2="")
2+
if s1 == s2
3+
true
4+
else
5+
if s1.split("").sort == s2.split("").sort
6+
true
7+
else
8+
false
9+
end
10+
end
11+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def urlify(string)
2+
if string.include?(' ') == true
3+
string.gsub!(' ', '%20')
4+
else
5+
string
6+
end
7+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def one_array(s1, s2)
2+
if s1 == s2
3+
true
4+
else
5+
if s1.length > s2.length + 1 || s1.length < s2.length - 1
6+
false
7+
else
8+
count = 0
9+
s2.chars.each do |char|
10+
if s1.include?(char) != true
11+
count += 1
12+
end
13+
end
14+
15+
if count > 1
16+
false
17+
else
18+
true
19+
end
20+
21+
end
22+
end
23+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def string_compression(string)
2+
container = [
3+
[]
4+
]
5+
word = string.split('')
6+
7+
word.each do |letter|
8+
if container[-1] == []
9+
container[-1] << letter
10+
elsif container[-1][0] == letter
11+
container[-1] << letter
12+
else
13+
container << Array.new
14+
container[-1] << letter
15+
end
16+
end
17+
compressed = ""
18+
container.length.times do |count|
19+
compressed = compressed + container[count][0]+container[count].length.to_s
20+
end
21+
22+
if string.length <= compressed.length
23+
string
24+
else
25+
compressed
26+
end
27+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def rotate_matrix(array)
2+
array_flipped = []
3+
array.transpose.each do |row|
4+
array_flipped << row.reverse
5+
end
6+
array_flipped
7+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def zero_matrix(matrix)
2+
zero = nil
3+
matrix.length.times do |row|
4+
if matrix[row].include?(0)
5+
matrix[row].each_with_index do |zeros, idx|
6+
if zeros == 0
7+
zero = idx
8+
else
9+
matrix[row][idx] = 0
10+
end
11+
end
12+
end
13+
end
14+
matrix = matrix.transpose
15+
matrix.length.times do |row|
16+
if zero == row
17+
matrix[row].each_with_index do |zeros, idx|
18+
matrix[row][idx] = 0
19+
end
20+
end
21+
end
22+
matrix = matrix.transpose
23+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def string_rotation(s1, s2)
2+
matching = false
3+
if s1.length == s2.length
4+
count = 0
5+
until count == s1.length
6+
splitted = s1.split('')
7+
s1 = splitted.push(splitted.shift).join
8+
if s1 == s2
9+
matching = true
10+
end
11+
count += 1
12+
end
13+
else
14+
false
15+
end
16+
matching
17+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require_relative('../Is_Unique')
2+
3+
describe 'detects if characters are unique in a word' do
4+
let(:word1) {"jason"}
5+
let(:word2) {"banana"}
6+
let(:word3) {"cool"}
7+
let(:word4) {"cat"}
8+
9+
it 'Should return true, since nothing repeats' do
10+
expect(is_unique(word1)).to eq(true)
11+
end
12+
13+
it 'Should return true, since nothing repeats' do
14+
expect(is_unique(word4)).to eq(true)
15+
end
16+
17+
it 'Should return false, since there repeating characters' do
18+
expect(is_unique(word2)).to eq(false)
19+
end
20+
21+
it 'Should return false, since there repeating characters' do
22+
expect(is_unique(word3)).to eq(false)
23+
end
24+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require_relative('../Check_Permutation')
2+
3+
describe 'check if two words are permutations of each other' do
4+
let(:lemon) {'lemon'}
5+
let(:melon) {'melon'}
6+
let(:jason) {'jason'}
7+
let(:horse) {'horse'}
8+
9+
it 'should true for being a permutation' do
10+
expect(check_permutation(lemon, melon)).to eq(true)
11+
end
12+
13+
14+
it 'should true for being a permutation' do
15+
expect(check_permutation(melon, lemon)).to eq(true)
16+
end
17+
18+
19+
it 'should false for not being a permutation' do
20+
expect(check_permutation(jason, horse)).to eq(false)
21+
end
22+
23+
it 'can handle no input' do
24+
expect(check_permutation()).to eq(true)
25+
end
26+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require_relative('../URLify')
2+
3+
describe '' do
4+
let(:string){"Jason Loves TDD"}
5+
let(:url) {"Jason%20Loves%20TDD"}
6+
let(:name){"jason"}
7+
8+
9+
10+
it 'it should convert convert the string if it contains spaces' do
11+
expect(urlify(string)).to eq(url)
12+
end
13+
14+
it 'it should output the word if it does not contain spaces' do
15+
expect(urlify(name)).to eq(name)
16+
end
17+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require_relative('../One_Array')
2+
3+
describe 'Checks if two words are one edit away' do
4+
let(:pale) {'pale'}
5+
let(:ple) {'ple'}
6+
let(:cake) {'cake'}
7+
let(:bake) {'bake'}
8+
let(:cat) {'cat'}
9+
let(:dog) {'dog'}
10+
let(:cakess) {'cakess'}
11+
12+
it 'it will return true if 1 edit away' do
13+
expect(one_array(pale, ple)).to eq(true)
14+
end
15+
16+
it 'it will return true if 1 edit away' do
17+
expect(one_array(cake, bake)).to eq(true)
18+
end
19+
20+
it 'it will return false due to over 1 edit away' do
21+
expect(one_array(cat, dog)).to eq(false)
22+
end
23+
24+
it 'it will return false due to over 1 edit away' do
25+
expect(one_array(cake, cakess)).to eq(false)
26+
end
27+
28+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require_relative('../String_Compression')
2+
3+
describe 'string compression' do
4+
let(:aabcccccaaa){'aabcccccaaa'}
5+
let(:a2b1c5a3){'a2b1c5a3'}
6+
let(:abcc){'abcc'}
7+
8+
it 'it should compress the word down ' do
9+
expect(string_compression(aabcccccaaa)).to eq(a2b1c5a3)
10+
end
11+
12+
it 'it does not compress the word down that are smaller than compress version' do
13+
expect(string_compression(abcc)).to eq(abcc)
14+
end
15+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require_relative('../Rotate_Matrix')
2+
3+
describe 'turn an array 90 degrees to the right' do
4+
let(:portrait){[["a", "b", "c"],
5+
["d", "e", "f"],
6+
["g", "h", "i"]]}
7+
8+
let(:portrait_90cc){[["g", "d", "a"],
9+
["h", "e", "b"],
10+
["i", "f", "c"]]}
11+
12+
13+
it 'it will return a new array that has been rotated' do
14+
expect(rotate_matrix(portrait)).to eq(portrait_90cc)
15+
end
16+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require_relative('../Zero_Matrix')
2+
3+
describe 'it will zero out rows and col' do
4+
let(:matrix){[[1, 0, 3], [4, 5, 6], [7, 8, 9]]}
5+
let(:zeros){[[0, 0, 0], [4, 0, 6], [7, 0, 9]]}
6+
7+
it 'return a new matrix with 0s replacing the row and col that the 0 was in' do
8+
expect(zero_matrix(matrix)).to eq(zeros)
9+
end
10+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require_relative('../String_Rotation')
2+
3+
describe 'checks 2 strings if they are rotations of each other' do
4+
let(:waterbottle){'waterbottle'}
5+
let(:erbottlewat){'erbottlewat'}
6+
let(:jason){'jason'}
7+
let(:not_jason){'pizza&tacos'}
8+
let(:trees){'trees'}
9+
let(:fishs){'fishs'}
10+
11+
it 'it will return true if s1 is a rotation of s2' do
12+
expect(string_rotation(waterbottle, erbottlewat)).to be(true)
13+
end
14+
15+
it 'it will return false if s1 is not a rotation of s2' do
16+
expect(string_rotation(jason, not_jason)).to be(false)
17+
end
18+
19+
it 'it will return false if s1 is not a rotation of s2' do
20+
expect(string_rotation(trees, fishs)).to be(false)
21+
end
22+
end

Chap_2_Listed_Lists/.DS_Store

6 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Node
2+
attr_accessor :value, :next_node
3+
def initialize(value, next_node)
4+
@value = value
5+
@next_node = next_node
6+
end
7+
end
8+
9+
class LinkedList
10+
def self.delete_node(head_node, node_num)
11+
count = 0
12+
node = head_node
13+
until count == node_num
14+
previous = node
15+
node = head_node.next_node
16+
next_node = node.next_node
17+
count += 1
18+
end
19+
previous.next_node = next_node
20+
head_node
21+
end
22+
end
23+
24+
#create the linked list
25+
node4 = Node.new("D", nil)
26+
node3 = Node.new("C", node4)
27+
node2 = Node.new("B", node3)
28+
node1 = Node.new("A", node2)
29+
30+
#show orginal linked list
31+
p node1
32+
33+
#Show updated linked list after deleted middle node
34+
p delete = LinkedList.delete_node(node1, 1)
Binary file not shown.

Chap_2_Listed_Lists/linked_list_spec/Q2_03_Delete_Middle_Node_Spec.rb

Whitespace-only changes.

Chap_3_Stacks_and_Queues/.DS_Store

6 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def three_in_one(array)
2+
three_stacks = [
3+
num_stack = [],
4+
string_stack = [],
5+
other_stack = [],
6+
]
7+
until array.length == 0
8+
if array[0].class == String
9+
string_stack << array.shift
10+
elsif array[0].class == Fixnum || array[0].class == Float
11+
num_stack << array.shift
12+
else
13+
other_stack << array.shift
14+
end
15+
end
16+
three_stacks
17+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def higest_num_stack(stack)
2+
return stack[0] if stack.length == 1
3+
temp_queue = []
4+
smallest = stack.min
5+
6+
7+
until stack.length == 0
8+
temp_queue << stack.pop
9+
end
10+
11+
until smallest == temp_queue.last
12+
temp_queue.push(temp_queue.shift)
13+
end
14+
15+
16+
stack = temp_queue
17+
stack.pop
18+
19+
higest_num_stack(stack)
20+
end

0 commit comments

Comments
 (0)