Skip to content

Instantly share code, notes, and snippets.

View sawant's full-sized avatar
🏠
Working from home

Sawant Shah sawant

🏠
Working from home
View GitHub Profile
@sawant
sawant / homebrew old version.txt
Last active March 16, 2024 05:35
Install older version of Formula in Homebrew
[From: http://hanxue-it.blogspot.com/2018/08/macos-homebrew-installing-older-version-of-software.html - just created a copy to keep it for long term]
Homebrew always wants to install the latest version of the Formula (software). This is by design, because every time there is an update to a formula, it wants to be tested against all the other formulas that it depends on. Mixing new and old versions of software is a recipe for incompatibility disaster.
But sometimes there are situations where you need an older version of software. In my specific case, Yarn was compiled against an older version of icu4c, and I want that older version instead of recompiling Yarn.
$ yarn install
dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.61.dylib
Referenced from: /usr/local/bin/node
@sawant
sawant / chess-ascii.js
Last active July 27, 2023 17:50
JavaScript Chess Board
/*
http://eloquentjavascript.net/02_program_structure.html
Write a program that creates a string that represents an 8×8 grid, using newline characters to separate lines. At each position of the grid there is either a space or a “#” character. The characters should form a chess board.
Passing this string to console.log should show something like this:
# # # #
# # # #
# # # #
@sawant
sawant / 4-2-reversearray.js
Created September 2, 2014 12:33
Arrays have a method reverse, which changes the array by inverting the order in which its elements appear. For this exercise, write two functions, reverseArray and reverseArrayInPlace. The first, reverseArray, takes an array as argument and produces a new array that has the same elements in the inverse order. The second, reverseArrayInPlace, doe…
/*
http://eloquentjavascript.net/04_data.html
Arrays have a method reverse, which changes the array by inverting the order in which its elements appear. For this exercise, write two functions, reverseArray and reverseArrayInPlace. The first, reverseArray, takes an array as argument and produces a new array that has the same elements in the inverse order. The second, reverseArrayInPlace, does what the reverse method does: it modifies the array given as argument in order to reverse its elements. Neither may use the standard reverse method.
*/
function reverseArray( array ) {
var reversedArray = [];
while( i = array.pop() )
@sawant
sawant / 4-3-alist.js
Created September 2, 2014 13:26
Write a function arrayToList that builds up a data structure like the previous one when given [1, 2, 3] as argument, and write a listToArray function that produces an array from a list. Also write the helper functions prepend, which takes an element and a list and creates a new list that adds the element to the front of the input list, and nth, …
/*
http://eloquentjavascript.net/04_data.html
Write a function arrayToList that builds up a data structure like the previous one when given [1, 2, 3] as argument, and write a listToArray function that produces an array from a list. Also write the helper functions prepend, which takes an element and a list and creates a new list that adds the element to the front of the input list, and nth, which takes a list and a number and returns the element at the given position in the list, or undefined when there is no such element.
If you haven’t already, also write a recursive version of nth.
*/
function arrayToList( array ) {
var list = null;
// Bonfire: Where do I belong
// Author: @sawant
// Challenge: http://www.freecodecamp.com/challenges/bonfire-where-do-i-belong?solution=function%20where(arr%2C%20num)%20%7B%0A%20%20%2F%2F%20Find%20my%20place%20in%20this%20sorted%20array.%0A%20%20arr.push(num)%3B%0A%20%20return%20arr.sort(function(a%2C%20b)%20%7Breturn%20a%20%3E%20b%3B%7D).indexOf(num)%3B%0A%7D%0A%0A%2F%2Fwhere(%5B40%2C%2060%5D%2C%2050)%3B%0Awhere(%5B5%2C%203%2C%2020%2C%203%5D%2C%205)%3B
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function where(arr, num) {
// Find my place in this sorted array.
arr.push(num);
return arr.sort(function(a, b) {return a > b;}).indexOf(num);
}
// Bonfire: Seek and Destroy
// Author: @sawant
// Challenge: http://www.freecodecamp.com/challenges/bonfire-seek-and-destroy?solution=function%20destroyer(arr)%20%7B%0A%20%20%2F%2F%20Remove%20all%20the%20values%0A%20%20var%20result%3B%20var%20args%20%3D%20%5B%5D%3B%0A%0A%20%20for%20(var%20i%20%3D%201%3B%20i%20%3C%20arguments.length%3B%20i%2B%2B)%20%7B%0A%20%20%20%20args.push(arguments%5Bi%5D)%3B%0A%20%20%7D%0A%0A%20%20result%20%3D%20arr.filter(function(val)%20%7B%0A%20%20%20%20return%20args.indexOf(val)%20%3D%3D%3D%20-1%3B%0A%20%20%7D)%3B%0A%20%20%0A%20%20return%20result%3B%0A%7D%0A%0Adestroyer(%5B1%2C%202%2C%203%2C%201%2C%202%2C%203%5D%2C%202%2C%203)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function destroyer(arr) {
// Remove all the values
var result; var args = [];
for (var i = 1; i < arguments.length; i++) {
// Bonfire: Falsy Bouncer
// Author: @sawant
// Challenge: http://www.freecodecamp.com/challenges/bonfire-falsy-bouncer?solution=function%20bouncer(arr)%20%7B%0A%20%20%2F%2F%20Don%27t%20show%20a%20false%20ID%20to%20this%20bouncer.%0A%20%20return%20arr.filter(function(val)%20%7B%0A%20%20%20%20return%20Boolean(val)%3B%0A%20%20%7D)%3B%0A%7D%0A%0Abouncer(%5B7%2C%20%22ate%22%2C%20%22%22%2C%20false%2C%209%5D)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function bouncer(arr) {
// Don't show a false ID to this bouncer.
return arr.filter(function(val) {
return Boolean(val);
});
// Bonfire: Mutations
// Author: @sawant
// Challenge: http://www.freecodecamp.com/challenges/bonfire-mutations?solution=function%20mutation(arr)%20%7B%0A%20%20for%20(var%20i%20%3D%200%3B%20i%20%3C%20arr%5B1%5D.length%3B%20i%2B%2B)%20%7B%0A%20%20%20%20if%20(%20arr%5B0%5D.toLowerCase().indexOf(%20arr%5B1%5D%5Bi%5D.toLowerCase()%20)%20%3D%3D%3D%20-1%20)%20return%20false%3B%0A%20%20%7D%0A%20%20%0A%20%20return%20true%3B%0A%7D%0A%0Amutation(%5B%22hello%22%2C%20%22hey%22%5D)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function mutation(arr) {
for (var i = 0; i < arr[1].length; i++) {
if ( arr[0].toLowerCase().indexOf( arr[1][i].toLowerCase() ) === -1 ) return false;
}
// Bonfire: Chunky Monkey
// Author: @sawant
// Challenge: http://www.freecodecamp.com/challenges/bonfire-chunky-monkey?solution=function%20chunk(arr%2C%20size)%20%7B%0A%20%20%2F%2F%20Break%20it%20up.%0A%20%20var%20result%20%3D%20%5B%5D%3B%0A%20%20for%20(var%20i%20%3D%200%3B%20i%20%3C%20arr.length%2Fsize%3B%20i%2B%2B)%20%7B%0A%20%20%20%20result.push(%20arr.slice(%20i%20*%20size%2C%20size%20*%20(i%2B1)%20)%20)%3B%0A%20%20%7D%0A%0A%20%20return%20result%3B%0A%7D%0A%0Achunk(%5B%22a%22%2C%20%22b%22%2C%20%22c%22%2C%20%22d%22%5D%2C%202)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function chunk(arr, size) {
// Break it up.
var result = [];
for (var i = 0; i < arr.length/size; i++) {
result.push( arr.slice( i * size, size * (i+1) ) );
// Bonfire: Truncate a string
// Author: @sawant
// Challenge: http://www.freecodecamp.com/challenges/bonfire-truncate-a-string?solution=function%20truncate(str%2C%20num)%20%7B%0A%20%20%2F%2F%20Clear%20out%20that%20junk%20in%20your%20trunk%0A%20%20return%20num%20%3C%20str.length%20%3F%20str.slice(0%2C%20num%20%3C%203%20%3F%20num%20%3A%20num%20-%203)%20%2B%20%22...%22%20%3A%20str%3B%0A%7D%0A%0Atruncate(%22A-tisket%20a-tasket%20A%20green%20and%20yellow%20basket%22%2C%2011)%3B
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function truncate(str, num) {
// Clear out that junk in your trunk
return num < str.length ? str.slice(0, num < 3 ? num : num - 3) + "..." : str;
}