1. はじめに
Mohammad S. Anwar が主催する Weekly Challenge は、開発者が 2 つのタスクを解決することで競うフレンドリーなコンテストです。学習、共有、楽しみを通じて、あらゆる言語とレベルの開発者の参加を奨励します。
タスク 1: The Weekly Challenge の 1 とゼロでは、開発者は最大で x 個のゼロと y 個の 1 を含む最大のサブセットを見つけます。
この投稿では、タスク 1: 1 と 0 に対する Python 言語の解決策について説明し、提示し、短い結論で終わります。
2. タスク 1: 1 と 0
バイナリ文字列の配列 @str と 2 つの整数 $x と $y が与えられます。
サブセット内に最大 $x 0 と $y 1 が存在するように、@str の最大サブセットのサイズを返すスクリプトを作成します。
m のすべての要素が n の要素でもある場合、集合 m は n の部分集合です。
ウィークリー チャレンジ 302、タスク 1: 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 の strs のすべてのサブセットを生成します。最大サブセット長 r = len(strs) - 1 から開始し、最小サブセット長 r = 1 まで減分します。
- 長さ r の各部分集合について
- サブセット内のゼロの合計数 (total_zeros) を数えます。
- サブセット内の 1 の合計数 (total_ones) を数えます。
- 必要な条件 (total_zeros
- str のサブセットがない場合は、None を返します。
4. 結論
この投稿では、タスク 1: 1 と 0 について説明し、このタスクに対する私の解決策を示しました。
最新および過去のチャレンジの詳細については、The Weekly Challenge Web サイトをご覧ください:
https://theweeklychallenge.org/
The Weekly Challenge での競争に関するよくある質問:
https://theweeklychallenge.org/faq/
以上がPython でウィークリー チャレンジ タスク 1 とゼロを解決するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

forhandlinglaredataSetsinpython、usenumpyArrays forbetterperformance.1)numpyarraysarememory-effictientandfasterfornumericaloperations.2)nusinnnnedarytypeconversions.3)レバレッジベクトル化は、測定済みのマネージメーシェイメージーウェイズデイタイです

inpython、listsusedynamicmemoryallocation with allocation、whilenumpyArraysalocatefixedmemory.1)listsallocatemorememorythanneededededinitivative.2)numpyArrayasallocateexactmemoryforements、rededicablebutlessflexibilityを提供します。

inpython、youcanspecthedatatypeyfelemeremodelernspant.1)usenpynernrump.1)usenpynerp.dloatp.ploatm64、フォーマーpreciscontrolatatypes。

numpyisessentialfornumericalcomputinginpythonduetoitsspeed、memory efficiency、andcomprehensivematicalfunctions.1)それは、performsoperations.2)numpyArraysaremoremory-efficientthanpythonlists.3)Itofderangeofmathematicaloperty

contiguousMemoryAllocationisucial forArraysは、ForeffienceAndfastelementAccess.1)iteenablesConstantTimeAccess、O(1)、DuetodirectAddresscalculation.2)itemprovesefficiencyByAllowingMultiblementFechesperCacheLine.3)itimplifieMememm

slicingapythonlistisdoneusingtheyntaxlist [start:stop:step] .hore'showitworks:1)startisthe indexofthefirstelementtoinclude.2)spotisthe indexofthefirmenttoeexclude.3)staptistheincrementbetbetinelements

numpyallows forvariousoperationsonarrays:1)basicarithmeticlikeaddition、減算、乗算、および分割; 2)AdvancedperationssuchasmatrixMultiplication;

Arraysinpython、特にnumpyandpandas、aresentialfordataanalysis、offeringspeedandeficiency.1)numpyarraysenable numpyarraysenable handling forlaredatasents andcomplexoperationslikemoverages.2)Pandasextendsnumpy'scapabivitieswithdataframesfortruc


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

Dreamweaver Mac版
ビジュアル Web 開発ツール

メモ帳++7.3.1
使いやすく無料のコードエディター

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

ホットトピック









