搜索
首页后端开发Python教程用 Python 解决每周挑战任务 1 和 0

Solving the Weekly Challenge  Task Ones and Zeroes in Python

一、简介

每周挑战赛由 Mohammad S. Anwar 组织,是一场友好的竞赛,开发者通过解决两个任务进行竞争。它鼓励所有语言和级别的开发者通过学习、分享和娱乐来参与。

每周挑战中的任务 1:1 和 0 要求开发人员找到最多包含 x 0 和 y 1 的最大子集。

在这篇文章中,我讨论并展示了我的 Python 语言解决方案,任务 1:1 和 0,并总结了一个简短的结论。

2. 任务 1:1 和 0

您将获得一个二进制字符串数组 @str 和两个整数 $x 和 $y。

编写一个脚本来返回 @str 的最大子集的大小,使得子集中最多有 $x 0 和 $y 1。

如果 m 的所有元素也是 n 的元素,则集合 m 是 n 的子集。

每周挑战 302,任务 1:一和零

示例 1 和 2 展示了给定输入的预期输出。

实施例1

Input: @str = ("10", "0001", "111001", "1", "0")
       $x = 5
       $y = 3
Output: 4

最多包含 5 个 0 和 3 个 1 的最大子集:("10", "0001", "1", "0")。

实施例2

Input: @str = ("10", "1", "0")
       $x = 1
       $y = 1
Output: 2

最多包含 1 个 0 和 1 个 1 的最大子集:("1", "0")。

3.我对任务1的解决方案

from itertools import combinations

def return_subset(strs: list[list], x: int, y: int) -> int | None:
    for r in range(len(strs) - 1, 1, -1):
        subsets = combinations(strs, r)
        for subset in subsets:
            total_zeros = 0
            total_ones = 0
            for element in subset:
                total_zeros += element.count('0')
                total_ones += element.count('1')
            if total_zeros 



<p>我的解决方案使用 itertools.combinations、for 循环和 if 语句来查找与任务要求匹配的子集:</p>

  • 我使用组合函数生成长度为 r 的所有 str 子集。我从最大子集长度 r = len(strs) - 1 开始,然后递减到最小子集长度 r = 1。
  • 对于长度 r 的每个子集
    • 我计算子集中零的总数 (total_zeros)。
    • 我计算子集中的总数 (total_ones)。
    • 如果子集符合所需条件(total_zeros
  • 如果没有 str 的子集,那么我返回 None。

4. 结论

在这篇文章中,我讨论了任务 1:1 和 0,并提出了该任务的解决方案。

在每周挑战网站上了解有关最新和过去挑战的更多信息:
https://theweeklychallenge.org/

了解有关参加每周挑战常见问题解答的更多信息:
https://theweeklychallenge.org/faq/

以上是用 Python 解决每周挑战任务 1 和 0的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
列表和阵列之间的选择如何影响涉及大型数据集的Python应用程序的整体性能?列表和阵列之间的选择如何影响涉及大型数据集的Python应用程序的整体性能?May 03, 2025 am 12:11 AM

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

说明如何将内存分配给Python中的列表与数组。说明如何将内存分配给Python中的列表与数组。May 03, 2025 am 12:10 AM

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

您如何在Python数组中指定元素的数据类型?您如何在Python数组中指定元素的数据类型?May 03, 2025 am 12:06 AM

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

什么是Numpy,为什么对于Python中的数值计算很重要?什么是Numpy,为什么对于Python中的数值计算很重要?May 03, 2025 am 12:03 AM

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

讨论'连续内存分配”的概念及其对数组的重要性。讨论'连续内存分配”的概念及其对数组的重要性。May 03, 2025 am 12:01 AM

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

您如何切成python列表?您如何切成python列表?May 02, 2025 am 12:14 AM

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

在Numpy阵列上可以执行哪些常见操作?在Numpy阵列上可以执行哪些常见操作?May 02, 2025 am 12:09 AM

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,减法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Python的数据分析中如何使用阵列?Python的数据分析中如何使用阵列?May 02, 2025 am 12:09 AM

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

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 英文版

推荐:为Win版本,支持代码提示!

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境