每週挑戰 303:Python 和 Perl 解決方案
Mohammad S. Anwar 的每週挑戰提供了定期的編碼練習。 下面介紹的我的解決方案最初是用 Python 編寫的,然後適用於 Perl。 這種雙重方法提高了編碼能力。
挑戰 303:解決方案
任務 1:產生偶數 3 位元整數
任務描述:
給定一個正整數列表,產生可以使用列表中的數字組成的所有唯一偶數 3 位整數。
Python 解:
此 Python 解決方案利用 itertools.permutations
函數有效產生所有可能的 3 位數組合。 集合用於保持唯一性。
from itertools import permutations def three_digits_even(ints: list) -> list: solution = set() for p in permutations(ints, 3): num_str = "".join(map(str, p)) num = int(num_str) if num >= 100 and num % 2 == 0 and num_str[0] != '0': solution.add(num) return sorted(list(solution))
Perl 解:
Perl 等效項使用 Algorithm::Permute
模組進行排列和雜湊以確保唯一性。
use Algorithm::Permute; sub three_digits_even { my @ints = @_; my %seen; my @result; my $p = Algorithm::Permute->new(\@ints, 3); while (my @perm = $p->next) { my $num_str = join('', @perm); my $num = $num_str; if ($num >= 100 and $num % 2 == 0 and $num_str !~ /^0/) { push @result, $num unless $seen{$num}++; } } return sort {$a <=> $b} @result; }
範例:
<code># Python print(three_digits_even([2, 1, 3, 0])) # Output: [102, 120, 130, 132, 210, 230, 302, 310, 312, 320] print(three_digits_even([2, 2, 8, 8, 2])) # Output: [222, 228, 282, 288, 822, 828, 882] # Perl print "@{[three_digits_even(2, 1, 3, 0)]}\n"; # Output: 102 120 130 132 210 230 302 310 312 320 print "@{[three_digits_even(2, 2, 8, 8, 2)]}\n"; # Output: 222 228 282 288 822 828 882</code>
任務 2:刪除並賺取
任務描述:
給定一個整數數組,透過重複刪除一個元素,獲得其值,然後刪除所有值比被刪除元素少一和多一的元素,找到你可以獲得的最大分數。
Python 解:
此 Python 解決方案使用 Counter
來追蹤元素頻率,並採用遞歸函數來探索不同的刪除策略。
from collections import Counter def delete_and_earn(ints: list) -> int: freq = Counter(ints) return max_score(freq) def max_score(freq: Counter) -> int: max_points = 0 for num in list(freq): # Iterate through a copy to safely delete points = num * freq[num] new_freq = freq.copy() del new_freq[num] if num - 1 in new_freq: del new_freq[num - 1] if num + 1 in new_freq: del new_freq[num + 1] max_points = max(max_points, points + (0 if not new_freq else max_score(new_freq))) return max_points
Perl 解:
Perl 解反映了 Python 方法,使用雜湊進行頻率計數和遞歸函數。
sub delete_and_earn { my %freq = map { $_ => 1 + $freq{$_} // 0 } @_; return max_score(\%freq); } sub max_score { my $freq = shift; my $max_points = 0; foreach my $num (keys %$freq) { my $points = $num * $freq->{$num}; my %new_freq = %$freq; delete $new_freq{$num}; delete $new_freq{$num - 1}; delete $new_freq{$num + 1}; $max_points = max($max_points, $points + (0 || max_score(\%new_freq))); } return $max_points; } sub max { return shift if @_ == 1; return $_[0] > $_[1] ? $_[0] : $_[1]; }
範例:
<code># Python print(delete_and_earn([3, 4, 2])) # Output: 6 print(delete_and_earn([2, 2, 3, 3, 3, 4])) # Output: 9 # Perl print delete_and_earn(3, 4, 2), "\n"; # Output: 6 print delete_and_earn(2, 2, 3, 3, 3, 4), "\n"; # Output: 9</code>
這些解決方案展示了解決每週挑戰 303 中這兩項任務的高效且清晰的方法。 Python 和 Perl 的使用凸顯了演算法問題解決在不同程式語言之間的可移植性。
以上是用 igt 賺錢的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。Python以简洁和强大的生态系统著称,C 则以高性能和底层控制能力闻名。

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Python3.6環境下加載Pickle文件報錯:ModuleNotFoundError:Nomodulenamed...

如何解決jieba分詞在景區評論分析中的問題?當我們在進行景區評論分析時,往往會使用jieba分詞工具來處理文�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3漢化版
中文版,非常好用

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能