npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

perm_displacement

v1.0.1

Published

Functions for mesing around with string permutation displacements

Downloads

3

Readme

String Permutation Displacements

Functions for mesing around with string permutation displacements.

There was that clickbaity Facebook message that went around sometime ago. It was a passage of text with some of the words with their characters slightly rearranged and you were a genius if you could read it...obviously almost everyone could. But at the same time I also saw a video of a man who could look at any English word and tell you another word that could be made with the same characters rearranged. So on the one end of the spectrum you have 2 letter swops which is easy to detect and read and this guy on the other. So I surmise that with practice one could, theoretically, move on that spectrum. This idea is predicated on the fact that when one reads, you don't read individual charaters but rather the overall pattern of the word.

That's why I built this small bit of code. I want to practice on passages of text with only certain length words displaced by set amounts and practice. So we'll see if my conjecture is correct. I'll update this readme with any results.

So the basic idea is to measure how similar one permutation of a word is to another. This measure is called displacement (from the combinatorics field)

eg. 'acbd' has a displacement of 2 because 'c' and 'b' are both 1 position from their original position assuming 'abcd'

When I looked this up I got scared by the crazy math symbols but I found out that that the previous example ('acbd') has a displacemnt of 1 and displacement value go like 1,2,3 etc. whereas what I'm building, the displacement goes like 2,4,6 etc.. Since this module for me and it serves it's purpose, it can stay as it is :wink:

Functions

permutations(string, n)

accepts a string and n(optional) and returns an array of arrays of all permutations of the string or n number of permutations

permutations("ab")

returns: [ [ 'a', 'b' ], [ 'b', 'a' ] ]

permutations("abc",2)

returns: [ [ 'a', 'b', 'c' ], [ 'a', 'c', 'b' ] ]

random_permutation(string)

accepts a string an returns a random permutation. Uses the Durstenfeld Shuffle

random_permutation("abcdef")

returns: [ 'f', 'b', 'c', 'd', 'a', 'e' ]

absolute_displacement(string, gnirts)

accepts 2 strings and returns the absolute displacement of the 2nd string with regards to the first.

absolute_displacement("abcdef","fbcdae")

returns: 10

max_absolute_displacement(string)

accepts a string and return the max displacement. ie The maximum a word can be mixed up. Given by this formula:

Math.floor(2 * (string.length/2)2)

max_absolute_displacement("antidisestablishmentarianism")

returns: 392

all_absolute_displacements(string, n)

accepts a string and number(optional) and returns all permutations of that string with max displacement or n displacement.

all_absolute_displacements("abc", 4)

returns:

[
    [ 'b', 'c', 'a' ],
    [ 'c', 'a', 'b' ],
    [ 'c', 'b', 'a' ]
]

all_absolute_displacements("abcd")

returns:

[
  [ 'c', 'd', 'a', 'b' ],
  [ 'c', 'd', 'b', 'a' ],
  [ 'd', 'c', 'a', 'b' ],
  [ 'd', 'c', 'b', 'a' ]
]

absolute_displacement_distribution(string)

returns an array of objects with key:value pairs.

key: is the absolute displacement

value: is the sum of permutations of that displacement

(I'm sure there's a sexier, mathier way of doing this, but I'm just a hack :smile:)

absolute_displacement_distribution("abcdefg")

  '0': 1,
  '2': 6,
  '4': 25,
  '6': 76,
  '8': 187,
  '10': 366,
  '12': 591,
  '14': 744,
  '16': 884,
  '18': 832,
  '20': 716,
  '22': 360,
  '24': 252
}

displacement_code(string, gnirts)

returns an array of the relative individual displacments of characters...not sure how this is useful but hey!