Make function negative-safe
def fibonacci(x): if x < 0: return None if x == 0 or x == 1: return x return fibonacci(x - 1) + fibonacci(x - 2)
- def fibonacci(x):
- if x < 0:
- return None
- if x == 0 or x == 1:
- return x
else :return fibonacci(x-1) + fibonacci(x-2)- return fibonacci(x - 1) + fibonacci(x - 2)
Test.describe('fibonacci(n)') Test.it('should calculate properly') test.assert_equals(fibonacci(8), 21) Test.it('should fail if n is negative') test.assert_equals(fibonacci(-1), None)
# TODO: Replace examples and use TDD development by writing your own tests# These are some of the methods available:# test.expect(boolean, [optional] message)test.assert_equals(fibonacci(8),21 )# test.assert_not_equals(actual, expected, [optional] message)- Test.describe('fibonacci(n)')
- Test.it('should calculate properly')
- test.assert_equals(fibonacci(8), 21)
# You can use Test.describe and Test.it to write BDD style test groupings- Test.it('should fail if n is negative')
- test.assert_equals(fibonacci(-1), None)
Now with 100% more lambda!
fibonacci = lambda n: n if n == 0 or n == 1 else None if n < 0 else fibonacci(n - 1) + fibonacci(n - 2)
def fibonacci(x):if x == 0 or x == 1:return xelse :return fibonacci(x-1) + fibonacci(x-2)- fibonacci = lambda n: n if n == 0 or n == 1 else None if n < 0 else fibonacci(n - 1) + fibonacci(n - 2)
Test.describe('fibonacci(n)') Test.it('should calculate properly') test.assert_equals(fibonacci(8), 21) Test.it('should fail if n is negative') test.assert_equals(fibonacci(-1), None)
# TODO: Replace examples and use TDD development by writing your own tests# These are some of the methods available:# test.expect(boolean, [optional] message)test.assert_equals(fibonacci(8),21 )# test.assert_not_equals(actual, expected, [optional] message)- Test.describe('fibonacci(n)')
- Test.it('should calculate properly')
- test.assert_equals(fibonacci(8), 21)
# You can use Test.describe and Test.it to write BDD style test groupings- Test.it('should fail if n is negative')
- test.assert_equals(fibonacci(-1), None)
- Adds a second argument to
generateComment
,seed
, which allows for deterministic strings - Adds a few tests (still bad coverage)
const generateComment = (array = [], seed) => (seed => array.map((each, ii) => each[seed[ii]]).join` `) (seed ? seed.split`.`.map(Number) : array.map(ii => Math.round(Math.random() * (ii.length - 1)))) const dictionary = [ ['hi,', 'hello,', 'hey,'], ['how are'], ['you', 'ya'], ['doing', 'going'], ['?', '?!', ''] ] console.log(generateComment(dictionary)) console.log(generateComment(dictionary, '2.0.1.0.1'))
const generateComment = array => array.map(x=>x[Math.floor(Math.random() * x.length)]).join` `- const generateComment = (array = [], seed) =>
- (seed => array.map((each, ii) => each[seed[ii]]).join` `)
- (seed
- ? seed.split`.`.map(Number)
- : array.map(ii => Math.round(Math.random() * (ii.length - 1))))
- const dictionary = [
- ['hi,', 'hello,', 'hey,'],
- ['how are'],
- ['you', 'ya'],
- ['doing', 'going'],
- ['?', '?!', '']
- ]
// console.log(generateComment([['hi,', 'hello,', 'hey,'], ['how are'], ['you', 'ya'], ['doing', 'going'], ['?', '?!', '']]));- console.log(generateComment(dictionary))
- console.log(generateComment(dictionary, '2.0.1.0.1'))
describe('generateComment', () => { it('should work with null values', () => { Test.assertEquals(typeof generateComment(), 'string') }) it('should return a string from any array', () => { Test.assertEquals( typeof generateComment([['sup', 'yo'], ['mate', 'buddy']]), 'string' ) }) it('should work deterministically if a second argument is provided', () => { Test.assertEquals( generateComment(dictionary, '2.0.1.0.1'), 'hey, how are ya doing ?!', 'Strings don\'t match' ) Test.assertEquals( generateComment(dictionary, '0.0.0.0.0'), 'hi, how are you doing ?', 'Strings don\'t match' ) }) })
// TODO: Replace examples and use TDD development by writing your own tests- describe('generateComment', () => {
- it('should work with null values', () => {
- Test.assertEquals(typeof generateComment(), 'string')
- })
- it('should return a string from any array', () => {
- Test.assertEquals(
- typeof generateComment([['sup', 'yo'], ['mate', 'buddy']]),
- 'string'
- )
- })
// These are some CW specific test methods available://Test.expect(boolean, [optional] message)//Test.assertEquals(actual, expected, [optional] message)//Test.assertSimilar(actual, expected, [optional] message)//Test.assertNotEquals(actual, expected, [optional] message)// NodeJS assert is also automatically required for you.// assert(true)// assert.strictEqual({a: 1}, {a: 1})// assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]})// You can also use Chai (http://chaijs.com/) by requiring it yourself// var expect = require("chai").expect;// var assert = require("chai").assert;// require("chai").should();/*describe("Solution", function(){it("Test for the given arrays", function(){Test.assertEquals([['hi,', 'hello,', 'hey,'], ['how are'], ['you', 'ya'], ['doing', 'going'], ['?','?!', '']], 'hi, how are you doing?');});});*/- it('should work deterministically if a second argument is provided', () => {
- Test.assertEquals(
- generateComment(dictionary, '2.0.1.0.1'),
- 'hey, how are ya doing ?!',
- 'Strings don\'t match'
- )
- Test.assertEquals(
- generateComment(dictionary, '0.0.0.0.0'),
- 'hi, how are you doing ?',
- 'Strings don\'t match'
- )
- })
- })