Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addie's List Implementations #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion lib/array_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,76 @@

class ArrayList
def initialize
@storage = []
#pretend @storage is a native array: fixed size, indexing only (no dot methods)
#so we have the capacity to store 10 values but size should be zero right now
#fixed size = fixed capacity. size is how much is in there at the time
@storage = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
@size = 0
end

# Adds _value_ at the end of the list
def add(value)
#raise some error if @size == @storage.length
@storage[@size] = value
@size += 1
end

def add_in_order(value)
@size.downto(0) do |i|
if i == 0 || @storage[i-1] <= value
@storage[i] = value
break
else
@storage[i] = @storage[i-1]
end
end
@size += 1
end

# Deletes the _last_ value in the array
def delete
# raise some error if @size == 0
# @storage[@size] = 0 #probs don't need this
@size -= 1
end

def include?(key)
@size.times do |i|
return true if @storage[i] == key
end
return false
end

def size
return @size
end

def max
#raise some error if @size == 0
max = @storage[0]
@size.times do |i|
max = @storage[i] if @storage[i] > max
end
return max
end

def remove_all(num)
shift_count = 0
@size.times do |i|
if @storage[i] == num
shift_count += 1
else
@storage[i - shift_count] = @storage[i]
end
end
@size -= shift_count
end

def to_s
str = ""
@size.times do |i|
str += @storage[i].to_s + ", "
end
return "[#{str[0..-3]}]"
end
end
38 changes: 38 additions & 0 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ def add(value)
self
end

def add_in_order(value)
if @size == 0
@head = Node.new(value)
elsif value < @head.data
@head = Node.new(value, @head)
else
current = @head
loop do
if current.next == nil
current.next = Node.new(value)
break
elsif current.next.data >= value
current.next = Node.new(value, current.next)
break
end
current = current.next
end
end
@size += 1
end

def delete(val)
return nil if @size == 0
if @head.data == val
Expand All @@ -57,12 +78,29 @@ def delete(val)


def include?(key)
current = @head
while current != nil
return true if current.data == key
current = current.next
end
return false
end

def size
return @size
end

def max
return nil if @size == 0
current = @head
max = current.data
while current != nil
if current.data > max
max = current.data
end
current = current.next
end
return max
end

def to_s
Expand Down
2 changes: 1 addition & 1 deletion lib/lotto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize
while @ticket.size < 5
auto_num = rand(55) + 1
if [email protected]?(auto_num)
@ticket.add(auto_num)
@ticket.add_in_order(auto_num)
end
end
end
Expand Down