Created
November 14, 2012 21:56
-
-
Save arjunvenkat/4075116 to your computer and use it in GitHub Desktop.
ruby challenge wk 7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Gene Sequencing | |
# Given an input array and a test array, create a method that takes in | |
# two arrays as inputs, named 'input' and 'test,' respectively. | |
# 'input' is an array of any number of letters | |
# 'test' is an array of 4 letters | |
# The method returns 'match!' if the 'test' array is found inside the | |
# 'input' array and returns 'no match =(' if the 'test' array is not found | |
# Bonus: Try to make the script work even if your test sequence is reversed | |
# Example) | |
# input = ['G', 'A', 'T', 'T', 'A', 'C', 'A'] | |
# test = ['G', 'A', 'T', 'T'] | |
# the method should return 'match!' | |
# Example 2) | |
# input = ['G', 'A', 'T', 'T', 'A', 'C', 'A'] | |
# test = ['A', 'A', 'A', 'A'] | |
# the method should return 'no match =(' | |
# Example 3) | |
# input = ['G', 'A', 'T', 'T', 'A', 'C', 'A'] | |
# test = ['G', 'A', 'C', 'A'] | |
# the method should return 'no match =(' | |
# Bonus Example) | |
# input = ['G', 'A', 'T', 'T', 'A', 'C', 'A'] | |
# test = ['T', 'T', 'A', 'G'] | |
# the method should return 'match!' | |
# here's some code to get you started: | |
# input = ['G', 'A', 'T', 'T', 'A', 'C', 'A'] | |
# test = ['G', 'A', 'T', 'T'] | |
# def gene_test(input_array, test_array) | |
# <your code goes here> | |
# end | |
# gene_test input, test | |
# SOLUTIONS: | |
# My lazy, brute-force solution | |
# | |
# def gene_test(input_array, test_array) | |
# # test_array = test | |
# result = 'no match =(' | |
# input_array.count.times do |index| | |
# if index < (input_array.count - test_array.count + 1) | |
# if input_array[index] == test_array[0] && input_array[index + 1] == test_array[1] && input_array[index + 2] == test_array[2] && input_array[index + 3] == test_array[3] | |
# result = 'match!' | |
# elsif input_array[index] == test_array[3] && input_array[index + 1] == test_array[2] && input_array[index + 2] == test_array[1] && input_array[index + 3] == test_array[0] | |
# result = 'match!' | |
# end | |
# end | |
# end | |
# puts result | |
# end | |
# A much more elegant, Ruby-ish solution, inspired by Raghu | |
# | |
# def gene_test(input_array, test_array) | |
# if input_array.join.include?(test_array.join) || input_array.join.include?(test_array.reverse.join) | |
# puts "match!" | |
# else | |
# puts "no match =(" | |
# end | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment