Code Monkey home page Code Monkey logo

problem-solving-path's Introduction

Problem Solving Approach

How do you improve?

  • Devise a plan for solving problems
  • Master common problem solving patterns

1. Understand the Problem
  • Can I restate the problem in my own words?
  • What are the inputs that go into the problem?
  • What are the outputs that should come from the solution to the problem?
  • Can the outputs be determined from the inputs? In other words, do I have enough information to solve the problem?
  • How should I label the important pieces of data that are a part of the problem?

2. Explore Concrete Examples
  • Start with Simple Examples
  • Progress to More Complex Examples
  • Explore Examples with Empty Inputs
  • Explore Examples with Invalid Inputs

Write a function which takes in a string and returns count of each character in the string.

// # Simple Examples #
charCount("aaaa");
/* {
  a: 4
} */

charCount("hello"); //
/* {
  h: 1,
  e: 1,
  l: 2,
  o: 1
} */

// # Complex Examples #
/*
  "my phone number is 182763"
  "Hello hi" // uppercase "h", lowercase "h"
*/
charCount("Your PIN number is 1234!");
/* {
  1: 1,
  2: 1,
  3: 1,
  4: 1,
  b: 1,
  e: 1,
  i: 2,
  m: 1,
  n: 2,
  o: 1,
  p: 1,
  r: 2,
  s: 1,
  u: 2,
  y: 1,
} */

// # Empty Inputs #
charCount(""); // return "{}" or null or false or error?

// # Invalid Inputs #
charCount(123123);

3. Break It Down

Explicitly write out the steps you need to take.

// Skeleton of the function

function charCount(str) {
  // do something
  // return an object with keys that are lowercase alphanumeric characters in the string; values should be the counts for those characters
}

function charCount(str) {
  // make object to return at end
  // loop over string, for each character...
  //if the char is a number/letter AND is a key in object, add one to count
  //if the char is a number/letter AND not in object, add it to object and set value to 1
  //if character is something else (space, period, etc.) don't do anything
  // return object at end
}

4. Solve/Simplify
  • Find the core difficulty in what you're trying to do
  • Temporarily ignore that difficulty
  • Write a simplified solution
  • Then incorporate that difficulty back in

5. Look Back and Refactor
function charCount(str) {
  const obj = {};
  for (let i = 0; i < str.length; i++) {
    const char = str[i].toLowerCase();
    if (/a-z0-9/.test(char)) {
      if (obj[char] > 0) {
        obj[char]++;
      } else {
        obj[char] = 1;
      }
    }
  }
  return obj;
}

// --------

function charCount(str) {
  const obj = {};
  for (let char of str) {
    if (isAlphaNumeric(char)) {
      char = char.toLowerCase();
      obj[char] = ++obj[char] || 1;
    }
  }
  return obj;
}

// more efficient compared to regex
function isAlphaNumeric(char) {
  const code = char.charCodeAt(0);
  if (
    !(code > 47 && code < 58) && // numeric (0-9)
    !(code > 64 && code < 91) && // upper alpha (A-Z)
    !(code > 96 && code < 123) // lower alpha (a-z)
  ) {
    return false;
  }
  return true;
}

problem-solving-path's People

Contributors

kushadige avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.