Advertisement
lutunovoleg

table only np and pd

Oct 9th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | Source Code | 0 0
  1. # таблица истинности
  2. import pandas as pd
  3. import numpy as np
  4.  
  5.  
  6. def binary_number(number=0, size=0):
  7.     binary = []
  8.     for _ in range(size):
  9.         binary.append(number % 2)
  10.         number //= 2
  11.     binary.reverse()
  12.     return binary
  13.  
  14.  
  15. def table(f: list):
  16.     tmp = len(f)
  17.     count = 0
  18.     while tmp > 1:
  19.         if tmp % 2 == 1:
  20.             raise Exception("Длина вектора неправильная")
  21.         tmp /= 2
  22.         count += 1
  23.     variables = [f"x{i + 1}" for i in range(count)]
  24.     variables.append("f")
  25.     tmp_array = [[*binary_number(i, count), f[i]] for i in range(len(f))]
  26.     a = pd.DataFrame(np.array(tmp_array), columns=variables)
  27.     print(a.to_string(index=False))
  28.  
  29.  
  30. table([1, 0, 1, 0, 1, 1, 0, 1])
  31.  
  32.  
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement