Last active
January 24, 2017 06:13
-
-
Save tps2015gh/37d3912d9a994e13fe8b1c84768335a3 to your computer and use it in GitHub Desktop.
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
/* จาก คำถาม Facebook สมาคมโปรแกรมเมอร์ไทย */ | |
/* คำถามโดย Chetaphong Preecha , วันที่ 23 มค 2560 เช้า */ | |
/* Coding Javascript By Thitipong Samranvanich */ | |
/* Javascript , can run on any browser F12 > Console */ | |
/* Original Concept from Ruby Code - ใครสักคนเขียนไว้ แค่ 1 บรรทัดจบ */ | |
/* ปรับเป็น JavaScript แล้ว รวม เกือบ 50 บรรทัด 555+ */ | |
// init result | |
a_result = [] ; | |
for(i =0;i <= 999 ; i++){ | |
var tx = ("000" + i ); | |
var tx3 = tx.substring(tx.length-3); | |
a_result.push(tx3); | |
} | |
function to_arr(tx3){ | |
return [tx3.substring(0,1),tx3.substring(1,2),tx3.substring(2,3)]; | |
} | |
function test(tx3,result){ | |
var ar_test = to_arr(tx3); | |
var ar_result = to_arr(result); | |
var count_num = 0 , count_pos = 0 ; | |
var ar_intersec = intersec(ar_test,ar_result); | |
count_num = ar_intersec.length; | |
count_pos = get_count_pos(ar_test,ar_result); | |
return count_num + "/" + count_pos; | |
} | |
function intersec(array1,array2){ | |
return array1.filter(function(n) { | |
return array2.indexOf(n) != -1; | |
}); | |
} | |
function get_count_pos(array1,array2){ | |
return array1.reduce(function(total,a1val,idx){ | |
//console.log("compare " +idx); | |
var num1 = (a1val==array2[idx] ? 1 :0); | |
//console.log("idx " + idx + " => "+ num1); | |
return total + num1 ; | |
},0 /*init as 0 */ ); | |
} | |
function test5(try_result){ | |
if( | |
test("682",try_result) == "1/1" && | |
test("614",try_result) == "1/0" && | |
test("206",try_result) == "2/0" && | |
test("738",try_result) == "0/0" && | |
test("870",try_result) == "1/0" | |
){ return true ; } | |
else{ | |
return false ; | |
} | |
} | |
a_result.filter(function(try_result){ | |
return test5(try_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
<?php | |
/* จาก คำถาม Facebook สมาคมโปรแกรมเมอร์ไทย */ | |
/* คำถามโดย Chetaphong Preecha , วันที่ 23 มค 2560 เช้า */ | |
/* Coding Javascript By Thitipong Samranvanich */ | |
/* Javascript , can run on any browser F12 > Console */ | |
/* Original Concept from Ruby Code - ใครสักคนเขียนไว้ แค่ 1 บรรทัดจบ */ | |
/* ปรับเป็น JavaScript แล้ว รวม เกือบ 50 บรรทัด 555+ แล้วเปลงเป็น PHP ต่อ */ | |
//============================================================== | |
// ทดลอง โค้ด PHP Online ได้ที่ http://phptester.net/ | |
// init result | |
$a_result = [] ; | |
$MAX = 60 ; | |
for($i =0;$i <= $MAX ; $i++){ | |
$tx = ("000" . $i ); | |
$tx3 = substr($tx, -3); | |
$a_result[] = $tx3; | |
} | |
//echo "<pre>"; | |
//print_r($a_result); | |
//echo "</pre>"; | |
function to_arr($tx3){ | |
return [substr($tx3,0,1) ,substr($tx3,1,1),substr($tx3,2,1)]; | |
} | |
function test($tx3,$result){ | |
$ar_test = to_arr($tx3); | |
$ar_result = to_arr($result); | |
$count_num = 0 ; $count_pos = 0 ; | |
$ar_intersec = intersec($ar_test,$ar_result); | |
$count_num = count($ar_intersec); | |
$count_pos = get_count_pos($ar_test,$ar_result); | |
$out1 = $count_num . "/" . $count_pos; | |
// echo "<br>out1= $out1 "; | |
return $out1 ; | |
} | |
function intersec($array1, $array2){ | |
$ar_ret = array_filter( $array1 , | |
function ($e) use ($array2){ /* closure , anonymous function*/ | |
return in_array($e,$array2) ; | |
}); | |
return $ar_ret; | |
} | |
function get_count_pos($array1,$array2){ | |
$idx = -1 ; | |
return array_reduce($array1, function($total,$a1val) use($array2 , &$idx ){ | |
$idx ++ ; /*start from index=0 */ | |
return $total + ($a1val==$array2[$idx] ? 1 :0) ; | |
}, 0 /*init as 0 */ ); | |
} | |
function test5($try_result){ | |
if( | |
test("682",$try_result) == "1/1" && | |
test("614",$try_result) == "1/0" && | |
test("206",$try_result) == "2/0" && | |
test("738",$try_result) == "0/0" && | |
test("870",$try_result) == "1/0" | |
){ return true ; } | |
else{ | |
return false ; | |
} | |
} | |
$ar = array_filter($a_result,function($try_result){ | |
return test5($try_result); | |
}); | |
print_r($ar); |
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
ar = Array(999).fill().map((_, i) => ("000"+i).slice(-3)) | |
function to_arr(tx3){ | |
return [tx3.substring(0,1),tx3.substring(1,2),tx3.substring(2,3)]; | |
} | |
function test(tx3,result){ | |
var a1 = to_arr(tx3); | |
var ar_r = to_arr(result); | |
return intersec(a1,ar_r).length + "/" + get_c_pos(a1,ar_r); | |
} | |
function intersec(arr1,arr2){ | |
return arr1.filter(function(n) { | |
return arr2.indexOf(n) != -1; | |
}); | |
} | |
function get_c_pos(arr1,arr2){ | |
return arr1.reduce(function(total,a1val,idx){ | |
var num1 = (a1val==arr2[idx] ? 1 :0); | |
return total + num1 ;},0 ); | |
} | |
function test5(try1){ | |
if( test("682",try1) == "1/1" && test("614",try1) == "1/0" && | |
test("206",try1) == "2/0" && test("738",try1) == "0/0" && | |
test("870",try1) == "1/0" | |
){ return true ; } | |
else{ return false ; } | |
} | |
ar.filter(function(try1){ /*main*/ | |
return test5(try1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment