iT邦幫忙

2021 iThome 鐵人賽

DAY 14
0
自我挑戰組

從真「新」鎮出發!30天的刷題修行篇,讓寫程式成為新必殺技系列 第 15

歡迎進入 ip 的世界,Ruby 30 天刷題修行篇第十五話

嗨,我是 A Fei,來看看今天的題目:
(題目來源:Codewars


Take the following IPv4 address: 128.32.10.1 This address has 4 octets where each octet is a single byte (or 8 bits).

1st octet 128 has the binary representation: 10000000
2nd octet 32 has the binary representation: 00100000
3rd octet 10 has the binary representation: 00001010
4th octet 1 has the binary representation: 00000001
So 128.32.10.1 == 10000000.00100000.00001010.00000001

Because the above IP address has 32 bits, we can represent it as the 32 bit number: 2149583361.

Write a function ip_to_int32(ip) ( JS: ipToInt32(ip) ) that takes an IPv4 address and returns a 32 bit number.


題目要我們將 IPv4 address 每段轉換成二進位(要補 0),並且轉換成 32 bit number。嗯,前半段轉換成二進位還可以理解,但後面轉換成 32 bit number 真的沒概念,只能呼叫 Google 大神現身說法,首先我們到 wiki 找 IPv4,其中在 「位址格式」 一節中寫道:

「IPv4 位址可被寫作任何表示一個 ** 32 位元整數值** 的形式,但為了方便人類閱讀和分析,它通常被寫作點分十進位的形式,即四個位元組被分開用十進位寫出,中間用點分隔。」

嗯...好像有點頭緒了,但是要怎麼把範例中的 10000000.00100000.00001010.00000001 轉成 2149583361。可能 32 位元太大了,我們試著以 2 位元思考,在電腦的世界中,1 個位元會有 0 和 1 兩種狀態,所以 2 位元最小值是 00,最大的值是 11,可以儲存的整數範圍就是 0 到 3,這裡可以稍微看出計算的規律,即 1 x 2^1 + 1 x 2^0 = 3,換句話說,32 位元整數換算的公式就是 32th digit number X 2^31 + 31th digit number X 2^30 ...,這樣就可以算出答案了~

以下是我的解答:

def ip_to_int32(ip)
  ip_binary = ip.split(".").map { |n| n.to_i.to_s(2).rjust(8, '0') }
  bits_arr = ip_binary.join("").split("")
  arr = []
  31.downto(0) {|n| arr.push(bits_arr[31 - n].to_i * (2 ** n))}
  arr.sum
end

對比評價最高的解答:

require 'ipaddr'

def ip_to_int32(ip)
  x = IPAddr.new(ip).to_i
end

在 Codewars 上原來可以使用 lib,我還是第一次看到!IPAddr 看名字就知道是和 ip 有關的類別,趕快去查一下手冊,其中 to_i 方法寫道:「Returns the integer representation of the ipaddr」。嗯,算是又學到了一招呢。

不使用 lib 的解法:

def ip_to_int32(ip)
  ip.split( '.' ).reduce( 0 ) { |total, p| total * 256 + p.to_i }
end

以上就是今天的解題紀錄,大家掰掰


上一篇
好想中樂透啊,Ruby 30 天刷題修行篇第十四話
下一篇
誰喜歡這則貼文,初探 case...when 用法,Ruby 30 天刷題修行篇第十六話
系列文
從真「新」鎮出發!30天的刷題修行篇,讓寫程式成為新必殺技16
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言