Hello World
print("Hello, world!")
Run the script:
python main.py
This prints a line in Python with the built-in print function. It is the normal first script example.
Variables
Python variables are dynamically typed and can be annotated when the expected type matters.
name = "Dan"
age: int = 30
is_active = True
Python lets you bind values without declaring a type first. Add annotations when you want the expected shape to be clearer.
Functions
def greet(name: str) -> str:
return f"Hello, {name}!"
message = greet("world")
print(message)
This defines a function with type hints, calls it, and prints the returned value.
Python Add
def add(param1: int, param2: int) -> int:
return param1 + param2
This just adds the two input numbers with the language’s normal arithmetic and returns the sum.
Python Add Border
def add_border(picture: list[str]) -> list[str]:
width = len(picture[0]) + 2
border = "*" * width
return [border] + [f"*{row}*" for row in picture] + [border]
This builds a new grid with a * border around every side. It adds a full top and bottom row, then wraps each existing row from left and right.
Python Adjacent Elements Product
def adjacent_elements_product(input_array: list[int]) -> int:
return max(
input_array[i] * input_array[i + 1] for i in range(len(input_array) - 1)
)
This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.
Python Almost Magic Square
def almost_magic_square(a: list[int]) -> list[int]:
grid = [a[i:i + 3] for i in range(0, 9, 3)]
row_sum = [0, 0, 0]
col_sum = [0, 0, 0]
for i in range(3):
for j in range(3):
row_sum[i] += grid[i][j]
col_sum[i] += grid[j][i]
max_sum = 0
for k in range(3):
max_sum = max(max_sum, row_sum[k])
max_sum = max(max_sum, col_sum[k])
i = j = 0
while i < 3 and j < 3:
diff = min(max_sum - row_sum[i], max_sum - col_sum[j])
grid[i][j] += diff
row_sum[i] += diff
col_sum[j] += diff
if row_sum[i] == max_sum:
i += 1
if col_sum[j] == max_sum:
j += 1
return [v for row in grid for v in row]
This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.
Python Are Equally Strong
def are_equally_strong(
your_left: int, your_right: int, friends_left: int, friends_right: int
) -> bool:
return max(your_right, your_left) == max(friends_left, friends_right) and min(
your_left, your_right
) == min(friends_right, friends_left)
This compares each person’s strongest and weakest arm. If both pairs match, the result is true.
Python Array Change
def array_change(a: list[int]) -> int:
moves = 0
for k in range(len(a) - 1):
if a[k] >= a[k + 1]:
diff = a[k] - a[k + 1] + 1
a[k + 1] += diff
moves += diff
return moves
This moves left to right and bumps values only when needed so the array becomes strictly increasing.
Python Array Maximal Adjacement Difference
def array_maximal_adjacent_difference(a: list[int]) -> int:
diff = 0
for i in range(1, len(a) - 1):
diff = max(diff, abs(a[i] - a[i - 1]), abs(a[i] - a[i + 1]))
return diff
This checks the gap between each pair of neighbors and returns the largest difference.
Python Binary Gap
def binary_gap(n: int) -> int:
zeroes = bin(n)[2:].strip("0").split("1")
return max((len(z) for z in zeroes), default=0)
This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.
Python Bracket
def bracket(s: str) -> int:
closers = {")": "(", "]": "[", "}": "{"}
stack: list[str] = []
for ch in s:
if ch in closers:
if not stack or stack.pop() != closers[ch]:
return 0
elif ch:
stack.append(ch)
return 1 if not stack else 0
This uses a simple stack approach: open brackets go in, matching closing brackets pop them out.