Skip to content

Instantly share code, notes, and snippets.

@ssciolist
Last active July 13, 2018 17:00
Show Gist options
  • Save ssciolist/14866be9ed2e2304654f60c4f55aa7d7 to your computer and use it in GitHub Desktop.
Save ssciolist/14866be9ed2e2304654f60c4f55aa7d7 to your computer and use it in GitHub Desktop.
Arellano-tech-challenge-w3
class ListNode
  attr_accessor :val, :next
  def initialize(val)
    @val = val
    @next = nil
  end

  def find_the_length_of_the_list(list)
    current_node = list.value
    @length = 1
    while current_node.next != nil
      current_node = current_node.next
      @length += 1
    end
  end

  def remove_nth_from_end(head, n)
    find_the_length_of_the_list(head)

    current_node = head

    last_node = (@length-1).times do
      next_node = current_node.next
    end

    node_before_removal_node = (@length - n).times do
      current_node = current_node.next
    end

    return current_node.next
    node_before_removal_node.next = last_node
  end
end

class Search
  def return_matching_neighbors(nums, index, target)
    if nums[index+1] == target
      return [index, index +1]
    elsif nums[index-1] == target
      return [index -1, index]
    else
      return [index]
    end
  end

  def midpoint(array)
    if array.length.even?
      (array.length/2) + 1
    else
      (array.length/2).ceil
    end
  end

  def search_range(nums, target)
    if midpoint(nums) == target
      return_matching_neighbors(nums, (midpoint(nums)), target )
    elsif midpoint(nums) > target
      search_range(nums[(midpoint(nums)+1), -1], target)
    elsif midpoint(nums) < target
      search_range(nums[0, (midpoint(nums)-1)], target)
    else
      return [-1, -1]
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment