Пример #1
0
 def vigenere_decode(
         self,
         ctext,
         LANG=[ascii_lowercase, ascii_uppercase],
         ALPHABET=len(ascii_lowercase)):
     count = 0
     plaintext = ""
     for i in range(len(ctext)):
         j = (i - count) % self.L
         if ctext[i].islower():
             plaintext += LANG[0][(LANG[0].index(ctext[i]) +
                            ALPHABET -
                            LANG[0].index(self.key[j])) %
                           ALPHABET]
         elif ctext[i].isupper():
             plaintext += LANG[1][(LANG[1].index(ctext[i]) +
                            ALPHABET -
                            LANG[0].index(self.key[j])) %
                           ALPHABET]
         elif ctext[i].isdigit():
             plaintext += digits[(digits.index(ctext[i]) +
                           LANG[0].index(self.key[j])) % 10]
         else:
             plaintext += ctext[i]
             count += 1
     return plaintext
Пример #2
0
 def vigenere_encode(
         self,
         plaintext,
         LANG=[ascii_lowercase, ascii_uppercase],
         ALPHABET=len(ascii_lowercase)):
     count = 0  # set count for special characters
     # for every character in plaintext, shift it by key[j] (wrap around the
     # alphabet, if key+pt[i] > ALPHABET)
     ctext = ""
     for i in range(len(plaintext)):
         j = (i - count) % self.L
         if plaintext[i].islower():
             ctext += LANG[0][(LANG[0].index(plaintext[i]) +
                               LANG[0].index(self.key[j])) %
                              ALPHABET]
         elif plaintext[i].isupper():
             ctext += LANG[1][(LANG[1].index(plaintext[i]) +
                               LANG[0].index(self.key[j])) %
                              ALPHABET]
         elif plaintext[i].isdigit():
             ctext += digits[(digits.index(plaintext[i]) +
                              LANG[0].index(self.key[j])) % 10]
         else:
             ctext += plaintext[i]
             count += 1
     return ctext
Пример #3
0
def parse_ship_location(loc):
    loc = loc.lower().strip().replace(" ", "")
    row = ascii_lowercase.index(loc[0])
    col = digits.index(loc[1])
    if loc[-1] == 'a':
        dx, dy = 1, 0
    elif loc[-1] == 'd':
        dx, dy = 0, 1
    return col, row, dx, dy
Пример #4
0
def rotFunc(prRotation, prPlainText):
    prPlainText = prPlainText.replace(' ', '')
    encryptedText = ''
    for n in prPlainText:
        if n.isalpha():
            encryptedText += ascii_lowercase[(ascii_lowercase.index(n.lower()) + prRotation) % len(ascii_lowercase)]
        elif n.isdigit():
            encryptedText += digits[(digits.index(n) + prRotation) % len(digits)]
        else:
            encryptedText += SYMBOLS[(SYMBOLS.index(n) + prRotation) % len(SYMBOLS)]
    return hashlib.sha256(bytes(encryptedText, 'UTF-8')).hexdigest()
Пример #5
0
def cc(rot, plaintext):
    plaintext = plaintext.replace(' ', '')
    encrypted = ''

    for c in plaintext:
        if c.isalpha():
            encrypted += ascii_lowercase[(ascii_lowercase.index(c.lower()) + rot) % len(ascii_lowercase)]
        elif c.isdigit():
            encrypted += digits[(digits.index(c) + rot) % len(digits)]
        else:
            encrypted += SYMBOLS[(SYMBOLS.index(c) + rot) % len(SYMBOLS)]
    return hashlib.sha256(bytes(encrypted, 'UTF-8')).hexdigest()
Пример #6
0
def restore(wm, config):
    "Build my standard work environment."

    wm.panes_list[0].maximize()

    # Disconnect all windows from panes to avoid displaying everything
    # that's about to happen. wm.panes_restore() will put things back.
    for c in wm.query_clients():
        c.panes_pane = None

    # Disconnect any remaining panes from their windows as well.
    wm.panes_list[0].window = None

    places = dict()
    for node in config:
        if node.tag == 'vertical':
            wm.panes_list[wm.panes_current].vertical_split(float(node.get('fraction')))
        elif node.tag == 'horizontal':
            wm.panes_list[wm.panes_current].horizontal_split(float(node.get('fraction')))
        elif node.tag == 'newpane':
            pane = wm.panes_list[int(node.get('pane'))]
        elif node.tag == 'goto':
            wm.panes_goto(int(node.get('pane')))
        elif node.tag == 'number':
            wm.panes_number(int(node.get('new')))
        elif node.tag == 'placewindow':
            places[node.get('name')] = int(node.get('pane'))
        else:
            raise ValueError, 'Unrecognized pane config element %s' % node

    # Now fix any windows wired by title
    panecount = len(wm.panes_list)
    for s in wm.screens:
        for c in s.query_clients(cfilter.true, 1):
            title = c.get_title()
            if title[-2] == '@' and title[-1] in digits:
                pane = digits.index(title[-1])
                if pane < panecount:
                    wm.panes_list[pane].add_window(c)
                    continue
            else:
                for name in places:
                    if name in title:
                        if places[name] < panecount:
                            wm.panes_list[places[name]].add_window(c)
                            break

    # And now make the world sane
    wm.panes_goto(0)
    wm.panes_restore()
Пример #7
0
 def caesar_encode(self,
                   plaintext,
                   LANG=[ascii_lowercase, ascii_uppercase],
                   ALPHABET=len(ascii_lowercase)):
     # for every character in plaintext, shift it by shiftsize k (wrap
     # around the alphabet, is k > ALPHABET)
     ctext = ""
     for c in plaintext:
         if c.islower():
             ctext += LANG[0][(LANG[0].index(c) + self.key) % ALPHABET]
         elif c.isupper():
             ctext += LANG[1][(LANG[1].index(c) + self.key) % ALPHABET]
         elif c.isdigit():
             ctext += digits[(digits.index(c) + self.key) % 10]
         else:
             ctext += c
     return ctext
Пример #8
0
 def caesar_decode(self,
                   ctext,
                   LANG=[ascii_lowercase, ascii_uppercase],
                   ALPHABET=len(ascii_lowercase)):
     plaintext = ""
     for c in ctext:
         if c.islower():
             plaintext += LANG[0][(LANG[0].index(c) + ALPHABET - self.key) %
                                  ALPHABET]
         elif c.isupper():
             plaintext += LANG[1][(LANG[1].index(c) + ALPHABET - self.key) %
                                  ALPHABET]
         elif c.isdigit():
             plaintext += digits[(digits.index(c) - self.key) % 10]
         else:
             plaintext += c
     return plaintext
Пример #9
0
def parse(nums: str) -> Collection[Tuple[int, int]]:
    """
    Input: a sequence of numbers
    Output: A collection of tuples (x,n) | x was pressed n times, n > 0 & len(xs) <= len(nums)
    eg, fn '66688777' -> [(6,3), (8,2), (7,3)]
    """
    prev = None
    xs = []
    for num in nums:
        cur = digits.index(num)
        if cur == prev:
            _, count = xs.pop()
            xs.append((cur, count + 1))
        else:
            xs.append((cur, 1))
        prev = cur
    return xs
def string_to_int(s: str) -> int:
    # TODO - you fill in here.
    """
    res = 0
    negative = False
    for i, ch in enumerate(s):
        if i == 0 and (ch == '-' or ch == '+'):
            negative = ch == '-'
        else:
            res = res * 10 + ord(ch) - ord('0')

    if negative:
        res = -res

    return res
    """
    x = (-1 if s[0] == '-' else 1) * reduce(
        lambda running_sum, c: running_sum * 10 + digits.index(c),
        s[s[0] in '+-':], 0)
    return x
Пример #11
0
def string_to_int(s):
    return reduce(lambda running_sum, c: running_sum * 10 + digits.index(c),
                  s[s[0] == '-':], 0) * (-1 if s[0] == '-' else 1)
Пример #12
0
def parse_bomb_location(loc):
    loc = loc.lower().strip().replace(" ", "")
    row = ascii_lowercase.index(loc[0])
    col = digits.index(loc[1])
    return col, row
def caesar_crypt(text_in, task_select="", shift=None):
    """ Text encrypter/decrypter

    Args:
        text_in: Text being encrypter/decrypter
        task_select: Selecting encrypting or decrypting
        shift: Number of shifts in the symbol line.

    Returns: Encrypter/decrypter text

    """
    # Selecting encrypter or decrypter.
    while True:
        if task_select.lower() == "e" or task_select.lower() == "d":
            task = task_select
            break
        elif task_select == "":
            task = input("Press E for encode, or D for decode: ")
            if task.lower() == "e" or task.lower() == "d":
                break
            else:
                print("\nInvalid input! Please try again!\n")

    # Selecting number of shifts.
    while True:
        try:
            if shift is None:
                shift = int(
                    input("Please enter your shift key as an whole number: "))
            break
        except ValueError:
            print("\nInvalid input! Please try again!\n")

    if task.lower() == "e":
        pass
    elif task.lower() == "d":
        shift = -shift

    text_out_temp = []
    text_out = []
    line_text_out = ""
    crypt_value = []
    # Adding space to the symbols in punctuation
    special = " " + punctuation

    # Shifts each letter in each word according to Shift
    for line in range(len(text_in)):
        for symbol in text_in[line]:
            if symbol in ascii_uppercase:
                index = ascii_uppercase.index(symbol)
                processing = (index + shift) % 26
                crypt_value.append(processing)
                new_value = ascii_uppercase[processing]
                text_out_temp.append(new_value)

            elif symbol in ascii_lowercase:
                index = ascii_lowercase.index(symbol)
                processing = (index + shift) % 26
                crypt_value.append(processing)
                new_value = ascii_lowercase[processing]
                text_out_temp.append(new_value)

            elif symbol in digits:
                index = digits.index(symbol)
                processing = (index + shift) % 10
                crypt_value.append(processing)
                new_value = digits[processing]
                text_out_temp.append(new_value)

            elif symbol in special:
                index = special.index(symbol)
                processing = (index + shift) % 33
                crypt_value.append(processing)
                new_value = special[processing]
                text_out_temp.append(new_value)

            # Converts the list to a string.
            line_text_out = "".join(text_out_temp)

        text_out_temp.clear()
        text_out.append(line_text_out)

    # Converts the list to a string.
    text_out = "".join(text_out)
    return text_out