From 360eee8076210c8ea422600ffa5996ce0f5aa163 Mon Sep 17 00:00:00 2001 From: Dementev Roman Date: Fri, 4 Jul 2014 16:13:09 +0400 Subject: [PATCH] add issues --- doc/intro.md | 3 -- issues/array_fetch.yml | 38 +++++++++++------- issues/array_interleave.yml | 24 +++++++----- issues/array_transpose.yml | 33 +++++++++------- issues/broken_include.yml | 13 +++++++ issues/build_hash_with_default.yml | 12 ++++++ issues/build_key_value.yml | 30 +++++++------- issues/check_phone_number_format.yml | 18 +++++++++ issues/clash.yml | 22 +++++++++++ issues/counting_array_elements.yml | 13 +++++++ issues/credit_card_validator.yml | 40 ++++++++++++------- issues/dash.yml | 22 +++++++++++ issues/fibonacci.yml | 14 +++++++ issues/find_palindrome.yml | 15 +++++++ issues/fizzbuzz.yml | 32 ++++++++++----- issues/flatten.yml | 17 ++++++++ issues/happy_numbers.yml | 15 +++++++ issues/hash_deep_merge.yml | 23 +++++++++++ issues/hash_with_indifferent_access.yml | 14 +++++++ issues/integer_difference.yml | 12 ++++++ issues/invert_hash.yml | 10 +++++ issues/join_url_params.yml | 21 ++++++++++ issues/key_for_min_value.yml | 14 +++++++ issues/mash.yml | 15 +++++++ issues/maybe.yml | 52 +++++++++++++++++++++++++ issues/mixins.yml | 12 ++++++ issues/more_than_medium.yml | 16 ++++++++ issues/parse_date.yml | 13 +++++++ issues/pascals_triangle.yml | 12 ++++++ issues/power_digits_sum.yml | 10 +++++ issues/prime_factorization.yml | 12 ++++++ issues/prime_number.yml | 9 +++++ issues/reciprocal_cycles.yml | 22 +++++++++++ issues/redefine_nil.yml | 6 +++ issues/related_keys_of_hash.yml | 19 +++++++++ issues/reverse_each_word.yml | 11 ++++++ issues/separate_with_comma.yml | 15 +++++++ issues/sexp_eval.yml | 11 ++++++ issues/shell_words_split.yml | 10 +++++ issues/simple_object.yml | 13 +++++++ issues/stack.yml | 16 ++++++++ issues/string_format.yml | 9 +++++ issues/sum_of_array_plus_one.yml | 9 +++++ issues/sum_primes.yml | 8 ++++ issues/trash.yml | 14 +++++++ issues/twenty_one.yml | 9 +++++ issues/word_positions.yml | 10 +++++ project.clj | 3 +- source/flatten.yml | 3 +- src/battle_asserts/core.clj | 22 ++++------- src/battle_asserts/reader.clj | 32 +++++++-------- 51 files changed, 738 insertions(+), 110 deletions(-) delete mode 100644 doc/intro.md create mode 100644 issues/broken_include.yml create mode 100644 issues/build_hash_with_default.yml create mode 100644 issues/check_phone_number_format.yml create mode 100644 issues/clash.yml create mode 100644 issues/counting_array_elements.yml create mode 100644 issues/dash.yml create mode 100644 issues/fibonacci.yml create mode 100644 issues/find_palindrome.yml create mode 100644 issues/flatten.yml create mode 100644 issues/happy_numbers.yml create mode 100644 issues/hash_deep_merge.yml create mode 100644 issues/hash_with_indifferent_access.yml create mode 100644 issues/integer_difference.yml create mode 100644 issues/invert_hash.yml create mode 100644 issues/join_url_params.yml create mode 100644 issues/key_for_min_value.yml create mode 100644 issues/mash.yml create mode 100644 issues/maybe.yml create mode 100644 issues/mixins.yml create mode 100644 issues/more_than_medium.yml create mode 100644 issues/parse_date.yml create mode 100644 issues/pascals_triangle.yml create mode 100644 issues/power_digits_sum.yml create mode 100644 issues/prime_factorization.yml create mode 100644 issues/prime_number.yml create mode 100644 issues/reciprocal_cycles.yml create mode 100644 issues/redefine_nil.yml create mode 100644 issues/related_keys_of_hash.yml create mode 100644 issues/reverse_each_word.yml create mode 100644 issues/separate_with_comma.yml create mode 100644 issues/sexp_eval.yml create mode 100644 issues/shell_words_split.yml create mode 100644 issues/simple_object.yml create mode 100644 issues/stack.yml create mode 100644 issues/string_format.yml create mode 100644 issues/sum_of_array_plus_one.yml create mode 100644 issues/sum_primes.yml create mode 100644 issues/trash.yml create mode 100644 issues/twenty_one.yml create mode 100644 issues/word_positions.yml diff --git a/doc/intro.md b/doc/intro.md deleted file mode 100644 index 2066f2e6..00000000 --- a/doc/intro.md +++ /dev/null @@ -1,3 +0,0 @@ -# Introduction to battle_asserts - -TODO: write [great documentation](http://jacobian.org/writing/great-documentation/what-to-write/) diff --git a/issues/array_fetch.yml b/issues/array_fetch.yml index 39a983d7..16bb9aef 100644 --- a/issues/array_fetch.yml +++ b/issues/array_fetch.yml @@ -1,15 +1,27 @@ -tags: [array] level: elementary +tags: [array] checks: - ruby: ['arr = ["a", "b", "c"]', 'assert(=("b", fetch(arr, 1, "d")))', 'assert(=("d", - fetch(arr, 5, "d")))', 'assert(=("c", fetch(arr, -1, "d")))', 'assert(=("d", - fetch(arr, -5, "d")))'] - javascript: ['var arr = ["a", "b", "c"];', 'assert(=("b", fetch(arr, 1, "d")));', - 'assert(=("d", fetch(arr, 5, "d")));', 'assert(=("c", fetch(arr, -1, "d")));', - 'assert(=("d", fetch(arr, -5, "d")));'] - python: ['arr = ["a", "b", "c"]', 'assert(=("b", fetch(arr, 1, "d")))', 'assert(=("d", - fetch(arr, 5, "d")))', 'assert(=("c", fetch(arr, -1, "d")))', 'assert(=("d", - fetch(arr, -5, "d")))'] - php: ['$arr = array("a", "b", "c");', 'assert(=("b", fetch(arr, 1, "d")));', 'assert(=("d", - fetch(arr, 5, "d")));', 'assert(=("c", fetch(arr, -1, "d")));', 'assert(=("d", - fetch(arr, -5, "d")));'] + ruby: |- + arr = ['a', 'b', 'c'] + assert_equal('b', fetch(arr, 1, 'd')) + assert_equal('d', fetch(arr, 5, 'd')) + assert_equal('c', fetch(arr, -1, 'd')) + assert_equal('d', fetch(arr, -5, 'd')) + javascript: |- + var arr = ['a', 'b', 'c']; + assertEqual('b', fetch(arr, 1, 'd')); + assertEqual('d', fetch(arr, 5, 'd')); + assertEqual('c', fetch(arr, -1, 'd')); + assertEqual('d', fetch(arr, -5, 'd')); + python: |- + arr = ['a', 'b', 'c'] + assertEqual('b', fetch(arr, 1, 'd')) + assertEqual('d', fetch(arr, 5, 'd')) + assertEqual('c', fetch(arr, -1, 'd')) + assertEqual('d', fetch(arr, -5, 'd')) + php: |- + $arr = array('a', 'b', 'c'); + assertEqual('b', fetch(arr, 1, 'd')); + assertEqual('d', fetch(arr, 5, 'd')); + assertEqual('c', fetch(arr, -1, 'd')); + assertEqual('d', fetch(arr, -5, 'd')); diff --git a/issues/array_interleave.yml b/issues/array_interleave.yml index f7b1948e..b1a321f5 100644 --- a/issues/array_interleave.yml +++ b/issues/array_interleave.yml @@ -1,11 +1,17 @@ -tags: [array] level: easy +tags: [array] checks: - ruby: ['arr = [1, 3, 5]', 'assert(=([1, 2, 3, 4, 5, 6], interleave(arr, 2, 4, 6)))', - 'assert(=([1, 2, 3, 4, 5], interleave(arr, [2, 4])))', 'assert(=([1, "2", 3, "4", - 5], interleave(arr, "2", "4")))', 'assert(=([2, 4, 6], interleave([], 2, 4, - 6)))', 'assert(=([1, 3, 5], interleave(arr)))'] - javascript: ['var arr = [1, 3, 5];', 'assert(=([1, 2, 3, 4, 5, 6], interleave(arr, - 2, 4, 6)));', 'assert(=([1, 2, 3, 4, 5], interleave(arr, [2, 4])));', 'assert(=([1, - "2", 3, "4", 5], interleave(arr, "2", "4")));', 'assert(=([2, 4, 6], interleave([], - 2, 4, 6)));', 'assert(=([1, 3, 5], interleave(arr)));'] + ruby: |- + arr = [1, 3, 5] + assert_equal([1, 2, 3, 4, 5, 6], interleave(arr, 2, 4, 6)) + assert_equal([1, 2, 3, 4, 5], interleave(arr, [2, 4])) + assert_equal([1, '2', 3, '4', 5], interleave(arr, '2', '4')) + assert_equal([2, 4, 6], interleave([], 2, 4, 6)) + assert_equal([1, 3, 5], interleave(arr)) + javascript: |- + var arr = [1, 3, 5]; + assertEqual([1, 2, 3, 4, 5, 6], interleave(arr, 2, 4, 6)); + assertEqual([1, 2, 3, 4, 5], interleave(arr, [2, 4])); + assertEqual([1, '2', 3, '4', 5], interleave(arr, '2', '4')); + assertEqual([2, 4, 6], interleave([], 2, 4, 6)); + assertEqual([1, 3, 5], interleave(arr)); diff --git a/issues/array_transpose.yml b/issues/array_transpose.yml index 474ca227..8eeb2ae2 100644 --- a/issues/array_transpose.yml +++ b/issues/array_transpose.yml @@ -1,17 +1,20 @@ -description: Re-implement Ruby's Array#transpose. -tags: [] level: medium +tags: [] +description: Re-implement Ruby's Array#transpose. checks: - ruby: ['assert(=([[1, :a], [2, :b], [3, :c]], transpose([[1, 2, 3], [:a, :b, :c]])))', - 'assert(=([[1, 3, 5], [2, 4, 6]], transpose([[1, 2], [3, 4], [5, 6]])))', 'assert(=([], - transpose([])))'] - javascript: ['assert(=([[1, "a"], [2, "b"], [3, "c"]], transpose([[1, 2, 3], ["a", - "b", "c"]])));', 'assert(=([[1, 3, 5], [2, 4, 6]], transpose([[1, 2], [3, 4], - [5, 6]])));', 'assert(=([], transpose([])));'] - python: ['assert(=([[1, "a"], [2, "b"], [3, "c"]], transpose([[1, 2, 3], ["a", "b", - "c"]])))', 'assert(=([[1, 3, 5], [2, 4, 6]], transpose([[1, 2], [3, 4], [5, - 6]])))', 'assert(=([], transpose([])))'] - php: ['assert(=(array(array(1, "a"), array(2, "b"), array(3, "c")), transpose(array(array(1, - 2, 3), array("a", "b", "c")))));', 'assert(=(array(array(1, 3, 5), array(2, - 4, 6)), transpose(array(array(1, 2), array(3, 4), array(5, 6)))));', 'assert(=(array(), - transpose(array())));'] + ruby: |- + assert_equal([[1, :a], [2, :b], [3, :c]], transpose([[1, 2, 3], [:a, :b, :c]])) + assert_equal([[1, 3, 5], [2, 4, 6]], transpose([[1, 2], [3, 4], [5, 6]])) + assert_equal([], transpose([])) + javascript: |- + assertEqual([[1, 'a'], [2, 'b'], [3, 'c']], transpose([[1, 2, 3], ['a', 'b', 'c']])); + assertEqual([[1, 3, 5], [2, 4, 6]], transpose([[1, 2], [3, 4], [5, 6]])); + assertEqual([], transpose([])); + python: |- + assertEqual([[1, 'a'], [2, 'b'], [3, 'c']], transpose([[1, 2, 3], ['a', 'b', 'c']])) + assertEqual([[1, 3, 5], [2, 4, 6]], transpose([[1, 2], [3, 4], [5, 6]])) + assertEqual([], transpose([])) + php: |- + assertEqual(array(array(1, 'a'), array(2, 'b'), array(3, 'c')), transpose(array(array(1, 2, 3), array('a', 'b', 'c')))); + assertEqual(array(array(1, 3, 5), array(2, 4, 6)), transpose(array(array(1, 2), array(3, 4), array(5, 6)))); + assertEqual(array(), transpose(array())); diff --git a/issues/broken_include.yml b/issues/broken_include.yml new file mode 100644 index 00000000..136b9a8e --- /dev/null +++ b/issues/broken_include.yml @@ -0,0 +1,13 @@ +level: elementary +tags: [array, set] +description: | + Check presence of element in array. +checks: + ruby: + setup: | + set = [1, 2, 3, 4, 5, 6, 7] + asserts: ['assert include?(set, 5)', 'assert !include?(set, 10)'] + javascript: + setup: | + var set = [1, 2, 3, 4, 5, 6, 7]; + asserts: ['assert(contains(set, 5))', 'assert(!contains(set, 10))'] diff --git a/issues/build_hash_with_default.yml b/issues/build_hash_with_default.yml new file mode 100644 index 00000000..6c2e2105 --- /dev/null +++ b/issues/build_hash_with_default.yml @@ -0,0 +1,12 @@ +level: elementary +tags: [hash] +checks: + ruby: + asserts: ['assert_equal({:draft => 0, :completed => 0}, generate([:draft, :completed], + 0))', 'assert_equal({:one => 4, :two => 4}, generate([:one, :two], 4))'] + javascript: + asserts: + - | + assertEqual({draft: 0, completed: 0}, generate(["draft", "completed"], 0)) + - | + assertEqual({one: 4, two: 4}, generate(["one", "two"], 4)) diff --git a/issues/build_key_value.yml b/issues/build_key_value.yml index faaa7586..fcc6e48b 100644 --- a/issues/build_key_value.yml +++ b/issues/build_key_value.yml @@ -1,15 +1,19 @@ -tags: [array, hash] level: medium +tags: [array, hash] checks: - ruby: ['d = {"a" => {"b" => 3, "c" => 2, "d" => [1, 2]}, "x" => ["1", "2", "3"]}', - 'expected = {"a[b]" => 3, "a[c]" => 2, "a[d][0]" => 1, "a[d][1]" => 2, "x[0]" - => "1", "x[1]" => "2", "x[2]" => "3"}', 'assert(=(expected, b_key_val(d)))'] - javascript: ['var d = {a: {b: 3, c: 2, d: [1, 2]}, x: ["1", "2", "3"]};', 'var expected - = {a[b]: 3, a[c]: 2, a[d][0]: 1, a[d][1]: 2, x[0]: "1", x[1]: "2", x[2]: "3"};', - 'assert(=(expected, bKeyVal(d)));'] - php: ['$d = array("a" => array("b" => 3, "c" => 2, "d" => array(1, 2)), "x" => array("1", - "2", "3"));', '$expected = array("a[b]" => 3, "a[c]" => 2, "a[d][0]" => 1, "a[d][1]" - => 2, "x[0]" => "1", "x[1]" => "2", "x[2]" => "3");', 'assert(=(expected, bKeyVal(d)));'] - python: ['d = {"a": {"b": 3, "c": 2, "d": [1, 2]}, "x": ["1", "2", "3"]}', 'expected - = {"a[b]": 3, "a[c]": 2, "a[d][0]": 1, "a[d][1]": 2, "x[0]": "1", "x[1]": "2", - "x[2]": "3"}', 'assert(=(expected, bKeyVal(d)))'] + ruby: |- + d = {'a' => {'b' => 3, 'c' => 2, 'd' => [1, 2]}, 'x' => ['1', '2', '3']} + expected = {'a[b]' => 3, 'a[c]' => 2, 'a[d][0]' => 1, 'a[d][1]' => 2, 'x[0]' => '1', 'x[1]' => '2', 'x[2]' => '3'} + assert_equal(expected, b_key_val(d)) + javascript: |- + var d = {a: {b: 3, c: 2, d: [1, 2]}, x: ['1', '2', '3']}; + var expected = {a[b]: 3, a[c]: 2, a[d][0]: 1, a[d][1]: 2, x[0]: '1', x[1]: '2', x[2]: '3'}; + assertEqual(expected, bKeyVal(d)); + php: |- + $d = array('a' => array('b' => 3, 'c' => 2, 'd' => array(1, 2)), 'x' => array('1', '2', '3')); + $expected = array('a[b]' => 3, 'a[c]' => 2, 'a[d][0]' => 1, 'a[d][1]' => 2, 'x[0]' => '1', 'x[1]' => '2', 'x[2]' => '3'); + assertEqual(expected, bKeyVal(d)); + python: |- + d = {'a': {'b': 3, 'c': 2, 'd': [1, 2]}, 'x': ['1', '2', '3']} + expected = {'a[b]': 3, 'a[c]': 2, 'a[d][0]': 1, 'a[d][1]': 2, 'x[0]': '1', 'x[1]': '2', 'x[2]': '3'} + assertEqual(expected, bKeyVal(d)) diff --git a/issues/check_phone_number_format.yml b/issues/check_phone_number_format.yml new file mode 100644 index 00000000..a1c4b8af --- /dev/null +++ b/issues/check_phone_number_format.yml @@ -0,0 +1,18 @@ +level: medium +tags: [regexp] +description: | + Write a method to validate some strings that could potentially represent phone numbers. + See if you can do it with a regular expression. +checks: + ruby: + asserts: ['assert phone_number?("5555555555")', 'assert !phone_number?("555555555")', + 'assert phone_number?("555-5555")', 'assert phone_number?("(555) 555-5555")', + 'assert !phone_number?("(555) 555-555")', 'assert phone_number?("555-555-5555")', + 'assert !phone_number?("555a-555-5555")', 'assert !phone_number?("555*555-5555")', + 'assert !phone_number?("55a-555-5555")'] + javascript: + asserts: [assert(isPhoneNumber("5555555555")), assert(!isPhoneNumber("555555555")), + assert(isPhoneNumber("555-5555")), assert(isPhoneNumber("(555) 555-5555")), + assert(!isPhoneNumber("(555) 555-555")), assert(isPhoneNumber("555-555-5555")), + assert(!isPhoneNumber("555a-555-5555")), assert(!isPhoneNumber("555*555-5555")), + assert(!isPhoneNumber("55a-555-5555"))] diff --git a/issues/clash.yml b/issues/clash.yml new file mode 100644 index 00000000..bceba777 --- /dev/null +++ b/issues/clash.yml @@ -0,0 +1,22 @@ +level: medium +tags: [hash] +author: {github_nickname: intridea, web_page: 'http://intridea.com'} +description: '' +checks: + ruby: + setup: | + c1 = Clash.new + c.where(:abc => 'def').order(:created_at) + + c2 = Clash.new + c.where!.abc('def').ghi(123)._end!.order(:created_at) + + c3 = Clash.new + c.where(:abc => 'def').where(:hgi => 123) + asserts: + - | + assert_equal({where: {abc: 'def'}, order: :created_at}, c1.to_hash) + - | + assert_equal({where: {abc: 'def', ghi: 123}, order: :created_at}, c2.to_hash) + - | + assert_equal({where: {abc: 'def', hgi: 123}}, c3.to_hash) diff --git a/issues/counting_array_elements.yml b/issues/counting_array_elements.yml new file mode 100644 index 00000000..320a186c --- /dev/null +++ b/issues/counting_array_elements.yml @@ -0,0 +1,13 @@ +level: easy +tags: [] +checks: + ruby: + setup: | + test = ['cat', 'dog', 'fish', 'fish'] + asserts: ['assert_equal({''cat'' => 1, ''dog'' => 1, ''fish'' => 2}, count(test))'] + javascript: + setup: | + test = ['cat', 'dog', 'fish', 'fish'] + asserts: + - | + assertEqual({cat: 1, dog: 1, fish: 2}, count(test)) diff --git a/issues/credit_card_validator.yml b/issues/credit_card_validator.yml index 0c9b7ebe..5852c199 100644 --- a/issues/credit_card_validator.yml +++ b/issues/credit_card_validator.yml @@ -1,21 +1,33 @@ +level: medium +tags: [string] description: | Credit card numbers can be validated with a process called the Luhn algorithm. Simply stated, the Luhn algorithm works like this: 1. Starting with the next to last digit and continuing with every other digit going back to the beginning of the card, double the digit. 2. Sum all doubled and untouched digits in the number. 3. If that total is a multiple of 10, the number is valid. -tags: [string] -level: medium checks: - ruby: ['assert(!valid_credit_card?("1234567890123456"))', 'assert(valid_credit_card?("4408041234567893"))', - 'assert(!valid_credit_card?("440804l234567893"))', 'assert(valid_credit_card?("38520000023237"))', - 'assert(valid_credit_card?("4222222222222"))'] - javascript: ['assert(!validCreditCard?("1234567890123456"));', 'assert(validCreditCard?("4408041234567893"));', - 'assert(!validCreditCard?("440804l234567893"));', 'assert(validCreditCard?("38520000023237"));', - 'assert(validCreditCard?("4222222222222"));'] - python: ['assert(!validCreditCard?("1234567890123456"))', 'assert(validCreditCard?("4408041234567893"))', - 'assert(!validCreditCard?("440804l234567893"))', 'assert(validCreditCard?("38520000023237"))', - 'assert(validCreditCard?("4222222222222"))'] - php: ['assert(!validCreditCard?("1234567890123456"));', 'assert(validCreditCard?("4408041234567893"));', - 'assert(!validCreditCard?("440804l234567893"));', 'assert(validCreditCard?("38520000023237"));', - 'assert(validCreditCard?("4222222222222"));'] + ruby: |- + assert(!valid_credit_card?('1234567890123456')) + assert(valid_credit_card?('4408041234567893')) + assert(!valid_credit_card?('4408042234567893')) + assert(valid_credit_card?('38520000023237')) + assert(valid_credit_card?('4222222222222')) + javascript: |- + assert(!validCreditCard?('1234567890123456')); + assert(validCreditCard?('4408041234567893')); + assert(!validCreditCard?('4408042234567893')); + assert(validCreditCard?('38520000023237')); + assert(validCreditCard?('4222222222222')); + python: |- + assert(!validCreditCard?('1234567890123456')) + assert(validCreditCard?('4408041234567893')) + assert(!validCreditCard?('4408042234567893')) + assert(validCreditCard?('38520000023237')) + assert(validCreditCard?('4222222222222')) + php: |- + assert(!validCreditCard?('1234567890123456')); + assert(validCreditCard?('4408041234567893')); + assert(!validCreditCard?('4408042234567893')); + assert(validCreditCard?('38520000023237')); + assert(validCreditCard?('4222222222222')); diff --git a/issues/dash.yml b/issues/dash.yml new file mode 100644 index 00000000..45dadf40 --- /dev/null +++ b/issues/dash.yml @@ -0,0 +1,22 @@ +level: medium +tags: [hash] +author: {github_nickname: intridea, web_page: 'http://intridea.com'} +description: | + Dash is an extended Hash that has a discrete set of defined properties and only those properties may be set on the hash. + Additionally, you can set defaults for each property. You can also flag a property as required. + Required properties will raise an exception if unset. +checks: + ruby: + setup: | + class Person < Dash + property :name, :required => true + property :email + property :occupation, :default => 'Rubyist' + end + + p = Person.new(:name => "Bob") + p.email = 'abc@def.com' + asserts: ['assert_raise(ArgumentError) { Person.new }', 'assert_equal "Bob", p.name', + 'assert_equal "abc@def.com", p.email', 'assert_raise(ArgumentError) { p.name + = nil }', 'assert_equal "Rubyist", p.occupation', 'assert_equal "Rubyist", + p[:occupation]', 'assert_raise(NoMethodError) { p[:awesome] }'] diff --git a/issues/fibonacci.yml b/issues/fibonacci.yml new file mode 100644 index 00000000..054b5fa9 --- /dev/null +++ b/issues/fibonacci.yml @@ -0,0 +1,14 @@ +level: easy +tags: [] +description: | + "Write a method that handles Fibonacci sequences. + Have it return the nth item in the Fibonacci sequence. + Hint: The first item in the sequence is 0." +checks: + ruby: + asserts: ['assert_equal 0, fibo_finder(0)', 'assert_equal 1, fibo_finder(1)', + 'assert_equal 3, fibo_finder(4)', 'assert_equal 13, fibo_finder(7)', 'assert_equal + 55, fibo_finder(10)'] + javascript: + asserts: ['assertEqual(0, fiboFinder(0))', 'assertEqual(1, fiboFinder(1))', 'assertEqual(3, + fiboFinder(4))', 'assertEqual(13, fiboFinder(7))', 'assertEqual(55, fiboFinder(10))'] diff --git a/issues/find_palindrome.yml b/issues/find_palindrome.yml new file mode 100644 index 00000000..141958d1 --- /dev/null +++ b/issues/find_palindrome.yml @@ -0,0 +1,15 @@ +level: medium +tags: [] +description: | + A palindrome is a string that is written the same forward as it is in reverse. + Write a method to return the longest palindrome in a given string. +checks: + ruby: + asserts: ['assert_equal "yzzy", longest_palindrome("xyzzy")', 'assert_equal "dhfdkjfffhhfffjkdfhd", + longest_palindrome("afbbbfjdjklgdfdhfdkjfffhhfffjkdfhdhkyejejfjkd")', 'assert_equal + "racecar", longest_palindrome("bartarcarracecarbartar")', 'assert_equal "46264", + longest_palindrome("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982")'] + javascript: + asserts: ['assertEqual("yzzy", longestPalindrome("xyzzy"))', 'assertEqual("dhfdkjfffhhfffjkdfhd", + longestPalindrome("afbbbfjdjklgdfdhfdkjfffhhfffjkdfhdhkyejejfjkd"))', 'assertEqual("racecar", + longestPalindrome("bartarcarracecarbartar"))', 'assertEqual("46264", longestPalindrome("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982"))'] diff --git a/issues/fizzbuzz.yml b/issues/fizzbuzz.yml index 37409fa3..ad10cfde 100644 --- a/issues/fizzbuzz.yml +++ b/issues/fizzbuzz.yml @@ -1,15 +1,27 @@ +level: elementary +tags: [arithmetic, string] description: | If a number is divisible by 3, return "Fizz". If a number is divisible by 5, return "Buzz". If a number is divisible by 3 and 5, return "FizzBuzz" -tags: [arithmetic, string] -level: elementary checks: - ruby: ['assert(=("Fizz", fizzbuzz(3)))', 'assert(=("Buzz", fizzbuzz(50)))', 'assert(=("FizzBuzz", - fizzbuzz(150)))', 'assert(=("FizzBuzz", fizzbuzz(5175)))'] - javascript: ['assert(=("Fizz", fizzbuzz(3)));', 'assert(=("Buzz", fizzbuzz(50)));', - 'assert(=("FizzBuzz", fizzbuzz(150)));', 'assert(=("FizzBuzz", fizzbuzz(5175)));'] - php: ['assert(=("Fizz", fizzbuzz(3)));', 'assert(=("Buzz", fizzbuzz(50)));', 'assert(=("FizzBuzz", - fizzbuzz(150)));', 'assert(=("FizzBuzz", fizzbuzz(5175)));'] - python: ['assert(=("Fizz", fizzbuzz(3)))', 'assert(=("Buzz", fizzbuzz(50)))', 'assert(=("FizzBuzz", - fizzbuzz(150)))', 'assert(=("FizzBuzz", fizzbuzz(5175)))'] + ruby: |- + assert_equal('Fizz', fizzbuzz(3)) + assert_equal('Buzz', fizzbuzz(50)) + assert_equal('FizzBuzz', fizzbuzz(150)) + assert_equal('FizzBuzz', fizzbuzz(5175)) + javascript: |- + assertEqual('Fizz', fizzbuzz(3)); + assertEqual('Buzz', fizzbuzz(50)); + assertEqual('FizzBuzz', fizzbuzz(150)); + assertEqual('FizzBuzz', fizzbuzz(5175)); + php: |- + assertEqual('Fizz', fizzbuzz(3)); + assertEqual('Buzz', fizzbuzz(50)); + assertEqual('FizzBuzz', fizzbuzz(150)); + assertEqual('FizzBuzz', fizzbuzz(5175)); + python: |- + assertEqual('Fizz', fizzbuzz(3)) + assertEqual('Buzz', fizzbuzz(50)) + assertEqual('FizzBuzz', fizzbuzz(150)) + assertEqual('FizzBuzz', fizzbuzz(5175)) diff --git a/issues/flatten.yml b/issues/flatten.yml new file mode 100644 index 00000000..2a9cda61 --- /dev/null +++ b/issues/flatten.yml @@ -0,0 +1,17 @@ +level: easy +tags: [] +checks: + ruby: + asserts: + - | + assert_equal [1, 2, {:a => :b}], flatten([1, 2, [[{a: :b}]]]) + - assert_equal [1, 2, 3, 4, 5, 6, 7, 8], flatten([1, [2, 3, [4, 5, [6, 7]]], 8]) + - assert_equal [], flatten([]) + - assert_equal [1, 2, 3, 4], flatten([[1, 2], [3, 4]]) + javascript: + asserts: + - | + assertEqual([1, 2, {a: "b"}], flatten([1, 2, [[{a: "b"}]]])) + - assertEqual([1, 2, 3, 4, 5, 6, 7, 8], flatten([1, [2, 3, [4, 5, [6, 7]]], 8])) + - assertEqual([], flatten([])) + - assertEqual([1, 2, 3, 4], flatten([[1, 2], [3, 4]])) diff --git a/issues/happy_numbers.yml b/issues/happy_numbers.yml new file mode 100644 index 00000000..923f1196 --- /dev/null +++ b/issues/happy_numbers.yml @@ -0,0 +1,15 @@ +level: medium +tags: [] +description: | + Happy numbers are positive integers that follow a particular formula: + take each individual digit, square it, and then sum the squares to get a new number. + Repeat with the new number and eventually, you might get to a number whose squared sum is 1. + This is a happy number. An unhappy number (or sad number) is one that loops endlessly. + Write a function that determines if a number is happy or not. +checks: + ruby: + asserts: ['assert happy_number?(7)', 'assert happy_number?(986543210)', 'assert + !happy_number?(2)', 'assert !happy_number?(189)'] + javascript: + asserts: [assert(isHappyNumber(7)), assert(isHappyNumber(986543210)), assert(!isHappyNumber(2)), + assert(!isHappyNumber(189))] diff --git a/issues/hash_deep_merge.yml b/issues/hash_deep_merge.yml new file mode 100644 index 00000000..1b56b7b7 --- /dev/null +++ b/issues/hash_deep_merge.yml @@ -0,0 +1,23 @@ +level: easy +tags: [hash] +description: Returns a new hash with **self** and **other_hash** merged recursively. +author: {github_nickname: zzet, web_page: 'http://zzet.org'} +checks: + ruby: + setup: | + h1 = { x: { y: [4, 5, 6] }, z: [7, 8, 9] } + h2 = { x: { y: [7, 8, 9] }, z: 'xyz' } + asserts: + - | + assert_equal({x: {y: [7, 8, 9]}, z: "xyz"}, h1.deep_merge(h2)) + - | + assert_equal({x: {y: [4, 5, 6]}, z: [7, 8, 9]}, h2.deep_merge(h1)) + javascript: + setup: | + var h1 = { x: { y: [4, 5, 6] }, z: [7, 8, 9] } + var h2 = { x: { y: [7, 8, 9] }, z: 'xyz' } + asserts: + - | + assertEqual({x: {y: [7, 8, 9]}, z: "xyz"}, deepMerge(h1, h2)) + - | + assertEqual({x: {y: [4, 5, 6]}, z: [7, 8, 9]}, deepMerge(h2, h1)) diff --git a/issues/hash_with_indifferent_access.yml b/issues/hash_with_indifferent_access.yml new file mode 100644 index 00000000..99627d00 --- /dev/null +++ b/issues/hash_with_indifferent_access.yml @@ -0,0 +1,14 @@ +level: easy +tags: [hash] +description: Implement a hash class which does not distinguish between symbols and strings for its keys. +checks: + ruby: + setup: | + composers = HashWithIndifferentAccess.new + composers[:Janacek] = "Leos Janacek" + composers["Sweelinck"] = "Jan Pieterszoon Sweelinck" + + mathematicians = HashWithIndifferentAccess["Yutaka", "Taniyama", :Alonzo, "Church"] + asserts: ['assert_equal "Leos Janacek", composers["Janacek"]', 'assert_equal "Jan + Pieterszoon Sweelinck", composers[:Sweelinck]', 'assert_equal "Taniyama", + mathematicians[:Yutaka]', 'assert_equal mathematicians["Alonzo"], mathematicians[:Alonzo]'] diff --git a/issues/integer_difference.yml b/issues/integer_difference.yml new file mode 100644 index 00000000..9d22b05c --- /dev/null +++ b/issues/integer_difference.yml @@ -0,0 +1,12 @@ +level: easy +tags: [array, arithmetic] +description: | + Write a function that accepts an array of random integers and an integer n. + Determine the number of times where two integers in the array have the difference of n. +checks: + ruby: + asserts: ['assert_equal 3, integer_difference(4, [1, 1, 5, 6, 9, 16, 27])', 'assert_equal + 4, integer_difference(2, [1, 1, 3, 3])'] + javascript: + asserts: ['assertEqual(3, integerDifference(4, [1, 1, 5, 6, 9, 16, 27]))', 'assertEqual(4, + integerDifference(2, [1, 1, 3, 3]))'] diff --git a/issues/invert_hash.yml b/issues/invert_hash.yml new file mode 100644 index 00000000..1d11169a --- /dev/null +++ b/issues/invert_hash.yml @@ -0,0 +1,10 @@ +level: elementary +tags: [enumerable, hash] +description: Rewrite source hash to new representation +checks: + ruby: + asserts: ['assert_equal({10=>:c, 66=>:f, 78=>:b, "gAqtSP"=>:d}, invert_hash({:b=>78, + :c=>10, :d=>"gAqtSP", :f=>66}))', 'assert_equal({0=>[:c, :d], 55=>:b, "jXXuj1"=>:a}, + invert_hash({:a=>"jXXuj1", :b=>55, :c=>0, :d=>0}))', 'assert_equal({0=>[:c, + :d, :e], 55=>:b, "jXXuj1"=>:a}, invert_hash({:a=>"jXXuj1", :b=>55, :c=>0, + :d=>0, :e=>0}))'] diff --git a/issues/join_url_params.yml b/issues/join_url_params.yml new file mode 100644 index 00000000..ee7d73d1 --- /dev/null +++ b/issues/join_url_params.yml @@ -0,0 +1,21 @@ +level: easy +tags: [hash, string] +checks: + ruby: + setup: | + url_params = {first_param: 123, second_param: 456, third_param: 678} + + url = "http://www.foobar.com" + expected = "http://www.foobar.com?first_param=123&second_param=456&third_param=678" + asserts: ['assert_equal expected, join_params(url, url_params)'] + javascript: + setup: | + var urlParams = { + first_param: 123, + second_param: 456, + third_param: 678 + } + + var url = "http://www.foobar.com" + var expected = "http://www.foobar.com?first_param=123&second_param=456&third_param=678" + asserts: ['assertEqual(expected, join_params(url, urlParams))'] diff --git a/issues/key_for_min_value.yml b/issues/key_for_min_value.yml new file mode 100644 index 00000000..6b68a019 --- /dev/null +++ b/issues/key_for_min_value.yml @@ -0,0 +1,14 @@ +level: elementary +tags: [hash] +checks: + ruby: + asserts: ['assert_equal "j", key_for_min_value({"k" => 2, "h" => 3, "j" => 1})', + 'assert_equal "z", key_for_min_value({"o" => 0, "z" => -2, "j" => 1})', 'assert_equal + nil, key_for_min_value({})'] + javascript: + asserts: + - | + assertEqual("j", keyForMinValue({k: 2, h: 3, j: 1})) + - | + assertEqual("z", keyForMinValue({o: 0, z: -2, j: 1})) + - assertEqual(null, keyForMinValue({})) diff --git a/issues/mash.yml b/issues/mash.yml new file mode 100644 index 00000000..b18bea16 --- /dev/null +++ b/issues/mash.yml @@ -0,0 +1,15 @@ +level: easy +tags: [hash] +author: {github_nickname: intridea, web_page: 'http://intridea.com'} +description: Mash is an extended Hash that gives simple pseudo-object functionality that can be built from hashes and easily extended. It is designed to be used in RESTful API libraries to provide easy object-like access to JSON and XML parsed hashes. +checks: + ruby: + setup: | + mash = Mash.new + mash.name = "My Mash" + + mash2 = Mash.new + mash2.author!.name = "Michael Bleigh" + asserts: ['assert !mash.post?', 'assert_equal nil, mash.post', 'assert_equal "My + Mash", mash.name', 'assert mash.name?', 'assert_equal "Michael Bleigh", mash2.author', + 'assert mash.author.is_a?(Mash)'] diff --git a/issues/maybe.yml b/issues/maybe.yml new file mode 100644 index 00000000..4327de05 --- /dev/null +++ b/issues/maybe.yml @@ -0,0 +1,52 @@ +level: elementary +tags: [monads] +checks: + ruby: + description: | + When you want to call a method on an object only if it is not nil, + the simplest way to achieve it is with conditional statements, adding unnecessary clutter. + The alternative is to use try. try is like Object#send except that it returns nil if sent to nil. + asserts: + - assert_equal nil, nil.try(:hello) + - assert_equal 5, 'hello'.try(:length) + - assert 3.try(:is_a?, Numeric) + - assert_equal nil, nil.try {|obj| obj.pifpaf} + - | + assert_equal "superclass: Object", String.try {|obj| "superclass: #{obj.superclass}"} + javascript: + description: | + var person = { + "name":"Homer Simpson", + "address": { + "street":"123 Fake St.", + "city":"Springfield" + } + }; + + if (person != null && person["address"] != null) { + var state = person["address"]["state"]; + if (state != null) { + console.log(state); + } + else { + console.log("State unknown"); + } + } + setup: | + var person1 = null; + var person2 = {address: "street"}; + var person3 = {address: {state: "State unknown"}} + var person4 = {address: {state: "california"}} + + var maybe = function(person) { + return Maybe(person).bind(function(p) { + return p["address"]; + }).bind(function(a) { + return a["state"]; + }).maybe("State unknown", function(s) { + return s; + })(); + }; + asserts: ['assertEqual("State unknown", maybe(person1))', 'assertEqual("State + unknown", maybe(person2))', 'assertEqual("State unknown", maybe(person3))', + 'assertEqual("california", maybe(person4))'] diff --git a/issues/mixins.yml b/issues/mixins.yml new file mode 100644 index 00000000..9b3bb3eb --- /dev/null +++ b/issues/mixins.yml @@ -0,0 +1,12 @@ +level: elementary +tags: [mixin] +checks: + ruby: + setup: | + guy, girl, dog = Man.new, Woman.new, Dog.new + guy.set_name("John") + girl.set_name("Jessika") + dog.set_name("Pit") + asserts: ['assert_equal "John", guy.name', 'assert_equal true, guy.human?', 'assert_equal + "Pit", dog.name', 'assert_equal Object, Man.superclass', 'assert_equal Object, + Woman.superclass', 'assert_equal Object, Dog.superclass'] diff --git a/issues/more_than_medium.yml b/issues/more_than_medium.yml new file mode 100644 index 00000000..a56531c7 --- /dev/null +++ b/issues/more_than_medium.yml @@ -0,0 +1,16 @@ +level: easy +tags: [arithmetic, string] +description: | + Write a function that will return the words, + which are longer than the average length of all the words. +checks: + ruby: + asserts: ['assert_equal ["This", "sample", "string"], more_than_medium("This is + a sample string")', 'assert_equal ["another", "sample"], more_than_medium("Some + another sample")', 'assert_equal [], more_than_medium("Do, do, do, do... do + it!")'] + javascript: + asserts: ['assertEqual(["This", "sample", "string"], moreThanMedium("This is a + sample string"))', 'assertEqual(["another", "sample"], moreThanMedium("Some + another sample"))', 'assertEqual([], moreThanMedium("Do, do, do, do... do + it!"))'] diff --git a/issues/parse_date.yml b/issues/parse_date.yml new file mode 100644 index 00000000..dec64e11 --- /dev/null +++ b/issues/parse_date.yml @@ -0,0 +1,13 @@ +level: easy +tags: [] +description: | + Write a function to parse some dates and return a standard format. Hint: the help page might be of use. +checks: + ruby: + asserts: ['assert_equal "Nov 30, 1835", pretty_date("11-30-1835")', 'assert_equal + "Jul 20, 1933", pretty_date("July 20, 1933")', 'assert_equal "Nov 11, 1922", + pretty_date("1922-11-11")', 'assert_equal "Sep 20, 1978", pretty_date("9/20/1978")'] + javascript: + asserts: ['assertEqual("Nov 30, 1835", prettyDate("11-30-1835"))', 'assertEqual("Jul + 20, 1933", prettyDate("July 20, 1933"))', 'assertEqual("Nov 11, 1922", prettyDate("1922-11-11"))', + 'assertEqual("Sep 20, 1978", prettyDate("9/20/1978"))'] diff --git a/issues/pascals_triangle.yml b/issues/pascals_triangle.yml new file mode 100644 index 00000000..d6d4dd70 --- /dev/null +++ b/issues/pascals_triangle.yml @@ -0,0 +1,12 @@ +level: medium +tags: [arithmetic, math] +description: Write a method that will return the nth row of Pascal's Triangle. +checks: + ruby: + asserts: ['assert_equal [1], pt(0)', 'assert_equal [1, 1], pt(1)', 'assert_equal + [1, 2, 1], pt(2)', 'assert_equal [1, 3, 3, 1], pt(3)', 'assert_equal [1, 4, + 6, 4, 1], pt(4)'] + javascript: + asserts: ['assertEqual([1], pt(0))', 'assertEqual([1, 1], pt(1))', 'assertEqual([1, + 2, 1], pt(2))', 'assertEqual([1, 3, 3, 1], pt(3))', 'assertEqual([1, 4, 6, + 4, 1], pt(4))'] diff --git a/issues/power_digits_sum.yml b/issues/power_digits_sum.yml new file mode 100644 index 00000000..96df4b7c --- /dev/null +++ b/issues/power_digits_sum.yml @@ -0,0 +1,10 @@ +level: elementary +tags: [arithmetic] +description: | + Create a method to calculate sum of digits for 2**n + For example, 2**15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. +checks: + ruby: + asserts: ['assert_equal 13, power_digits_sum(8)', 'assert_equal 26, power_digits_sum(15)'] + javascript: + asserts: ['assertEqual(13, powerDigitsSum(8))', 'assertEqual(26, powerDigitsSum(15))'] diff --git a/issues/prime_factorization.yml b/issues/prime_factorization.yml new file mode 100644 index 00000000..d183274a --- /dev/null +++ b/issues/prime_factorization.yml @@ -0,0 +1,12 @@ +level: medium +tags: [arithmetic, math] +description: Any number can be written as a product of prime numbers. For example, 42 would be "2 * 3 * 7" while 44 would be "2 * 2 * 11". Write a number to return an array of prime factors in ascending order for a given number. +checks: + ruby: + asserts: ['assert_equal [2, 3, 7], prime_factorization(42)', 'assert_equal [2, + 2, 11], prime_factorization(44)', 'assert_equal [3, 5, 5], prime_factorization(75)', + 'assert_equal [2, 2, 2, 2, 2, 2, 3, 643], prime_factorization(123456)'] + javascript: + asserts: ['assertEqual([2, 3, 7], primeFactorization(42))', 'assertEqual([2, 2, + 11], primeFactorization(44))', 'assertEqual([3, 5, 5], primeFactorization(75))', + 'assertEqual([2, 2, 2, 2, 2, 2, 3, 643], primeFactorization(123456))'] diff --git a/issues/prime_number.yml b/issues/prime_number.yml new file mode 100644 index 00000000..ca7240ce --- /dev/null +++ b/issues/prime_number.yml @@ -0,0 +1,9 @@ +level: elementary +tags: [arithmetic] +description: Write a function that will return whether the argument is prime number +checks: + ruby: + asserts: ['assert !is_prime?(1)', 'assert is_prime?(2)', 'assert is_prime?(3)', + 'assert !is_prime?(10)'] + javascript: + asserts: [assert(!isPrime(1)), assert(isPrime(2)), assert(isPrime(3)), assert(!isPrime(10))] diff --git a/issues/reciprocal_cycles.yml b/issues/reciprocal_cycles.yml new file mode 100644 index 00000000..02004f54 --- /dev/null +++ b/issues/reciprocal_cycles.yml @@ -0,0 +1,22 @@ +level: medium +tags: [arithmetic, math] +description: | + A unit fraction contains 1 in the numerator. + The decimal representation of the unit fractions with denominators 2 to 10 are given: + 1/2 = 0.5 + 1/3 = 0.(3) + 1/4 = 0.25 + 1/5 = 0.2 + 1/6 = 0.1(6) + 1/7 = 0.(142857) + 1/8 = 0.125 + 1/9 = 0.(1) + 1/10 = 0.1 + Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. + It can be seen that 1/7 has a 6-digit recurring cycle. + Find the value of d < 100 for which 1/d contains the longest recurring cycle in its decimal fraction part. +checks: + ruby: + asserts: ['assert_equal 7, longest_recurring_cycle(10)', 'assert_equal 13, longest_recurring_cycle(16)'] + javascript: + asserts: ['assertEqual(7, longestRecurringCycle(10))', 'assertEqual(13, longestRecurringCycle(16))'] diff --git a/issues/redefine_nil.yml b/issues/redefine_nil.yml new file mode 100644 index 00000000..14b7016d --- /dev/null +++ b/issues/redefine_nil.yml @@ -0,0 +1,6 @@ +level: easy +tags: [] +description: Prevent the following code from throwing an expection +checks: + ruby: + asserts: [assert nil.length != 5, assert nil.reverse != "rubeque"] diff --git a/issues/related_keys_of_hash.yml b/issues/related_keys_of_hash.yml new file mode 100644 index 00000000..e03aa2a6 --- /dev/null +++ b/issues/related_keys_of_hash.yml @@ -0,0 +1,19 @@ +level: elementary +tags: [hash] +checks: + ruby: + asserts: + - | + assert_equal [:a], keys_of(1, {a: 1, b: 2, c: 3}) + - | + assert_equal [:a, :d], keys_of(1, {a: 1, b: 2, c: 3, d: 1}) + - | + assert_equal [:a, :b, :d], keys_of([1, 2], {a: 1, b: 2, c: 3, d: 1}) + javascript: + asserts: + - | + assertEqual(["a"], keyOf(1, {a: 1, b: 2, c: 3})) + - | + assertEqual(["a", "d"], keyOf(1, {a: 1, b: 2, c: 3, d: 1})) + - | + assertEqual(["a", "b", "d"], keyOf([1, 2], {a: 1, b: 2, c: 3, d: 1})) diff --git a/issues/reverse_each_word.yml b/issues/reverse_each_word.yml new file mode 100644 index 00000000..45900bb3 --- /dev/null +++ b/issues/reverse_each_word.yml @@ -0,0 +1,11 @@ +level: elementary +tags: [string] +checks: + ruby: + setup: | + str = "olleH ,ereht dna woh era ?uoy" + asserts: ['assert_equal str, reverse_each_word("Hello there, and how are you?")'] + javascript: + setup: | + str = "olleH ,ereht dna woh era ?uoy" + asserts: ['assertEqual(str, reverseEachWord("Hello there, and how are you?"))'] diff --git a/issues/separate_with_comma.yml b/issues/separate_with_comma.yml new file mode 100644 index 00000000..f870dcc5 --- /dev/null +++ b/issues/separate_with_comma.yml @@ -0,0 +1,15 @@ +level: elementary +tags: [string] +checks: + ruby: + asserts: ['assert_equal "1", separate_with_comma(1)', 'assert_equal "10", + separate_with_comma(10)', 'assert_equal "100", separate_with_comma(100)', + 'assert_equal "1,000", separate_with_comma(1000)', 'assert_equal "10,000", + separate_with_comma(10000)', 'assert_equal "100,000", separate_with_comma(100000)', + 'assert_equal "1,000,000", separate_with_comma(1000000)'] + javascript: + asserts: ['assertEqual( "1", separateWithComma(1))', 'assertEqual( "10", + separateWithComma(10))', 'assertEqual( "100", separateWithComma(100))', + 'assertEqual( "1,000", separateWithComma(1000))', 'assertEqual( "10,000", + separateWithComma(10000))', 'assertEqual( "100,000", separateWithComma(100000))', + 'assertEqual("1,000,000", separateWithComma(1000000))'] diff --git a/issues/sexp_eval.yml b/issues/sexp_eval.yml new file mode 100644 index 00000000..08305c8a --- /dev/null +++ b/issues/sexp_eval.yml @@ -0,0 +1,11 @@ +level: medium +tags: [] +description: Take these S-expressions and evaluate them as if they were Ruby code. +checks: + ruby: + setup: | + sexp1 = [:flatten, [1, 2, [:to_a, (4..6)], [:-, 8, 4]]] + sexp2 = [:==, [:*, 2, 3], [:remainder, 13, 7]] + sexp3 = [(-> x, y {Math.sqrt(x**2 + y**2)}), [:-, 9, 3, 3], [:+, 1, 1, 1, 1]] + asserts: ['assert_equal [1, 2, 4, 5, 6, 4], sexp_eval(sexp1)', 'assert_equal true, + sexp_eval(sexp2)', 'assert_equal 5.0, sexp_eval(sexp3)'] diff --git a/issues/shell_words_split.yml b/issues/shell_words_split.yml new file mode 100644 index 00000000..028e52db --- /dev/null +++ b/issues/shell_words_split.yml @@ -0,0 +1,10 @@ +level: medium +tags: [string] +checks: + ruby: + asserts: ['assert_equal([''here'', ''are'', ''two words''], shell_words_split(''here + are "two words"''))', 'assert_equal([''foo'', ''bar'', ''foo bar baz'', ''foo + bar''], shell_words_split(%q{foo bar ''foo bar''\ baz ''foo bar''}))', 'assert_equal([''A'', + ''B'', ''C'', ''D E F'', ''G,H I''], shell_words_split(%q{A B C ''D E F'' + ''G'',''H I''}))', 'assert_equal([''grep'', ''Jan 15'', ''file name with spaces''], + shell_words_split(''grep Jan\ 15 file\ name\ with\ spaces''))'] diff --git a/issues/simple_object.yml b/issues/simple_object.yml new file mode 100644 index 00000000..a85bc623 --- /dev/null +++ b/issues/simple_object.yml @@ -0,0 +1,13 @@ +level: elementary +tags: [object] +checks: + ruby: + setup: | + joe = Person.new('Joe') + asserts: ['assert_equal ''Hello Kate, my name is Joe'', joe.greet(''Kate'')', + 'assert_equal ''Joe'', joe.name'] + javascript: + setup: | + joe = new Person('Joe') + asserts: ['assertEqual(''Hello Kate, my name is Joe'', joe.greet(''Kate''))', + 'assertEqual(''Joe'', joe.name())'] diff --git a/issues/stack.yml b/issues/stack.yml new file mode 100644 index 00000000..e8b3487c --- /dev/null +++ b/issues/stack.yml @@ -0,0 +1,16 @@ +level: easy +tags: [] +description: | + I like big stacks and I cannot lie + All you other developers can't deny + That linked lists have too much hype + And you'd rather use a last-in-first-out datatype +checks: + ruby: + setup: stack = Stack.new([5, 6, 7, 8]) + asserts: ['assert_equal 8, stack.pop', 'assert_equal 7, stack.pop', 'assert stack.push([4, + 2])', 'assert_equal [2, 4, 6], stack.pop(3)'] + javascript: + setup: stack = new Stack([5, 6, 7, 8]) + asserts: ['assertEqual(8, stack.pop())', 'assertEqual(7, stack.pop())', 'assert(stack.push([4, + 2]))', 'assertEqual([2, 4, 6], stack.pop(3))'] diff --git a/issues/string_format.yml b/issues/string_format.yml new file mode 100644 index 00000000..a7d9ce51 --- /dev/null +++ b/issues/string_format.yml @@ -0,0 +1,9 @@ +level: elementary +tags: [string] +checks: + ruby: + asserts: ['assert_equal "Value is 00005", solution(5)', 'assert_equal "Value is + 00123", solution(123)', 'assert_equal "Value is 12345", solution(12345)'] + javascript: + asserts: ['assertEqual("Value is 00005", solution(5))', 'assertEqual("Value is + 00123", solution(123))', 'assertEqual("Value is 12345", solution(12345))'] diff --git a/issues/sum_of_array_plus_one.yml b/issues/sum_of_array_plus_one.yml new file mode 100644 index 00000000..b03695c4 --- /dev/null +++ b/issues/sum_of_array_plus_one.yml @@ -0,0 +1,9 @@ +level: elementary +tags: [array, arithmetic] +description: | + Write a function that takes an array of integers + and returns the sum of the integers after adding 1 to each. +checks: + javascript: + asserts: ['assertEqual(14, plusOneSum([1, 2, 3, 4]))', 'assertEqual(16, plusOneSum([12, + -3, 0, 3]))'] diff --git a/issues/sum_primes.yml b/issues/sum_primes.yml new file mode 100644 index 00000000..5af95256 --- /dev/null +++ b/issues/sum_primes.yml @@ -0,0 +1,8 @@ +level: medium +tags: [] +description: Write some code to sum the first 1000 primes without using Ruby's Prime class. +checks: + ruby: + asserts: ['assert_equal 76127, sum_primes(1000)', 'assert_equal 4227, sum_primes(200)'] + javascript: + asserts: ['assertEqual(76127, sumPrimes(1000))', 'assertEqual(4227, sumPrimes(200))'] diff --git a/issues/trash.yml b/issues/trash.yml new file mode 100644 index 00000000..ab6e0c92 --- /dev/null +++ b/issues/trash.yml @@ -0,0 +1,14 @@ +level: medium +tags: [hash] +author: {github_nickname: intridea, web_page: 'http://intridea.com'} +description: null +checks: + ruby: + setup: | + class Result < Trash + property :id, :with => lambda { |v| v.to_i } + property :created_at, :from => :creation_date, :with => lambda { |v| Time.parse(v) } + end + + result = Result.new(id: '123', creation_date: '2012-03-30 17:23:28') + asserts: ['assert_equal 123, result.id', 'assert_equal Time, result.created_at.class'] diff --git a/issues/twenty_one.yml b/issues/twenty_one.yml new file mode 100644 index 00000000..0995a053 --- /dev/null +++ b/issues/twenty_one.yml @@ -0,0 +1,9 @@ +level: elementary +tags: [] +checks: + ruby: + asserts: ['assert twenty_one?(3, 4, 5, 6, 3)', 'assert !twenty_one?(3, 11, 10)', + 'assert twenty_one?(10, 11)', 'assert !twenty_one?(10, 10)'] + javascript: + asserts: ['assert(isTwentyOne(3, 4, 5, 6, 3))', 'assert(!isTwentyOne(3, 11, 10))', + 'assert(isTwentyOne(10, 11))', 'assert(!isTwentyOne(10, 10))'] diff --git a/issues/word_positions.yml b/issues/word_positions.yml new file mode 100644 index 00000000..42bfac91 --- /dev/null +++ b/issues/word_positions.yml @@ -0,0 +1,10 @@ +level: elementary +tags: [array, string] +description: | + Given a text file and a word, find the positions that the word occurs in the file. + We’ll be asked to find the positions of many words in the same file. +checks: + javascript: + asserts: ['assertEqual([0], wordPositions(''test'')(''test''))', 'assertEqual([0, + 4, 9], wordPositions(''test test test'')(''test''))', 'assertEqual([11], wordPositions(''find + a word in some sentence'')(''in''))'] diff --git a/project.clj b/project.clj index 79dc7250..c2ff13a4 100644 --- a/project.clj +++ b/project.clj @@ -14,4 +14,5 @@ 'pst)] (alter-var-root orig (constantly (deref new))))] :source-paths ["src"] - :eval-in :nrepl) + :eval-in :nrepl + :main battle-asserts.core) diff --git a/source/flatten.yml b/source/flatten.yml index 0db15a2a..8c610c24 100644 --- a/source/flatten.yml +++ b/source/flatten.yml @@ -5,7 +5,8 @@ tags: [] checks: ruby: asserts: - - assert_equal [1, 2, {:a => :b}], flatten([1, 2, [[{a: :b}]]]) + - | + assert_equal [1, 2, {:a => :b}], flatten([1, 2, [[{a: :b}]]]) - assert_equal [1, 2, 3, 4, 5, 6, 7, 8], flatten([1, [2, 3, [4, 5, [6, 7]]], 8]) - assert_equal [], flatten([]) - assert_equal [1, 2, 3, 4], flatten([[1, 2], [3, 4]]) diff --git a/src/battle_asserts/core.clj b/src/battle_asserts/core.clj index 47d405b6..6b14b556 100644 --- a/src/battle_asserts/core.clj +++ b/src/battle_asserts/core.clj @@ -3,27 +3,21 @@ (:require [battle-asserts.checker :as checker] [battle-asserts.reader :as reader])) -(defn create-source-dir [file-name] - (str "source/" file-name "/")) - -(defn create-output-dir [file-name] - (str "issues/" )) - -(defn get-sub-dir [folder] +(defn get-files-in-dir [folder] (map - #(.getName %) + #((re-find #"(.+?)(\.[^.]*$|$)" (.getName %)) 1) (filter - #(.isDirectory %) - (drop 1 (file-seq (clojure.java.io/file folder)))))) + #(not (.isDirectory %)) + (file-seq (clojure.java.io/file folder))))) (defn transform-file [file-name] - (let [src-dir (create-source-dir file-name) - out-dir (create-output-dir file-name) + (let [src-dir "source/" + out-dir "issues/" src-url (str src-dir "/" file-name ".yml")] (if (checker/check-file src-url) (reader/transform file-name src-dir out-dir) (throw (Exception. "Wrong input file"))))) -(defn transform-all-in [folder-name] - (map transform-file (get-sub-dir folder-name))) +(defn -main [] + (doall (map #(transform-file %) (get-files-in-dir "source")))) \ No newline at end of file diff --git a/src/battle_asserts/reader.clj b/src/battle_asserts/reader.clj index 32ec12cb..fa2cd354 100644 --- a/src/battle_asserts/reader.clj +++ b/src/battle_asserts/reader.clj @@ -17,35 +17,33 @@ (with-open [wrtr (io/writer file-name)] (.write wrtr file-yml))) -(defn- generate-check-for-lang [lang input-dir] - (clojure.string/split-lines (clojure.string/replace - (mc/prettify-code - (keyword lang ) - (parse-solution/get-asserts-from-file - (str input-dir "multicode.clj"))) - #"'" "\""))) - -(defn- generate-check-for-langs [source-hash input-dir] - (let [langs ((source-hash :multicode_checks) :langs)] - (reduce #(assoc %1 (keyword %2) (generate-check-for-lang %2 input-dir)) {} langs))) - -(defn- generate-hash [source-hash input-dir] +(defn- generate-check-for-lang [lang file-name] + (mc/prettify-code + (keyword lang ) + (parse-solution/get-asserts-from-file + (str "test/battle_solutions/" file-name "_test.clj")))) + +(defn- generate-check-for-langs [source-hash file-name] + (let [langs ((or (source-hash :multicode_checks) {}) :langs)] + (reduce #(assoc %1 (keyword %2) (generate-check-for-lang %2 file-name)) {} langs))) + +(defn- generate-hash [source-hash file-name] (dissoc (assoc source-hash :checks (merge (source-hash :checks) - (generate-check-for-langs source-hash input-dir))) + (generate-check-for-langs source-hash file-name))) :multicode_checks)) (defn- create-yml - [file-hash input-dir] - (yaml/generate-string (into {} (reverse (generate-hash file-hash input-dir))))) + [file-hash file-name] + (yaml/generate-string (into {} (reverse (generate-hash (into {} (reverse file-hash)) file-name))))) (defn transform "input file name, output file name" [file-name input-dir output-dir] (let [input-path (str input-dir file-name ".yml") output-path (str output-dir file-name ".yml")] - (write-file (create-yml (parse-file (read-file input-path)) input-dir) output-path))) \ No newline at end of file + (write-file (create-yml (parse-file (read-file input-path)) file-name) output-path))) \ No newline at end of file