Data Structures Exercises

Fill in each of the method bodies. Instructions for each method are in the comments. Do not modify any code at the bottom of the file (it's used for testing).

All the problems from medium on are as difficult as real App Academy admission problems, so don't be discouraged!

Test your code by running it line-by-line in the shell.

Click run to test your code against the specs: successes and errors will appear in the shell. Do your best to correct any failing code before reviewing the solutions.

When you're done, check out the solutions and video walkthrough. Happy solving!

# EASY

# Write a method that returns a boolean indicating whether an array is in sorted order. Use the equality operator (==), which returns a boolean indicating whether its operands are equal, e.g., 2 == 2 => true, ["cat", "dog"] == ["dog", "cat"] => false
def in_order?(arr)
  # your code goes here
end


# MEDIUM

# Write a method that returns the range of its argument (an array of integers).
def range(arr)
  # your code goes here
end


# HARD

# Write a method that returns an array of the digits of a non-negative integer in descending order and as strings, e.g., descending_digits(4291) #=> ["9", "4", "2", "1"]
def descending_digits(int)
  # your code goes here
end

# Write a method that converts an array of ten integers into a phone number in the format "(123) 456-7890".
def to_phone_number(arr)
  # your code goes here
end

# Write a method that returns the range of a string of comma-separated integers, e.g., str_range("4,1,8") #=> 7
def str_range(str)
  # your code goes here
end


# DO NOT MODIFY CODE BELOW

$success_count = 0
$failure_count = 0

def deep_dup(arr)
  arr.inject([]) { |acc, el| el.is_a?(Array) ? acc << deep_dup(el) : acc << el }
end

def note_success(returned, invocation, expectation)
  puts "success: #{invocation} => #{expectation}"
  $success_count += 1
end

def note_failure(returned, invocation, expectation)
  puts "failure: #{invocation}: expected #{expectation}, returned #{returned}"
  $failure_count += 1
end

def format_args(args)
  o_args = deep_dup(args)
  o_args.map! do |arg|
    arg = prettify(arg)
    arg.class == Array ? arg.to_s : arg  
  end
  o_args.join(', ')
end

def prettify(statement)
  case statement
  when Float
    statement.round(5)
  when String
    "\"#{statement}\""
  when NilClass
    "nil"
  else
    statement
  end
end

def equality_test(returned, invocation, expectation)
  if returned == expectation && returned.class == expectation.class
    note_success(returned, invocation, expectation)
  else
    note_failure(returned, invocation, expectation)
  end
end

def identity_test(returned, invocation, expectation, args)
  if returned.__id__ == args[0].__id__
    equality_test(returned, invocation, expectation)
  else
    puts "failure: #{invocation}: You did not mutate the original array!"
    $failure_count += 1
  end
end

def method_missing(method_name, *args)
  method_name = method_name.to_s
  expectation = args[-1]
  args = args[0...-1]
  if method_name.start_with?("test_")
    tested_method = method_name[5..-1]
    print_test(tested_method, args, expectation)
  else
    method_name = method_name.to_sym
    super
  end
end

def print_test(method_name, args, expectation)
  returned = self.send(method_name, *args)
  returned = prettify(returned)
  expectation = prettify(expectation)
  args_statement = format_args(args)
  invocation = "#{method_name}(#{args_statement})"
  method_name.include?("!") ? identity_test(returned, invocation, expectation, args) : equality_test(returned, invocation, expectation)
  rescue Exception => e
    puts "failure: #{invocation} threw #{e}"
    puts e.backtrace.select {|t| !t.include?("method_missing") && !t.include?("print_test")}
    $failure_count += 1
end

puts "
in_order?:
" + "*" * 15 + "
"
test_in_order?(["a", "z", "c"], false)
test_in_order?([0, 1, 2], true)
puts "
range:
" + "*" * 15 + "
"
test_range([-1, 5, 0], 6)
test_range([0, 0], 0)
puts "
descending_digits:
" + "*" * 15 + "
"
test_descending_digits(4291, ["9", "4", "2", "1"])
test_descending_digits(0, ["0"])
puts "
to_phone_number:
" + "*" * 15 + "
"
test_to_phone_number([1,2,3,4,5,6,7,8,9,0], "(123) 456-7890")
puts "
str_range:
" + "*" * 15 + "
"
test_str_range("4,1,8", 7)
test_str_range("0,0", 0)
puts "
"
puts "TOTAL CORRECT: #{$success_count} / #{$success_count + $failure_count}"
puts "TOTAL FAILURES: #{$failure_count}"
$success_count = 0
$failure_count = 0

Try running the code. (Click the run button.)

results matching ""

    No results matching ""