Skip to content

Commit b7b3d11

Browse files
committed
Add python programming fundamental & refactoring
1 parent ae6edb2 commit b7b3d11

File tree

5 files changed

+193
-13
lines changed

5 files changed

+193
-13
lines changed

fundamentals/custom_functions.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Author: Nguyen Truong Thinh
2+
# Contact me: [email protected] || +84393280504
3+
from math import sqrt
4+
5+
6+
# Custom functions
7+
def get_data_from_list(stocks):
8+
"""
9+
Get a list of:
10+
First 3 items in list, first and last item, number of items
11+
:param stocks: A stocks list
12+
:return: A new stocks list
13+
"""
14+
size = len(stocks)
15+
another_stocks = stocks
16+
17+
print('The first item is: ', another_stocks[0])
18+
print('The last item is: ', another_stocks[size - 1])
19+
print('The number of items is: ', size)
20+
21+
return [another_stocks[:3], another_stocks[0], another_stocks[size - 1], size]
22+
23+
24+
def ret_first_n_stocks(stocks, n):
25+
"""
26+
Get elements of a stocks list
27+
:param stocks: A stocks list
28+
:param n: The element's index
29+
:return: All items up to index n
30+
"""
31+
return stocks[:n]
32+
33+
34+
def tuple_distance(tuple1, tuple2):
35+
"""
36+
Calculate the Cartesian distance between two given points.
37+
d = sqrt ((x1 - x2)^2 +(y1 - y2)^2)
38+
:param tuple1: point (x1, y1)
39+
:param tuple2: point (x2, y2)
40+
:return: The Cartesian distance
41+
"""
42+
return sqrt((tuple1[0] - tuple2[0]) ** 2 + (tuple1[1] - tuple2[1]) ** 2)
43+
44+
45+
def print_basic_arithmetic(result = 'I would like to have a result of arithmetic expression please.'):
46+
# Use a breakpoint in the code line below to debug your script.
47+
print(f'Result: {result}') # Press Ctrl+F8 to toggle the breakpoint.

main.py

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,18 @@
22

33
# Press Shift+F10 to execute it or replace it with your code.
44
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
5-
from math import sqrt
6-
7-
8-
def print_basic_arithmetic(result):
9-
# Use a breakpoint in the code line below to debug your script.
10-
print(f'Result: {result}') # Press Ctrl+F8 to toggle the breakpoint.
11-
12-
13-
def tuple_distance(tuple1, tuple2):
14-
return sqrt((tuple1[0] - tuple2[0]) ** 2 + (tuple1[1] - tuple2[1]) ** 2)
15-
5+
# Use a breakpoint in the code line below to debug your script.
6+
from fundamentals.custom_functions import *
7+
from object_oriented_mindset.cartesian_system_coordinate import Point
8+
from object_oriented_mindset.util_cartesian_system_coordinate import *
169

1710
# Press the green button in the gutter to run the script.
1811
if __name__ == '__main__':
1912
# Basic Arithmetic:
13+
print_basic_arithmetic()
2014
print_basic_arithmetic(((4 - 2) + 4) / 5 * 9)
2115
# Variable Assignment:
22-
x = (1, 0)
16+
x = (1, 0) # Tuple: stores multiple fixed values in a sequence
2317
y = (5, 3)
2418
print(f'Distance between {x} & {y} is {tuple_distance(x, y)}')
2519
# Strings
@@ -28,7 +22,7 @@ def tuple_distance(tuple1, tuple2):
2822
print(s1 + s2)
2923
my_age = 31
3024
print('My age is: {}'.format(my_age - 2))
31-
# Lists
25+
# Lists (stores multiple values in an ordered index)
3226
aList = [5, 6, 7, 8, 9, True, 'a lot of different type of things']
3327
print(aList)
3428
otherList = [1, 2, 3, 4] + aList
@@ -42,6 +36,7 @@ def tuple_distance(tuple1, tuple2):
4236
print(otherList)
4337
otherList.append(True)
4438
print(otherList)
39+
# Set (stores multiple unique values in an unordered collection)
4540
# Sets operations: Union - Intersection - Difference
4641
A = {'Dog', 'Cat', 'Pig', 'Chicken', 'Rabbit', 'Turtle'}
4742
B = {'Dog', 'Chicken', 'Monkey', 'Cow'}
@@ -51,5 +46,43 @@ def tuple_distance(tuple1, tuple2):
5146
print('The intersection of set A & set B is: ', N)
5247
D = A.difference(B)
5348
print('The difference of set A & set B is ', D)
49+
# Dictionary (stores multiple unordered key:value pairs)
50+
info_dict_dev = {'name': 'DevOpsThinh', 'majors': 'Mobile Engineer', 'age': '29'}
51+
info_dict_tester = {'name': 'NgocMai', 'majors': 'Tester', 'age': '24'}
52+
info_dict_po = {'name': 'VanToan', 'majors': 'Product Owner', 'age': '32'}
53+
info_dict_boss = {'name': 'Thang', 'majors': 'Director', 'age': '35'}
54+
print('Info: ', info_dict_dev)
55+
print('Is There A specialized Key?: ', 'specialized' in info_dict_dev)
56+
# Logic & Loops
57+
for i in range(0, 10):
58+
boolIsGreaterThanSix = (i >= 3)
59+
if i == 5:
60+
print('Ignores at 5, continues at i = 6')
61+
continue
62+
if i == 9:
63+
print('Over flow at i = 9')
64+
break
65+
print(f'{i} - Spam: {boolIsGreaterThanSix}')
66+
67+
for i in range(0, 30):
68+
my_step_by_steps = (i % 3)
69+
if my_step_by_steps == 0:
70+
print(i)
71+
72+
cube_nums = [i ** 3 for i in range(10)]
73+
print(cube_nums)
74+
75+
# Custom functions
76+
friends_stock_list = [info_dict_dev, info_dict_tester, info_dict_po, info_dict_boss]
77+
data = get_data_from_list(friends_stock_list)
78+
print(data)
79+
# Objects
80+
p_x = Point(1, 0)
81+
p_y = Point(5, 3)
82+
# draw(p_x.x, p_x.y)
83+
# draw(p_y.x, p_y.y)
84+
print(f'\nDistance between {p_x} & {p_y} is {p_x.distance(p_y)}')
85+
del p_x
86+
del p_y
5487

5588
# See PyCharm help at https://www.jetbrains.com/help/pycharm/

object_oriented_mindset/__init__.py

Whitespace-only changes.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Author: Nguyen Truong Thinh
2+
# Contact me: [email protected] || +84393280504
3+
from math import sqrt
4+
5+
6+
class Point:
7+
"""
8+
A class for 2D point objects in Cartesian coordinate system.
9+
"""
10+
def __init__(self, x, y):
11+
"""
12+
Initializer function.
13+
:param x: The x coordinate of the specific point
14+
:param y: The Y coordinate of the specific point
15+
:return: None
16+
"""
17+
self.x = x
18+
self.y = y
19+
20+
def __str__(self):
21+
"""
22+
The customized method for better string representation.
23+
:return: Point(x, y) string representation
24+
"""
25+
return f'Point ({self.x}, {self.y})'
26+
27+
def __add__(self, other):
28+
"""
29+
To perform coordinate-wise additions
30+
:param other: Another point
31+
:return: A new point which correspond to the coordinates
32+
"""
33+
return Point(self.x + other.x, self.y + other.y)
34+
35+
def __sub__(self, other):
36+
"""
37+
To perform coordinate-wise subtractions
38+
:param other: Another point
39+
:return: A new point which correspond to the coordinates
40+
"""
41+
return Point(self.x - other.x, self.y - other.y)
42+
43+
def distance(self, other):
44+
"""
45+
Computes the Cartesian distance between the calling point
46+
and another point objects
47+
:param other: Another point
48+
:return: The Cartesian distance
49+
"""
50+
dif = self - other
51+
return sqrt(dif.x ** 2 + dif.y ** 2)
52+
53+
def __del__(self):
54+
"""
55+
A destructor method for confirmation when Point instances of
56+
class are destroyed.
57+
:return: None
58+
"""
59+
print(f'{self} Say Goodbye!')
60+
61+
62+
63+
64+
65+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Author: Nguyen Truong Thinh
2+
# Contact me: [email protected] || +84393280504
3+
from matplotlib import pyplot as plt
4+
5+
6+
def draw_arrow(axis):
7+
left, right = axis.get_xlim()
8+
bottom, top = axis.get_ylim()
9+
plt.arrow(
10+
left, 0, right - left, 0, length_includes_head=True, head_width=0.15
11+
)
12+
plt.arrow(
13+
0, bottom, 0, top - bottom, length_includes_head=True, head_width=0.15
14+
)
15+
16+
17+
def draw(x, y):
18+
# set up range of the plot
19+
limit = max(x, y) + 1
20+
21+
fig = plt.figure()
22+
axis = fig.add_subplot(111)
23+
axis.set_aspect('equal')
24+
# lines corresponding to x- and y- coordinates
25+
plt.plot([x, x], [0, y], '-', c='blue', linewidth=3)
26+
plt.plot([0, x], [y, y], '-', c='blue', linewidth=3)
27+
# actual point
28+
plt.scatter(x, y, s=100, marker='o', c='red')
29+
30+
axis.set_xlim((-limit, limit))
31+
axis.set_ylim((-limit, limit))
32+
# axis arrows
33+
draw_arrow(axis)
34+
plt.grid()
35+
plt.show()

0 commit comments

Comments
 (0)