find_top_k 0.0.0
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/lib/find_top_k.rb +25 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e61ede4c0fe2f62ee1984ea77ce373485c31478b4403a3797fb6a3330b15b820
|
4
|
+
data.tar.gz: 3635285a3bb2ad3e17c0fc1cb4b43185a0d1b509b41eec7ccaaa991ade900bdb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9c0d6a04d79ef89c4ac6000581132f4a144ebe6253aeda8d649bfebadddec150649353b3bd169688918bb1983505fdf8653e57d024eadc70870cd8734c41d95f
|
7
|
+
data.tar.gz: 54a70e771bd92e8f7710f38303657f80386a10ef4e2eaac96ce2ed98f5e07ae949981e23cd7017b38b5297526537ad2e16b98bfe81bd444d32af96dc27bce38e
|
data/lib/find_top_k.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
class FindTopK
|
2
|
+
def self.run(nums, k)
|
3
|
+
h = {}
|
4
|
+
a = []
|
5
|
+
nums.each do |num|
|
6
|
+
if h[num].nil?
|
7
|
+
h[num] = 1
|
8
|
+
else
|
9
|
+
h[num] += 1
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
return nums if h.count == k
|
14
|
+
return 'k cannot bigger than the number of unique elements in the array' if h.count < k
|
15
|
+
|
16
|
+
h.each do |key, val|
|
17
|
+
if a[val].nil?
|
18
|
+
a[val] = [key]
|
19
|
+
else
|
20
|
+
a[val] << key
|
21
|
+
end
|
22
|
+
end
|
23
|
+
a.compact.flatten[(-1-(k-1))..-1]
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: find_top_k
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Huei
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-01-30 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: input an array only with integers and a integer k which bigger then 0,
|
14
|
+
this gem will find out the top frequent elements in the array
|
15
|
+
email: iceland101113@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/find_top_k.rb
|
21
|
+
homepage: https://rubygems.org/gems/find_top_k
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubygems_version: 3.0.6
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: find the top k frequent elements in an array
|
44
|
+
test_files: []
|