ClosestSum 1.0.0-universal-linux
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README +9 -0
- data/lib/ClosestSum.rb +53 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 73a1a35c10fe80ea85a4dae42cb1072fabd2b8ef
|
4
|
+
data.tar.gz: 5f0d2000bd22f8291e3299fd145b0980a649bbb6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e73eb16348576b76b869adec3c287b2674d09044a462263c594719cf53849dbf464eeb7a536f906a12df76304512e56c97df99e0c47a84cd3effa177451bb91e
|
7
|
+
data.tar.gz: 731f0c1378e9a1df73fa62909117be1559531430d41660fb187c29ebf75574039377798211676d86646183f25f3f3495997a202b8daa72bb74214c847694ace8
|
data/README
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
Given a hash of keys and numeric values and another integer value (target size), this module will select the largest keys from the given hash such that the sum of the selected keys is less than equal to the target size, or the sum of the selected keys will be closest to the target size but not exceeding it.
|
2
|
+
|
3
|
+
Library takes in a hash with a string as keys and a number as it's value. This's the second argument. After computation, it outputs the keys which has been selected to reach the target sum (first argument).
|
4
|
+
Endpoint function -- ClosestSum.calculate. ClosestSum is a module.
|
5
|
+
Returns a hash of selected keys with their values.
|
6
|
+
Returns TypeError exception if wrong arguments are sent.
|
7
|
+
|
8
|
+
e.g. --
|
9
|
+
ClosestSum.calculate(998994, { sysadf: 8984, asdfiejf: 3434, asdokfjal: 89909 })
|
data/lib/ClosestSum.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
#! /usr/bin/ruby
|
2
|
+
# Library takes a hash of string keys and numeric values as the second argument and outputs the keys which has been selected to reach the target sum (first argument).
|
3
|
+
# Endpoint function -- calculate
|
4
|
+
# Returns a hash of selected keys.
|
5
|
+
# Returns TypeError exception if wrong arguments are sent
|
6
|
+
|
7
|
+
# Algorithm --
|
8
|
+
# 20) Sort the hash as per the largest to the smallest.
|
9
|
+
# 40) Check if adding the next entity (i.e. the 1st entity for the 1st run) to the current 'final list' will exceed the first argument. If so move to 60, otherwise 50.
|
10
|
+
# 50) Add the entity to final list and move to 40 if 'final list' total size != the first argument, otherwise exist program.
|
11
|
+
# 60) Skip to the next entity if it exists. If it does not, return final list and exit program.
|
12
|
+
|
13
|
+
# Variable list --
|
14
|
+
# flhash -- A hash with the key as the file name and value as the size of the entity.
|
15
|
+
#
|
16
|
+
|
17
|
+
# Function list --
|
18
|
+
# calculate
|
19
|
+
|
20
|
+
module ClosestSum
|
21
|
+
def ClosestSum.totalSize(input)
|
22
|
+
sum = 0
|
23
|
+
input.each {
|
24
|
+
|x, y|
|
25
|
+
sum += y
|
26
|
+
sum
|
27
|
+
}
|
28
|
+
sum
|
29
|
+
end
|
30
|
+
# Check for non-numeric values and raise an exception just in case.
|
31
|
+
def ClosestSum.validate (inputHash)
|
32
|
+
inputHash.each {
|
33
|
+
|x, y|
|
34
|
+
raise TypeError.new 'non-numeric value detected' if y.class.to_s != 'Fixnum'
|
35
|
+
}
|
36
|
+
end
|
37
|
+
def ClosestSum.calculate (targetSize, inputHash)
|
38
|
+
validate(inputHash)
|
39
|
+
finalList = Hash.new
|
40
|
+
inputHash = inputHash.sort_by {
|
41
|
+
|x, y|
|
42
|
+
y
|
43
|
+
}.reverse
|
44
|
+
totalCurrentSize = totalSize(finalList)
|
45
|
+
inputHash.each {
|
46
|
+
|key, value|
|
47
|
+
if finalList[key] == nil
|
48
|
+
finalList[key] = value if totalCurrentSize + value <= targetSize
|
49
|
+
end
|
50
|
+
}
|
51
|
+
finalList
|
52
|
+
end
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ClosestSum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: universal-linux
|
6
|
+
authors:
|
7
|
+
- dE
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-07-30 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Given a hash of keys and numeric values and another integer value (target
|
14
|
+
size), this module will select the largest keys from the given hash such that the
|
15
|
+
sum of the selected keys is less than equal to the target size
|
16
|
+
email: de.techno@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- README
|
22
|
+
- lib/ClosestSum.rb
|
23
|
+
homepage: http://delogics.blogspot.com
|
24
|
+
licenses:
|
25
|
+
- Apache-2.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.5.2
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: Given a list of names/paths and the corresponding numeric value, will select
|
47
|
+
items of the list who's sum will be closest to another supplied value.
|
48
|
+
test_files: []
|