Skip to content

Instantly share code, notes, and snippets.

@saodat1998
Last active June 16, 2021 06:38
Show Gist options
  • Save saodat1998/3b05f7ea7f224073bce3e8ef989629b4 to your computer and use it in GitHub Desktop.
Save saodat1998/3b05f7ea7f224073bce3e8ef989629b4 to your computer and use it in GitHub Desktop.

Problem

What we want to implement is Array.prototype.filter() function, just like the existing Array.prototype.filter(). Another similar function is _.filter() in underscore.js and lodash.js. The usage will be quite simple, like: [1, 2, 3, 4, 5].filter(num => num > 3) == [4, 5]

My solution:

Array.prototype.filter = function (func) {
  let len = this.length,
      res = new Array(),
      arr = this, counter=0, index = 0;
  for(counter; counter < len; counter++)
    if(func(arr[counter]))
       res[index++] = arr[counter];
    
  return res
}

Problem

Can you realize a function that returns word count from a given string? You have to ensure that spaces in string is a whitespace for real. What we want and finish of work:

countWords("Hello"); // returns 1 as int countWords("Hello, World!") // returns 2 countWords("No results for search term s") // returns 6 countWords(" Hello") // returns 1 // ... and so on What kind of tests we got for your code: Function have to count words, but not spaces, so be sure that it does right. Empty string has no words. String with spaces around should be trimmed. Non-whitespace (ex. breakspace, unicode chars) should be assumed as delimiter Be sure that words with chars like -, ', ` are counted right.

My solution:

function countWords(str) {
  let arr 
  str = str.trim().replace('\ufeff', ' ')
  arr = str.split(' ')
  arr = arr.filter(el => el !== '')
  return arr.length
}

Best Practices

function countWords(str) {
  return (str.match(/[^\s]+/g) || []).length;
}

Write a function which partitions a list of items based on a given predicate.

After the partition function is run, the list should be of the form [ F, F, F, T, T, T ] where the Fs (resp. Ts) are items for which the predicate function returned false (resp. true).

NOTE: the partitioning should be stable; in other words: the ordering of the Fs (resp. Ts) should be preserved relative to each other.

For convenience and utility, the partition function should return the boundary index. In other words: the index of the first T value in items.

For example:

var items = [1, 2, 3, 4, 5, 6]; function isEven(n) {return n % 2 == 0} var i = partitionOn(isEven, items); // items should now be [1, 3, 5, 2, 4, 6] // i should now be 3

My solution:

function partitionOn(pred, items) {
  let evenItems = items.filter(el=>pred(el));
  let oddItems =  items.filter(el=>!pred(el));
  
  items.length = 0; 
  items.push.apply(items,  oddItems.concat(evenItems)); 
  
  return items.indexOf(evenItems[0])
}

You are going to be given a word. Your job is to return the middle character of the word. If the word's length is odd, return the middle character. If the word's length is even, return the middle 2 characters.

My solution:

function getMiddle(s)
{
  let center = parseInt(s.length / 2)
  
  if(s.length % 2 === 1)
    return s[center]
  else
    return s[center-1] + s[center]
  
}

Best practices:

function getMiddle(s)
{
  return s.substr(Math.ceil(s.length / 2 - 1), s.length % 2 === 0 ? 2 : 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment