+1 (315) 557-6473 

Work With String Manipulation Questions in The Python Assignment Solution.


Instructions

Objective
Write a python assignment program to work with string manipulation questions.

Requirements and Specifications

Program to work with string manipulation questions in python
Program to work with string manipulation questions in python 1
Program to work with string manipulation questions in python 2
Program to work with string manipulation questions in python 3
Program to work with string manipulation questions in python 4

Source Code

QUESTION 1

def up_down_strings(los):

    alpha = 'abcdefghijklomnpqrstuvwxyz'

    for j in range(len(los)-1, -1, -1):

        s = los[j]

        prev = alpha.index(s[0]) - alpha.index(s[1])

        is_ok = True

        for i in range(1, len(s)-1):

            curr = alpha.index(s[i]) - alpha.index(s[i+1])

            if prev * curr >= 0:

                is_ok = False

                break

            prev = curr

        if not is_ok:

            del los[j]

if __name__ == '__main__':

    test = ['yygrees', 'itsy', 'garbs', 'beets', 'abc', 'sole']

    up_down_strings(test)

    print(test)

QUESTION 2

def othello(board, turn, row, col):

    if board[row][col] != '':

        return False

    rows = len(board)

    cols = len(board[0])

    # down

    step = 0

    while True:

        step += 1

        if row + step >= rows:

            break

        if board[row+step][col] == '':

            break

        if board[row+step][col] == turn:

            if step >= 2:

                return True

            else:

                break

    # up

    step = 0

    while True:

        step += 1

        if row - step < 0:

            break

        if board[row - step][col] == '':

            break

        if board[row - step][col] == turn:

            if step >= 2:

                return True

            else:

                break

    # right

    step = 0

    while True:

        step += 1

        if col + step >= rows:

            break

        if board[row][col+step] == '':

            break

        if board[row][col+step] == turn:

            if step >= 2:

                return True

            else:

                break

    # left

    step = 0

    while True:

        step += 1

        if col - step < 0:

            break

        if board[row][col - step] == '':

            break

        if board[row][col - step] == turn:

            if step >= 2:

                return True

            else:

                break

    # right-down

    step = 0

    while True:

        step += 1

        if row + step >= rows:

            break

        if col + step >= cols:

            break

        if board[row+step][col+step] == '':

            break

        if board[row+step][col+step] == turn:

            if step >= 2:

                return True

            else:

                break

    # left-down

    step = 0

    while True:

        step += 1

        if row + step >= rows:

            break

        if col - step < 0:

            break

        if board[row + step][col-step] == '':

            break

        if board[row + step][col-step] == turn:

            if step >= 2:

                return True

            else:

                break

    # right-up

    step = 0

    while True:

        step += 1

        if row - step < 0:

            break

        if col + step >= cols:

            break

        if board[row-step][col+step] == '':

            break

        if board[row-step][col+step] == turn:

            if step >= 2:

                return True

            else:

                break

    # left-up

    step = 0

    while True:

        step += 1

        if row - step < 0:

            break

        if col - step < 0:

            break

        if board[row - step][col-step] == '':

            break

        if board[row - step][col-step] == turn:

            if step >= 2:

                return True

            else:

                break

    return False

if __name__ == '__main__':

    board = [[ '', '', '', '', '', '', '', ''],

             [ '', '', '', '', '', '', '', ''],

             [ '', '', 'B', 'B', 'B', '', '', ''],

             [ '', 'W', 'B', 'W', 'W', '', 'B', ''],

             [ '', 'W', 'B', 'W', 'W', 'W', 'W', ''],

             [ '', '', 'W', 'W', 'W', '', '', ''],

             [ '', '', '', 'W', '', '', '', ''],

             [ '', '', '', '', '', '', '', '']]

    print(othello(board, 'B', 3, 0))

    print(othello(board, 'W', 3, 0))

    board2 = [['B', 'W', 'B', 'B', 'B', 'W', 'B', 'B'],

             ['W', 'B', 'B', 'W', 'W', 'B', 'B', 'B'],

             ['B', 'B', 'W', 'W', 'B', 'B', 'B', 'B'],

             ['B', 'W', 'W', 'W', 'W', 'W', 'W', 'W'],

             ['W', 'W', 'W', 'W', 'W', 'W', 'B', 'B'],

             ['B', 'B', 'W', 'W', 'W', 'B', 'B', 'W'],

             ['B', 'B', 'W', 'B', 'B', 'B', 'W', 'W'],

             ['B', 'B', 'W', 'B', 'B', 'B', 'W', 'W']]

    print(othello(board2, 'W', 1, 2))

QUESTION 3

def multiply_polynomials(poly1, poly2):

    m = max(len(poly1), len(poly2))

    n = len(poly1) + len(poly2) - 1

    result = []

    for i in range(n):

        result.append(0)

    for i in range(len(poly1)):

        for j in range(len(poly2)):

            result[i+j] += poly1[i] * poly2[j]

    return result

if __name__ == '__main__':

    P1 = [151493]

    P2 = [215578]

    print(multiply_polynomials(P1, P2))

    P1 = [1, 2, 3]

    P2 = [4 ,5]

    print(multiply_polynomials(P1, P2))