-
-
Save pashaie/ff4b65dfaeecf0514f22 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
// Check Iranian National Code Validity - Clojure, C#, Ruby, JavaScript, Python, Scala, Java 8, PHP | |
// بررسی صحت کد ملی ایران - کلوژر، سیشارپ، روبی، جاوااسکریپت، پایتون، اسکالا، جاوا ۸، پیاچپی | |
// در نسخههای قبل یکسان بودن اعداد نا معتبر تشخیص داده میشد ولی | |
// اعداد یکسان نامعتبر نیست http://www.fardanews.com/fa/news/127747 | |
/** | |
* @author Ebrahim Byagowi | |
* @lincense Multi-licensed under the MIT and GPLv3 licenses. | |
*/ | |
// Clojure | |
(defn valid-iran-code? [input] | |
(if (re-matches #"^\d{10}$" input) | |
(let | |
[check (Integer/parseInt (subs input 9 10)) | |
sum (mod (reduce + (map (fn [x] (* (Integer/parseInt | |
(subs | |
input x (+ x 1))) | |
(- 10 x))) | |
(range 9))) 11)] | |
(or (and (< sum 2) (== check sum)) | |
(and (>= sum 2) (== (+ check sum) 11)))) | |
false)) | |
// Ruby | |
def is_valid_iran_code(input) | |
return false if /^\d{10}$/ !~ input | |
check = Integer(input[9]) | |
s = (0..8).map{|x| Integer(input[x]) * (10 - x)}.inject(:+) % 11 | |
s < 2 && check == s || s >= 2 && check + s == 11 | |
end | |
// C# | |
using System.Text.RegularExpressions; | |
public static bool IsValidIranianNationalCode(string input) | |
{ | |
if (!Regex.IsMatch(input, @"^\d{10}$")) | |
return false; | |
var check = Convert.ToInt32(input.Substring(9, 1)); | |
var sum = Enumerable.Range(0, 9) | |
.Select(x => Convert.ToInt32(input.Substring(x, 1)) * (10 - x)) | |
.Sum() % 11; | |
return sum < 2 && check == sum || sum >= 2 && check + sum == 11; | |
} | |
// JavaScript | |
function isValidIranianNationalCode(input) { | |
if (!/^\d{10}$/.test(input)) | |
return false; | |
var check = parseInt(input[9]); | |
var sum = [0, 1, 2, 3, 4, 5, 6, 7, 8] | |
.map(function (x) { return parseInt(input[x]) * (10 - x); }) | |
.reduce(function (x, y) { return x + y; }) % 11; | |
return sum < 2 && check == sum || sum >= 2 && check + sum == 11; | |
} | |
// Python | |
import re | |
def is_valid_iran_code(input): | |
if not re.search(r'^\d{10}$', input): | |
return False | |
check = int(input[9]) | |
s = sum(map(lambda x: int(input[x]) * (10 - x), range(0, 9))) % 11 | |
return s < 2 and check == s or s >= 2 and check + s == 11 | |
// Scala | |
def isValidIranianNationalCode(input: String) = { | |
val pattern = """^\d{10}$""".r | |
input match { | |
case pattern(_) => | |
val check = input.substring(9, 10).toInt | |
val sum = (0 to 8) | |
.map(x => input.substring(x, x + 1).toInt * (10 - x)) | |
.sum % 11 | |
sum < 2 && check == sum || sum >= 2 && check + sum == 11 | |
case _ => false | |
} | |
} | |
// Java 8 | |
import java.util.stream.Streams; | |
import java.util.function.IntUnaryOperator; | |
public static boolean isValidIranianNationalCode(String input) { | |
// check if input has 10 digits that all of them are not equal | |
if (!input.matches("^\\d{10}$")) | |
return false; | |
int check = Integer.parseInt(input.substring(9, 10)); | |
int sum = Streams.intRange(0, 9) | |
.map((IntUnaryOperator) x -> | |
Integer.parseInt(input.substring(x, x + 1)) * (10 - x)) | |
.sum() % 11; | |
return sum < 2 && check == sum || sum >= 2 && check + sum == 11; | |
} | |
// PHP | |
function isValidIranianNationalCode($input) { | |
# check if input has 10 digits that all of them are not equal | |
if (!preg_match("/^\d{10}$/", $input)) { | |
return false; | |
} | |
$check = (int) $input[9]; | |
$sum = array_sum(array_map(function ($x) use ($input) { | |
return ((int) $input[$x]) * (10 - $x); | |
}, range(0, 8))) % 11; | |
return $sum < 2 && $check == $sum || $sum >= 2 && $check + $sum == 11; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment