Commits

ronald martinez  committed c07dd65

problema 09

  • Participants
  • Parent commits 207d129

Comments (0)

Files changed (2)

         phase = raw_input('Enter phase: ')
         sys.stdout.write(os.popen('clear').read())
 
-problem_10()
+
+def problem_09():
+
+    table = {
+        13: (
+                {'prefix': '4', 'type': 'visa', 'check': True},
+            ),
+        14: (
+                {'prefix': '300-305', 'type': 'dinners/carte', 'check': True},
+                {'prefix': '36', 'type': 'dinners/carte', 'check': True},
+                {'prefix': '38', 'type': 'dinners/carte', 'check': True},
+            ),
+        15: (
+                {'prefix': '34', 'type': 'amex', 'check': True},
+                {'prefix': '37', 'type': 'amex', 'check': True},
+                {'prefix': '2014', 'type': 'enroute', 'check': False},
+                {'prefix': '2149', 'type': 'enroute', 'check': False},
+                {'prefix': '2131', 'type': 'jcb', 'check': True},
+                {'prefix': '1800', 'type': 'jcb', 'check': True},
+            ),
+        16:  (
+                {'prefix': '51-55', 'type': 'mastercard', 'check': True},
+                {'prefix': '4', 'type': 'visa', 'check': True},
+                {'prefix': '6011', 'type': 'discover', 'check': True},
+                {'prefix': '3', 'type': 'jcb', 'check': True},
+            ),
+    }
+
+    def check_table(number):
+
+        data = table.get(len(number))
+
+        if data:
+
+            for x in data:
+
+                prefix = x.get('prefix')
+                type = x.get('type')
+                check = x.get('check')
+
+                if '-' in prefix:
+
+                    prefixs = prefix.split('-')
+                    prefixs = [str(x) for x in \
+                        range(int(prefixs[0]), int(prefixs[1]) + 1)]
+
+                    for p in prefixs:
+
+                        size = len(p)
+
+                        if number[:size] == p:
+
+                            if check:
+
+                                if check_primary_account(number[size:]):
+                                    return type
+                else:
+
+                    size = len(prefix)
+
+                    if number[:size] == prefix:
+
+                        if check:
+
+                            if check_primary_account(number[size:]):
+                                return type
+
+        return None
+
+    def check_primary_account(number):
+
+        n = len(number) - 1
+        i = 0
+        tot = 0
+        flat = False
+
+        while 0 <= n:
+
+            digit = int(number[n])
+
+            # if i % 2 == 1
+            if flat:
+                flat = False
+                tot += sum([int(x) for x in str(digit * 2)])
+            else:
+                flat = True
+                tot += digit
+
+            n -= 1
+            i += 1
+
+        return True if tot % 10 == 0 else False
+
+    input = open('tarjetas.txt', 'r')
+    input = input.read().split('\n')[:-1]
+
+    #number = '30249927398716' dinners
+    #print check_table(number)
+
+    for number in input:
+
+        type = check_table(number)
+
+        if type:
+            print 'card number %s: %s is valid' % (type, number)
+        else:
+            print 'card number: %s is not valid' % number
+
+problem_09()

File tarjetas.txt

+304354204103566
+30049927398716
+201487596034966
+2149100351389764
+376146290058124
+30117955511813
+1800145141533093
+345181774733872
+601150497047052
+555278061426715
+4526660065079838
+2131941557191171
+52766401438034402
+2149210210009059
+4486705580049071
+30402812855578
+201443169151496
+30109162169416
+46093552887094512
+454256271759
+55381661991924103
+6011955364744787
+2131487046897280
+3042896152468
+6011744389278353
+54656989325229297
+30264548935215
+213161217628386
+386351965746853
+54988043095977453
+213168961164832
+3056801325647
+2131732499487162
+30546644379381
+1800879094700475
+304034308705600
+5494287030866525
+349369711473073
+180041099356038
+20141736732866
+30376256497690
+180032449565039
+5444424368936695
+30077862002890
+30142437599889
+214981812559102
+304580528563236
+2149463236927028
+601183195778189
+2149363022852483
+5291006928019639
+301925745065064
+30245981651618
+4517667623985398
+60114827907889623
+34885999245471
+4419009304432092
+5245572429187217
+213106683507451
+30434792623553
+30301106416994
+214992138711953
+4526332048727
+54413898900031449
+3771117907770945
+4257518610649
+213169835234747
+34415972259551
+3059073543539
+1800697313786923
+377376041771816
+55401802184567952
+2014008805756652
+213153981854384
+5423584317561104
+522884861213275
+52574196540618041
+305193237974543
+3035365037608
+43501498727652
+492492613691317
+6011050206136081
+30146899664251
+180068290427146
+3042923044475
+213197614921431
+3039290338817
+513543978160736
+304211150955946
+3406260061431517
+5218896036612441
+45029936886729
+3760974084391990
+5151002480130203
+40379119927238201
+30331211819411
+469162008478
+4193689500553276
+53749847879880849
+21317467303189
+53657752672049931