Best Python code snippet using hypothesis
Exercise4.py
Source:Exercise4.py
1n = 152q = 0.15345def initialize_a():6 a = [[0] * n for i in range(n)]7 a[0][1] = a[0][8] = a[1][2] = a[1][4] = a[1][6] = a[2][1] = a[2][5] = a[2][7] = a[3][2] = a[3][11] = 18 a[4][0] = a[4][9] = a[5][9] = a[5][10] = a[6][9] = a[6][10] = a[7][3] = a[7][10] = a[8][4] = a[8][5] = 19 a[8][9] = a[9][12] = a[10][14] = a[11][6] = a[11][7] = a[11][10] = a[12][8] = a[12][13] = a[13][9] = 110 a[13][10] = a[13][12] = a[13][14] = a[14][11] = a[14][13] = 111 return a121314def print_array(p):15 print(" p= [", end="")16 n = len(p)17 for i in range(n-1):18 print("%.5f" % round(p[i], 5), end=", ")19 print("%.5f" % round(p[-1], 5), end="]\n\n")202122def athrisma_i_grammhs(a, i):23 athrisma = 024 n = len(a)25 for j in range(n):26 athrisma += a[i][j]27 return athrisma282930def methodos_dynamewn(G):31 b0 = [G[i][0] for i in range(len(G))]32 b1 = [0]*len(G)33 for k in range(2*len(G)):34 for i in range(len(G)):35 b1[i] = 036 for j in range(len(G)):37 b1[i] += b0[j] * G[i][j]38 if k < 2*len(G)-1:39 temp = b1[0]40 else:41 temp = sum(b1)42 for j in range(len(G)):43 b1[j] /= temp44 b0[j] = b1[j]45 return b1464748def create_g(A):49 n = len(A)50 G = [[0.0] * n for i in range(n)]51 for j in range(n):52 for i in range(n):53 G[i][j] = q / n + A[j][i] * (1 - q) / athrisma_i_grammhs(A, j)54 return G555657a = initialize_a()58g = create_g(a) # ÎημιοÏ
Ïγεί Ïον Ïίνακα Google596061print("1.Το άθÏοιÏμα κάθε ÏÏÎ®Î»Î·Ï ÏοÏ
Ïίνακα G(oogle) : ")62for j in range(n):63 temp = sum(g[i][j] for i in range(n)) # εÏιÏÏÏÎÏει Ïο άθÏοιÏμα ÏÎ·Ï ÏÏÎ®Î»Î·Ï j και Ïο ÏÏ
ÏÏνει αÏοδεικνÏονÏαÏ64 print(" ÎθÏοιÏμα ÏÏήληÏ", j+1, "=", temp) # ÎÏÏι ÏÏι ο G είναι ÏÏοÏαÏÏικÏÏ ÎºÎ±Î¹ ειδικÏÏεÏα65 # είναι αÏιÏÏεÏά ÏÏοÏαÏÏικÏÏ666768print("2.Το κανονικοÏοιημÎνο ιδιοδιάνÏ
Ïμα ÏÎ·Ï Î¼ÎγιÏÏÎ·Ï Î¹Î´Î¹Î¿ÏιμήÏ: ", end="\n\n")69p = methodos_dynamewn(g) # ÎºÎ¬Î½Ï Ïήν μÎθοδο ÏÎ·Ï Î´Ï
νάμεÏÏ ÎºÎ±Î¹ εÏιÏÏÏÎÏÏ Ïο ιδιοδιάνÏ
Ïμα70print_array(p) # ÏÏ
ÏÏÎ½Ï Ïο ιδιοδιάνÏ
Ïμα717273print("3.Îλλαγή Ïίνακα Πγια βελÏίÏÏη ÏÎ·Ï 1Î·Ï ÏÎµÎ»Î¯Î´Î±Ï Î¼Îµ νÎo ιδιοδιάνÏ
Ïμα: ", end="\n\n")74a[0][7] = 0 # ÎÏαιÏÏ 1 ÏÏνδεÏη75a[9][0] = 1 # Î ÏοÏθÎÏÏ 4 ÏÏ
νδÎÏειÏ76a[10][0] = 177a[12][0] = 1 # ÏÏοÏθαÏαιÏÎÏÎµÎ¹Ï Î³Î¹Î± να βελÏιÏÏÏ Ïον Î²Î±Î¸Î¼Ï ÏημανÏικÏÏηÏÎ±Ï ÏÎ·Ï 1Î·Ï Î¹ÏÏοÏελίδαÏ78a[14][0] = 179g = create_g(a) # ÎημιοÏ
ÏÎ³Ï Ïον νÎο Ïίνακα Google80p = methodos_dynamewn(g) # ÎºÎ¬Î½Ï Ïήν μÎθοδο ÏÎ·Ï Î´Ï
νάμεÏÏ ÎºÎ±Î¹ εÏιÏÏÏÎÏÏ Ïο ιδιοδιάνÏ
Ïμα για Ïον νÎο Ïίνακα81print_array(p) # ÏÏ
ÏÏÎ½Ï Ïο νÎο ιδιοδιάνÏ
Ïμα828384print("4.Îλλαγή ÏÎ·Ï ÏιθανÏÏηÏÎ±Ï Î¼ÎµÏαÏήδηδηÏ: ", end="\n\n")85print(" (a) Îια q = 0.02: ")86q = 0.0287g = create_g(a) # δημιοÏ
Ïγεί Ïον Ïίνακα Google για Ïήν νÎα ÏιθανÏÏηÏα μεÏαÏήδηÏηÏ88p = methodos_dynamewn(g) # κάνει Ïήν μÎθοδο ÏÎ·Ï Î´Ï
νάμεÏÏ Î³Î¹Î± Ïον νÎο Ïίνακα Google89print_array(p) # ÏÏ
ÏÏνει Ïο νÎο ιδιοδιάνÏ
Ïμα9091print(" (a) Îια q = 0.6: ")92q = 0.693g = create_g(a) # ÏαÏÏμοια με ÏάνÏ94p = methodos_dynamewn(g)95print_array(p)96# Î ÏκοÏÏÏ ÏÎ·Ï ÏιθανÏÏηÏÎ±Ï Î¼ÎµÏαÏήδηÏÎ·Ï ÎµÎ¯Î½Î±Î¹ για να δείξει ÏÏÏο εÏκολα μÏοÏÏ Î½Î± μεÏÎ±Î²Ï Î±Ïο μία Ïελίδα Ïε μία97# άλλη. ÎÏο Ïιο μεγάλη είναι η ÏιθανÏÏηÏα αÏ
Ïή ÏÏÏο μικÏαίνοÏ
ν οι διαÏοÏÎÏ ÏημανÏικÏÏηÏÎ±Ï ÏÏν ιÏÏοÏελίδÏν98# και ÏÏο μικÏαίνει ÏÏÏο αÏ
ξάνονÏαι οι διαÏοÏÎÏ ÏÏην Ïάξη ÏÏν ÏελίδÏν.99100101print("5.Îλλαγή ÏÎ·Ï ÏÏνδεÏÎ·Ï ÏÎ·Ï Î¹ÏÏοÏÎµÎ»Î¯Î´Î±Ï 11 με καλÏÏεÏο ÏÏÏÏο: ")102q = 0.15103a = initialize_a() # αÏÏικÏÏ ÏίνακαÏ104print(" ÎÏλή ÏÏνδεÏη:")105g = create_g(a)106p = methodos_dynamewn(g) # ÎºÎ¬Î½Ï Ïήν μÎθοδο ÏÎ·Ï Î´Ï
νάμεÏÏ ÎºÎ±Î¹ εÏιÏÏÏÎÏÏ Ïο ιδιοδιάνÏ
Ïμα107print_array(p)108print(" ÎαλÏÏεÏη ÏÏνδεÏη:")109a[8][11] = 3110a[12][11] = 3111g = create_g(a)112p = methodos_dynamewn(g) # ÎºÎ¬Î½Ï Ïήν μÎθοδο ÏÎ·Ï Î´Ï
νάμεÏÏ ÎºÎ±Î¹ εÏιÏÏÏÎÏÏ Ïο ιδιοδιάνÏ
Ïμα113print_array(p)114# ÎÏÏÏ ÏαÏαÏηÏοÏμε δοÏ
λεÏει δλδ αÏ
ξάνει Ïήν ÏημανÏικÏÏηÏα ÏÎ·Ï 11 και μειÏνει ÏÎ·Ï 10115# ÎÏ
ξάνει ÏμÏÏ ÎºÎ±Î¹ Ïήν ÏημανÏικÏÏηÏÎ±Ï ÏÎ·Ï 12 & 14 & 8116117118print("6.ÎιαγÏαÏή ÏÎ·Ï ÏÎµÎ»Î¯Î´Î±Ï 10 αÏο Ïο γÏάÏημα: ")119print(" Î Ïιν Ïην διαγÏαÏή ÏÎ·Ï Î¹ÏÏοÏÎµÎ»Î¯Î´Î±Ï 10:")120a = initialize_a()121g = create_g(a)122p = methodos_dynamewn(g)123print_array(p)124125print(" ÎεÏά Ïην διαγÏαÏή ÏÎ·Ï Î¹ÏÏοÏÎµÎ»Î¯Î´Î±Ï 10:")126a = [[0] * 14 for i in range(14)]127a[0][1] = a[0][8] = a[1][2] = a[1][4] = a[1][6] = a[2][1] = a[2][5] = a[2][7] = a[3][2] = a[3][10] = 1128a[4][0] = a[4][9] = a[5][9] = a[6][9] = a[7][3] = a[8][4] = a[8][5] = 1129a[8][9] = a[9][11] = a[10][6] = a[10][7] = a[11][8] = a[11][12] = a[12][9] = 1130a[12][11] = a[12][13] = a[13][10] = a[13][12] = 1131g = create_g(a)132p = methodos_dynamewn(g)133print_array(p)134
...
test_clusterInitializers.py
Source:test_clusterInitializers.py
1import unittest2import numpy as np3import os4import sys56from .context import cluster_initializers78# from bmdcluster.initializers.cluster_initializers import initialize_A9# from bmdcluster.initializers.cluster_initializers import initialize_B101112class Testinitialize_A(unittest.TestCase):1314 def setUp(self):15 self.n = 41617 def test_initialize_A_assertions(self):1819 with self.subTest('Check data_cluster size assertion'):20 # Check that assertion error raised when the number of data clusters is greater21 # than or equal to the size of the dataset.22 with self.assertRaises(AssertionError):23 cluster_initializers.initialize_A(n = self.n, n_clusters = self.n)24 25 with self.subTest('Check init_ratio assertions'):2627 # Check that assertion error is raised when the init_ratio is outside of28 # the interval (0,1].2930 with self.assertRaises(AssertionError):31 cluster_initializers.initialize_A(n = self.n, n_clusters = self.n - 1, init_ratio = 1.1)3233 with self.assertRaises(AssertionError):34 cluster_initializers.initialize_A(n = self.n, n_clusters = self.n - 1, init_ratio = 0)35363738 def test_initialize_A_outputs(self):3940 with self.subTest('Check sum of entries'):41 # When init_ratio not set, each point should be assigned exactly one cluster.42 # Check sum of elements of cluster assignment matrix A.43 A = cluster_initializers.initialize_A(self.n, self.n-1)44 self.assertEqual(A.sum(), self.n)45464748 with self.subTest('Check init_ratio'):49 # Check that when init_ratio is set, the number of assigned clusters is50 # the expected number.51 A = cluster_initializers.initialize_A(n = self.n, n_clusters = self.n - 1, init_ratio = 0.5)52 self.assertEqual(A.sum(), self.n // 2)53545556 with self.subTest('Check bootstrap list passing'):57 # Test passing of list of tuples containing the positions of entries58 # to be set in the returned matrix.5960 A_expected = np.array([[1,0],61 [0,0],62 [0,1],63 [0,0]])646566 A = cluster_initializers.initialize_A(n = self.n, n_clusters = 2, bootstrap = [(0,0),(2,1)])67 self.assertTrue(np.array_equal(A, A_expected))68697071class TestInitializeB(unittest.TestCase):7273 def setUp(self):74 self.m = 3757677 def test_initializeB_output(self):7879 with self.subTest('Check B_ident'):80 # Check feature cluster matrix B is initialized to identity when B_ident set to True.81 B = cluster_initializers.initialize_B(self.m, B_ident = True)82 self.assertTrue(np.array_equal(np.identity(self.m), B))8384 85 # def test_check_assertions(self):8687 # with self.assertRaises(AssertionError):88 # initialize_B(self.m, B_ident = False, f_clusters = self.m + 1)8990 # with self.assertRaises(AssertionError):91 # initialize_B(self.m, B_ident = False, f_clusters = 1)9293 #@unittest.skip("No longer using keyword arguments in initialize_B")94 def test_initializeB_assertions(self):9596 with self.subTest('Check missing keyword argument'):97 # Check that MissingKeywordArgument raised when B_ident set to False and98 # without additional keyword arguments.99 with self.assertRaises(KeyError):100 cluster_initializers.initialize_B(self.m, B_ident = False)101102 with self.subTest('Check assertions'):103 # Check that when f_clusters is passed, that AssertionError is raised104 # f_clusters is not in the interval (1, m].105106 with self.assertRaises(AssertionError):107 cluster_initializers.initialize_B(self.m, B_ident = False, f_clusters = self.m + 1)108109 with self.assertRaises(AssertionError):110 cluster_initializers.initialize_B(self.m, B_ident = False, f_clusters = 1)111112if __name__ == '__main__':
...
Exercise3.3.py
Source:Exercise3.3.py
...25 i += 126 print()272829def initialize_a(n):30 A = [[0] * n for i in range(n)]31 for i in range(n):32 if i + 1 < n:33 A[i+1][i] = -234 A[i][i+1] = -235 A[i][i] = 536 return A373839def initialize_b(n):40 B = [1] * n41 B[0] = 342 B[-1] = 343 return B444546def find(a, b, x):47 norma = a[0][0] * x[0] + a[0][1] * x[1] - b[0]48 n = len(a)4950 for i in range(1, n):51 temp = 052 temp += a[i][i - 1] * x[i - 1] + a[i][i] * x[i]53 if i + 1 < n:54 temp += a[i][i + 1] * x[i + 1]5556 temp -= b[i]57 norma = max(abs(norma), abs(temp))5859 if abs(norma) >= 0.5e-4:60 return True61 else:62 return False636465def sumForCholesky(a, i, x0, xn):66 s = 067 if i - 1 >= 0:68 s += a[i][i-1] * xn[i-1]69 if i + 1 < len(a):70 s += a[i][i+1] * x0[i+1]71 return s727374def gauss_seidel(a, b):75 x0 = [0] * len(a)76 xn = [0] * len(a)77 flag = True78 counter = 07980 while flag:81 for i in range(0, len(a)):82 athrisma = sumForCholesky(a, i, x0, xn)83 xn[i] = (b[i] - athrisma) / a[i][i]84 counter += 185 flag = find(a, b, xn)86 for i in range(0, len(a)):87 x0[i] = xn[i]8889 print("ΧÏειάÏÏηκαν", counter, "ÎÏαναλήÏÎµÎ¹Ï ")90 return x0919293a10 = initialize_a(10) # ÎÏÏικοÏοιεί Ïον Ïίνακα Î10 (10x10) ÏÏοιÏεία94b10 = initialize_b(10) # ÎÏÏικοÏοιεί Ïον Ïίνακα Î10 (10) ÏÏοιÏεία95a10000 = initialize_a(10000) # ÎÏÏικοÏοιεί Ïον Ïίνακα Î10000 (10000x10000)96b10000 = initialize_b(10000)9798print("Îια n=10 : ")99# printAB(a10, b10) # ΤÏ
ÏÏνει Ïον εÏαÏ
ξημÎνο Ïίνακα100x10 = gauss_seidel(a10, b10) # Îάνει Gauss_Seidel και εÏιÏÏÏÎÏει Îναν Ïίνακα με ÏÎ¹Ï ÏίζεÏ101# Î ÏÏ
νάÏÏηÏη Gauss_Seidel ÏÏιν ολοκληÏÏθεί ÏÏ
ÏÏνει Ïον αÏÎ¹Î¸Î¼Ï ÏÏν εÏαναλήÏεÏν ÏοÏ
ÏÏειάÏÏηκαν102print_result(x10) # ÏÏ
ÏÏνει Ïον Ïίνακα με ÏÎ¹Ï ÏίζεÏ103104print("Îια n=10000 : ")105gauss_seidel(a10000, b10000) # ΠαÏÏμοια με εÏάνÏ106# ÎµÎ´Ï Î´ÎµÎ½ ÏÏ
ÏÏνοÏ
με Ïον Ïίνακα καθÏÏ ÎµÎ¯Î½Î±Î¹ ÏÎ¿Î»Ï Î¼ÎµÎ³Î¬Î»Î¿Ï107108print()109print("End of program", end="")
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!