In Ruby MRI 2.5.0, If you add assertion messaage with a '\n
in it. The message after the newline character will be logged as a regular message instead of an error message.
puts "Uncomment the test cases to see the errors"
describe "Example" do
it "Will log error" do
Test.expect(true)
# Test.expect(false, "This is an error\nThis is a log")
# Test.assert_equals(false, true, "This is an error\nThis is a log")
# Test.assert_not_equals("error", "error", "This is an error\nThis is a log")
end
end
In Node v12, if you overwrite Math.pow
with a function that throws an error, it will automatically triggered despite there is no code in the Code
, Preloaded
, and Test Cases
section that calls them. Maybe used by the test framework?
This behavior does not appear in Node v8 or v10.
Note
Not tested locally (only tested it on Codewars).
// Switch to Node v12 to see the error
console.log("Hello, World!")
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
// const Test = require("@codewars/test-compat");
describe("Solution", function() {
it("should test for something", function() {
// Test.assertEquals(1 + 1, 2);
// assert.strictEqual(1 + 1, 2);
});
});
Your job is to group the words in anagrams.
What is an anagram ?
star
and tsar
are anagram of each other because you can rearrange the letters for star to obtain tsar.
Example
A typical test could be :
// input
groupAnagrams(["tsar", "rat", "tar", "star", "tars", "cheese"]);
// output
[
["tsar", "star", "tars"],
["rat", "tar"],
["cheese"]
]
## Helpers
The method `assertSimilarUnsorted` has been preloaded for you **in the Solution Sandbox only** to compare to arrays without relying on the sorting of the elements.
`assertSimilarUnsorted([[1,2], [3]], [[3], [1,2]]); // returns true`
Hvae unf !
I'd advise you to find an efficient way for grouping the words in anagrams otherwise you'll probably won't pass the heavy superhero test cases
def group_anagrams(words):
def sort_word(word):
charlist = list(word)
charlist.sort()
return "".join(charlist)
anagrams = {}
for word in words:
sorted = sort_word(word)
if sorted in anagrams:
anagrams[sorted].append(word)
else:
anagrams[sorted] = [word]
return list(anagrams.values())
import random
import string
import codewars_test as test
def get_randword(length):
charlist = string.ascii_letters
return "".join([random.choice(charlist) for x in range(length)])
def generate_anagrams(word, amount):
charlist = list(word)
anagrams = []
for i in range(amount):
random.shuffle(charlist)
anagrams.append("".join(charlist))
return anagrams
def deep_sort(array):
for i in range(len(array)):
array[i].sort()
array.sort()
@test.describe("Fixed Tests")
def _():
@test.it("Tests")
def _():
test.assert_equals(group_anagrams(["rat", "tar", "star"]), [["rat", "tar"], ["star"]])
@test.describe("Random Tests")
def _():
@test.it("Test")
def _():
for i in range(100):
anagram_list = []
shuffled_list = []
# Rare chance solution would fail if the second loop iterates >= 500 times
# Medium chance solutiom would fail if the second loop iterates >= 1000 times
for i in range(1000):
randword = get_randword(random.randint(3, 10))
anagram = generate_anagrams(randword, random.randint(3, 10))
anagram_list.append(anagram)
shuffled_list.extend(anagram)
deep_sort(anagram_list)
random.shuffle(shuffled_list)
actual = group_anagrams(shuffled_list)
deep_sort(actual)
test.assert_equals(actual, anagram_list, "Testing for {0}\n\n".format(shuffled_list))