把一個Python程式改寫為Julia
Python
def f(m): if m < 3: return m k = 0 # remainders[ r ] is the smallest n, which is composed from at most # k digits (0, 1, 2) and which satisfies n % m == r remainders = {0:0} best = None while best == None: k += 1 for i in list(remainders.values()): for d in range(3): n = 10*i+d r = n % m if r not in remainders or n < remainders[ r ] : remainders[ r ] = n # Find the smallest integer n with at most 2*k digits composed # from the digits 0, 1 and 2 which satisfies n % m == 0. mult = (-pow(10, k, m))%m for r in remainders: u = r * mult % m #;print(u) if u in remainders: n = 10**k * remainders[ r ] + remainders[ u ] if best == None or n < best: if n != 0: best = n return best
Julia
function f(m) if m < 3 return m end k = 0 # remainders[ r ] is the smallest n, which is composed from at most # k digits (0, 1, 2) and which satisfies n % m == r remainders = Dict{Int,Int64}();remainders[0]=0 max64=typemax(Int64) best = max64 while best == max64 k += 1 rl=[i for (_,i) in remainders] for i in rl #remainders value list for d in 0:2 n = Int64(10)*i+d r = n % m if get(remainders,r,-1)==-1 || n < remainders[ r ] remainders[ r ] = n end end end # Find the smallest integer n with at most 2*k digits composed # from the digits 0, 1 and 2 which satisfies n % m == 0. mult = (m-powermod(Int64(10), k, m))%m for (r,_) in remainders u = r * mult % m if get(remainders,u,-1)>-1 n = Int128(10)^k * remainders[ r ] + remainders[ u ] if best == max64 || n < best if n != 0 best = n end end end end #for end #while return best end
程式碼行數增加了不少。主要是Julia需要end語句,還不支援None和value 語句,還有一個區別是取模運算的符號依賴於第一個運算元。所以,執行速度也變慢了。
PS:
在julia中也是有values的,還有一個keys
help?> values search: values SSAValue values(a::Associative) Return an iterator over all values in a collection. collect(values(a)) returns an array of values. Since the values are stored internally in a hash table, the order in which they are returned may vary. But keys(a) and values(a) both iterate a and return the elements in the same order. julia> a = Dict('a'=>2, 'b'=>3) Dict{Char,Int64} with 2 entries: 'b' => 3 'a' => 2 julia> collect(values(a)) 2-element Array{Int64,1}: 3 2 julia> collect(keys(a)) 2-element Array{Char,1}: 'b' 'a'
相關文章
- 把一個python程式改寫成JuliaPython
- 將使用yield的python程式改寫成JuliaPython
- Julia會成為下一個程式設計大語言嗎?程式設計
- 一個Julia編寫的求解線性模型的包模型
- 將一個前端專案改寫為chromo外掛(一)前端
- 一個巧合,我把文件寫進了程式碼裡
- 一個julia語言部落格
- Python使用Socket寫一個簡單聊天程式Python
- 幽默:把Java寫成Python風格的程式碼JavaPython
- 一個新手為老程式碼寫測試程式的心得
- 碾壓Python!為什麼Julia速度這麼快?Python
- Google 為什麼把幾十億行程式碼放在一個庫Go行程
- Julia:調查顯示76% 的 Julia 使用者將 Python 作為首選替代語言Python
- 用 Python 編寫一個國際象棋 AI 程式PythonAI
- 使用PyQt來編寫第一個Python GUI程式QTPythonGUI
- 用python為喜歡的人寫一個程式,每天傳送貼心的訊息Python
- Python的四個挑戰者:Swift、Go、Julia、RPythonSwiftGo
- 如何讓 Python 像 Julia 一樣快地執行Python
- 把一個JVM嵌入到本地程式中JVM
- 手把手教你把Vim改裝成一個IDE程式設計環境(圖文)IDE程式設計
- Julia加入TPU,這是一個靠自己也要融入機器學習的程式語言機器學習
- 把程式碼寫在照片裡
- 寫的一個perl程式
- 為什麼要學習 Julia
- 怎樣把自己培養成為一個優秀的程式設計師程式設計師
- 七個不一樣的Python程式碼寫法,讓你寫出一手漂亮的程式碼Python
- 使用 Python 把多個 MP4 合成一個視訊Python
- Python新手教程:40行python程式碼寫一個桌面翻譯器Python
- 一條SQL的改寫SQL
- 50行Python程式碼寫一個語言檢測器Python
- 為什麼Julia比Python快?因為天生理念就更先進啊Python
- Julia語言程式基礎
- Laravel 把傳參的分頁 url 改為 /page/11Laravel
- mpvue寫一個CPASS小程式Vue
- 編寫第一個Qt程式QT
- MIT正式釋出程式語言Julia 1.0:Python、R、C++三合一MITPythonC++
- julia與python類似之處Python
- 如何把SQL Server中一個表,一個儲存過程,一個檢視等改為系統表,系統儲存過程,系統檢視等...SQLServer儲存過程