-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alessandro Giordo
committed
Mar 1, 2016
1 parent
38fd8e6
commit 9cc52f9
Showing
10 changed files
with
267 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
var indexOfMinimum = function(array, startIndex) { | ||
// Set initial values for minValue and minIndex, | ||
// based on the leftmost entry in the subarray: | ||
var minValue = array[startIndex]; | ||
var minIndex = startIndex; | ||
|
||
// Loop over items starting with startIndex, | ||
// updating minValue and minIndex as needed: | ||
for (var i = minIndex + 1; i < array.length ; i++) { | ||
if (array[i] < minValue) { | ||
minIndex = i; | ||
minValue = array[i]; | ||
} | ||
} | ||
return minIndex; | ||
}; | ||
|
||
var array = [18, 6, 66, 44, 9, 22, 14]; | ||
var index = indexOfMinimum(array, 2); | ||
|
||
// For the test array [18, 6, 66, 44, 9, 22, 14], | ||
// the value 9 is the smallest of [..66, 44, 9, 22, 14] | ||
// Since 9 is at index 4 in the original array, | ||
// "index" has value 4 | ||
println("The index of the minimum value of the subarray starting at index 2 is " + index + "." ); | ||
Program.assertEqual(index, 4); | ||
|
||
var array = [18, 6, 66, 44, 9, 22, 14]; | ||
var index = indexOfMinimum(array, 3); | ||
println("The index of the minimum value of the subarray starting at index 3 is " + index + "." ); | ||
Program.assertEqual(index, 4); | ||
var array = [18, 6, 66, 44, 9, 22, 14]; | ||
var index = indexOfMinimum(array, 4); | ||
println("The index of the minimum value of the subarray starting at index 4 is " + index + "." ); | ||
Program.assertEqual(index, 4); | ||
|
||
|
||
/* | ||
function indexOfMinimum, which takes an array and a number startIndex, | ||
and returns the index of the smallest value that occurs with index startIndex or greater. | ||
If this smallest value occurs more than once in this range, | ||
then return the index of the leftmost occurrence within this range. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* Returns either the index of the location in the array, | ||
or -1 if the array did not contain the targetValue */ | ||
var doSearch = function (array, targetValue){ | ||
var min = 0; | ||
var max = array.length - 1; | ||
var guess; | ||
var total = 0; | ||
while (max >= min) { | ||
total ++; | ||
guess = Math.floor((min + max) / 2); // first is 11 | ||
println(guess); | ||
if (array[guess] === targetValue) { | ||
println(total); | ||
return guess; | ||
} | ||
if (array[guess] < targetValue) { min = guess + 1; } // if 11 is < 20 (target Index) add guess 11 + 1 to minimum so min is now 12 (range 12 to 24) | ||
else if (array[guess] > targetValue) { max = guess - 1; } | ||
} | ||
return -1; // if max < min return -1 as it's outside the true statement of the loop. This only returns if the loop is false. | ||
}; | ||
|
||
var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, | ||
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]; | ||
|
||
var result = doSearch(primes, 73); | ||
println("Found prime at index " + result); | ||
|
||
Program.assertEqual(doSearch(primes, 73), 20); | ||
Program.assertEqual(doSearch(primes, 22), -1); | ||
Program.assertEqual(doSearch(primes, 23), 8); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var swap = function(array, firstIndex, secondIndex) { | ||
var temp = array[secondIndex]; // 9 | ||
array[secondIndex] = array[firstIndex]; // 7 | ||
array[firstIndex] = temp; | ||
}; | ||
|
||
var testArray = [7, 9, 4]; | ||
|
||
swap(testArray, 0, 1); | ||
println(testArray); | ||
Program.assertEqual(testArray, [9, 7, 4]); | ||
swap(testArray, 1, 2); | ||
println(testArray); | ||
Program.assertEqual(testArray, [9, 4, 7]); | ||
swap(testArray, 0, 2); | ||
println(testArray); | ||
Program.assertEqual(testArray, [7, 4, 9]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
function rot13(str) { | ||
var newStr=""; | ||
for(var i in str){ | ||
//Checks if character lies between A-Z | ||
if(str.charCodeAt(i) < 65 || str.charCodeAt(i) > 91) { | ||
newStr += String.fromCharCode(str.charCodeAt(i)); | ||
continue; | ||
} | ||
//N = ASCII 78, if the character code is less than 78, shift forward 13 places | ||
if(str.charCodeAt(i) < 78){ | ||
newStr += String.fromCharCode(str.charCodeAt(i) + 13); | ||
} | ||
else{ | ||
//Otherwise shift the character 13 places backward | ||
newStr += String.fromCharCode(str.charCodeAt(i) - 13); | ||
} | ||
} | ||
return newStr; | ||
} | ||
|
||
// Change the inputs below to test | ||
rot13("SERR PBQR PNZC"); | ||
|
||
// or | ||
|
||
function rot13(str) { | ||
// Split str into a character array | ||
return str.split('') | ||
// Iterate over each character in the array | ||
.map.call(str, function(char) { | ||
// Convert char to a character code | ||
x = char.charCodeAt(0); | ||
// Checks if character lies between A-Z | ||
if (x < 65 || x > 91) { | ||
return String.fromCharCode(x); // Return un-converted character | ||
} | ||
//N = ASCII 78, if the character code is less than 78, shift forward 13 places | ||
else if (x < 78) { | ||
return String.fromCharCode(x + 13); | ||
} | ||
// Otherwise shift the character 13 places backward | ||
return String.fromCharCode(x - 13); | ||
}).join(''); // Rejoin the array into a string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
function destroyer(arr) { | ||
var l = arguments.length; | ||
var myArr = arguments[0]; | ||
for( var i = 0; i < myArr.length; i++ ){ | ||
for( var j = 1; j < arguments.length; j++ ){ | ||
if( myArr[i] === arguments[j] ){ | ||
myArr.splice( i, 1 ); | ||
i -= 1; | ||
break; | ||
} | ||
} | ||
} | ||
return myArr; | ||
} | ||
|
||
destroyer([1, 2, 3, 1, 2, 3], 2, 3); | ||
|
||
// | ||
|
||
function destroyer(arr) { | ||
var args = Array.prototype.slice.call(arguments); | ||
args.splice(0, 1); | ||
return arr.filter(function(element) { | ||
return args.indexOf(element) === -1; | ||
}); | ||
} | ||
|
||
destroyer([1, 2, 3, 1, 2, 3], 2, 3); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
function where(arr, num) { | ||
arr = arr.sort(function(a,b){ | ||
return (a - b); | ||
}); | ||
if (arr.indexOf(num) === -1 && num > arr[arr.length - 1]) { | ||
console.log("first check"); | ||
return arr.length; | ||
} | ||
if (arr.indexOf(num) >= 0) { | ||
console.log("second check"); | ||
return arr.indexOf(num); | ||
} | ||
else { | ||
for (i=0; i<arr.length; i++){ | ||
if (arr[i] > num) { | ||
console.log("third check"); | ||
return arr.indexOf(arr[i]); | ||
} | ||
} | ||
} | ||
} | ||
where([10, 20, 30, 40, 50], 35); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
function flatten(arr) { | ||
var newArr = []; | ||
for (i=0; i<arr.length; i++){ | ||
for (j=0; j<arr[i].length; j++){ | ||
newArr.push(arr[i][j]); | ||
} | ||
} | ||
return newArr; | ||
} | ||
|
||
function flatten(l){ | ||
return l.reduce(function(a,b){return a.concat(b);}, []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
function isOdd(n) { | ||
return Math.abs(n % 2) == 1; | ||
} | ||
|
||
// more checks | ||
|
||
function isOdd(n) { | ||
if (n % 2 === 0) return false; | ||
else { | ||
console.log("number is ", n); | ||
if (n < 0) { | ||
if (Math.abs(n) % 1 === 0) { // check if absolute is odd | ||
console.log("first check"); | ||
return true; | ||
} | ||
else return false; | ||
} | ||
if (Number(n) === n && n % 1 !== 0) { // check if decimal | ||
if (n === parseInt(n)) { // is number == to it's integer? | ||
console.log(parseInt(n)); | ||
console.log("2nd check"); | ||
return true; | ||
} | ||
else return false; | ||
} | ||
else return true; | ||
if (n % 1 !== 0) { | ||
console.log("third check"); | ||
return false; | ||
} | ||
else return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class CWStack(object): | ||
def __init__(self): | ||
self.stack = [] | ||
def __str__(self): | ||
return "Stack of size: %d" % len(self.stack) | ||
def isEmpty(self): | ||
return self.stack == [] | ||
def push(self, elem): | ||
self.stack.append(elem) | ||
def pop(self): | ||
return self.stack.pop() | ||
def top(self): | ||
if not self.isEmpty(): | ||
return self.stack[-1] | ||
def reverse_str(str, stack): | ||
for c in str: | ||
stack.push(c) | ||
result = "" | ||
while not stack.isEmpty(): | ||
result += stack.pop() | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
def flatten(list): | ||
return [item for sublist in list for item in sublist] | ||
|
||
# or | ||
|
||
def flatten(l): | ||
return [i[x] for i in l for x in range(len(i))] | ||
|
||
|
||
# or | ||
|
||
def flatten(lists): | ||
flat_list = [] | ||
for list in lists: | ||
flat_list += list | ||
return flat_list |