Last active
October 30, 2020 10:23
-
-
Save wildonion/c9460845066fd0304c570577a0df9a27 to your computer and use it in GitHub Desktop.
python playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import math | |
# lambda complex syntax | |
print((lambda name, number: str(number)+name)(input("[+] Enter A Name : "), 456)) | |
# ===================================== | |
# overloading * operator | |
class int2(int): | |
def __init__(self, x): | |
# super(int2, self).__init__() | |
self = x | |
def __matmul__(self, i2): | |
return self * i2 # i2 is int2 type | |
a = int2(2.3) | |
b = int2(3.5) | |
c = a @ b | |
print(c) | |
# ------------------- | |
class int2(): | |
def __init__(self, x): | |
self.value = x | |
# self = x | |
def __matmul__(self, i2): | |
return int2(self.value * i2.value) | |
a = int2(2) | |
b = int2(3) | |
c = a @ b | |
print(c.value) | |
# ===================================== | |
# matrix manipulation | |
n = 5 | |
A = np.diag(list(range(1, n+1))) | |
diag_B = np.diag(list(range(1, n))) | |
col_B = np.hstack((diag_B, np.zeros((diag_B.shape[0], 1), dtype=diag_B.dtype))) # add a new col of zeros at the end of mat | |
B = np.insert(col_B, 0, 0, axis=0) # insert zeros at first row | |
diag_C = np.diag(list(range(n-1, 0, -1))) | |
col_C = np.insert(diag_C, 0, 0, axis=1) # insert zeros at first col | |
C = np.insert(col_C, n-1, 0, axis=0) # insert zeros at the last row | |
mat = np.add(np.add(A, B), C) | |
# ===================================== | |
# convert 1D list into 2D list | |
# https://www.quora.com/How-can-we-convert-a-1-dimensional-array-to-a-2-dimensional-array-in-Python | |
# https://stackoverflow.com/questions/14681609/create-a-2d-list-out-of-1d-list | |
def reshape(arr, m, n): | |
if len(arr) % m != 0 and len(arr) % n != 0: | |
return False | |
mat = [] | |
for row in range(m): | |
lst = [] | |
for mult in range(row*n, (row+1)*n): | |
lst.append(arr[mult]) | |
mat.append(lst) | |
print(mat) | |
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] | |
reshape(arr, 3, 5) | |
# ===================================== | |
# convert 2D list into 1D list | |
def twodto1d(mat): | |
new_list = [] | |
for row in mat: | |
for j in row: | |
new_list.append(j) | |
return new_list | |
mat = [[1,2,3], [1,2], [1, 4, 5, 6, 7]] | |
print(twodto1d(mat)) | |
# ===================================== | |
# reverse number | |
N = int(input("Enter a number ")) | |
s = [] | |
while N > 0: | |
d = N//10 | |
r = N - 10*d | |
s.append(r) | |
N = d | |
d = 0 | |
for i in range(len(s)): | |
d += s[i]*math.pow(10,len(s)-i)/10 | |
print(int(d)) | |
# ===================================== | |
# OOP stuff | |
class A(): | |
def __init__(self, a=None): | |
self.a = a | |
self.avar = "im a varible for A" | |
def where(self): | |
print("im inside {}".format(self.a)) | |
print(self.avar) | |
class B(A): | |
def __init__(self, b=None): | |
super().__init__("A") | |
def edit(self): | |
self.where() | |
self.avar = "i edited a var ; now im a varible for B" | |
print(self.avar) | |
if __name__ == "__main__": | |
for i in range(4): | |
b = B(b="{}".format(i)) | |
print(b) | |
b.edit() | |
# ----------------------------- | |
class A: | |
def __init__(self, val): | |
self.val = val | |
print("[+] IM INSIDE A WIHT VALUE {}".format(self.val)) | |
def __a(self): | |
class B(A): | |
def __init__(self): | |
print("[+] IM INSIDE B") | |
super().__init__(67) | |
return B() | |
def b(self): | |
self.__a() | |
if __name__ == "__main__": | |
a = A(3) | |
print(a.b()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment