Exemple #1
0
        except KeyError:
            level[len(implied)] = [implied]

    full = []
    for l, impl in sorted(level.items()):
        if l == 1:
            for ii in impl:
                full = merge(full, ii[0])
        else:
            for ii in impl:
                full = mergemult(full, ii)

    assert len(full) <= N, 'too big %s' % full

    return full + (N - len(full)) * [2]


@parsers.iter_parser
def parse(nxt):
    inxt = lambda: [int(x) for x in nxt().split()]
    r, n, m, k = inxt()
    nums = []
    for _ in xrange(r):
        nums += [inxt()]

    return n, m, k, nums


if __name__ == "__main__":
    CodeJam(parse, solve).main()
Exemple #2
0
                c = 'P'
                a = 'R'
            else:
                return 'IMPOSSIBLE'

    def expand(a, r):
        if r == 0:
            return a
        x = expand(a, r - 1)
        y = expand(beats[a], r - 1)
        return ''.join(sorted((x, y)))

    return expand(a, n)
    #field = a
    #for round in range(n):
    #    newfield = ''
    #    for ct in field:
    #        newfield += ''.join(sorted(ct + beats[ct]))
    #    field = newfield
    #return field


cj = CodeJam(docase)

# After importing cj into an interactive terminal, I test the code by
# running:
# >>> cj.processtext("""examples""")
#
# Then after downloading the problem set, I solve it with:
# >>> cj.processfile('filename')
Exemple #3
0
from math import log


def testcase(f):
    (N, K) = (int(i) for i in f.readline().split())
    rounds = int(log(K, 2))
    thisround = 2**rounds
    stallsleft = N - (thisround - 1)
    mindist = int(stallsleft / thisround)
    maxdist = mindist + 1
    nummax = stallsleft - (mindist * thisround)
    nummin = thisround - nummax
    if K - (thisround - 1) > nummax:
        span = mindist
    else:
        span = maxdist
    if span % 2:
        return '{0} {0}'.format((span - 1) // 2)
    else:
        return '{} {}'.format((span - 1) // 2 + 1, (span - 1) // 2)


cj = CodeJam(testcase)

# After importing cj into an interactive terminal, I test the code by
# running:
# >>> cj.processtext("""examples""")
#
# Then after downloading the problem set, I solve it with:
# >>> cj.processfile('filename')
Exemple #4
0
import itertools
import decimal
import math
from codejam import CodeJam

cj = CodeJam(debug=False)

three = decimal.Decimal(3)
five = decimal.Decimal(5).sqrt()
for case in cj.cases:
    n = cj.get_int()
    total = (three + five)**n
    t = total.to_eng_string()
    entera = t.split('.', 1)[0]
    print entera, t
    cj.write_case(entera[-3:].zfill(3))
Exemple #5
0
def jamcoins(f):
    (length, num) = (int(_) for _ in f.readline().split())
    n = 0
    coins = []
    while len(coins) < num:
        factors = []
        s = '1{n:0{width}b}1'.format(n=n, width=length - 2)
        for base in range(2, 11):
            factors.append(findfactor(int(s, base)))
            if factors[-1] is None:
                break
        else:
            coins.append((s, factors))
        n += 1
    out = ''
    for coin in coins:
        out += '\n' + coin[0] + ' '
        out += ' '.join(str(_) for _ in coin[1])
    return out


cj = CodeJam(jamcoins)

# After importing cj into an interactive Python shell, I test the code by
# running:
# >>> cj.processtext("""examples""")
#
# Then after downloading the problem set, I solve it with:
# >>> cj.processfile('filename')
Exemple #6
0

def process_case(line):

    recycled = set()
    A_s, B_s = line.split(' ')
    A = int(A_s)
    B = int(B_s)

    length = len(str(A))
    range_len = range(1, length)

    for n in range(A, B):
        n_l = list(str(n))
        for i in range_len:
            if n_l[-i] > B_s[0] or \
                        n_l[-i] < A_s[0] or n_l[-i] == '0':
                continue
            #m_l = n_l[-i:] + n_l[:-i]
            #m_s = ''.join(m_l)
            #m = int(m_s)
            m = int(''.join(n_l[-i:] + n_l[:-i]))
            if A <= n < m <= B:
                recycled.add((n, m))
    return len(recycled)


if __name__ == '__main__':
    codejam = CodeJam()
    codejam(process_case)
Exemple #7
0
'''
from codejam import CodeJam, parsers
from codejam.helpers import memoize
from math import ceil


def solve(params, ns):
    E, r, n = params
    maxn = max(ns)

    @memoize
    def best(e, *rest):
        if len(rest) == 0:
            return 0
        else:
            t, rest = rest[0], rest[1:]
            if t == maxn:
                return e * t + best(r, *rest)

            all = [
                x * t + best(min(e - x + r, E), *rest)
                for x in reversed(xrange(e + 1))
            ]
            return max(all)

    return best(E, *ns)


if __name__ == "__main__":
    CodeJam(parsers.ints, solve).main()
Exemple #8
0
#!/usr/bin/python3

from codejam import CodeJam


def sheep(f):
    last = n = int(f.readline())
    if n == 0:
        return 'INSOMNIA'
    digs = set(str(n))
    while len(digs) < 10:
        last += n
        digs |= set(str(last))
    return last


cj = CodeJam(2016, 'qual', 'A', sheep)

# After importing cj into an interactive terminal, I test the code by
# running:
# >>> cj.processtext("""examples""")
#
# Then after downloading the problem set, I solve it with:
# >>> cj.processfile('filename')
Exemple #9
0
#!/usr/bin/python3

from codejam import CodeJam


def pancakes(f):
    stack = [True if p == '+' else False for p in f.readline().strip()]
    flips = int(not stack[-1])
    ptr = 0
    parity = stack[0]
    while ptr < len(stack):
        try:
            ptr += stack[ptr:].index(not parity)
        except:
            return flips
        flips += 1
        parity = not parity


cj = CodeJam(2016, 'qual', 'B', pancakes)
Exemple #10
0
                    'rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd '
                    'de kr kd eoya kw aej tysr re ujdr lkgc jv')

sample_decrypted = ('our language is impossible to understand '
                    'there are twenty six factorial possibilities '
                    'so it is okay if you want to just give up')


def load_dict():
    for i, enc_letter in enumerate(sample_encrypted):
        dict_[enc_letter] = sample_decrypted[i]


def translate(text):
    translated = []
    for enc_letter in text:
        translated.append(dict_.get(enc_letter))

    return ''.join(translated)


def test_dict():
    assert translate(sample_encrypted) == sample_decrypted


if __name__ == '__main__':
    load_dict()
    #test_dict()
    codejam = CodeJam('A-small-attempt0.in')
    codejam(translate)
Exemple #11
0
    newStrNum = ''
    for i, char in enumerate(strNum):
        if i >= untidyAt:
            newStrNum += '0'
        else:
            newStrNum += strNum[i]

    num = int(newStrNum)
    num -= 1
    if isTidy(num)[0]:
        return num
    else:
        return tidyNumber(num)


assert tidyNumber(11243799) == 11239999
assert tidyNumber(111) == 111
assert tidyNumber(9876) == 8999
assert tidyNumber(1234) == 1234
assert tidyNumber(1023) == 999
assert tidyNumber(111111111111111110) == 99999999999999999


def solveTidy(xs):
    return tidyNumber(xs[0])


if __name__ == "__main__":
    from codejam import CodeJam, parsers
    CodeJam(parsers.ints, solveTidy).main()