Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

One a Day One Liners with Python — Week 5

Jeremy Brown
Python in Plain English
4 min readJan 29, 2023

--

Photo by Dave Mullen on Unsplash

Feb 4, 2023

from argparse import Namespace as NS

r = 4
c = 6
w = 5
h = 10
grid = [[NS(l=j*w, r=j*w+w, t=i*h, b=i*h+h) for j in range(c)] for i in range(r)]

Feb 3, 2023

a = 6, 8, 4
b = 7.5, 10, 5

sim = [
sorted([
(t[y]**2+t[z]**2-t[x]**2)/(2*t[y]*t[z])
for x,y,z in [[0,1,2],[1,2,0],[2,0,1]]
])
for t in [a, b]
]

assert sim[0] == sim[1]
(8^2 + 4^2 - 6^2) / (2 * 8 * 4) = 0.6875
(4^2 + 6^2 - 8^2) / (2 * 4 * 6) = -0.25

Feb 2, 2023

from argparse import Namespace as NS

a = NS(l=0, t=0, r=10, b=10)
b = NS(l=4, t=4, r=12, b=12)

intersects = \
max(min(a.l, a.r), min(b.l, b.r)) < min(max(a.l, a.r), max(b.l, b.r)) and \
max(min(a.t, a.b), min(b.t, b.b)) < min(max(a.t, a.b), max(b.t, b.b))

Feb 1, 2023

from argparse import Namespace as NS

a = NS(l=0, t=0, r=3, b=2)
b = NS(l=1, t=1, r=5, b=3)

intersection = NS(l=max(a.l, b.l), t=max(a.t, b.t), r=min(a.r, b.r), b=min(a.b, b.b))

Jan 31, 2023

from math import cos, tau, sin

r = 5
n = 5
t = tau / n

pentagon = [(cos(t * i) * r, sin(t * i) * r) for i in range(0, n)]
centroid = [sum(col) / float(len(pentagon)) for col in zip(*pentagon)]

Jan 30, 2023

from math import cos, tau, sin

r = 5
n = 6
t = tau / n

hexagon = [(cos(t * i) * r, sin((t * i) * r) for i in range(0, n)]

Jan 29, 2023

from argparse import Namespace

p = Namespace(x=10, y=10)
plane = Namespace(x1=0, x2=20, y1=0, y2=20)

point_in_plane = p.x >= plane.x and p.x <= plane.x2 and p.y >= plane.y1 and p.y <= plane.y2

--

--

Published in Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Written by Jeremy Brown

Software Engineer, originally from the U.S. and now living and working in Vienna, Austria. I write about Programming, Music, Machine Learning & NLP

Responses (1)

Write a response