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
Explanation: 
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
(more…)

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.

https://github.com/NLKNguyen/papercolor-theme

C/C++
(more…)

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 = translator.new() //=>  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.

(more…)

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](https://lodash.com/)')

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

Construct Regular Expression object from string:

let re = new RegExp(escapedString)

let match = re.exec('Should only match [lodash](https://lodash.com/)')
console.log(match)
// [
//   '[lodash](https://lodash.com/)',
//   index: 18,
//   input: 'Should only match [lodash](https://lodash.com/)',
//   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:

(more…)