Coding Practice: Find Max Consecutive Ones

Given a binary array nums, return the maximum number of consecutive 1’s in the array.

Example 1

Input: nums = [1,1,0,1,1,1]
Output: 3
The first two digits or the last three digits are consecutive 1s. 
The maximum number of consecutive 1s is 3.

Example 2

Input: nums = [1,0,1,1,0,1]
Output: 2

PaperColor Theme v1.0

This is an import from my own blog post in 2015 when I open-sourced my Vim color scheme that had received a warm welcome from many Vim users and inspired others to port to different platforms and create different UI projects. This was before Visual Studio Code/Atom era, and yet, there are even more Vim users now as I see the number of downloads grow higher every day. PaperColor currently gets 1500+ downloads every 2 weeks.

After years of improvements since the last release, I think it’s time for PaperColor to reach an official 1.0 release.
Below is the story behind PaperColor copied from the original post.

I started using Linux and Vim last Summer, and one of the first things I did in Vim, as I would do in any code editor, was to find a decent color scheme. I was surprised by how useful and colorful the terminal could be. Coming from Windows background, I realized how much I missed out. In the beginning, I was happy with the themes I found, but as soon as I worked with less mainstream languages, most of the themes don’t work well enough. Or, some work on these languages but not on the others, so I had to switch theme based on the language I was programming, and it quickly became annoying. So, I decided to create my own color scheme.


Regram Later v1.6

First of all, I’d like to thanks 1200+ first users of Regram Later (Chrome Extension for Instagram repost with smart caption and automation) ūüôā From now on, new updates will come with announcement blog posts like this so that you can see changes to each iteration and be able to provide feedback or report issues right in the comment below. I hope this makes it easier for users to track changes and engage with the discussions.

What’s news in v1.6:

  • Fix incorrect author name when there is a blue verified check
  • Fix incorrect handling of multi-photo posts
  • Fix incorrect handling of queued thumbnail photos

Thanks for your feedback on the app page and direct emails that help me realize the issues. Now you can also report right in the comment section of this blog.

Generate random, unique string in JavaScript

Whether you need to generate a unique identifier for data key, object reference, url shortener, or duplication avoidance, a good random value generator library is a must-have. I recommend 4 libraries: uuid, short-uuid, nanoid, and crypto-random-string

This is not something you want to implement ad-hoc naively because there are serious number theory considerations were already put into these libraries to minimize collision probability thus enhance safety and more suitable for cryptography related use cases.

uuid is a straightforward way to generate standard RFC4122 UUIDs (128-bit Universally Unique Identifier)

const { v4: uuidv4 } = require('uuid')
uuidv4() // ‚á® '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'

short-uuid serves similar purpose like above but allows converting back and forth between UUID and a shorter format for storage efficiency.

const short = require('short-uuid')

// Generate a shortened v4 UUID
const shortId = //=>  mhvXdrZT4jP5T8vBxuvm75
// Translate UUIDs to and from the shortened format
const regularUUID = translator.toUUID(shortId); 
// a44521d0-0fb8-4ade-8002-3385545c3318
translator.fromUUID(regularUUID) // => mhvXdrZT4jP5T8vBxuvm75

nanoid is another alternative to UUID that has similar mathematical properties but designed to be faster and more compact by using a larger alphabet than UUID therefore less characters needed, whereby the size and alphabet are customizable. Moreover, there is also an async API that allows collecting more entropy by acquiring CPU noise in the background.

const nanoid = require('nanoid').nanoid

nanoid() //=> V1StGXR8_Z5jdHi6B-myT
// custom size, trade off between less storage and more collision probability
nanoid(10) //=> IRFa-VaY2b

crypto-random-string is a utility that generates random string with flexible customization. It worries less about collision probability, so it’s best suit where UUID is not needed and you care more about output representation.


Escape JavaScript Regex characters using Lodash

Sometimes you want to construct a Regular Expression object dynamically from a string instead of a literal regex. It’s important to safely escape special characters in the string that is not intended to be regex syntax. Turned out, the swift army knife Lodash library has a dedicated escapeRegExp method for this sole purpose.

const _ = require('lodash')

let escapedString = _.escapeRegExp('[lodash](')

// => '\[lodash\]\(https://lodash\.com/\)'

Construct Regular Expression object from string:

let re = new RegExp(escapedString)

let match = re.exec('Should only match [lodash](')
// [
//   '[lodash](',
//   index: 18,
//   input: 'Should only match [lodash](',
//   groups: undefined
// ]
// => match[0] is the matched string

Bonus: should you only need the escape feature and don’t wish to include Lodash library to your code, the simple function below is sufficient:


Common text case conversion in JavaScript

change-case NPM package includes many text conversion methods for most common cases: camel case,¬†capital case,¬†constant case,¬†dot case,¬†header case,¬†no case,¬†param case,¬†pascal case,¬†path case,¬†sentence case,¬†and snake case. It’s very easy to use, see example:

const changeCase = require('change-case')

const input = 'An_Example string-with.mixed CASES 11.12-2020.. @ #$ %^  & * ('

// => anExampleStringWithMixedCases_11_12_2020

// => An Example String With Mixed Cases 11 12 2020


// => an.example.string.with.mixed.cases.11.12.2020

// => An-Example-String-With-Mixed-Cases-11-12-2020

// => an example string with mixed cases 11 12 2020

// => an-example-string-with-mixed-cases-11-12-2020

// => AnExampleStringWithMixedCases_11_12_2020

// => an/example/string/with/mixed/cases/11/12/2020

// => An example string with mixed cases 11 12 2020

// => an_example_string_with_mixed_cases_11_12_2020