#include <string> bool hasCapital(std::string password) { for(int i=0; i < password.length(); i++) { if (password[i] > 64 && password[i] < 91) { return true; } } return false; } bool hasSpecial(std::string password) { char temp; for(int i=0;i<password.length();i++) { temp = password[i]; if ((temp > 32 && temp < 48) || (temp > 57 && temp < 64)) { return true; } } return false; } bool isLongPassword(std::string password) { return password.length() > 7; } bool testPassword(std::string password) { bool cap = hasCapital(password); bool spec = hasSpecial(password); bool number = isLongPassword(password); //provide final answer if (cap && number && spec) { return true; } else { return false; } }
- #include <string>
bool testPassword(std::string password)- bool hasCapital(std::string password)
- {
bool cap = false;bool spec = false;bool number = false;- for(int i=0; i < password.length(); i++)
- {
- if (password[i] > 64 && password[i] < 91)
- {
- return true;
- }
- }
- return false;
- }
- bool hasSpecial(std::string password)
- {
- char temp;
- for(int i=0;i<password.length();i++)
- {
- temp = password[i];
- if ((temp > 32 && temp < 48) || (temp > 57 && temp < 64))
- {
spec = true;- return true;
- }
- }
//check each digit and see if any are capital lettersfor(int i=0;i<password.length();i++){if (password[i] > 64 && password[i] < 91){cap = true;}- return false;
- }
//see if the password is over 8 digits longif (password.length() > 7)- bool isLongPassword(std::string password)
- {
number = true;- return password.length() > 7;
- }
- bool testPassword(std::string password)
- {
- bool cap = hasCapital(password);
- bool spec = hasSpecial(password);
- bool number = isLongPassword(password);
- //provide final answer
- if (cap && number && spec)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
const expect = require('chai').expect describe("testPassword", function() { context("when the password is strong", function() { let password = "Pas$word1"; it("returns true", function () { expect(testPassword(password)).to.be.true; }) }); context("when the password is missing uppercase letters", function() { let password = "all_lower_case!"; it("returns false", function () { expect(testPassword(password)).to.be.false; }) }); context("when the password is missing special charaters", function() { let password = "noSpecialsAllowed"; it("returns false", function () { expect(testPassword(password)).to.be.false; }) }); context("when the password is too short", function() { let password = "Hi!"; it("returns false", function () { expect(testPassword(password)).to.be.false; }) }); });
Test.assertEquals(testPassword("password"), false);Test.assertEquals(testPassword("Pas$word1"), true);Test.assertEquals(testPassword("WodT$m1"), false);- const expect = require('chai').expect
- describe("testPassword", function() {
- context("when the password is strong", function() {
- let password = "Pas$word1";
- it("returns true", function () { expect(testPassword(password)).to.be.true; })
- });
- context("when the password is missing uppercase letters", function() {
- let password = "all_lower_case!";
- it("returns false", function () { expect(testPassword(password)).to.be.false; })
- });
- context("when the password is missing special charaters", function() {
- let password = "noSpecialsAllowed";
- it("returns false", function () { expect(testPassword(password)).to.be.false; })
- });
- context("when the password is too short", function() {
- let password = "Hi!";
- it("returns false", function () { expect(testPassword(password)).to.be.false; })
- });
- });
function testPassword(password){ let tests = [hasUppercase,hasSpecial,isLong]; // every test must pass return tests.every(func=>func(password)); } // checks if password has a special character in it function hasSpecial(password) { return password.split('').some((char) => { let charCode = char.charCodeAt(); return (charCode > 32 && charCode < 48) || (charCode > 57 && charCode < 64); }); } // checks if a password has an uppercase character in it function hasUppercase(password) { return password.split('').some((char) => { let charCode = char.charCodeAt(); return charCode > 64 && charCode < 91; }); } // checks if a password meets the length requirement function isLong(password) { return password.length > 7; }
- function testPassword(password){
- let tests = [hasUppercase,hasSpecial,isLong];
- // every test must pass
- return tests.every(func=>func(password));
- }
- // checks if password has a special character in it
- function hasSpecial(password) {
- return password.split('').some((char) => {
- let charCode = char.charCodeAt();
if((charCode > 32 && charCode < 48) || (charCode > 57 && charCode < 64)){return true;} else {return false;}- return (charCode > 32 && charCode < 48) || (charCode > 57 && charCode < 64);
- });
- }
- // checks if a password has an uppercase character in it
- function hasUppercase(password) {
- return password.split('').some((char) => {
- let charCode = char.charCodeAt();
if(charCode > 64 && charCode < 91){return true;} else {return false;}- return charCode > 64 && charCode < 91;
- });
- }
- // checks if a password meets the length requirement
- function isLong(password) {
if(password.length > 7){return true;}- return password.length > 7;
- }
Algorithms
Logic
Algorithms
Logic
describe("Test cases", function() { let tests = [ { input: [1, 2, [3, 5], [[4, 3], 2]], expected: [1, 2, 3, 5, 4, 3, 2], }, { input: [[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]], expected: [1, 5, -3, 'hi', 'string', 10, 5], }, ] tests.forEach(test => { describe(`when given '${JSON.stringify(test.input)}'`, function () { it("flattens correctly", function() { Test.assertSimilar(flatten(test.input), test.expected); }); }); }); });
describe("Test cases", function() {it("[1, 2, [3, 5], [[4, 3], 2]]", function() {Test.assertSimilar(flatten([1, 2, [3, 5], [[4, 3], 2]]), [1, 2, 3, 5, 4, 3, 2]);});- describe("Test cases", function() {
- let tests = [
- {
- input: [1, 2, [3, 5], [[4, 3], 2]],
- expected: [1, 2, 3, 5, 4, 3, 2],
- },
- {
- input: [[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]],
- expected: [1, 5, -3, 'hi', 'string', 10, 5],
- },
- ]
it("[[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]]", function() {Test.assertSimilar(flatten([[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]]), [1, 5, -3, 'hi', 'string', 10, 5]);- tests.forEach(test => {
- describe(`when given '${JSON.stringify(test.input)}'`, function () {
- it("flattens correctly", function() {
- Test.assertSimilar(flatten(test.input), test.expected);
- });
- });
- });
- });
Algorithms
Logic
describe("Test cases", function() { it("[1, 2, [3, 5], [[4, 3], 2]]", function() { Test.assertSimilar(flatten([1, 2, [3, 5], [[4, 3], 2]]), [1, 2, 3, 5, 4, 3, 2]); }); it("[[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]]", function() { Test.assertSimilar(flatten([[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]]), [1, 5, -3, 'hi', 'string', 10, 5]); }); });
const flatten = arr =>arr.reduce((acc, item) => acc.concat(Array.isArray(item) ? flatten(item) : item), []);- describe("Test cases", function() {
- it("[1, 2, [3, 5], [[4, 3], 2]]", function() {
- Test.assertSimilar(flatten([1, 2, [3, 5], [[4, 3], 2]]), [1, 2, 3, 5, 4, 3, 2]);
- });
- it("[[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]]", function() {
- Test.assertSimilar(flatten([[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]]), [1, 5, -3, 'hi', 'string', 10, 5]);
- });
- });
function testPassword(password){ let hasUppercase = false; let hasSpecial = false; let isLong = false; password.split('').forEach((char) => { let charCode = char.charCodeAt(); if((charCode > 32 && charCode < 48) || (charCode > 57 && charCode < 64)){ hasSpecial = true; } else if(charCode > 64 && charCode < 91){ hasUppercase = true; } }); if(password.length > 7){ isLong = true; } return hasUppercase && isLong && hasSpecial; }
- function testPassword(password){
var cap = false;var spec = false;var number = false;var temp;- let hasUppercase = false;
- let hasSpecial = false;
- let isLong = false;
for(i=0;i<password.length;i++){temp = password[i].charCodeAt();if(temp > 32 && temp < 48){spec = true;} else if(temp > 57 && temp < 64){spec = true;} else if(temp > 64 && temp < 91){cap = true;- password.split('').forEach((char) => {
- let charCode = char.charCodeAt();
- if((charCode > 32 && charCode < 48) || (charCode > 57 && charCode < 64)){
- hasSpecial = true;
- }
- else if(charCode > 64 && charCode < 91){
- hasUppercase = true;
- }
}- });
//see if the password is over 8 digits longif(password.length > 7){number = true;}//provide final answerif(cap && number && spec){return true;} else {return false;- if(password.length > 7){
- isLong = true;
- }
- return hasUppercase && isLong && hasSpecial;
- }
class Stack { // Just to hide the use of array implementation constructor() { this._items = []; } push(data) { this._items.push(data); } pop() { return this._items.pop(); } get length() { return this._items.length; } } class Queue { constructor() { this._stack = new Stack(); } enqueue(data) { this._stack.push(data); } dequeue() { if (this._stack.length === 1) return this._stack.pop(); else { var tmp = this._stack.pop(), result = this.dequeue(); this.enqueue(tmp); return result; } } }
- class Stack { // Just to hide the use of array implementation
- constructor() {
- this._items = [];
- }
- push(data) {
- this._items.push(data);
- }
- pop() {
- return this._items.pop();
- }
length() {- get length() {
- return this._items.length;
- }
- }
- class Queue {
- constructor() {
- this._stack = new Stack();
- }
- enqueue(data) {
- this._stack.push(data);
- }
- dequeue() {
if (this._stack.length() === 1)- if (this._stack.length === 1)
- return this._stack.pop();
- else {
- var tmp = this._stack.pop(), result = this.dequeue();
- this.enqueue(tmp);
- return result;
- }
- }
- }
Test.describe("Queue", function () { Test.it("is first-in-first-out", function () { var queue = new Queue(); let items = [1,2,3]; items.forEach(item => queue.enqueue(item)); Test.assertEquals(queue.dequeue(), 1); Test.assertEquals(queue.dequeue(), 2); Test.assertEquals(queue.dequeue(), 3); }); Test.describe("#enqueue", function() { Test.it("adds the item to the stack", function () { var queue = new Queue(); var mockStack = []; queue._stack = mockStack; queue.enqueue(1); Test.assertSimilar(mockStack, [1]); queue.enqueue(2); Test.assertSimilar(mockStack, [1,2]); }); }); Test.describe("#dequeue", function() { Test.it("removes items from the stack", function () { var queue = new Queue(); var mockStack = [1,2]; queue._stack = mockStack; queue.dequeue(); Test.assertSimilar(mockStack, [2]); queue.dequeue(); Test.assertSimilar(mockStack, []); }); Test.it("returns the first item from the stack", function () { var queue = new Queue(); var mockStack = [1,2]; var result = null; queue._stack = mockStack; result = queue.dequeue(); Test.assertEquals(result, 1); result = queue.dequeue(); Test.assertEquals(result, 2); }); }); }); Test.describe("Stack", function () { let stack = new Stack(); Test.it("has #pop", function () { Test.expect(typeof stack.pop === "function"); }); Test.it("has #push", function () { Test.expect(typeof stack.push === "function"); }); Test.it("has .length", function () { Test.expect(typeof stack.length === "number"); }); Test.it("does not have #shift", function () { Test.expect(typeof stack.shift === "undefined"); }); });
- Test.describe("Queue", function () {
Test.it("should have working enqueue and dequeue operations", function () {- Test.it("is first-in-first-out", function () {
- var queue = new Queue();
queue.enqueue(1);queue.enqueue(2);queue.enqueue(3);- let items = [1,2,3];
- items.forEach(item => queue.enqueue(item));
- Test.assertEquals(queue.dequeue(), 1);
queue.enqueue(4);queue.enqueue(5);queue.enqueue(6);- Test.assertEquals(queue.dequeue(), 2);
- Test.assertEquals(queue.dequeue(), 3);
queue.enqueue(7);queue.enqueue(8);queue.enqueue(9);queue.enqueue(10);Test.assertEquals(queue.dequeue(), 4);Test.assertEquals(queue.dequeue(), 5);Test.assertEquals(queue.dequeue(), 6);Test.assertEquals(queue.dequeue(), 7);Test.assertEquals(queue.dequeue(), 8);Test.assertEquals(queue.dequeue(), 9);Test.assertEquals(queue.dequeue(), 10);- });
- Test.describe("#enqueue", function() {
- Test.it("adds the item to the stack", function () {
- var queue = new Queue();
- var mockStack = [];
- queue._stack = mockStack;
- queue.enqueue(1);
- Test.assertSimilar(mockStack, [1]);
- queue.enqueue(2);
- Test.assertSimilar(mockStack, [1,2]);
- });
- });
- Test.describe("#dequeue", function() {
- Test.it("removes items from the stack", function () {
- var queue = new Queue();
- var mockStack = [1,2];
- queue._stack = mockStack;
- queue.dequeue();
- Test.assertSimilar(mockStack, [2]);
- queue.dequeue();
- Test.assertSimilar(mockStack, []);
- });
- Test.it("returns the first item from the stack", function () {
- var queue = new Queue();
- var mockStack = [1,2];
- var result = null;
- queue._stack = mockStack;
- result = queue.dequeue();
- Test.assertEquals(result, 1);
- result = queue.dequeue();
- Test.assertEquals(result, 2);
- });
- });
- });
- Test.describe("Stack", function () {
- let stack = new Stack();
- Test.it("has #pop", function () {
- Test.expect(typeof stack.pop === "function");
- });
- Test.it("has #push", function () {
- Test.expect(typeof stack.push === "function");
- });
- Test.it("has .length", function () {
- Test.expect(typeof stack.length === "number");
- });
- Test.it("does not have #shift", function () {
- Test.expect(typeof stack.shift === "undefined");
- });
- });
yawn I need a cup of coffee...
class GroundCoffee { }
class Coffee { }
class Cupboard {
constructor() {
this.contents = {
"GroundCoffee": new GroundCoffee(),
};
}
}
class CoffeeMaker {
makeCoffee(groundCoffee) {
if(!(groundCoffee instanceof GroundCoffee)) {
throw new Error("Only GroundCoffee can be used to make Coffee");
}
return new Coffee();
}
}
// -----
const cupboard = new Cupboard();
const coffeeMaker = new CoffeeMaker();
function makeCoffee() {
let groundCoffee = cupboard.contents["GroundCoffee"];
return coffeeMaker.makeCoffee(groundCoffee);
}
var expect = require("chai").expect;
describe("makeCoffee", function(){
it("makes coffee", function(){
expect(makeCoffee()).to.be.an.instanceof(Coffee);
});
});
describe("CoffeeMaker", function() {
beforeEach(function() {
this.coffeeMaker = new CoffeeMaker();
});
describe("#makeCoffee", function() {
context("when passed GroundCoffee", function() {
beforeEach(function () {
this.groundCoffee = new GroundCoffee();
});
it("makes coffee", function(){
expect(this.coffeeMaker.makeCoffee(this.groundCoffee)).to.be.an.instanceof(Coffee);
});
});
context("when not passed GroundCoffee", function() {
beforeEach(function () {
this.groundCoffee = "I'm not coffee!";
});
it("throws an error", function(){
expect(() => this.coffeeMaker.makeCoffee(this.groundCoffee)).to.throw(
Error, "Only GroundCoffee can be used to make Coffee"
);
});
});
});
});
describe "Solution" do it "returns with 'world'" do expect(hello()).to eq("world") end end
describe("Solution", function(){it("should test for something", function(){Test.assertEquals(hello(), "world");});});- describe "Solution" do
- it "returns with 'world'" do
- expect(hello()).to eq("world")
- end
- end
class Locale { sayHelloTo(whoever){ throw new Error("Not implemented"); } } class EnglishLocale extends Locale { sayHelloTo(whoever){ return `hello ${whoever}`; } } class PirateLocale extends Locale { sayHelloTo(whoever){ return `yar ${whoever}`; } } class BinaryLocale extends EnglishLocale { sayHelloTo(whoever) { let msg = super.sayHelloTo(whoever); return this.txtToBin(msg); } txtToBin(text) { let result = []; for(let character of text){ let binaryArr = this.numberToBinaryArray(character.charCodeAt()); result = result.concat(binaryArr); } return result.join(""); } numberToBinaryArray(number) { let result = []; while(number > 0){ let bit = Math.floor(number % 2) != 0 ? 1 : 0; result.unshift(bit) number = Math.floor(number / 2); } while(result.length != 8) { result.unshift(0); } return result; } } const GREET_LANG = { ENGLISH: new EnglishLocale(), PIRATE: new PirateLocale(), BINARY: new BinaryLocale(), } const hello = (whoever, lang=GREET_LANG.ENGLISH) => lang.sayHelloTo(whoever);
// Declare new languages here and map them in parseGreeting() functionconst GREET_LANG = {ENGLISH: 0,PIRATE: 1,BINARY: 2- class Locale {
- sayHelloTo(whoever){
- throw new Error("Not implemented");
- }
- }
function numberToBinaryArray(number) {let result = [];while(number > 0){let bit = Math.floor(number % 2) != 0 ? 1 : 0;result.unshift(bit)number = Math.floor(number / 2);}while(result.length != 8)result.unshift(0);return result;- class EnglishLocale extends Locale {
- sayHelloTo(whoever){
- return `hello ${whoever}`;
- }
- }
function txtToBin(text) {let result = [];for(let character of text){let binaryArr = numberToBinaryArray(character.charCodeAt());result = result.concat(binaryArr);}return result.join("");- class PirateLocale extends Locale {
- sayHelloTo(whoever){
- return `yar ${whoever}`;
- }
- }
function parseGreeting(lang) {switch(lang){case GREET_LANG.PIRATE:return 'yar';case GREET_LANG.BINARY:return txtToBin('hello');default:return 'hello';- class BinaryLocale extends EnglishLocale {
- sayHelloTo(whoever) {
- let msg = super.sayHelloTo(whoever);
- return this.txtToBin(msg);
- }
}- txtToBin(text) {
- let result = [];
- for(let character of text){
- let binaryArr = this.numberToBinaryArray(character.charCodeAt());
- result = result.concat(binaryArr);
- }
const parseWhoever = (whoever, lang) => lang == GREET_LANG.BINARY ? txtToBin(whoever) : whoever;- return result.join("");
- }
- numberToBinaryArray(number) {
- let result = [];
- while(number > 0){
- let bit = Math.floor(number % 2) != 0 ? 1 : 0;
- result.unshift(bit)
- number = Math.floor(number / 2);
- }
- while(result.length != 8) {
- result.unshift(0);
- }
- return result;
- }
- }
- const GREET_LANG = {
- ENGLISH: new EnglishLocale(),
- PIRATE: new PirateLocale(),
- BINARY: new BinaryLocale(),
- }
const hello = (whoever, lang=GREET_LANG.ENGLISH) => `${parseGreeting(lang)} ${parseWhoever(whoever, lang)}`;- const hello = (whoever, lang=GREET_LANG.ENGLISH) => lang.sayHelloTo(whoever);
describe("Solution", function(){ it("defaults to english", function(){ Test.assertEquals(hello('gotham'), "hello gotham"); }); it("responds in pirate when passed 'pirate' as the language", function(){ Test.assertEquals(hello('gotham', GREET_LANG.PIRATE), "yar gotham"); }); it("responds in real time binary when passed 'binary' as the language", function(){ Test.assertEquals(hello('gotham', GREET_LANG.BINARY), "011010000110010101101100011011000110111100100000011001110110111101110100011010000110000101101101"); }); });
- describe("Solution", function(){
- it("defaults to english", function(){
- Test.assertEquals(hello('gotham'), "hello gotham");
- });
- it("responds in pirate when passed 'pirate' as the language", function(){
- Test.assertEquals(hello('gotham', GREET_LANG.PIRATE), "yar gotham");
- });
it("responds in real time binarry when passed 'binary' as the language", function(){Test.assertEquals(hello('gotham', GREET_LANG.BINARY), "0110100001100101011011000110110001101111 011001110110111101110100011010000110000101101101");- it("responds in real time binary when passed 'binary' as the language", function(){
- Test.assertEquals(hello('gotham', GREET_LANG.BINARY), "011010000110010101101100011011000110111100100000011001110110111101110100011010000110000101101101");
- });
- });
Let's add a bit of some i18n
const helloLangs = { english: "hello", pirate: "yar" } const hello = (whoever, lang="english") => `${helloLangs[lang]} ${whoever}`;
const hello = whoever => `hello ${whoever}`;- const helloLangs = {
- english: "hello",
- pirate: "yar"
- }
- const hello = (whoever, lang="english") => `${helloLangs[lang]} ${whoever}`;
describe("Solution", function(){ it("defaults to english", function(){ Test.assertEquals(hello('gotham'), "hello gotham"); }); it("responds in pirate when passed 'pirate' as the language", function(){ Test.assertEquals(hello('gotham', 'pirate'), "yar gotham"); }); });
- describe("Solution", function(){
it("should test for something", function(){- it("defaults to english", function(){
- Test.assertEquals(hello('gotham'), "hello gotham");
- });
- it("responds in pirate when passed 'pirate' as the language", function(){
- Test.assertEquals(hello('gotham', 'pirate'), "yar gotham");
- });
- });