コード例 #1
0
ファイル: factorize.py プロジェクト: cowboysmall/crypto
def main(argv):
    lines      = files.read_lines(argv[0])
    N_1        = mpz(lines[0])
    N_2        = mpz(lines[1])
    N_3        = mpz(lines[2])

    lines      = files.read_lines(argv[1])
    C          = mpz(lines[0])
    E          = int(lines[1])

    p1, q1, i1 = number.factorize(N_1, 1)
    p2, q2, i2 = number.factorize(N_2, 1048576)
    p3, q3, i3 = number.factorize(mul(N_3, 24), 1)

    print
    print 'Factorization Demo'
    print
    print '       1st:'
    print 'Iterations: ', i1
    print '         p: ', p1
    print '         q: ', q1
    print
    print '       2nd:'
    print 'Iterations: ', i2
    print '         p: ', p2
    print '         q: ', q2
    print
    print '       3rd:'
    print 'Iterations: ', i3
    print '         p: ', div(p3, 6)
    print '         q: ', div(q3, 4)
    print
    print
    print ' Plaintext: ', encdec.rsa_decrypt(C, E, p1, q1, N_1)
    print
コード例 #2
0
def main(argv):
    lines = files.read_lines(argv[0])
    N     = int(lines[0])
    S     = lines[1]
    K     = int(lines[2])

    print(caesar_cipher(S, K))
コード例 #3
0
def main(argv):
    lines   = files.read_lines(argv[0])
    N, M, K = [int(i) for i in lines[0].split()]
    B       = lines[1:]
    C       = coin_on_the_table(N, M, K, B)

    print(-1 if C == 999999 else C)
コード例 #4
0
def main(argv):
    lines = files.read_lines(argv[0])
    T     = int(lines[0])

    for i in range(T):
        A = lines[(2 * i) + 1]
        B = lines[(2 * i) + 2]
        print(merge(A, B))
コード例 #5
0
ファイル: two_strings.py プロジェクト: cowboysmall/hackerrank
def main(argv):
    lines = files.read_lines(argv[0])
    T     = int(lines[0])

    for i in range(T):
        A = lines[(2 * i) + 1]
        B = lines[(2 * i) + 2]
        if substring(A, B):
            print('YES')
        else:
            print('NO')
コード例 #6
0
ファイル: discrete_log.py プロジェクト: cowboysmall/crypto
def main(argv):
    P, G, H    = [int(i) for i in files.read_lines(argv[0])]

    # calculated = number.discrete_log_numbthy(P, G, H)
    calculated = number.discrete_log(P, G, H)


    print
    print "Discrete Log Demo"
    print 
    print 'Successfully found x with a value of %s' % calculated
    print
コード例 #7
0
ファイル: decrypt.py プロジェクト: cowboysmall/crypto
def main(argv):
    lines = [line.decode('hex') for line in files.read_lines(argv[0])]


    print
    print 'AES Demo'
    print
    print encdec.aes_decrypt(lines[1], lines[0])
    print encdec.aes_decrypt(lines[2], lines[0])
    print
    print encdec.aes_decrypt(lines[4], lines[3], True)
    print encdec.aes_decrypt(lines[5], lines[3], True)
    print
コード例 #8
0
def main(argv):
    lines = files.read_lines(argv[0])

    T     = int(lines[0])
    C     = 1

    for _ in range(T):
        N  = int(lines[C])
        C += 1

        G  = [[ord(c) for c in line] for line in lines[C:C + N]]
        C += N

        print(grid_challenge(N, G))
コード例 #9
0
def main(argv):
    lines = files.read_lines(argv[0])
    T     = int(lines[0])

    count = 1
    for _ in range(T):
        R, C   = [int(i) for i in lines[count].split()]
        count += 1
        G      = lines[count:count + R]
        count += R
        r, c   = [int(i) for i in lines[count].split()]
        count += 1
        P      = lines[count:count + r]
        count += r
        print('YES' if grid_search(R, C, G, r, c, P) else 'NO')
コード例 #10
0
def test_various_cards():
  effects = [''] + files.read_lines('input/effects.txt')
  NUM_CARDS = 53
  SELECTED_CARDS = [
    # Simple tests
    Acquirable("Apprentice", "0R", 0, "Hero", compound([
      fetch_simple_effect(effects, 3, 1, False)
    ])),
    Acquirable("Hedron Link Device", "7R", 7, "Mechana Construct", compound([
      fetch_simple_effect(effects, 21, None, False)
    ])),

    # Test multiple effects
    Acquirable("Arbiter of the Precipice", "4R", 1, "Void Hero", compound([
      fetch_simple_effect(effects, 2, 2, False),
      fetch_simple_effect(effects, 6, 1, False)
    ])),

    # Test optional effects
    Acquirable("Seer of the Forked Path", "2R", 1, "Enlightened Hero", compound([
      fetch_simple_effect(effects, 2, 1, False),
      fetch_simple_effect(effects, 7, 1, True)
    ])),

    # Test compound effects (and construct)
    Acquirable("Yggdrasil Staff", "4R", 2, "Lifebound Construct", compound([
      fetch_simple_effect(effects, 5, 1, False),
      CompoundEffect("AND", [
        fetch_simple_effect(effects, 3, -4, False),
        fetch_simple_effect(effects, 4, 3, False)
      ], True)
    ])),

    # Test defeatable (and with comma in name)
    Defeatable("Xeron, Duke of Lies", "6P", "Monster", compound([
      fetch_simple_effect(effects, 4, 3, False),
      fetch_simple_effect(effects, 22, None, False)
    ]))
  ]

  card_dictionary = get_dict()
  assert len(card_dictionary.cards) == NUM_CARDS

  for card in SELECTED_CARDS:
    check_card_correct(card_dictionary, card)
コード例 #11
0
def test_effects_not_reodered():
  EXPECTED_EFFECTS = [
    "Discard %d cards",
    "Draw %d cards",
    "Gain %d runes",
    "Gain %d honor",
    "Gain %d power",
    "Banish %d card in hand",
    "Banish %d card in center",
    "Banish %d card from discard pile",
    "Defeat monster of %d power or less without cost",
    "Pay %d less runes when acquiring a mechana construct this turn",
    "Acquire a hero with cost %d or less without cost. Place it on top of your deck",
    "First time you play a lifebound hero, gain %d honor",
    "Draw %d card when putting a Mechana Construct into play",
    "Banish this card to take an additional turn",
    "Pay %d less runes when acquiring a construct this turn",
    "First time you defeat a monster, gain %d honor",
    "Draw a card if you control 2 or more constructs",
    "Gain %d honor for each faction among Constructs you control",
    "Each opponent must destroy a construct",
    "Once per turn, when you acquire another Mechana Construct, put it directly into play",
    "Treat all constructs as mechana constructs",
    "Take a card at random from each opponent's hand and add that to your hand",
    "If you have played another lifebound hero, gain %d power",
    "Each opponent destroys all but one construct",
    "Gain %d power for each Mechana Construct in play that you control",
    "Copy the effect of a Hero",
    "Unbanishable. Acquire or defeat any card in the center without paying its cost."
  ]

  effects = files.read_lines('input/effects.txt')
  fail_message = """It looks like effects.txt has been changed.
If it was just a minor change (rewording a description), just change it in this test.
Be careful about reordering effects, though, because we reference effects by indices
so you'll probably break a lot of things."""

  assert effects == EXPECTED_EFFECTS, fail_message
コード例 #12
0
ファイル: decrypt.py プロジェクト: cowboysmall/crypto
def main(argv):
    cypher_texts = [common.hex_to_ascii(cypher_text) for cypher_text in files.read_lines(argv[0])]
    key          = encdec.many_time_pad_crack(cypher_texts, len(cypher_texts[10]))

    plain_text   = common.string_to_ascii('The secret message is: When using a stream cipher, never use the key more than once')
    new_key      = common.xor(plain_text, cypher_texts[10])


    print
    print 'Decrypt Many-time Pad Demo'
    print

    for i in xrange(len(cypher_texts)):
        print 'Message %2d = %s' % ((i + 1), common.ascii_to_string(common.xor(cypher_texts[i], key)))

    print
    print 'Make best guess and use to derive key'
    print

    for i in xrange(len(cypher_texts)):
        print 'Message %2d = %s' % ((i + 1), common.ascii_to_string(common.xor(cypher_texts[i], new_key)))

    print
コード例 #13
0
def main(argv):
    lines = files.read_lines(argv[0])
    T     = int(lines[0])

    for line in lines[1:]:
        print(deletions(line))
コード例 #14
0
def main(argv):
    lines = files.read_lines(argv[0])
    T     = int(lines[0])

    for line in lines[1:]:
        print(square_subsequences(line))
コード例 #15
0
def main(argv):
    lines = files.read_lines(argv[0])

    print(deletions(*lines))
コード例 #16
0
ファイル: xor_and_sum.py プロジェクト: cowboysmall/hackerrank
def main(argv):
    lines = files.read_lines(argv[0])
    a     = lines[0]
    b     = lines[1]

    print(xor_and_sum(a, b))
コード例 #17
0
def main(argv):
    lines = files.read_lines(argv[0])
    n     = int(lines[0])
    M     = lines[1:]

    print('\n'.join(cavity_map(n, M)))
コード例 #18
0
def main(argv):
    lines = files.read_lines(argv[0])
    T     = int(lines[0])

    for line in lines[1:]:
        print(anagramic_pairs(line))
コード例 #19
0
ファイル: mr_k_marsh.py プロジェクト: cowboysmall/hackerrank
def main(argv):
    lines = files.read_lines(argv[0])
    M, N  = [int(i) for i in lines[0].split()]
    A     = lines[1:]
    R     = perimeter(M, N, A)
    print(R if R > 0 else 'impossible')
コード例 #20
0
def main(argv):
    lines  = files.read_lines(argv[0])
    topics = [int(val, 2) for val in lines[1:]]

    print('\n'.join(str(value) for value in max_topics(topics)))
コード例 #21
0
ファイル: anagram.py プロジェクト: cowboysmall/hackerrank
def main(argv):
    lines = files.read_lines(argv[0])
    T     = int(lines[0])

    for line in lines[1:]:
        print(changes(line))
コード例 #22
0
def main(argv):
    lines = files.read_lines(argv[0])
    taxa = lines[0].split()
    trees = lines[1:]

    print quartet_distance(*trees)
コード例 #23
0
ファイル: cavity_map.py プロジェクト: cowboysmall/hackerrank
def main(argv):
    lines = files.read_lines(argv[0])
    n     = int(lines[0])
    M     = lines[1:]

    print('\n'.join(cavity_map(n, M)))
コード例 #24
0
def main(argv):
    lines = files.read_lines(argv[0])
    T     = int(lines[0])

    for line in lines[1:]:
        print(greater_than(line))
コード例 #25
0
def main(argv):
    lines = files.read_lines(argv[0])
    T = int(lines[0])

    for line in lines[1:]:
        print(anagramic_pairs(line))
コード例 #26
0
def main(argv):
    lines = files.read_lines(argv[0])
    N = int(lines[0])
    R = [parse_record(line.split(',')) for line in lines[1:]]

    print(','.join(str(f) for f in fraud_prevention(R)))
コード例 #27
0
ファイル: gem_stones.py プロジェクト: cowboysmall/hackerrank
def main(argv):
    lines = files.read_lines(argv[0])
    T     = int(lines[0])

    print(len(gem_stones(lines[1:])))
コード例 #28
0
ファイル: love_letter.py プロジェクト: cowboysmall/hackerrank
def main(argv):
    alpha = [c for c in 'abcdefghijklmnopqrstuvwxyz']
    lines = files.read_lines(argv[0])

    for line in lines[1:]:
        print(palindrome(alpha, line))
コード例 #29
0
def main(argv):
    lines = files.read_lines(argv[0])
    N     = int(lines[0])
    R     = [parse_record(line.split(',')) for line in lines[1:]]

    print(','.join(str(f) for f in fraud_prevention(R)))
コード例 #30
0
def main(argv):
    lines = files.read_lines(argv[0])
    edges = graphs.edges_from_adjacency_list(lines)
    nodes = graphs.nodes_from_edges(edges)

    print ', '.join(graphs.topological_sort(nodes, edges))