Giter Club home page Giter Club logo

backtoschool's Introduction

Back to school! :)

About

This is a small repository containing two examples that I was asked to work on:

01: Pattern matching

Given a string s to search in and a pattern t of length m to look for, find the first index i such that t is a factor of s at position i.

Find the pattern t in string s and return its position i in the string.

If not found, return -1

Input Pattern Output
lalopalalali lala 6
aaaa a 0
aaaa b -1
abcd a 0
abcd b 1

02: Power strings

Given an input containing periods/repetitions (or not), find the shortest sub-string that is repeating Relaxed constraint: the length of the input is a multiple of the period.

Examples:

Input Output
abcd (abcd)1
aaaa a4
ababab (ab)3

Installation

  • install node and npm
  • install the dependencies: npm install
  • verify that everything is fine with npm test

Running the test suite

  • execute npm test

Building the project (generating JS code)

  • execute npm tsc or npm run tsc

Running the CLI for exercise 01 (pattern matching)

  • build the project
  • execute node ./dist/01-recherche-motif/pattern-matcher-cli.js

Running the CLI for exercise 02 (period)

  • build the project
  • execute node ./dist/02-periodicite/periodicite-cli.js

Approach and rationale

01: Pattern matching

Approach taken so far:

  • naive implementation using the standard indexOf function available in Node and most languages
  • the comparison is case insensitive and takes the available locales into account.

This is naive because it could prove problematic depending on the data sets, the locale(s) of the data, its length, ...

Computationally, if we assume that a given pattern has to be checked against a large set of input strings, then looping over all the input strings and applying toLower/indexOf to each entry will be very costly: O(n²). It gets worse if there are multiple patterns to check against each input string, as there would be an additional loop, hence O(m*n²) -- I'm not sure about this, I need to review my algorithms courses and books from Donald Knuth.

Written in TypeScript, using the Jest testing library and the jest-each package for parameterized testing in order to allow to easily define and use a test data set.

02: Power strings

Approach taken at first: naive implementation iterating through the string. Afterwards, I've looked at more efficient algorithms that could be applicable here such as Knuth-Morris-Pratt (KMP).

Same language and testing approach.

Ideas to improve

01-recherche-motif

If we consider the harder case, having a lot of data to parse / check and maybe multiple patterns to try and match, then caching could be useful. For example, I could use a hashmap which has O(1) lookup time. In it, assuming that patterns are short, I could use the pattern as key and map it to a set of matching inputs. At runtime, this data structure could drastically reduce the amount of checks needed and would improve performance over time, at the cost of more memory usage.

This makes sense if patterns are not too long and if we can dedicate enough memory to the process.

If we have a known list of patterns, then we could also construct such maps at startup in order to have a "hot" cache. To host the cache, we could use things like Redis (in-memory data store). It all depends on the use case and non-functional requirements.

If the use case is not real-time checks, then we could also consider implementing such checks as batch jobs, executed at night with lower priority, ...

02-periodicite

Performance

First of all, before optimizing we need to investigate if there are actual performance issues.

To be able to know that there is a performance issue:

  • benchmark the current algorithm with a relevant test suite (i.e., with relevant/sufficient data) ** cpu ** memory

Possibilities to improve if actual performance issues are detected (depending on their type):

Other interesting links:

backtoschool's People

Contributors

dsebastien avatar

Watchers

 avatar  avatar  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.