Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# frozen_string_literal: true
source "https://rubygems.org"

gem 'rake'
gem 'minitest'
gem 'minitest-spec'
gem 'minitest-reporters'
gem "rake"
gem "minitest"
gem "minitest-spec"
gem "minitest-reporters"
gem "pry"
gem 'minitest-skip'

gem "minitest-skip"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Given a non-empty array of integers, return the *k* most frequent elements.
## Example 1

```
Input: nums = [1,1,1,2,2,3], k = 2
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]

```
Expand Down
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'rake/testtask'
require "rake/testtask"

Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
t.test_files = FileList['test/*_test.rb']
t.test_files = FileList["test/*_test.rb"]
end

task default: :test
64 changes: 54 additions & 10 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,73 @@

# This method will return an array of arrays.
# Each subarray will have strings which are anagrams of each other
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)

def grouped_anagrams(strings)
raise NotImplementedError, "Method hasn't been implemented yet!"
hash = Hash.new
strings.each do |element|
key = element.chars.sort.join()
groupedAnagrams = hash[key]
if groupedAnagrams == nil
hash[key] = []
groupedAnagrams = hash[key]
end
groupedAnagrams << element
end
return hash.values
end

# This method will return the k most common elements
# in the case of a tie it will select the first occuring element.
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(2n) -> O(n)
# Space Complexity: O(n)
def top_k_frequent_elements(list, k)
raise NotImplementedError, "Method hasn't been implemented yet!"
return nil if !list
# intialze an empty hash
## iterate throught the list
#chechk if the key is in the hash,
# if key is in the hash ++ value
# otherwise add key and value as 1 since it's first occurance
# now the key(uniqe) and value(how many time the int_value) happend is in the hash,
# my_hash.sort_by { |_, value| value }.each { |key, value| puts key } (iterate throught the hash, (hash.each {|key, value|}) )
# before pushing the key into the initalized array, check to see if the length is less than k
# if
hash = Hash.new
list.each do |int_value|
if hash.key?(int_value)
hash[key] = +1
else
hash[key] = 1
end
end
kth_frequent_elements = []
counter = 0
pervisou_value = 0
hash.sort_by { |k, v| -v }.each do |key, value|
if counter < k
kth_frequent_elements << key
if pervisou_value != value
pervisou_value = value
counter += 1
end
# end
# if kth_frequent_elements.length < k
# kth_frequent_elements << key
else
break
end
end
return kth_frequent_elements
end


# This method will return the true if the table is still
# a valid sudoku table.
# Each element can either be a ".", or a digit 1-9
# The same digit cannot appear twice or more in the same
# The same digit cannot appear twice or more in the same
# row, column or 3x3 subgrid
# Time Complexity: ?
# Space Complexity: ?
def valid_sudoku(table)
raise NotImplementedError, "Method hasn't been implemented yet!"
# seen_set = Set.new()
# for i in
end
90 changes: 45 additions & 45 deletions test/exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
it "will return [] for an empty array" do
#Arrange
list = []

# Act-Assert
expect(grouped_anagrams(list)).must_equal []
end
Expand All @@ -18,9 +18,9 @@
answer = grouped_anagrams(list)

expected_answer = [
["ate","eat","tea"],
["nat","tan"],
["bat"]
["ate", "eat", "tea"],
["nat", "tan"],
["bat"],
]

# Assert
Expand All @@ -42,7 +42,7 @@
["tar"],
["pop"],
["pan"],
["pap"]
["pap"],
]

# Assert
Expand All @@ -59,7 +59,7 @@
answer = grouped_anagrams(list)

expected_answer = [
[ "aet", "ate", "eat", "eta", "tae", "tea"]
["aet", "ate", "eat", "eta", "tae", "tea"],
]
# Assert
answer.each_with_index do |array, index|
Expand All @@ -68,17 +68,17 @@
end
end

describe "top_k_frequent_elements" do
xdescribe "top_k_frequent_elements" do
it "works with example 1" do
# Arrange
list = [1,1,1,2,2,3]
list = [1, 1, 1, 2, 2, 3]
k = 2

# Act
answer = top_k_frequent_elements(list, k)

# Assert
expect(answer.sort).must_equal [1,2]
expect(answer.sort).must_equal [1, 2]
end

it "works with example 2" do
Expand Down Expand Up @@ -143,15 +143,15 @@
it "works for the table given in the README" do
# Arrange
table = [
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
]

# Act
Expand All @@ -164,15 +164,15 @@
it "fails for the table given in the README" do
# Arrange
table = [
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
["8", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
]

# Act
Expand All @@ -185,15 +185,15 @@
it "fails for a duplicate number in a sub-box" do
# Arrange
table = [
["5","3",".",".","7",".",".",".","."],
["6",".","3","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", "3", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
]

# Act
Expand All @@ -206,15 +206,15 @@
it "fails for a duplicate number in a bottom right sub-box" do
# Arrange
table = [
["5","3",".",".","7",".",".",".","."],
["6",".","2","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9","8",".","5"],
[".",".",".",".","8",".",".","7","9"]
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", "2", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", "8", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
]

# Act
Expand Down
9 changes: 4 additions & 5 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

require 'minitest'
require 'minitest/autorun'
require 'minitest/reporters'
require "minitest"
require "minitest/autorun"
require "minitest/reporters"
require "minitest/skip_dsl"

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

require_relative '../lib/exercises'
require_relative "../lib/exercises"