Skip to content

Instantly share code, notes, and snippets.

@ajesler
Last active July 5, 2024 07:11
Show Gist options
  • Select an option

  • Save ajesler/5c06c3bc5a9856f55472c617403a83d8 to your computer and use it in GitHub Desktop.

Select an option

Save ajesler/5c06c3bc5a9856f55472c617403a83d8 to your computer and use it in GitHub Desktop.
Dev Share Problem
Take https://gist.github.com/ajesler/5c06c3bc5a9856f55472c617403a83d8 and solve the problem. Have a little fun with the solution, see what inefficient, ridiculous solutions you can come up with, push the bounds of your ruby knowledge a little. What constraints can you apply on how you solve it?
Full problem description at https://www.hackerrank.com/challenges/counting-valleys/problem?isFullScreen=true
Ideas: Can you do it in a single enumerable chain, can you write a program that generates the solver with meta programming, can you do it without using the letter e.
30m of problem solving in pairs, then 3m each to discuss what you tried to do, and what the highlights of your solution, working or otherwise. Don't worry if it doesn't work, its more about the journey and what you can learn.
# frozen_string_literal: true
# This is the Counting Valleys problem from hackerrank.com
# https://www.hackerrank.com/challenges/counting-valleys/problem?isFullScreen=true
# Run the solver with the input after __END__
# $ ruby counting_valleys.rb
#
# Run the solver using a file as input
# $ ruby counting_valleys.rb test_input.txt
#
# Run solver specs with
# $ ruby counting_valleys.rb --specs
require 'bundler/inline'
# Uncomment this block to install byebug
# gemfile true do
# source 'https://rubygems.org'
# gem 'test-unit'
# # Useful for debugging
# gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
# gem 'pry-byebug'
# end
require 'timeout'
require 'test/unit'
Test::Unit::AutoRunner.need_auto_run = false
class ValleyCounter
def initialize(steps, path)
@steps = steps
@path = path
end
def count
1
end
end
class ValleyCounterSpec < Test::Unit::TestCase
def expect_valley_count(expected_count, steps:, path:)
valley_count = ValleyCounter.new(steps, path).count
assert_equal expected_count, valley_count
end
def test_one
expect_valley_count(1, steps: 8, path: 'UDDDUDUU')
end
def test_two
expect_valley_count(2, steps: 12, path: 'DDUUDDUDUUUD')
end
def test_three
steps = 100
path = 'DDUUDDDUDUUDUDDDUUDDUDDDUDDDUDUUDDUUDDDUDDDUDDDUUUDUDDDUDUDUDUUDDUDUDUDUDUUUUDDUDDUUDUUDUUDUUUUUUUUU'
expect_valley_count(2, steps: steps, path: path)
end
end
if $PROGRAM_NAME == __FILE__
first_argument = ARGV.shift
if first_argument == '--specs'
# Run the specs
Test::Unit::AutoRunner.need_auto_run = true
else
data_source = first_argument.nil? ? DATA.read : File.read(first_argument)
raw_steps, raw_path = data_source.split(/\r?\n/, 2)
steps = raw_steps.to_i
path = raw_path.strip
if steps != path.delete('_').length
raise("path mismatch! Expected path of #{steps}, got #{path} which is #{path.delete('_').length}")
end
valley_counter = ValleyCounter.new(steps, path)
puts valley_counter.count
end
end
__END__
8
UDDDUDUU
This file has been truncated, but you can view the full file.
1000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment