Ad
Code
Diff
  • #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 letters
    • for(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 long
    • if (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;
    • }
    • }
Code
Diff
  • 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
Code
Diff
  • flatten = a => a.map ? [].concat(...a.map(flatten)) : a
    • flatten=a=>a.map?[].concat(...a.map(flatten)):a
    • flatten = a => a.map ? [].concat(...a.map(flatten)) : a
Algorithms
Logic
Algorithms
Logic
Code
Diff
  • 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 long
    • if(password.length > 7){
    • number = true;
    • }
    • //provide final answer
    • if(cap && number && spec){
    • return true;
    • } else {
    • return false;
    • if(password.length > 7){
    • isLong = true;
    • }
    • return hasUppercase && isLong && hasSpecial;
    • }
Code
Diff
  • 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;
    • }
    • }
    • }

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);
}
Code
Diff
  • def hello
      "world"
    end
    • function hello() {
    • return "world";
    • }
    • def hello
    • "world"
    • end
Code
Diff
  • 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() function
    • const 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);

Let's add a bit of some i18n

Code
Diff
  • 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}`;