pair_with_given_sum 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/pair_with_given_sum +5 -0
- data/lib/pair_with_given_sum.rb +28 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d1225f9b9cf166da49687ee75a6124a3b307b435
|
4
|
+
data.tar.gz: 705203a07557ae3772152a5658ee2757899fdefd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f1140cab5d3878079d799ced290e506c7190ed1381cb4100d0e1b3835a9964bb5c5c78b26f06e1d84d7e8ec067424829a93e3a80d4755a44849d1b92c45fbe66
|
7
|
+
data.tar.gz: 271f223136a8cde5a51744404e91a31abee8fc48012a80d0c9e12ab62dcbc3c884cfb794739a5ce8a67fc6973aa0399e220571b83d20ae487cb97c6148f5103b
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module PairWithGivenSum
|
2
|
+
def PairWithGivenSum.run(str, sum)
|
3
|
+
if str.nil? || str.length <=2 || sum.nil?
|
4
|
+
puts "Must be given a string representing an integer array and an integer sum."
|
5
|
+
else
|
6
|
+
array = PairWithGivenSum.parseArray(str)
|
7
|
+
array.sort!
|
8
|
+
smallIndex = 0
|
9
|
+
largeIndex = array.length - 1
|
10
|
+
while largeIndex > smallIndex
|
11
|
+
tmpSum = array[largeIndex] + array[smallIndex]
|
12
|
+
if tmpSum == sum
|
13
|
+
puts "Sum of #{array[smallIndex]} & #{array[largeIndex]} = #{sum}"
|
14
|
+
return
|
15
|
+
elsif tmpSum > sum
|
16
|
+
largeIndex -= 1
|
17
|
+
else
|
18
|
+
smallIndex += 1
|
19
|
+
end
|
20
|
+
end
|
21
|
+
puts "No pair with sum of #{sum}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def PairWithGivenSum.parseArray(arr)
|
26
|
+
return arr.scan(/\d+/).map(&:to_i)
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pair_with_given_sum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kenneth Baldauf
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-10 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Given a string representing an integer array and a sum, return a pair
|
14
|
+
that matches the sum.
|
15
|
+
email:
|
16
|
+
executables:
|
17
|
+
- pair_with_given_sum
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- bin/pair_with_given_sum
|
22
|
+
- lib/pair_with_given_sum.rb
|
23
|
+
homepage: https://github.com/kbaldauf/PairWithGivenSum
|
24
|
+
licenses:
|
25
|
+
- Apache v2.0
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 2.4.6
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: Finds a pair with a given sum.
|
47
|
+
test_files: []
|