今天的挑战很简单:
第 1 部分:
为了解决这个问题,我们必须
- 根据字符串输入创建 2 个数字列表,
- 按升序排序
- 对于List1中的每个数字,在List2中获取相同的索引,并找到两个数字之间的距离。
- 然后将距离相加
简单吧。
第 2 部分:
这有点棘手,但我的解决方案的主要概念是:
创建一个字典,其中 key 是 listA 中的数字,value 使用 Count() 方法统计 listB 中与 key 相同的数字(与 listA 索引相同的数字)。
那么这只是一个将键乘以值并对总数求和的简单情况。
Csharp 第 1 部分和第 2 部分:
var input = File.ReadAllLines("./input_1.txt") .Select(s => s.Split([' '], StringSplitOptions.RemoveEmptyEntries)).ToList(); var lists = ProcessLines(input); Part1(lists.list1, lists.list2); Part2(lists.list1, lists.list2); return; void Part1(List<int> list1, List<int> list2) { var ordered1 = list1.OrderBy(x => x).ToList(); var ordered2 = list2.OrderBy(x => x).ToList(); var totals = ordered1 .Select((t, i) => Math.Abs(t - ordered2[i])) .Select(difference => (long)difference) .ToList(); Console.WriteLine("Sum of differences: " + totals.Sum()); } void Part2(List<int> list1, List<int> list2) { var counts = list1.ToDictionary( number => number, number => list2.Count(x => x == number) ); var sum = counts.Sum(x => x.Value * x.Key); Console.WriteLine("Sum of counts: " + sum); } (List<int> list1, List<int> list2) ProcessLines(List<string> rows) { var list1 = new List<int>(); var list2 = new List<int>(); foreach (var row in rows) { list1.Add(int.Parse(row[0])); list2.Add(int.Parse(row[1])); } return (list1, list2); } </int></int></string></int></int></int></int></int></int>
第 1 部分 - Python
相同的概念不同的语法 - 很多
使用内置的 zip 函数和列表理解功能更加简洁。
def get_steps(list1, list2): totals = [abs(value - partner_item) for value, partner_item in zip(list1, list2)] print("Sum Total:", sum(totals)) def read_input(file_name): list1, list2 = zip(*((int(num1), int(num2)) for num1, num2 in (line.split() for line in open(file_name)))) return sorted(list1), sorted(list2) list1, list2 = read_input("input.txt") get_steps(list1, list2)
第 2 部分 - 进行中(已经晚了)⏰
以上是AoC '- 日历史学家歇斯底里(C# 和 Python)#剧透的详细内容。更多信息请关注PHP中文网其他相关文章!

Tomergelistsinpython,YouCanusethe操作员,estextMethod,ListComprehension,Oritertools

在Python3中,可以通过多种方法连接两个列表:1)使用 运算符,适用于小列表,但对大列表效率低;2)使用extend方法,适用于大列表,内存效率高,但会修改原列表;3)使用*运算符,适用于合并多个列表,不修改原列表;4)使用itertools.chain,适用于大数据集,内存效率高。

使用join()方法是Python中从列表连接字符串最有效的方法。1)使用join()方法高效且易读。2)循环使用 运算符对大列表效率低。3)列表推导式与join()结合适用于需要转换的场景。4)reduce()方法适用于其他类型归约,但对字符串连接效率低。完整句子结束。

pythonexecutionistheprocessoftransformingpypythoncodeintoExecutablestructions.1)InternterPreterReadSthecode,ConvertingTingitIntObyTecode,whepythonvirtualmachine(pvm)theglobalinterpreterpreterpreterpreterlock(gil)the thepythonvirtualmachine(pvm)

Python的关键特性包括:1.语法简洁易懂,适合初学者;2.动态类型系统,提高开发速度;3.丰富的标准库,支持多种任务;4.强大的社区和生态系统,提供广泛支持;5.解释性,适合脚本和快速原型开发;6.多范式支持,适用于各种编程风格。

Python是解释型语言,但也包含编译过程。1)Python代码先编译成字节码。2)字节码由Python虚拟机解释执行。3)这种混合机制使Python既灵活又高效,但执行速度不如完全编译型语言。

useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐个偏置,零indexingissues,andnestedloopineflinefficiencies


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3 Linux新版
SublimeText3 Linux最新版

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中