Created
June 8, 2017 21:17
-
-
Save m1n0/e05ea0b8c08653e583b6bbb3e4f3bad8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import sys | |
| import datetime | |
| from random import randint | |
| import re | |
| def random_with_n_digits(n): | |
| range_start = 10**(n-1) | |
| range_end = (10**n)-1 | |
| return randint(range_start, range_end) | |
| year_of_change = 1954 | |
| if len(sys.argv) > 1: | |
| # Validate | |
| errors = [] | |
| input_data = sys.argv[1] | |
| if not re.match(r'^\s*(\d){6}[ /]*(\d){3,4}\s*$', input_data): | |
| raise ValueError("Invalid format, aborting") | |
| year = int(input_data[0:2]) | |
| month = int(input_data[2:4]) | |
| day = int(input_data[4:6]) | |
| numeric_input = int(input_data.replace('/', '')) | |
| if month > 12: | |
| month = month - 50 | |
| if month < 1 or month > 12: | |
| errors.append('Invalid month') | |
| if numeric_input % 11 != 0: | |
| errors.append("Invalid checksum") | |
| if errors: | |
| print('INVALID INPUT:') | |
| for error in errors: | |
| print(error) | |
| else: | |
| # Generate | |
| # Get Date | |
| date = '' | |
| valid = False | |
| while not valid: | |
| date_text = input('Day of the birth [YYYY-MM-DD]:') | |
| try: | |
| date = datetime.datetime.strptime(date_text, '%Y-%m-%d') | |
| valid = True | |
| except ValueError: | |
| print("Invalid date, try again\n") | |
| # Get Sex | |
| sex = '' | |
| valid = False | |
| while not valid: | |
| sex = input('Sex [M/F]:') | |
| if sex in ['M', 'F']: | |
| valid = True | |
| else: | |
| print("Invalid choice, try again\n") | |
| month = date.month | |
| if sex == 'F': | |
| month += 50 | |
| year = int(date.year) | |
| if year >= year_of_change: | |
| n = 4 | |
| total_n = 10 | |
| else: | |
| n = 3 | |
| total_n = 9 | |
| random_number = random_with_n_digits(n) | |
| print('%02d' % (date.year % 100)) | |
| generated = int('%02d' % (date.year % 100) + '%02d' % month + '%02d' % date.day + str(random_number)) | |
| remain = generated % 11 | |
| if remain > 0: | |
| generated -= remain | |
| # Leading zero might be missing | |
| generated = ('%0' + str(total_n) + 'd') % generated | |
| print(str(generated)[0:6] + '/' + str(generated)[6:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment