Source

play-problems / index.py

Full commit
import os
import sys
import time
import random


def problem_01():

    l = []

    for x in range(100):

        while True:
            n = random.randint(1, 10000)

            if n not in l:
                l.append(n)
                break

    max = 0
    min = l[0]

    for x in l:
        max = x if x > max else max
        min = x if x < min else min

    return {'values': l, 'max': max, 'min': min}


def problem_02():
    tot = 0

    for x in range(1, 1000):
        if x % 3 == 0 and x % 5 == 0:
            tot += x

    return tot


def problem_03(n1, n2):

    n1 = int(n1)
    n2 = int(n2)

    if n1 < 0:
        n1 = n1 - (n1 * 2)

    if n2 < 0:
        n2 = n2 - (n2 * 2)

    old_n1 = n1

    if n2 > n1:
        n1 = n2
        n2 = old_n1

    if n1 == 0 or n2 == 0:
        mmc = n1
    elif n1 == 0 and n2 > 0:
        mmc = n2
    elif n1 > 0 and n2 == 0:
        mmc = n1
    else:

        res = 0

        while True:

            n1 -= n2

            if n1 > 0:
                res = n1

            elif n1 == 0:
                mmc = n2
                break
            else:
                n1 = n2
                n2 = res

    return mmc


def problem_04():

    placas = []

    input = open('prefijos_placas.txt', 'r')
    input = input.read().split('\n')[:-1]

    for p in range(100):
        while True:
            code = input[random.randint(0, len(input) - 1)]
            serie = random.randint(100, 999)
            placa = '%s-%d' % (code, serie)

            if placa not in placas:
                placas.append(placa)
                break

    return placas


def problem_06():

    data = {}

    words = open('alfabeto.txt', 'r')
    words = words.read().split('\n')[:-1]

    def find_words(value):

        if not value:
            return False

        for x in value:
            if not x in words:
                return False

        return True

    input = open('rfc2616.txt', 'r')
    input = input.read().split(' ')
    input = [x for x in input if find_words(x)]

    for x in input:

        if not x in data:
            data[x] = 1
        else:
            data[x] = data[x] + 1

    new_data = {}

    for x, y in data.items():
        if y >= 100:
            new_data[x] = y

    return new_data


def problem_07():

    total = 1000
    points = (50, 50, 50, 100, 100, 100, 150, 150, 150, 100)
    n1 = random.randint(1, 101)

    for x in points:

        n2 = int(raw_input('Enter number:'))

        if n1 == n2:
            print total
            break
        else:

            if n1 < n2:
                print 'el numero es menor que %d' % n2
            else:
                print 'el numero es mayor que %d' % n2

            total = total - x


def problem_08():

    input = open('matrix.txt', 'r')
    input = input.read().split('\n')[:-1]

    while True:

        body = ''

        for x in range(8):

            chr = input[random.randint(0, len(input) - 1)]
            body += '%s  ' % chr

        sys.stdout.write(os.popen('clear').read())
        print body
        time.sleep(2)


def problem_10():

    input = open('hangman.txt', 'r')
    input = input.read().split('\n')[:-1]

    n = 8
    chr = '*'

    phase = ''
    phase1 = input[random.randint(0, len(input) - 1)]
    phase_original = phase1

    #print 'phase_original: %s' % phase_original

    while 0 < n:

        n -= 1

        if phase in phase1:
            phase1 = phase1.replace(phase, chr * len(phase))

        output = ''
        for y, x in enumerate(phase1):

            if x == chr:
                output += phase_original[y]
            elif x == ' ':
                output += ' '
            else:
                output += chr

        if output == phase_original:
            print 'excelent'
            break
        else:
            if n == 0:
                print 'so bad'
                break

        print 'phase: %s' % output
        phase = raw_input('Enter phase: ')
        sys.stdout.write(os.popen('clear').read())


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()