Skip to content

Instantly share code, notes, and snippets.

@yunpengn
Last active September 20, 2021 15:39
Show Gist options
  • Save yunpengn/775ef2fd132e97d3821802d88072b083 to your computer and use it in GitHub Desktop.
Save yunpengn/775ef2fd132e97d3821802d88072b083 to your computer and use it in GitHub Desktop.
Some Python exercises
import datetime
# Defines the increment.
delta = datetime.timedelta(days=1)
# Reads the number of test cases and all inputs.
n = int(input())
ranges = [input() for _ in range(n)]
# Iterates over every test case
for i in range(n):
# Parses the input.
range = ranges[i]
date_start = datetime.datetime.strptime(range.split()[0], '%d/%m/%Y')
date_end = datetime.datetime.strptime(range.split()[1], '%d/%m/%Y')
# Iterates through the date range.
while date_start <= date_end:
# Constructs the output string.
part_date = '{:02d}:{:02d}'.format(date_start.month, date_start.day)
part_time = part_date[::-1]
# Increments the date.
date_start += delta
# Checks whether the time part is valid.
if int(part_time.split(':')[0]) < 24 and int(part_time.split(':')[1]) < 60:
print('{}:{}'.format(part_date, part_time))
# Prints the dash line to separate.
if i != n - 1:
print('-----------')
import math
# Defines the default unit.
sqrt3 = math.sqrt(3)
def cal_mix_result(base, height):
max_pair = int(height / sqrt3)
# Checks for each case, with different size of triangle region.
result = -1
for i in range(max_pair + 1):
# Counts the triangular region.
count_tri = i * (base * 2 + 1)
# Counts the rectangular region.
count_rect = int(height - sqrt3 * i + 1) * (base + 1)
# Decides the result.
if count_tri + count_rect > result:
result = count_tri + count_rect
# Returns the result.
return result
# Reads the number of test cases and all inputs.
n = int(input())
lines = [input() for _ in range(n)]
# Iterates over every test case.
for i in range(n):
# Parses the input.
length = int(lines[i].split()[0])
width = int(lines[i].split()[1])
# Mix two patterns along the width.
output_1 = cal_mix_result(width, length)
# Mix two patterns along the length.
output_2 = cal_mix_result(length, width)
# Prints the result.
print(max(output_1, output_2))
from functools import cmp_to_key
# Reads all words from the input.
input_file = open('input.txt')
lines = [line.rstrip() for line in input_file.readlines()]
words = " ".join(lines).split()
# Begins to sort the input.
words.sort(key=cmp_to_key(lambda a, b: len(a) - len(b) if len(a) != len(b) else (1 if a > b else (0 if a == b else -1))))
# Prints the output.
for word in words:
print(word)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment