Skip to content

Instantly share code, notes, and snippets.

:octocat:
Hacking

Viktor Karpov vitkarpov

:octocat:
Hacking
Block or report user

Report or block vitkarpov

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@vitkarpov
vitkarpov / Computer-Science-Club-27-12-2018.md
Last active Dec 27, 2018
Computer Science Club, 27.12.2018 (Strings)
View Computer-Science-Club-27-12-2018.md

Computer Science Club, 27.12.2018

Here's a set of problems for the class. Exploiring strings.

Is Unique

Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?

Hints:

@vitkarpov
vitkarpov / rm-element-from-the-stack.js
Created Oct 15, 2018
Removing an arbitrary element from the stack
View rm-element-from-the-stack.js
function removeItemFromStack(stack, item) {
const tmp = stack.pop();
// if we've looked up the whole stack there's no item
// just return tmp element back to the top and we're done
if (stack.length === 0) {
stack.push(tmp);
return;
}
View extra-long-factorial.js
function mult(a, b) {
let carry = 0;
for (let i = 0; i < a.length || carry > 0; i++) {
if (i === a.length) {
a.push(0);
}
const curr = carry + a[i] * b;
a[i] = curr % 10;
carry = Math.floor(curr / 10);
@vitkarpov
vitkarpov / question.md
Created Jul 6, 2018
How do I make a tokenizer faster?
View question.md

Hello software engineers and computer scientists! :)

I'm working on open source JavaScript project which has css parser as one if its modules. Node --perf shows me that the cricical part is REGEXP which is used to find the next word boundary.

const RE_WORD_END = /[ \n\t\r\f\(\)\{\}:;@!'"\\\]\[#]|\/(?=\*)/g;

// pos - current boundary
// next - next boundary which the code has to find
View example.md
/my-website/
  | package.json
  | /src/
  | | /pages/
  | | | index.html
  | | | about.html
  | | /assets/
  | | | site.css
  | | | site.js
@vitkarpov
vitkarpov / code-comarison-in-errors.js
Created Nov 11, 2017
The right way to distinguish errors
View code-comarison-in-errors.js
if (err.code === 'ERR_HTTP_HEADERS_SENT') {
//do something with the error
}
@vitkarpov
vitkarpov / message-comparison-in-errors.js
Created Nov 11, 2017
Don't do this anymore, there're erorrs' codes since Node.js 9.0.0
View message-comparison-in-errors.js
if (err.message === 'Can\'t set headers after they are sent.') {
//do something with the error
}
@vitkarpov
vitkarpov / callbacify.js
Last active Nov 11, 2017
Example of util.callbackify
View callbacify.js
const { callbackify } = require('util');
async function main() {
await Promise.resolve();
}
callbackify(main)(function(err) {
if (err) {
return console.error(err);
}
@vitkarpov
vitkarpov / promisify.js
Created Nov 11, 2017
Example of util.promisify
View promisify.js
// @see http://2ality.com/2017/05/util-promisify.html
const { promisify } = require('util');
const fs = require('fs');
const readFileAsync = promisify(fs.readFile);
const filePath = process.argv[2];
readFileAsync(filePath, { encoding: 'utf8' })
@vitkarpov
vitkarpov / http2-example.js
Last active Nov 10, 2017
Example of http/2 landed in Node.js 9 without --expose-http2 flag
View http2-example.js
const http2 = require('http2');
const fs = require('fs');
const server = http2.createSecureServer({
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
});
server.on('error', (err) => console.error(err));
server.on('socketError', (err) => console.error(err));
You can’t perform that action at this time.