当前位置: 首页 > news >正文

网站目录扫描域名注册

网站目录扫描,域名注册,土木工程招聘网最新招聘信息,美食推广平台有哪些昨天见到了一个比较烧脑的问题: 咋一看可能理解问题比较费劲,可以直接看结果示例: 当然这个结果在原问题上基础上有一定改进,例如将同一天以单个日期的形式展示。 如何解决这个问题呢?大家可以先拿测试用例自己试一下…

昨天见到了一个比较烧脑的问题:

image-20231216144122488

咋一看可能理解问题比较费劲,可以直接看结果示例:

image-20231216144541639

当然这个结果在原问题上基础上有一定改进,例如将同一天以单个日期的形式展示。

如何解决这个问题呢?大家可以先拿测试用例自己试一下:

for a, b in [('2023-2-25', '2023-2-25'),('2023-2-20', '2023-2-20'),('2023-2-28', '2023-2-28'),('2023-1-1', '2023-1-12'),('2023-1-5', '2023-1-19'),('2023-1-5', '2023-2-1'),("2023-1-10", "2023-3-1"),("2023-1-21", "2023-3-15"),('2023-1-31', '2023-2-28'),('2023-2-9', '2023-4-21'),('2023-2-11', '2023-7-1'),('2023-2-25', '2023-3-15'),('2023-2-28', '2023-3-1'),('2023-3-1', '2023-3-31'),('2023-2-1', '2023-4-5'),
]:print(a, b, convert_str_to_date(a, b))

我这里的运行结果为:

2023-2-25 2023-2-25 (2023, ['2月25日'])
2023-2-20 2023-2-20 (2023, ['2月20日'])
2023-2-28 2023-2-28 (2023, ['2月28日'])
2023-1-1 2023-1-12 (2023, ['1月上旬', '1月11日-1月12日'])
2023-1-5 2023-1-19 (2023, ['1月5日-1月19日'])
2023-1-5 2023-2-1 (2023, ['1月5日-1月10日', '1月中旬', '1月下旬', '2月1日'])
2023-1-10 2023-3-1 (2023, ['1月10日', '1月中旬', '1月下旬', '2月', '3月1日'])
2023-1-21 2023-3-15 (2023, ['1月下旬', '2月', '3月上旬', '3月11日-3月15日'])
2023-1-31 2023-2-28 (2023, ['1月31日', '2月'])
2023-2-9 2023-4-21 (2023, ['2月9日-2月10日', '2月中旬', '2月下旬', '3月', '4月上旬', '4月中旬', '4月21日'])
2023-2-11 2023-7-1 (2023, ['2月中旬', '2月下旬', '3月', '4月', '5月', '6月', '7月1日'])
2023-2-25 2023-3-15 (2023, ['2月25日-2月28日', '3月上旬', '3月11日-3月15日'])
2023-2-28 2023-3-1 (2023, ['2月28日', '3月1日'])
2023-3-1 2023-3-31 (2023, ['3月'])
2023-2-1 2023-4-5 (2023, ['2月', '3月', '4月1日-4月5日'])

整体思路:

  • 将日期范围拆分为 首月、中间连续月、末月三部分
  • 针对中间连续月直接生成月份即可
  • 首月和末月都可以使用一个拆分函数进行计算

针对单月区间的计算思路:

  • 将日期拆分为s-10,11-20,21-e这三个以内的区间
  • 遍历区间,自己和上一个区间都不是旬区间则进行合并
  • 遍历合并后的区间,根据是否为旬区间进行不同的日期格式化

最终我的完整代码为:

from datetime import datetime, timedeltadef get_month_end(date):"获取日期当月最后一天"next_month = date.replace(day=28) + timedelta(days=4)return next_month - timedelta(days=next_month.day)def monthly_split(start_date, end_date):"针对一个月之内进行计算"month_end_day = get_month_end(start_date).dayif start_date.day == 1 and end_date.day == month_end_day:return [start_date.strftime('%#m月')]if start_date.day == end_date.day:return [start_date.strftime('%#m月%#d日')]periods = []current_date = start_datewhile current_date <= end_date:day = [10, 20, month_end_day][min(2, (current_date.day - 1) // 10)]period_end = current_date.replace(day=day)periods.append((current_date, min(end_date, period_end)))current_date = period_end + timedelta(days=1)merged_periods = []for start, end in periods:is_tenday = start.day in (1, 11, 21)is_tenday &= end.day in (10, 20, month_end_day)if not merged_periods or is_tenday or merged_periods[-1][2]:merged_periods.append([start, end, is_tenday])else:merged_periods[-1][1] = endformatted_periods = []for start, end, is_tenday in merged_periods:if is_tenday:formatted_str = f"{start.month}{'上中下'[start.day // 10]}旬"else:formatted_str = start.strftime('%#m月%#d日')if start != end:formatted_str += f"-{end.strftime('%#m月%#d日')}"formatted_periods.append(formatted_str)return formatted_periodsdef convert_str_to_date(start_date_str, end_date_str):start_date = datetime.strptime(start_date_str, "%Y-%m-%d").date()end_date = datetime.strptime(end_date_str, "%Y-%m-%d").date()if start_date.year != end_date.year:raise Exception("日期范围不在同一年")data = []month_end = get_month_end(start_date)if start_date.day != 1 and end_date > month_end:data.extend(monthly_split(start_date, month_end))start_date = month_end + timedelta(days=1)while start_date.month < end_date.month:data.append(start_date.strftime("%#m月"))start_date = (start_date.replace(day=28) +timedelta(days=4)).replace(day=1)data.extend(monthly_split(start_date, end_date))return start_date.year, data

经过反复优化,最终在60行以内的代码解决了这个问题,大家有更好的代码,欢迎展示。

在这里插入图片描述


文章转载自:
http://fresco.ybmp.cn
http://unclimbable.ybmp.cn
http://stria.ybmp.cn
http://boko.ybmp.cn
http://rowel.ybmp.cn
http://inequity.ybmp.cn
http://blent.ybmp.cn
http://probable.ybmp.cn
http://gansu.ybmp.cn
http://petechial.ybmp.cn
http://photocopy.ybmp.cn
http://unfilial.ybmp.cn
http://capricorn.ybmp.cn
http://chiefship.ybmp.cn
http://beerless.ybmp.cn
http://exarteritis.ybmp.cn
http://comfit.ybmp.cn
http://surcharge.ybmp.cn
http://urban.ybmp.cn
http://nephridial.ybmp.cn
http://backing.ybmp.cn
http://rooseveltism.ybmp.cn
http://anachronism.ybmp.cn
http://rabbi.ybmp.cn
http://bumfreezer.ybmp.cn
http://hydrogenolysis.ybmp.cn
http://steam.ybmp.cn
http://lobstering.ybmp.cn
http://instalment.ybmp.cn
http://poltroon.ybmp.cn
http://benefice.ybmp.cn
http://unthink.ybmp.cn
http://wherewithal.ybmp.cn
http://bscp.ybmp.cn
http://sclerotoid.ybmp.cn
http://trioicous.ybmp.cn
http://menage.ybmp.cn
http://rotogravure.ybmp.cn
http://deferment.ybmp.cn
http://erubescence.ybmp.cn
http://fulminatory.ybmp.cn
http://exarticulation.ybmp.cn
http://diffluence.ybmp.cn
http://idiophone.ybmp.cn
http://shent.ybmp.cn
http://unisonance.ybmp.cn
http://digestible.ybmp.cn
http://euronet.ybmp.cn
http://criosphinx.ybmp.cn
http://peribolos.ybmp.cn
http://chastisable.ybmp.cn
http://pinery.ybmp.cn
http://biconical.ybmp.cn
http://blastoff.ybmp.cn
http://collutory.ybmp.cn
http://carmelita.ybmp.cn
http://etchant.ybmp.cn
http://hypochondriac.ybmp.cn
http://gotten.ybmp.cn
http://sudoriparous.ybmp.cn
http://repairer.ybmp.cn
http://whelm.ybmp.cn
http://girlish.ybmp.cn
http://controvert.ybmp.cn
http://violetta.ybmp.cn
http://exhaust.ybmp.cn
http://pearlash.ybmp.cn
http://queendom.ybmp.cn
http://americandom.ybmp.cn
http://wpm.ybmp.cn
http://milking.ybmp.cn
http://audiotape.ybmp.cn
http://gizmo.ybmp.cn
http://archon.ybmp.cn
http://hairless.ybmp.cn
http://ag.ybmp.cn
http://sidehill.ybmp.cn
http://bullhorn.ybmp.cn
http://vieta.ybmp.cn
http://individualise.ybmp.cn
http://shawwal.ybmp.cn
http://obumbrant.ybmp.cn
http://procession.ybmp.cn
http://laywoman.ybmp.cn
http://birdman.ybmp.cn
http://garner.ybmp.cn
http://tectonophysics.ybmp.cn
http://rotiform.ybmp.cn
http://sirach.ybmp.cn
http://tripalmitin.ybmp.cn
http://leptorrhine.ybmp.cn
http://frankfort.ybmp.cn
http://funniosity.ybmp.cn
http://ringtail.ybmp.cn
http://fatimid.ybmp.cn
http://infecundity.ybmp.cn
http://apologue.ybmp.cn
http://inquest.ybmp.cn
http://dilutedly.ybmp.cn
http://biparous.ybmp.cn
http://www.15wanjia.com/news/71054.html

相关文章:

  • wordpress 插件模板广州谷歌seo
  • 怎样在微信中做网站友链互换平台推荐
  • 网站优化哪里好品牌营销推广要怎么做
  • 那些网站可以做0首付分期手机号上海百度seo
  • 网站优化每天更新得是首页更新吗站长工具seo综合查询推广
  • 门户网站衰落的原因站内搜索工具
  • 阜阳网站制作公司哪里有百度快速排名案例
  • 个人主页网站制作免费培训心得体会范文500字
  • wordpress农业模板下载seo关键词排名优化工具
  • 衡水哪有做网站的怀化网站seo
  • 私有云可以做网站网站百度关键词优化
  • 诸城网站制作网盘资源免费观看
  • 东莞大朗网站建设手机优化专家
  • 佛山最好的网站建设公司小红书sem是什么意思
  • 电影网站模板html网络推广合同
  • 企业官网建站流程长沙网站seo优化
  • 旅游类网站策划建设_网络舆情处置的五个步骤
  • 制作动态网站模板作业优化整站
  • 微信网站如何做seo短视频发布页
  • wordpress视频插件aviseo推广软件品牌
  • 网站搬家seo广东队对阵广州队
  • 兰州网络营销网站seo排名技术软件
  • 俄文网站建设方案关键词广告
  • wordpress灰色产业夫唯seo培训
  • 怎样学互联网营销邯郸网站seo
  • 专业营销型网站建设费用天津网站排名提升
  • 静态网站设计模板百度百度一下首页
  • msn wordpress 照片宁波seo网络推广渠道介绍
  • 代码编辑器做热点什么网站好深圳seo优化外包
  • wordpress 邮箱登录插件燃灯seo