some more ruby...
This commit is contained in:
parent
0420a79060
commit
ec37071482
|
@ -1,13 +1,7 @@
|
|||
class PhoneNumber
|
||||
def self.clean(num)
|
||||
cl = num.scan(/\d/).join
|
||||
invalid?(num) ? nil : cl
|
||||
end
|
||||
|
||||
def self.invalid?(num)
|
||||
if num.start_with?("1")
|
||||
num.size == 11
|
||||
end
|
||||
n = num.scan(/\d/).join.delete_prefix('1')
|
||||
n.size == 10 && !n[3].match?(/[01]/) ? n : nil
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
# Prime Factors
|
||||
|
||||
Compute the prime factors of a given natural number.
|
||||
|
||||
A prime number is only evenly divisible by itself and 1.
|
||||
|
||||
Note that 1 is not a prime number.
|
||||
|
||||
## Example
|
||||
|
||||
What are the prime factors of 60?
|
||||
|
||||
- Our first divisor is 2. 2 goes into 60, leaving 30.
|
||||
- 2 goes into 30, leaving 15.
|
||||
- 2 doesn't go cleanly into 15. So let's move on to our next divisor, 3.
|
||||
- 3 goes cleanly into 15, leaving 5.
|
||||
- 3 does not go cleanly into 5. The next possible factor is 4.
|
||||
- 4 does not go cleanly into 5. The next possible factor is 5.
|
||||
- 5 does go cleanly into 5.
|
||||
- We're left only with 1, so now, we're done.
|
||||
|
||||
Our successful divisors in that computation represent the list of prime
|
||||
factors of 60: 2, 2, 3, and 5.
|
||||
|
||||
You can check this yourself:
|
||||
|
||||
- 2 * 2 * 3 * 5
|
||||
- = 4 * 15
|
||||
- = 60
|
||||
- Success!
|
||||
|
||||
* * * *
|
||||
|
||||
For installation and learning resources, refer to the
|
||||
[exercism help page](http://exercism.io/languages/ruby).
|
||||
|
||||
For running the tests provided, you will need the Minitest gem. Open a
|
||||
terminal window and run the following command to install minitest:
|
||||
|
||||
gem install minitest
|
||||
|
||||
If you would like color output, you can `require 'minitest/pride'` in
|
||||
the test file, or note the alternative instruction, below, for running
|
||||
the test file.
|
||||
|
||||
Run the tests from the exercise directory using the following command:
|
||||
|
||||
ruby prime_factors_test.rb
|
||||
|
||||
To include color from the command line:
|
||||
|
||||
ruby -r minitest/pride prime_factors_test.rb
|
||||
|
||||
|
||||
## Source
|
||||
|
||||
The Prime Factors Kata by Uncle Bob [http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata](http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata)
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
|
@ -0,0 +1,15 @@
|
|||
class PrimeFactors
|
||||
def self.for(i)
|
||||
r = []
|
||||
d = 2
|
||||
while i > 1
|
||||
if i % d == 0
|
||||
i /= d
|
||||
r << d
|
||||
else
|
||||
d += 1
|
||||
end
|
||||
end
|
||||
r
|
||||
end
|
||||
end
|
|
@ -0,0 +1,48 @@
|
|||
require 'minitest/autorun'
|
||||
require_relative 'prime_factors'
|
||||
|
||||
class PrimeFactorsTest < Minitest::Test
|
||||
def test_1
|
||||
assert_equal [], PrimeFactors.for(1)
|
||||
end
|
||||
|
||||
def test_2
|
||||
assert_equal [2], PrimeFactors.for(2)
|
||||
end
|
||||
|
||||
def test_3
|
||||
assert_equal [3], PrimeFactors.for(3)
|
||||
end
|
||||
|
||||
def test_4
|
||||
assert_equal [2, 2], PrimeFactors.for(4)
|
||||
end
|
||||
|
||||
def test_6
|
||||
assert_equal [2, 3], PrimeFactors.for(6)
|
||||
end
|
||||
|
||||
def test_8
|
||||
assert_equal [2, 2, 2], PrimeFactors.for(8)
|
||||
end
|
||||
|
||||
def test_9
|
||||
assert_equal [3, 3], PrimeFactors.for(9)
|
||||
end
|
||||
|
||||
def test_27
|
||||
assert_equal [3, 3, 3], PrimeFactors.for(27)
|
||||
end
|
||||
|
||||
def test_625
|
||||
assert_equal [5, 5, 5, 5], PrimeFactors.for(625)
|
||||
end
|
||||
|
||||
def test_901255
|
||||
assert_equal [5, 17, 23, 461], PrimeFactors.for(901_255)
|
||||
end
|
||||
|
||||
def test_93819012551
|
||||
assert_equal [11, 9539, 894_119], PrimeFactors.for(93_819_012_551)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,64 @@
|
|||
# Strain
|
||||
|
||||
Implement the `keep` and `discard` operation on collections. Given a collection
|
||||
and a predicate on the collection's elements, `keep` returns a new collection
|
||||
containing those elements where the predicate is true, while `discard` returns
|
||||
a new collection containing those elements where the predicate is false.
|
||||
|
||||
For example, given the collection of numbers:
|
||||
|
||||
- 1, 2, 3, 4, 5
|
||||
|
||||
And the predicate:
|
||||
|
||||
- is the number even?
|
||||
|
||||
Then your keep operation should produce:
|
||||
|
||||
- 2, 4
|
||||
|
||||
While your discard operation should produce:
|
||||
|
||||
- 1, 3, 5
|
||||
|
||||
Note that the union of keep and discard is all the elements.
|
||||
|
||||
The functions may be called `keep` and `discard`, or they may need different
|
||||
names in order to not clash with existing functions or concepts in your
|
||||
language.
|
||||
|
||||
## Restrictions
|
||||
|
||||
Keep your hands off that filter/reject/whatchamacallit functionality
|
||||
provided by your standard library! Solve this one yourself using other
|
||||
basic tools instead.
|
||||
|
||||
* * * *
|
||||
|
||||
For installation and learning resources, refer to the
|
||||
[exercism help page](http://exercism.io/languages/ruby).
|
||||
|
||||
For running the tests provided, you will need the Minitest gem. Open a
|
||||
terminal window and run the following command to install minitest:
|
||||
|
||||
gem install minitest
|
||||
|
||||
If you would like color output, you can `require 'minitest/pride'` in
|
||||
the test file, or note the alternative instruction, below, for running
|
||||
the test file.
|
||||
|
||||
Run the tests from the exercise directory using the following command:
|
||||
|
||||
ruby strain_test.rb
|
||||
|
||||
To include color from the command line:
|
||||
|
||||
ruby -r minitest/pride strain_test.rb
|
||||
|
||||
|
||||
## Source
|
||||
|
||||
Conversation with James Edward Gray II [https://twitter.com/jeg2](https://twitter.com/jeg2)
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
|
@ -0,0 +1,87 @@
|
|||
require 'minitest/autorun'
|
||||
require_relative 'strain'
|
||||
|
||||
class ArrayTest < Minitest::Test
|
||||
def test_empty_keep
|
||||
assert_equal [], [].keep { |e| e < 10 }
|
||||
end
|
||||
|
||||
def test_keep_everything
|
||||
skip
|
||||
assert_equal [1, 2, 3], [1, 2, 3].keep { |e| e < 10 }
|
||||
end
|
||||
|
||||
def test_keep_first_and_last
|
||||
skip
|
||||
assert_equal [1, 3], [1, 2, 3].keep(&:odd?)
|
||||
end
|
||||
|
||||
def test_keep_neither_first_nor_last
|
||||
skip
|
||||
assert_equal [2, 4], [1, 2, 3, 4, 5].keep(&:even?)
|
||||
end
|
||||
|
||||
def test_keep_strings
|
||||
skip
|
||||
words = %w(apple zebra banana zombies cherimoya zelot)
|
||||
result = words.keep { |word| word.start_with?('z') }
|
||||
assert_equal %w(zebra zombies zelot), result
|
||||
end
|
||||
|
||||
def test_keep_arrays
|
||||
skip
|
||||
rows = [
|
||||
[1, 2, 3],
|
||||
[5, 5, 5],
|
||||
[5, 1, 2],
|
||||
[2, 1, 2],
|
||||
[1, 5, 2],
|
||||
[2, 2, 1],
|
||||
[1, 2, 5]
|
||||
]
|
||||
result = rows.keep { |row| row.include?(5) }
|
||||
assert_equal [[5, 5, 5], [5, 1, 2], [1, 5, 2], [1, 2, 5]], result
|
||||
end
|
||||
|
||||
def test_empty_discard
|
||||
skip
|
||||
assert_equal [], [].discard { |e| e < 10 }
|
||||
end
|
||||
|
||||
def test_discard_nothing
|
||||
skip
|
||||
assert_equal [1, 2, 3], [1, 2, 3].discard { |e| e > 10 }
|
||||
end
|
||||
|
||||
def test_discard_first_and_last
|
||||
skip
|
||||
assert_equal [2], [1, 2, 3].discard(&:odd?)
|
||||
end
|
||||
|
||||
def test_discard_neither_first_nor_last
|
||||
skip
|
||||
assert_equal [1, 3, 5], [1, 2, 3, 4, 5].discard(&:even?)
|
||||
end
|
||||
|
||||
def test_discard_strings
|
||||
skip
|
||||
words = %w(apple zebra banana zombies cherimoya zelot)
|
||||
result = words.discard { |word| word.start_with?('z') }
|
||||
assert_equal %w(apple banana cherimoya), result
|
||||
end
|
||||
|
||||
def test_discard_arrays
|
||||
skip
|
||||
rows = [
|
||||
[1, 2, 3],
|
||||
[5, 5, 5],
|
||||
[5, 1, 2],
|
||||
[2, 1, 2],
|
||||
[1, 5, 2],
|
||||
[2, 2, 1],
|
||||
[1, 2, 5]
|
||||
]
|
||||
result = rows.discard { |row| row.include?(5) }
|
||||
assert_equal [[1, 2, 3], [2, 1, 2], [2, 2, 1]], result
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue