Skip to content

Instantly share code, notes, and snippets.

View qiuyujx's full-sized avatar

Christopher Tao qiuyujx

View GitHub Profile
@qiuyujx
qiuyujx / python_data_class_make_dataclass.py
Created May 2, 2022 12:43
python_data_class_make_dataclass.py
Student = dc.make_dataclass(
cls_name='Student',
fields=[
('firstname', str),
('lastname', str),
('student_no', str)
],
namespace={
'greeting': lambda self: f'Hello, {self.firstname} {self.lastname}!'
}
@qiuyujx
qiuyujx / python_dataclass_person_normal.py
Created May 2, 2022 12:12
python_dataclass_person_normal.py
class Person:
def __init__(self, firstname, lastname, age):
self.firstname = firstname
self.lastname = lastname
self.age = age
def __repr__(self):
return f"Person(firstname='{self.firstname}', lastname='{self.lastname}', age={self.age})"
def __eq__(self, other):
s = """Gur Mra bs Clguba, ol Gvz Crgref
Ornhgvshy vf orggre guna htyl.
Rkcyvpvg vf orggre guna vzcyvpvg.
Fvzcyr vf orggre guna pbzcyrk.
Pbzcyrk vf orggre guna pbzcyvpngrq.
Syng vf orggre guna arfgrq.
Fcnefr vf orggre guna qrafr.
Ernqnovyvgl pbhagf.
Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
import re
class Employee:
def __init__(self, name, year_joined):
self.name = name
self.year_joined = year_joined
@classmethod
def from_string(cls, string):
name = re.search('I am (.*?) ', string).group(1)
class Employee:
def __init__(self, name, year_joined):
self.name = name
self.year_joined = year_joined
@staticmethod
def calc_year(year_joined, year_now):
length = year_now - year_joined
if length > 0:
return f'{length} years'
def calc_year(year_joined, year_now):
length = year_now - year_joined
if length > 0:
return f'{length} years'
else:
return 'less than one year'
class Employee:
def __init__(self, name, year_joined):
from datetime import datetime
class Employee:
def __init__(self, name, year_joined):
self.name = name
self.year_joined = year_joined
def calc_year(self, year_joined, year_now):
length = year_now - year_joined
if length > 0:
def max_vote_dynamic_planning(states, total_days):
dp_matrix = [[0 for days_left in range(total_days + 1)] for index in range(len(states) + 1)]
for index in range(1, len(states) + 1):
for days_left in range(1, total_days + 1):
if states[index-1]['days'] <= days_left: # If we have enough days left
votes_if_go = dp_matrix[index-1][days_left - states[index-1]['days']] + states[index-1]['votes']
votes_if_not_go = dp_matrix[index-1][days_left]
# Save the maximum votes into cache
dp_matrix[index][days_left] = max(votes_if_go, votes_if_not_go)
def max_vote_recursive(states, days_left, index):
# Terminating conditions
if len(states) == 0 or index >= len(states) or days_left <= 0:
return 0
# If we have enough days, go to this state
votes_if_go = 0
if states[index]['days'] <= days_left:
votes_if_go = states[index]['votes'] + max_vote_recursive(states, days_left - states[index]['days'], index + 1)
# Original Function
f = (lambda x: x((lambda x: x(lambda x: x))(x)))(lambda x: x)
# Reduced confusion
f = (
lambda a: a(
(
lambda b: b(lambda c: c)
)(a)
)