जावास्क्रिप्ट - 6 भाषाएँ - उच्च सटीकता
वर्तमान भाषाएँ: जावा, सी, एचटीएमएल, पीएचपी, सीएसएस, जावास्क्रिप्ट
मैं इस सिद्धांत पर काम करता हूं कि जब भी कोई इनपुट किसी मापदंड को पूरा करता है, तो उसे एक अंक दिया जाता है, और उस स्कोर के आधार पर परिणाम दिए जाते हैं।
विशेषताएं:
- कोई अंतर्निहित कार्य जो भाषा प्रकार का उपयोग करता है, को निर्धारित करता है।
- किसी
xकीवर्ड को देखने पर इनपुट टेक्स्ट सीधे भाषा घोषित नहीं करता है ।
- अन्य संभावित भाषाओं को भी प्रस्तुत करता है।
क्या आपको यह महसूस करना चाहिए कि आपके किसी भी कार्यक्रम के इनपुट (जो मैंने अब तक किए हैं) पकड़े नहीं गए हैं या अमान्य परिणाम प्राप्त हुए हैं, तो कृपया रिपोर्ट करें और मुझे उन्हें ठीक करने में खुशी होगी।
नमूना इनपुट 1:
class A{public static void main(String[]a){System.out.println("<?php");}}
नमूना आउटपुट 1:
My program thinks you have :
Java with a chance of 100%
Php with a chance of 25%
----------------
स्पष्टीकरण:
यह प्रोग्राम को विफल कर देना चाहिए था और मैंने प्रिंट किया होगा PHP, लेकिन चूंकि मेरा प्रोग्राम स्कोर के आधार पर काम करता है, इसलिए कुछ भी विफल नहीं होता है और यह आसानी से पहले स्थान पर जावा की पहचान करता है, इसके बाद अन्य संभावित परिणाम मिलते हैं।
नमूना इनपुट 2:
class A{public static void main(String[]a){System.out.println("HelloWorld!");}}
नमूना आउटपुट 2:
Java
----------------
नमूना इनपुट 3:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
नमूना आउटपुट 3:
Language not catched! Sorry.
----------------
कोड:
// Helper functions
String.prototype.m = function(condition){
return this.match(condition);
};
String.prototype.capitalize = function(){
return this[0].toUpperCase() + this.substr(1);
};
function getFuncName(func){
var temp = func.toString();
temp = temp.substr( "function ".length);
temp = temp.substr( 0, temp.indexOf("("));
return temp.capitalize();
}
// Get input
var lang_input = prompt("Enter programming language");
// Max score of 4 per lang
function java(input){
var score = 0;
score += input.m(/class[\s\n]+[\w$]+[\s\n]*\{/) ? 1 : 0;
score += input.m(/public[\s\n]+static[\s\n]+void[\s\n]+main[\s\n]*/) ? 1 : 0;
score += input.m(/\}[\s\n]*\}[\s\n]*$/) ? 1 : 0;
score += input.m(/System[\s\n]*[.][\s\n]*out/) ? 1 : 0;
return score;
}
function c(input){
var score = 0;
// if java has passsed
if(checks[0][1] >= 3)return 0;
score += input.m(/^#include\s+<[\w.]+>\s*\n/) ? 1 : 0;
score += input.m(/main[\s\n]*\([\s\n]*(void)?[\s\n]*\)[\s\n]*\{/) ? 1 : 0;
score += input.m(/printf[\s\n]+\(/) || input.m(/%d/) ? 1 : 0;
score += input.m(/#include\s+<[\w.]+>\s*\n/) || input.m(/(%c|%f|%s)/) ? 1 : 0;
return score;
}
function PHP(input){
var score = 0;
score += input.m(/<\?php/) ? 1 : 0;
score += input.m(/\?>/) ? 1 : 0;
score += input.m(/echo/) ? 1 : 0;
score += input.m(/$[\w]+\s*=\s*/) ? 1 : 0;
return score;
}
function HTML(input){
var score = 0;
// if php has passed
if(checks[2][1] >= 2) return 0;
score += input.m(/<!DOCTYPE ["' \w:\/\/]*>/) ? 1 : 0;
score += input.m(/<html>/) && input.m(/<\/html>/) ? 1 : 0;
score += input.m(/<body>/) && input.m(/<\/body/) ? 1 : 0;
score += input.m(/<head>/) && input.m(/<\/head>/) ? 1 : 0;
return score;
}
function javascript(input){
var score = 0;
score += input.m(/console[\s\n]*[.][\s\n]*log[\s\n*]\(/) ? 1 : 0;
score += input.m(/[\s\n]*var[\s\n]+/) ? 1 : 0;
score += input.m(/[\s\n]*function[\s\n]+[\w]+[\s\n]+\(/) ? 1 : 0;
score += input.m(/document[\s\n]*[.]/) ||
( input.m(/\/\*/) && input.m(/\*\//) ) ||
( input.m(/\/\/.*\n/) )? 1 : 0;
return score;
}
function CSS(input){
var score = 0;
score += input.m(/[a-zA-Z]+[\s\n]*\{[\w\n]*[a-zA-Z\-]+[\s\n]*:/) ? 1 : 0;
// since color is more common, I give it a separate place
score += input.m(/color/) ? 1 : 0;
score += input.m(/height/) || input.m(/width/) ? 1 : 0;
score += input.m(/#[a-zA-Z]+[\s\n]*\{[\w\n]*[a-zA-Z\-]+[\s\n]*:/) ||
input.m(/[.][a-zA-Z]+[\s\n]*\{[\w\n]*[a-zA-Z\-]+[\s\n]*:/) ||
( input.m(/\/\*/) && input.m(/\*\//) ) ? 1 : 0;
return score;
}
// [Langs to check, scores]
var checks = [[java, 0], [c, 0], [PHP, 0], [HTML, 0], [javascript, 0], [CSS, 0]];
//Their scores
// Assign scores
for(var i = 0; i < checks.length; i++){
var func = checks[i][0];
checks[i][1] = func(lang_input);
}
// Sort the scores
checks.sort(function(a,b){ return b[1] - a[1]; });
var all_zero = true;
function check_all_zero(index){
if(checks[index][1] > 0){ all_zero = false; return 0; } // someone is above zero
// check next index only if it defined, else return zero
if(checks[index + 1])
check_all_zero(index + 1);
}
check_all_zero(0);
if(all_zero){
console.log("Language not catched! Sorry.");
}else {
var new_arr = []; // temp
checks.map(function(value, index){
if(value[1] > 0){
var temp = [getFuncName(value[0]), value[1]];
new_arr.push(temp);
}
});
checks = new_arr.slice(0); // array copy, because of mutation
if(checks.length === 1){
console.log(checks[0][0]);
}else{
console.log("My program thinks you have :");
checks.map(function(value){
var prob = (value[1]/4 * 100);
console.log(value[0] + " with a chance of " + prob + "%");
});
}
} // Main else block finish
console.log("----------------");
print("")कई भाषाओं में इस्तेमाल किया जा सकता है।