Skip to content

Commit

Permalink
new exercises on tuesday
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro Giordo committed Feb 9, 2016
1 parent 11e2a70 commit 93d64ef
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 2 deletions.
71 changes: 71 additions & 0 deletions Js/class_guesser_game.js
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
}
}
17 changes: 17 additions & 0 deletions Js/find duplicates.js
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
16 changes: 16 additions & 0 deletions Js/generate_range.js
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]
8 changes: 8 additions & 0 deletions Js/is_array_ordered.js
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]);
3 changes: 3 additions & 0 deletions Js/palyndrome-match.js
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);
}
13 changes: 13 additions & 0 deletions Js/prime_duo.js
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
37 changes: 37 additions & 0 deletions Js/prototyping.js
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);
};
32 changes: 32 additions & 0 deletions Js/rock_paper_scissors.js
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!";
}
};
4 changes: 2 additions & 2 deletions Js/sparse_dense.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ function toDense(sparse) {
newArray.push(sparse[i]);
}
}
console.log(newArray);
console.log(newArray);
return newArray;
}
toDense(sparse);
toDense(sparse);

// better below

Expand Down
26 changes: 26 additions & 0 deletions Py/find_duplicates.py
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

0 comments on commit 93d64ef

Please sign in to comment.