Skip to content

Instantly share code, notes, and snippets.

@jluismm2311
jluismm2311 / scrabbleScore.js
Created September 7, 2015 22:19
Scrabble Score
function scrabbleScore(str){
var value = 0;
for(i = 0 ; i< str.length; i++){
switch (true){
case( /[dg]/i.test(str[i])):
value += 2;
break;
case( /[bcmp]/i.test(str[i])):
value += 3;
break;
@jluismm2311
jluismm2311 / palindrome.js
Created September 8, 2015 21:00
A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction. Famous examples include "Amore, Roma", "A man, a plan, a canal: Panama" and "No 'x' in 'Nixon'". - wikipedia Our goal is to determine whether or not a given string is a valid pali…
function palindrome(string) {
let newString = string.replace(/\W/g, '').toLowerCase();
let stringLength = newString.length, midleString = stringLength % 2 === 0 ? stringLength / 2 : ((stringLength / 2)+1);
for(i = 0; i< midleString; i++){
if(newString[i] != newString[stringLength - 1 - i]){
return false;
}
}
return true;
@jluismm2311
jluismm2311 / christmasTree.js
Created September 8, 2015 22:02
Create a function christmasTree(height) that returns a christmas tree of the correct height christmasTree(5) should return: * *** ***** ******* ********* Height passed is always an integer between 0 and 100. Use \n for newlines between each line. Pad with spaces so each line is the same length. The last line having only stars, no spaces.
function christmasTree(height) {
var value = "";
for(i = 1; i<= height; i++ ) {
var blank = " ".repeat(height-i);
value += blank + "*".repeat(1+((i-1)*2)) + blank;
value += i < height ? "\n" : "";
}
return value
}
@jluismm2311
jluismm2311 / dubstep.js
Created September 8, 2015 22:38
Polycarpus works as a DJ in the best Berland nightclub, and he often uses dubstep music in his performance. Recently, he has decided to take a couple of old songs and make dubstep remixes from them. Let's assume that a song consists of some number of words. To make the dubstep remix of this song, Polycarpus inserts a certain number of words "WUB…
function songDecoder(song){
return song.replace(/(WUB)+/g, " ").trim();
}
@jluismm2311
jluismm2311 / toWeirdCase.js
Created September 11, 2015 22:18
Write a function toWeirdCase (weirdcase in Ruby) that accepts a string, and returns the same string with all even indexed characters in each word upper cased, and all odd indexed characters in each word lower cased. The indexing just explained is zero based, so the zero-ith index is even, therefore that character should be upper cased. The passe…
function toWeirdCase(string){
let newString = string.split(' ');
for(let i = 0; i < newString.length; i++){
newString[i] = newString[i].weird();
}
return newString.join(' ');
}
String.prototype.weird = function( ){
var newString = '';
@jluismm2311
jluismm2311 / feymanCount.js
Created September 14, 2015 16:38
Feynman's squares Richard Phillips Feynman was a well-known American physicist and a recipient of the Nobel Prize in Physics. He worked in theoretical physics and pioneered the field of quantum computing. Recently, an old farmer found some papers and notes that are believed to have belonged to Feynman. Among notes about mesons and electromagneti…
function countSquares(n){
let value = 0;
for(var i = 1; i<=n; i++){
value += i * i;
}
return value;
}
@jluismm2311
jluismm2311 / humanReadDate.js
Created September 14, 2015 17:49
Write a function, which takes a non-negative integer (seconds) as input and returns the time in a human-readable format (HH:MM:SS) HH = hours, padded to 2 digits, range: 00 - 99 MM = minutes, padded to 2 digits, range: 00 - 59 SS = seconds, padded to 2 digits, range: 00 - 59 The maximum time never exceeds 359999 (99:59:59)
function humanReadable(seconds) {
var hours = seconds / 3600, minutes = seconds / 60 % 60, newSeconds = seconds % 60 ;
return formatDate(hours) +':' + formatDate(minutes)+':'+formatDate(newSeconds)
}
function formatDate(n){
var number = Number.parseInt(n)
return number > 9? number : '0'+number;
}
@jluismm2311
jluismm2311 / palindromeChainLength.js
Created September 14, 2015 20:35
Number is a palindrome if it is equal to the number with digits in reversed order. For example, 5, 44, 171, 4884 are palindromes and 43, 194, 4773 are not palindromes. Write a method palindrome_chain_length which takes a positive number and returns the number of special steps needed to obtain a palindrome. The special step is: "reverse the digit…
var palindromeChainLength = function(n) {
let notIsPalindrome = true, index = 0, value=n;
while(notIsPalindrome){
let reversed = Number.parseInt(value.toString().split('').reverse().join(''));
if(value == reversed){
notIsPalindrome = false;
}else{
index++;
value += reversed;
}
@jluismm2311
jluismm2311 / toUnderScore.js
Created December 1, 2015 15:40
Complete the function/method so that it takes CamelCase string and returns the string in snake_case notation. Lowercase characters can be numbers. If method gets number, it should return string. Examples: # returns test_controller to_underscore('TestController') # returns movies_and_books to_underscore('MoviesAndBooks') # returns app7_test to_un…
function toUnderscore(string) {
return string.toString().replace(/[A-Z]/g,function(element, index){
element = element.toLowerCase();
return index == 0 ? element :'_'+ element
});
}
@jluismm2311
jluismm2311 / SumofnNumbers.java
Created December 2, 2015 19:08
Sum of 'n' Numbers sum_of_n (or SequenceSum.sumOfN in Java, SequenceSum.SumOfN in C#) takes an integer n and returns a List (an Array in Java/C#) of length abs(n) + 1. The List/Array contains the numbers in the arithmetic series produced by taking the sum of the consecutive integer numbers from 0 to n inclusive. n can also be 0 or a negative val…
public class SequenceSum {
public static int[] sumOfN(int n) {
boolean isNegative = n < 0;
int arraySize = Math.abs(n)+1;
int[] result= new int[arraySize];
for(int i = 1; i < arraySize;i++){
result[i] = isNegative? result[i-1] - i: result[i-1] + i;
}
return result;
}