搜索

嵌套之美

Dec 26, 2024 pm 10:22 PM

Nested beauty

每周挑战 300

穆罕默德·S·安瓦尔 (Mohammad S. Anwar) 每周都会发出“每周挑战”,让我们所有人都有机会为两周的任务提出解决方案。我的解决方案首先用Python编写,然后转换为Perl。这对我们所有人来说都是练习编码的好方法。

挑战,我的解决方案

这是第三百次挑战,让我个人感谢穆罕默德代表普华永道团队的每个人每周所做的所有工作。

任务一:美丽的布置

任务

给你一个正整数,$int。

编写一个脚本来返回您可以构建的精美排列的数量。

如果对于每个 i (1

  1. perm[i] 能被 i 整除
  2. i 可以被 perm[i]整除

我的解决方案

对于此任务,我使用 itertool 模块中的排列函数来完成所有排列。

那么只需确定该排列是否满足指定条件即可。如果没有,我将转向下一个排列。如果是,我会向 count 变量加一。

def beautiful_arrangement(n: list) -> str:
    count = 0

    for p in permutations(range(1, n+1)):
        for i in range(n):
            if p[i] % (i+1) != 0 and (i+1) % p[i] != 0:
                break
        else:
            count += 1

    return count

可能有一种更有效的方法来计算结果,而不涉及暴力。我的代码在处理较大的数字时会变得非常低效。我没有花任何时间调查这个。

示例

$ ./ch-1.py 1
1

$ ./ch-1.py 2
2

$ ./ch-1.py 10
700

任务 2:嵌套数组

任务

给你一个整数数组,@ints 长度为 n,包含 [0, n - 1] 范围内数字的排列。

编写一个脚本来构建一个集合,set[i] = ints[i], ints[ints[i]], ints[ints[ints[i]]], ...,遵循以下规则:

  1. set[i] 中的第一个元素以选择元素 ints[i] 开始。
  2. set[i] 中的下一个元素应该是 ints[ints[i]],然后是 ints[ints[ints[i]]],依此类推。
  3. 我们在 set[i] 中出现重复元素之前停止添加。

我的解决方案

这相对简单。我从一个名为longest_set 的变量开始,设置为0。然后迭代每个起始位置并将this_set 列表设置为集合的第一项(即ints[i])。我不断添加到这个集合,而 ints[this_set[-1]] 没有出现在 this_set 列表中。完成此操作后,我将 this_set 列表的长度与longest_set 值进行比较。如果它更大,我会更新longest_set值。

def nested_array(ints: list) -> int:
    longest_set = 0

    for start in range(len(ints)):
        this_set = [ints[start]]

        while ints[this_set[-1]] not in this_set:
            this_set.append(ints[this_set[-1]])

        if longest_set 



<h3>
  
  
  示例
</h3>



<pre class="brush:php;toolbar:false">$ ./ch-2.py 5 4 0 3 1 6 2
4

$ ./ch-2.py 0 1 2
1

$ ./ch-2.py 1 2 0 4 5 2
5

以上是嵌套之美的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python:深入研究汇编和解释Python:深入研究汇编和解释May 12, 2025 am 12:14 AM

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

Python是一种解释或编译语言,为什么重要?Python是一种解释或编译语言,为什么重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

对于python中的循环时循环与循环:解释了关键差异对于python中的循环时循环与循环:解释了关键差异May 12, 2025 am 12:08 AM

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

循环时:实用指南循环时:实用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

Python:它是真正的解释吗?揭穿神话Python:它是真正的解释吗?揭穿神话May 12, 2025 am 12:05 AM

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

与同一元素的Python串联列表与同一元素的Python串联列表May 11, 2025 am 12:08 AM

concateNateListsinpythonwithTheSamelements,使用:1)operatototakeepduplicates,2)asettoremavelemavphicates,or3)listCompreanspearensionforcontroloverduplicates,每个methodhasdhasdifferentperferentperferentperforentperforentperforentperfortenceandordormplications。

解释与编译语言:Python的位置解释与编译语言:Python的位置May 11, 2025 am 12:07 AM

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允许ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

循环时:您什么时候在Python中使用?循环时:您什么时候在Python中使用?May 11, 2025 am 12:05 AM

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

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

热门文章

热工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

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

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。