Swedish National Identity Number
Description:
In Sweden a Personal Identity Number (Swedish: personnummer) is used in dealings with public agencies, from health care to the tax authorities. It is also used as a customer number in banks and insurance companies. It is written on all approved identity documents, making the risk of mixing up people low.
The number can be either
YYMMDDXXXX
10 digits orYYYYMMDDXXXX
12 digits
It's usally separated by a -
hyphen. Then it's formatted like this:
YYMMDD-XXXX
YYYYMMDD-XXXX
Where YYMMDD
or YYYYMMDD
is the year of birth.
Valid dates is between 1800-01-01 and 2099-12-31.
When the year is formated with two digits YY
expect that the year is 19YY
.
XXXX
is divided into three parts. The first two are random, the third is also random but is odd for men and even for women.
The fourth digit is a control digit that checks if the complete number is valid.
To calculate this digit you take the first nine digits excluding the first 2 digits of the year if the number is twelve digits, and multiply each digit with 1 and 2 alternately.
After that you sum the digit sum of each answer and from that you subtract the sum with the nearest highest tenth of that sum. From that you get the last(fourth) digit.
Example
So if a girl is born January 4, 1897 the first part is 18970104
. We take two random digits and append a random even integer 2
.
We get the number 18970104792
.
When we calculate the control digit we want to exclude the first two digits of the year, so we get the number: 19970104792
Multiplies each digit with 1 and 2 alternately
9 7 0 1 0 4 7 9 2
x x x x x x x x x
2 1 2 1 2 1 2 1 2
You get the results and sum them up.
1 + 8 + 7 + 0 + 1 + 0 + 4 + 1 + 4 + 9 + 4 = 39
Subtract 39 with the nearest highest 10 40 - 39 = 1
and you get the last control digit.
This leaves you with the complete identity number 9701047921
The problem
We want a function given a identity number to return true
if it's valid and false
if it's not valid.
Allowed formats for the parameter are
YYMMDDXXXX
YYMMDD-XXXX
YYYYMMDDXXXX
YYYYMMDD-XXXX
19YYMMDD
or YYYYMMDD
needs to be a valid date between 1800-01-01 and 2099-12-31
1800-01-01 and 2099-12-31 are valid dates.
If you encounter a 10 digit number, the first two digits are 19
.
Make sure you check for leap years.
If the parameter is not correctly formated, it should return false.
Similar Kata:
Stats:
Created | Nov 23, 2016 |
Published | Nov 23, 2016 |
Warriors Trained | 57 |
Total Skips | 0 |
Total Code Submissions | 244 |
Total Times Completed | 17 |
JavaScript Completions | 17 |
Total Stars | 6 |
% of votes with a positive feedback rating | 58% of 6 |
Total "Very Satisfied" Votes | 2 |
Total "Somewhat Satisfied" Votes | 3 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 6 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 7 kyu |