-
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
Feb 9, 2016
1 parent
11e2a70
commit 93d64ef
Showing
10 changed files
with
225 additions
and
2 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,71 @@ | ||
/* | ||
Imagine you are creating a game where the user has to guess the correct number. But there is a limit of how many guesses the user can do. | ||
If the user tries to guess more than the limit the function show throw an error | ||
If the user guess wrong it should lose a life and return false (if you guess correctly you shouldn't remove a life) | ||
If the user guess right it should return true | ||
Can you finish the game so all the rules are met? | ||
//tests: | ||
describe('Guesser', () => { | ||
it('Correct guess should return true', () => { | ||
let guesser = new Guesser(10, 2); | ||
guesser.guess(10); | ||
guesser.guess(10); | ||
guesser.guess(10); | ||
guesser.guess(10); | ||
Test.expect(guesser.guess(10)); | ||
}); | ||
it('Wrong guess should return false', () => { | ||
let guesser = new Guesser(10, 2); | ||
guesser.guess(1); | ||
Test.expect(!guesser.guess(1)); | ||
}); | ||
it('Lives ran out should throw', () => { | ||
let guesser = new Guesser(10, 2); | ||
guesser.guess(1); | ||
guesser.guess(2); | ||
Test.expectError('Expect error already dead', () => { guesser.guess(10); }); | ||
}); | ||
}); | ||
*/ | ||
|
||
class Guesser { | ||
constructor(number, lives) { | ||
this.number = number; | ||
this.lives = lives; | ||
} | ||
guess(n) { | ||
var count = 0; | ||
if (count < this.lives){ // check if alive | ||
if (n == this.number){ | ||
count++; // counts number of tries | ||
return true; | ||
} | ||
if (n != this.number){ | ||
this.lives--; // | ||
count++; | ||
return false; | ||
} | ||
} | ||
else { | ||
throw "Expect error already dead"; // throw an error if lives are 0 | ||
}; | ||
} | ||
} | ||
|
||
// shorter | ||
|
||
class Guesser { | ||
constructor(number, lives) { | ||
this.number = number; | ||
this.lives = lives; | ||
} | ||
|
||
guess(n) { | ||
if (!this.lives) throw Error // check if lives are 0 | ||
this.lives-- | ||
return n === this.number // returns boolean result of comparison | ||
} | ||
} |
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 @@ | ||
function doubleCheck(str){ | ||
str = str.toLowerCase(); | ||
var len = str.length; | ||
for(var i = 1; i < len; i++) { | ||
if(str[i] === str[i - 1]) return true; | ||
} | ||
return false; | ||
} | ||
|
||
// so above starts from one and goes up to find if the next is same as previous element | ||
// or | ||
|
||
function doubleCheck(str){ | ||
return /(.)\1/i.test(str); | ||
} | ||
|
||
// matches any char except \n 1 more time |
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 @@ | ||
function generateRange(start, end, step){ | ||
var arr = []; | ||
for (var i = start; i <= end; i += step) { | ||
arr.push(i); | ||
} | ||
return arr; | ||
} | ||
|
||
// shorter | ||
|
||
function generateRange(min, max, step) { | ||
for (var res = []; min <= max; min += step) res.push(min) | ||
return res | ||
} | ||
|
||
generateRange(2, 10, 2); // [2,4,6,8,10] |
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,8 @@ | ||
function inAscOrder(arr) { | ||
for(var i = 1; i <= arr.length-1; i++) { | ||
if(arr[i] < arr[i-1]) return false; | ||
} | ||
return true; | ||
} | ||
|
||
inAscOrder([1,3,2,5,6]); |
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,3 @@ | ||
function pally(str){ | ||
return /^(?<l>\w)+\w?(\k<l>(?<-l>))+(?(l)(?!))$/.test(str); | ||
} |
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 sexy_prime(x, y){ | ||
if((x > 1 && x % 2 !== 0) && | ||
(y > 1 && y % 2 !== 0) && | ||
(x % 3 !== 0 && y % 3 !== 0)){ | ||
if(y - x == 6 || x - y == 6){ | ||
return true; | ||
} | ||
else return false; | ||
} | ||
else return false; | ||
} | ||
sexy_prime(5, 11); // true | ||
sexy_prime(5, 12); // false |
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,37 @@ | ||
/* | ||
function foo() {} | ||
foo.toString = function() { | ||
return 'blablabla'; | ||
}; | ||
Write a function _originalToString which takes a function as the argument, and returns a string which is as same as the argument's original toString's. | ||
Note: You should not change the argument. | ||
For example, the following statement returns function foo() {} instead of blablabla: | ||
_originalToString(foo); // function foo() {} | ||
testcases: | ||
function foo() {} | ||
var original = foo.toString(); | ||
foo.toString = function() { | ||
return 'blablabla'; | ||
}; | ||
Test.assertEquals(_originalToString(foo), original); | ||
*/ | ||
|
||
|
||
var _originalToString = function(func) { | ||
// we have to use object's to string method | ||
// because if we call toString like this: | ||
// func.prototype.toString() | ||
// then the prototype object is used as the this context | ||
// which results in the string [object Object] | ||
// which is different from the original toString method of a function | ||
return Object.toString.apply(func); | ||
}; |
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,32 @@ | ||
const rps = (p1, p2) => { | ||
if (p1 == "scissors"){ | ||
if (p2 == "paper") return "Player 1 won!"; | ||
if (p2 == "rock") return "Player 2 won!"; | ||
if (p2 == "scissors") return "Draw!"; | ||
} | ||
if (p1 == "rock"){ | ||
if (p2 == "paper") return "Player 2 won!"; | ||
if (p2 == "scissors") return "Player 1 won!"; | ||
if (p2 == "rock") return "Draw!"; | ||
} | ||
if (p1 == "paper"){ | ||
if (p2 == "scissors") return "Player 2 won!"; | ||
if (p2 == "rock") return "Player 1 won!"; | ||
if (p2 == "paper") return "Draw!"; | ||
} | ||
}; | ||
|
||
// shorter | ||
|
||
const rps = (p1, p2) => { | ||
if (p1 == p2){ | ||
return "Draw!"; | ||
} | ||
else if ((p1 == "scissors" && p2 == "paper") || (p1 == "paper" && p2 == "rock") || (p1 == "rock" && p2 == "scissors")){ | ||
return "Player 1 won!"; | ||
} | ||
|
||
else if ((p2 == "scissors" && p1 == "paper") || (p2 == "paper" && p1 == "rock") || (p2 == "rock" && p1 == "scissors")){ | ||
return "Player 2 won!"; | ||
} | ||
}; |
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
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,26 @@ | ||
import re | ||
|
||
def double_check(strng): | ||
match = re.search(r'(.)\1', strng, re.I) | ||
if match == None: return False | ||
else: return True | ||
|
||
double_check("aa b c") | ||
|
||
# shorter below | ||
|
||
import re | ||
|
||
def double_check(str): | ||
return bool(re.search(r"(.)\1", str.lower())) | ||
|
||
# or | ||
|
||
def double_check(strng): | ||
strng = strng.lower() | ||
for i in range(len(strng)-1): | ||
if strng[i] == strng[i+1]: | ||
return True | ||
else: | ||
return False | ||
|