Python Code Examples: Permutations, Polynomials, and Matrices

Permuting Words

Iterative


from math import *

def Permuta(Cad):
    n = len(Cad)
    np = factorial(n)  # Total number of permutations
    l = [''] * np
    for num in range(n - 1, -1, -1):
        repe = factorial(num)
        pos = 0
        c = 0
        for k in range(len(l)):
            while Cad[pos] in l[k]:
                pos = pos + 1
                if pos >= n:
                    pos = 0
            l[k] = l[k] + Cad[pos]
            c = c + 1
            if c == repe:
                c = 0
                pos = (pos + 1) % n
    return l

Recursive


def generar_permutaciones(cad1):
    if len(cad1) == 1:
        return [cad1]
    l = []
    for i in range(len(cad1)):
        cad2 = cad1[:i] + cad1[i + 1:]
        lsol = generar_permutaciones(cad2)
        for j in lsol:
            l.append(cad1[i] + j)
    return l

Polynomial Class


class Polinomio(object):
    def __init__(self, lista, cad):
        self.lista = lista
        self.cad = cad

    def __str__(self):
        s = ''
        for i in range(len(self.lista) - 1, -1, -1):
            if self.lista[i] > 0:
                s = s + '+' + str(self.lista[i]) + '*' + self.cad + '**' + str(i)
            elif self.lista[i] == 0:
                s = s
            else:
                s = s + str(self.lista[i]) + '*' + self.cad + '**' + str(i)
        return s

    def __add__(self, otro):
        l1 = len(self.lista)
        l2 = len(otro.lista)
        tmin = min(l1, l2)
        tmax = max(l1, l2)
        psol = Polinomio([0] * tmax, 'x')
        for i in range(tmin):
            psol.lista[i] = self.lista[i] + otro.lista[i]
        if l1 > l2:
            for i in range(tmin, tmax):
                psol.lista[i] = self.lista[i]
        else:
            for i in range(tmin, tmax):
                psol.lista[i] = otro.lista[i]
        return psol

    def __sub__(self, otro):
        l1 = len(self.lista)
        l2 = len(otro.lista)
        tmin = min(l1, l2)
        tmax = max(l1, l2)
        psol = Polinomio([0] * tmax, 'x')
        for i in range(tmin):
            psol.lista[i] = self.lista[i] - otro.lista[i]
        if l1 > l2:
            for i in range(tmin, tmax):
                psol.lista[i] = self.lista[i]
        else:
            for i in range(tmin, tmax):
                psol.lista[i] = otro.lista[i]
        return psol

    def MultPorCoef(self, n):
        psol = Polinomio(self.lista, self.cad)
        for i in range(len(self.lista)):
            psol.lista[i] = self.lista[i] * n
        return psol

    def __mul__(self, otro):
        l1 = len(self.lista)
        l2 = len(otro.lista)
        psol = otro.MultPorCoef(self.lista[-1])
        for i in range(l1 - 2, -1, -1):
            paux = otro.MultPorCoef(self.lista[i])
            psol.lista = [0] + psol.lista
            psol = psol + paux
        return psol

Matrix Class


from random import *


class Matriz(object):
    def __init__(self, nf, nc):
        self.mat = []
        for f in range(nf):
            fila = []
            for c in range(nc):
                fila.append(randint(0, 9))
            self.mat.append(fila)

    def __str__(self):
        cad = ''
        for subl in self.mat:
            cad = cad + str(subl) + '\n'
        return cad

    def __add__(self, otro):
        nf1 = len(self.mat)
        nc1 = len(self.mat[0])
        matsol = Matriz(nf1, nc1)
        for f in range(nf1):
            for c in range(nc1):
                matsol.mat[f][c] = self.mat[f][c] + otro.mat[f][c]
        return matsol

    def Traza(self):  # Sum of the diagonal
        nf = len(self.mat)
        nc = len(self.mat[0])
        if nf != nc:
            return 0
        s = 0
        for i in range(nf):
            s = s + self.mat[i][i]
        return s

    def __mul__(self, otro):
        nf1 = len(self.mat)
        nc1 = len(self.mat[0])
        nf2 = len(otro.mat)
        nc2 = len(otro.mat[0])
        if nc1 != nf2:
            return None
        mr = Matriz(nf1, nc2)
        for f in range(nf1):
            for c in range(nc2):
                s = 0
                for k in range(nc1):
                    s = s + self.mat[f][k] * otro.mat[k][c]
                mr.mat[f][c] = s
        return mr

    def Traspuesta(self):
        nf = len(self.mat)
        nc = len(self.mat[0])
        mt = Matriz(nc, nf)
        for f in range(nf):
            for c in range(nc):
                mt.mat[c][f] = self.mat[f][c]
        return mt

    def EsSimetrica(self):
        nf = len(self.mat)
        nc = len(self.mat[0])
        if nf != nc:
            return False
        for f in range(nf):
            for c in range(nc):
                if self.mat[f][c] != self.mat[c][f]:
                    return False
        return True


m1 = Matriz(2, 3)
print(m1)