Skip to content

Instantly share code, notes, and snippets.

@charany1
Last active November 3, 2015 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charany1/f761c0f92dc894fa8fe8 to your computer and use it in GitHub Desktop.
Save charany1/f761c0f92dc894fa8fe8 to your computer and use it in GitHub Desktop.
charany1@esaas_course:~/workspace/hw-ruby-intro (master) $ rspec spec/part1_spec.rb
/usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': /home/ubuntu/workspace/hw-ruby-intro/lib/ruby_intro.rb:47: syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError)
from /usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/ubuntu/workspace/hw-ruby-intro/spec/part1_spec.rb:1:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `load'
from /usr/local/rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
from /usr/local/rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
from /usr/local/rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
from /usr/local/rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
from /usr/local/rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
from /usr/local/rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
from /usr/local/rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
from /usr/local/rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.2.2/bin/rspec:23:in `load'
from /usr/local/rvm/gems/ruby-2.2.2/bin/rspec:23:in `<main>'
from /usr/local/rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval'
from /usr/local/rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>'
charany1@esaas_course:~/workspace/hw-ruby-intro (master) $
# When done, submit this entire file to the autograder.
# Part 1
def sum arr
# YOUR CODE HERE
answer = 0
for i in 0...arr.size do
answer += arr[i]
end
answer
end
def max_2_sum arr
# YOUR CODE HERE
sorted_arr = arr.sort
num_elements = arr.size
if num_elements == 0
return 0
else if num_elements == 1
return sorted_arr[0]
else
return sorted_arr[num_elements-1]+sorted_arr[num_elements-2]
end
end
def sum_to_n? arr, n
# YOUR CODE HERE
if arr.size == 0
return false
end
sorted_arr = arr.sort
left_ptr = 0
right_ptr = arr.size - 1
while left_ptr<right_ptr do
if (sorted_arr[left_ptr]+sorted_arr[right_ptr]) == n
return true
else if (sorted_arr[left_ptr]+sorted_arr[right_ptr]) > n
right_ptr -= 1
else
left_ptr += 1
end
end
return false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment