Skip to content

Instantly share code, notes, and snippets.

View erictleung's full-sized avatar
👨‍💻
Data sciencing

Eric Leung erictleung

👨‍💻
Data sciencing
View GitHub Profile
@erictleung
erictleung / hello.py
Last active November 12, 2015 07:43
"Hello, world!" in Python
#!/usr/bin/env python
# Python 2.7.9
print "Hello world!"
@erictleung
erictleung / palindrome.js
Created November 12, 2015 07:41
Checks if given string is a palindrome after removing punctuation (e.g. back and forward slashes, commas, periods, colons, semicolons, dashes, spaces)
function palindrome(str) {
tempStr = str.replace(/(\.|,|\s|_|:|;|\(|\)|\/|-)/g, "").toLowerCase();
return tempStr.split("").reverse().join("") == tempStr;
}
@erictleung
erictleung / reverse.js
Created November 17, 2015 01:59
Function to return a given string but in reverse
// Bonfire: Reverse a String
// Author: @erictleung
// Challenge: http://www.freecodecamp.com/challenges/bonfire-reverse-a-string
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function reverseString(str) {
return str.split("").reverse().join("");
}
reverseString("hello");
@erictleung
erictleung / factorial.js
Created November 17, 2015 02:06
Return factorial of number, given a number
function factorialize(num) {
var final = 1;
if (num === 0) {
return 1;
}
else {
for (i = 1; i <= num; i++ ) {
final *= i;
}
return final;
@erictleung
erictleung / python_install.sh
Created November 17, 2015 02:15
Download and compile Python from source (if user is not root)
#!/bin/bash
# Shell script to install Python
# Note: Change `wget` download file as necessary (also not validated, yet)
# https://my.bluehost.com/cgi/help/python-install
# download, unzip, and setup Python
mkdir ~/python
cd ~/python
wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tgz
tar zxfv Python-2.7.2.tgz
@erictleung
erictleung / findLongestWord.js
Created November 17, 2015 02:22
Find longest word within a space separated sentence
function findLongestWord(str) {
var splitStr = str.split(" ");
var long = 0;
for (i = 0; i < splitStr.length; i++) {
if (splitStr[i].length > long) {
long = splitStr[i].length;
}
}
return long;
}
@erictleung
erictleung / titleCase.js
Created November 21, 2015 08:57
Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lowercase.
function titleCase(str) {
var splitStr = str.split(" ");
var newStr = [];
for (i = 0; i < splitStr.length; i++) {
var temp = splitStr[i];
var fullTemp = temp[0].toUpperCase() + temp.substr(1, temp.length).toLowerCase();
newStr.push(fullTemp);
}
return newStr.join(" ");
}
@erictleung
erictleung / checkLast.js
Created November 27, 2015 10:34
Return true/false on whether the target string is at the end of a given string.
function end(str, target) {
return str.substr(str.length - target.length) == target;
}
@erictleung
erictleung / chunk.js
Created November 30, 2015 08:54
Breakup an array into an array of arrays of a given size
function chunk(arr, size) {
var final = [];
for (i = 0; i < Math.ceil(arr.length/size); i++) {
final.push(arr.slice(i*size, (i+1)*size));
}
return final;
}
chunk(["a", "b", "c", "d", "e"], 2); // returns [["a", "b"], ["c", "d"], ["e"]]
@erictleung
erictleung / ROT13.js
Created April 23, 2016 04:17
Caesars Cipher
function rot13(str) { // LBH QVQ VG!
var output = "";
for (var i = 0; i < str.length; i++) {
console.log("We are in loop " + i);
var ascii = str[i].charCodeAt();
console.log("Ascii value: " + ascii);
// Check if character is alphabetical or not
if (ascii <= 90 && ascii >= 65) {
console.log("Character is part of alphabet");