Last active
December 14, 2015 03:39
-
-
Save georgeOsdDev/5022484 to your computer and use it in GitHub Desktop.
Gengo Challenges
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
var calc; | |
calc = function(numStr){ | |
var left,right,sumLeft,sumRight; | |
if (typeof numStr !== "string" || isNaN(Number(numStr))){ | |
throw new TypeError("unexpected argument"); | |
} | |
if (numStr.length%2 !== 0){ | |
throw new Error("argument must be 2*N digits"); | |
} | |
left = numStr.substr(0,numStr.length/2); | |
right = numStr.substr(numStr.length/2); | |
function sum(str){ | |
var arr,ret = 0; | |
arr = str.split(""); | |
for (var i = 0;i<arr.length;i++){ | |
ret += Number(arr[i]); | |
} | |
return ret; | |
} | |
sumLeft = sum(left); | |
sumRight = sum(right); | |
return sumLeft === sumRight ? sumLeft : 0; | |
}; | |
module.exports = calc; |
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
var should = require("chai").should(), | |
calc = require('./a_1.js'); | |
describe("Sample Test",function(){ | |
it("should be return 6",function(){ | |
(calc("123231")).should.equal(6); | |
}); | |
it("should be return 0",function(){ | |
(calc("123232")).should.equal(0); | |
}); | |
it("should be TypeError",function(){ | |
try { | |
calc("string"); | |
}catch(e){ | |
e.message.should.equal("unexpected argument"); | |
} | |
}); | |
it("should be TypeError",function(){ | |
try { | |
calc({"foo":"bar"}); | |
}catch(e){ | |
e.message.should.equal("unexpected argument"); | |
} | |
}); | |
it("should be Error",function(){ | |
try { | |
calc("123"); | |
}catch(e){ | |
e.message.should.equal("argument must be 2*N digits"); | |
} | |
}); | |
}); |
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
var search,search2; | |
search = function(){ | |
var cnt =0, | |
flg = true, | |
index = 1, | |
f; | |
f = function(n){ | |
var arr = n.toString().split(""); | |
for (var i = 0;i < arr.length;i++){ | |
if (arr[i] === "1"){ | |
cnt +=1; | |
} | |
} | |
return cnt; | |
}; | |
while(flg){ | |
f(index); | |
if (cnt === index && index > 1) { | |
flg = false; | |
}else{ | |
index++; | |
} | |
} | |
return index; | |
}; | |
module.exports.search = search; | |
console.log("B-1's Answer is: "+ search()); | |
// yet another apploach | |
search2 = function(){ | |
var cnt =0, | |
flg = true, | |
index = 1, | |
f; | |
f = function(n){ | |
cnt += n.toString().replace(/[^1]/g,"").length; | |
return cnt; | |
}; | |
while(flg){ | |
f(index); | |
if (cnt === index && index > 1) { | |
flg = false; | |
}else{ | |
index++; | |
} | |
} | |
return index; | |
}; | |
module.exports.search2 = search2; | |
console.log("(another apploach) B-1's Answer is: "+ search2()); |
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
var should = require("chai").should(), | |
searchResult = require("./b_1.js").search(), | |
search2Result = require("./b_1.js").search2(); | |
describe("Test B-1",function(){ | |
it("should be equal",function(){ | |
searchResult.should.equal(search2Result); | |
}); | |
describe("Test B-1 assertion",function(){ | |
var test = ""; | |
before(function(){ | |
for(var i = 0;i<searchResult+1;i++){ | |
test +=i; | |
} | |
}); | |
it("should be return "+searchResult,function(){ | |
var arrtest,cnt = 0; | |
arrtest = test.split(""); | |
for (var j = 0;j < arrtest.length;j++){ | |
if (arrtest[j] === "1"){ | |
cnt +=1; | |
} | |
} | |
cnt.should.equal(searchResult); | |
}); | |
it("should be return"+searchResult,function(){ | |
test.replace(/[^1]/g,"").length.should.equal(searchResult); | |
}); | |
}); | |
}); |
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
var findsLongestPalindrome; | |
// findsLongestPalindrome is low performance use findsLongestPalindrome2 | |
findsLongestPalindrome = function(str){ | |
var targets = [], | |
result = null, | |
getLeft, | |
getRight, | |
chkPalindrome; | |
if (typeof str !== "string"){ | |
throw new TypeError("unexpected argument"); | |
} | |
String.prototype.reverse = function(){ | |
return this.split("").reverse().join(""); | |
}; | |
getLeft = function(str){ | |
return str.substr(0,str.length-1); | |
}; | |
getRight = function(str){ | |
return str.substr(1); | |
}; | |
chkPalindrome = function(){ | |
var str = targets.shift(); | |
if (str === str.reverse()){ | |
return str; | |
}else{ | |
targets.push(getLeft(str)); | |
targets.push(getRight(str)); | |
return null; | |
} | |
}; | |
targets.push(str); | |
while(targets.length > 0){ | |
result = chkPalindrome(); | |
if(result !== null){ | |
break; | |
} | |
} | |
return result; | |
}; | |
module.exports.findsLongestPalindrome = findsLongestPalindrome; | |
// findsLongestPalindrome is too late. So yet Another Apploach | |
var findsLongestPalindrome2; | |
findsLongestPalindrome2 = function(str){ | |
var charArr = str.split(""), | |
result = null, | |
temp = [], | |
longest; | |
for (var i = 0; i < charArr.length; i++){ | |
var obj = { | |
index:i, | |
len:1 | |
}, | |
j = 1; | |
if (charArr[i] === charArr[i+1]){ | |
obj.len = 2; | |
for (j =1; j <= i;j++){ | |
if (charArr[(i-j)] === charArr[(i+1+j)]){ | |
obj.len = j*2+2; | |
} else { | |
break; | |
} | |
} | |
}else{ | |
for (j =1; j <= i;j++){ | |
if (charArr[(i-j)] === charArr[(i+j)]){ | |
obj.len = j*2+1; | |
} else { | |
break; | |
} | |
} | |
} | |
temp.push(obj); | |
} | |
temp.sort(function(a, b) { | |
return ( a.len > b.len ? 1 : -1); | |
}); | |
longest = temp.pop(); | |
var stIdx = 0; | |
if (longest.len%2 === 0){ | |
stIdx = longest.index-(longest.len/2-1) < 0 ? 0: longest.index-(longest.len/2-1); | |
result = str.substr(stIdx,longest.len); | |
}else{ | |
stIdx = longest.index-Math.floor(longest.len/2) < 0 ? 0: longest.index-Math.floor(longest.len/2); | |
result = str.substr(stIdx,longest.len); | |
} | |
return result; | |
}; | |
module.exports.findsLongestPalindrome2 = findsLongestPalindrome2; |
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
var should = require("chai").should(), | |
findsLongestPalindrome = require("./c_1.js").findsLongestPalindrome; | |
findsLongestPalindrome2 = require("./c_1.js").findsLongestPalindrome2; | |
describe("Test findsLongestPalindrome",function(){ | |
it("should be a",function(){ | |
findsLongestPalindrome("a").should.equal("a"); | |
}); | |
it("should be bb",function(){ | |
findsLongestPalindrome("abb").should.equal("bb"); | |
}); | |
it("should be aba",function(){ | |
findsLongestPalindrome("aba").should.equal("aba"); | |
}); | |
it("should be cccc",function(){ | |
findsLongestPalindrome("abacccc").should.equal("cccc"); | |
}); | |
it("should be cccc",function(){ | |
findsLongestPalindrome("abaccccded").should.equal("cccc"); | |
}); | |
it("should be defed",function(){ | |
findsLongestPalindrome("abaccccdefed").should.equal("defed"); | |
}); | |
it("should be あいうえおおえいうあ",function(){ | |
findsLongestPalindrome("abcbeffeあいうえおおえういあ90").should.equal("あいうえおおえういあ"); | |
}); | |
}); | |
describe("Test findsLongestPalindrome2",function(){ | |
it("should be a",function(){ | |
findsLongestPalindrome2("a").should.equal("a"); | |
}); | |
it("should be a",function(){ | |
findsLongestPalindrome2("abb").should.equal("bb"); | |
}); | |
it("should be aba",function(){ | |
findsLongestPalindrome2("aba").should.equal("aba"); | |
}); | |
it("should be cccc",function(){ | |
findsLongestPalindrome2("abacccc").should.equal("cccc"); | |
}); | |
it("should be cccc",function(){ | |
findsLongestPalindrome2("abaccccded").should.equal("cccc"); | |
}); | |
it("should be defed",function(){ | |
findsLongestPalindrome2("abaccccdefed").should.equal("defed"); | |
}); | |
it("should be あいうえおおえいうあ",function(){ | |
findsLongestPalindrome2("abcbeffeあいうえおおえういあ90").should.equal("あいうえおおえういあ"); | |
}); | |
}); | |
describe('performance test', function(){ | |
var start; | |
beforeEach(function(done){ | |
start = new Date(); | |
done(); | |
}); | |
it("findsLongestPalindrome2 paerformance test",function(done){ | |
findsLongestPalindrome2("long sententce abaccccffffffffffffffffdefed").should.equal("ffffffffffffffff"); | |
var time = (new Date()).getTime() - start.getTime(); | |
console.log(time); | |
done(); | |
}); | |
// this test doesn't response | |
// it("findsLongestPalindrome paerformance test",function(done){ | |
// findsLongestPalindrome("long sententce abaccccffffffffffffffffdefed").should.equal("ffffffffffffffff"); | |
// var time = (new Date()).getTime() - start.getTime(); | |
// console.log(time); | |
// done(); | |
// }); | |
}); |
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
DROP TABLE my_collection; | |
DROP TABLE contents_type; | |
DROP TABLE rend; | |
CREATE TABLE no_stock( | |
asin VARCHAR(10) PRIMARY KEY NOT NULL | |
); | |
CREATE TABLE rend ( | |
id MEDIUMINT PRIMARY KEY NOT NULL AUTO_INCREMENT, | |
content_id VARCHAR(10), | |
friend_mail VARCHAR(50) REFERENCES friends(mail), | |
rend_ymd VARCHAR(8), | |
return_flg VARCHAR(8) DEFAULT 0, | |
return_ymd VARCHAR(8), | |
upd_date TIMESTAMP | |
); |
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
CREATE TABLE contents_type ( | |
id MEDIUMINT NOT NULL PRIMARY KEY, | |
val VARCHAR(20) | |
); | |
CREATE TABLE friends ( | |
mail VARCHAR(50) NOT NULL PRIMARY KEY, | |
name VARCHAR(50) | |
); | |
CREATE TABLE my_collection ( | |
id MEDIUMINT PRIMARY KEY NOT NULL AUTO_INCREMENT, | |
title VARCHAR(50), | |
contents_type MEDIUMINT REFERENCES contents_type(id) | |
); | |
CREATE TABLE rend ( | |
id MEDIUMINT PRIMARY KEY NOT NULL AUTO_INCREMENT, | |
content_id MEDIUMINT REFERENCES my_collection(id), | |
friend_mail VARCHAR(50) REFERENCES friends(mail), | |
rend_ymd VARCHAR(8), | |
return_flg VARCHAR(8) DEFAULT 0, | |
return_ymd VARCHAR(8), | |
upd_date TIMESTAMP | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment