Skip to content

Instantly share code, notes, and snippets.

@s2t2
Last active April 16, 2016 04:58
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 s2t2/66bbcfad1a1a94693a18f5925bd2ef7c to your computer and use it in GitHub Desktop.
Save s2t2/66bbcfad1a1a94693a18f5925bd2ef7c to your computer and use it in GitHub Desktop.
an exercise

Problem Solving Exercises

In the programming language of your choice, write a method that modifies a string using the following rules:

  1. Each word in the input string is replaced with the following: the first letter of the word, the count of distinct letters between the first and last letter, and the last letter of the word. For example, "Automotive" would be replaced by "A6e".

  2. A "word" is defined as a sequence of alphabetic characters, delimited by any non-alphabetic characters.

  3. Any non-alphabetic character in the input string should appear in the output string in its original relative location.

Installation

Prerequisites: install ruby and bundler.

git clone git@gist.github.com:66bbcfad1a1a94693a18f5925bd2ef7c.git
cd 66bbcfad1a1a94693a18f5925bd2ef7c
bundle install

Test

bundle exec rspec test.rb
# A sample Gemfile
source "https://rubygems.org"
# gem "rails"
gem "rspec"
gem "pry"
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.0)
diff-lcs (1.2.5)
method_source (0.8.2)
pry (0.10.3)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rspec (3.4.0)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-core (3.4.4)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-mocks (3.4.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-support (3.4.1)
slop (3.6.0)
PLATFORMS
ruby
DEPENDENCIES
pry
rspec
BUNDLED WITH
1.10.5
require "pp"
require "pry"
# @example "Automotive Repair 90000"
def alphabetic_sequences(input)
#binding.pry if word.include?("90000")
#words = input.split(/\W+/) #> ["Automotive", "Repair", "90000"]
words = input.scan(/[a-zA-Z]+/) #> ["Automotive","Repair"]
end
def transform(word)
first = word.chars.first
middle = word.chop.reverse.chop.reverse
last = word.chars.last
return "#{first}#{middle.chars.uniq.count}#{last}"
end
def my_method(input)
raise "PLEASE PASS A STRING" unless input.is_a?(String)
words = alphabetic_sequences(input)
pp words
words.each do |word|
new_word = transform(word)
input = input.gsub!(word,new_word)
end
return input
end
require "rspec"
require "./lib"
=begin
1. Each word in the input string is replaced with the following: the first letter of the word, the count of distinct letters between the first and last letter, and the last letter of the word.
2. A "word" is defined as a sequence of alphabetic characters, delimited by any non-alphabetic characters.
3. Any non-alphabetic character in the input string should appear in the output string in its original relative location.
=end
RSpec.describe "#my_method" do
context "when input is a single-word string" do
it "works" do
result = my_method("Automotive")
expect(result).to eql("A6e")
end
end
context "when input is a multi-word string" do
it "works" do
result = my_method("Automotive Repair")
expect(result).to eql("A6e R4r")
end
end
context "when input contains numeric non-alphabetics" do
let(:input){"Automotive Repair 90000"}
it "gets the right words" do # A "word" is defined as a sequence of alphabetic characters, delimited by any non-alphabetic characters.
words = alphabetic_sequences(input)
expect(words).to eql(["Automotive","Repair"])
end
it "works" do
result = my_method(input)
expect(result).to eql("A6e R4r 90000")
end
end
context "when input contains intra-word non-alphabetics" do
it "works with numbers" do
result = my_method("Automotive Champ1on Repair 90000")
expect(result).to eql("A6e C3p1o0n R4r 90000")
end
it "works with hyphens" do
result = my_method("Automotive Five-time Champ1on Repair 90000")
expect(result).to eql("A6e F2e-t2e C3p1o0n R4r 90000")
end
end
context "when input is edge-case" do
it "works" do
result = my_method("Automotive 5-time Champ1on Repair 90000")
expect(result).to eql("A6e 5-t2e C3p1o0n R4r 90000")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment