def test_Mult(self): self.assertEqual(mult(0, 0), 0) self.assertEqual(mult(0, 1), 0) self.assertEqual(mult(1, 0), 0) self.assertEqual(mult(10, 5), 50) self.assertEqual(mult(10, 10), 100) self.assertEqual(mult(10, -5), -50) self.assertEqual(mult(-10, 5), -50) self.assertEqual(mult(-10, -10), 100) self.assertEqual(mult(100, 1000), 100000) self.assertEqual(mult(7, 2), 14)
def main(): # Control de argumentos de línea de comandos: if len(sys.argv) != 2: print("Uso: {} scale".format(sys.argv[0])) sys.exit(0) # Escala N: try: N = float(sys.argv[1]) if not (-5.0 <= N <= 5.0): raise ValueError() except: print("N must be a float value between -5.0 and +5.0") sys.exit(-1) # Generamos una serie aleatoria creciente, a partir de la suma acumulativa números aleatorios # entre 0 y 1: SIZE = 50 # Tamaño del array. inp_arr = np.cumsum(np.random.rand(SIZE)) out_arr = np.zeros_like(inp_arr) # Llamada a la función externa a través de su wrapper, con la correspondiente toma de tiempo: t0 = time.time_ns() out_arr = mult(inp_arr, N) t_exec = (time.time_ns() - t0) / 1.0e9 print( "La función mult ha tardado {} segundos en ejecutarse.".format(t_exec)) # Mostramos gráficas (de líneas y de barras) y terminamos: plot_values(inp_arr, out_arr) show_plot_and_wait_for_key() plot_values(inp_arr, out_arr, line_else_bars=False) show_plot_and_wait_for_key()
def test1(self): n = 3 a = np.zeros((n, n), dtype=int) b = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] c = mult(a, b) == np.dot(a, b) self.assertTrue(c.all())
def multiplyMat(mat1, mat2): resultMat = [[], [], [], []] for i, row in enumerate(resultMat): for j in range(4): multMatCol1 = mat1[i] multMatCol2 = [item[j] for item in mat2] #print 'cols:', multMatCol1, multMatCol2 result = 0 for a, b in zip(multMatCol1, multMatCol2): x = int(mult(int(a, 2), int(b, 2)), 2) result = result ^ x #print 'x: ', x #print 'result:', result row.append(intToStr(result)) print resultMat return resultMat
def main(): while True: try: var_a = input('Введите первое число: ') if var_a == "exit": print('Выход') exit() else: var_a = float(var_a) pass var_c = input("Введите знак операции (+,-,*,/): ") if var_c in ('+', '-', '*', '/'): pass else: print("Неправильный знак. Попробуйте еще раз...") continue var_b = float(input('Введите второе число: ')) except (TypeError, ValueError): print("Неправильный ввод. Попробуйте еще раз...") continue break if var_c == "/": count = div.div(var_a, var_b) elif var_c == "*": count = mult.mult(var_a, var_b) elif var_c == "+": count = add.add(var_a, var_b) elif var_c == '-': count = sub.sub(var_a, var_b) print("Результат равен " + str(count))
def transform(col, i): # rotate #print 'col:', col d = deque(col) d.rotate(-1) rotated = list(d) #print 'rotated:', rotated # replace for idx, val in enumerate(rotated): sRow = int(val[0:4], 2) sCol = int(val[4:8], 2) rotated[idx] = intToBinaryStr(sBox[sRow][sCol]) #print 'replaced:', rotated # compute round constant power = (i - 4) / 4 r = 1 if power != 0: for i in range(power): r = mult(r, 2) #print 'r:', r # XOR e and r rotated[0] = intToBinaryStr(int(rotated[0], 2) ^ r) # print 'XOR:', rotated return rotated
#!/usr/bin/env python from add import add from mult import mult from logger import logger from sub import sub if __name__ == '__main__': # Log messages with several levels logger.log(20, "info") logger.log(30, "warning") logger.log(50, "critical") logger.info("info") logger.warning("warning") logger.critical("critical") # Log messages from another module add(1, 2) sub(1, 2) mult(1.0, 2)
import add import diff import mult import division c = add.sum(5, 6) print(c) c = diff.diff(6, 5) print(c) c = mult.mult(5, 2) print(c) c = division.divison_numbers(10, 2) print(c)
def test_multiply(x, y): assert mult.mult(x, y) == x * y
def test_mult_inteiros_grandes_para_inteiros_maiores_que_max_int(): n_1 = 10242204 n_2 = 10002400 result = mult(n_1, n_2) assert 102446621289600 == mult(n_1, n_2)
def test_mult_inteiros_grandes_para_inteiros_menores_que_max_int(): n_1 = 2101 n_2 = 1130 assert 2374130 == mult(n_1, n_2)
def test3(self): a = [1, 2, 3, 4] b = [5, 6, 7, 8] c = mult(a, b) == np.dot(a, b) self.assertTrue(c.all())
def test2(self): a = 1010 b = 12 c = mult(a, b) == np.dot(a, b) self.assertTrue(c.all())
"""Ejercicio 1""" import mult as m A01=[[0.45, 1.3,0.94,1.23],[0,0.83,0.2,2.37],[0.2,0.65,0.4,0.3],[0,0,0,1]] A12=[[1,0.2,0.85,2.467],[0.54,1.3,0,0.77],[0.12,0.68,1,0.8],[0,0,0,1]] x2=[[2.3],[0],[24.4],[1]] r1=m.mult(A01,A12) x0=m.mult(r1,x2) print(x0)
# from typing import Optional # def mult(a: int, n: int) -> int: # """ # This function works only with positive integers # # >>> mult(2, 4) == 8 # True # # >>> mult(2, 0) == 0 # True # # >>> mult(2, -4) # ValueError("This function works only with postive integers") # """ from mult import mult if __name__ == "__main__": assert mult(2, 4) == 8 assert mult(2, 0) == 0 mult(2, -4)
import mult mult.mult(10, 50)
import mult l = mult.mult() mult(2, 5)
def test_mult(self): self.assertEqual(mult.mult(5, 5), 25) self.assertEqual(mult.mult(6, -1), -6) self.assertEqual(mult.mult(-1.5, 1.5), -2.25) self.assertEqual(mult.mult(10, 0), 0)
from bsqrt import bsqrt from gcd import gcd fcnChosen = input( "Enter either the letters m,e,f,b or g to access the function, if you would like to quit, enter q:" ) while True: char = fcnChosen.lower() if char == "q": break elif char == "m": #assuming user enters integers a = int(input("Enter in a numeric value:")) b = int(input("Enter in a second numeric value:")) print("The product is: " + str(mult(a, b))) elif char == "e": #assuming user enters integers b = int(input("Enter a base:")) p = int(input("Enter an exponent:")) print("The power is: " + str(exponentiate(b, p))) elif char == "f": #assuming user enters integers num = int(input("Enter a number:")) print("The factorial is: " + str(factorial(num))) elif char == "b": #assuming user enters integers num = int(input("Enter a number:")) accuracy = int(input("Enter the amount of decimal places:")) sqrt = bsqrt(num, accuracy) if sqrt >= 0: