你的程式碼聞起來很臭嗎?不用擔心!大夫幫你...越治越臭

Exercism(Elixir從零開始系列 08)(鼠年全馬鐵人挑戰 W09)

這是 w3HexSchool 鼠年全馬鐵人挑戰 Week 9 唷

這是 Elixir 從零開始 系列 08 唷

前言

我們零零總總也「看」好多篇文章,現在是時候來動手做做看了!Exercism 是一個程式碼練習的網站,裡面有很多程式語言可以選擇,每個語言都有很多的練習題可以練習,更讓人興奮的是呢,我們可以上傳我們的程式碼,讓線上導師給予評論,或者我們也可以看到世界各地的菁英上傳的答案喔,透過線上導師的提醒或參考別人的程式碼,可以讓自己的實力更上一層樓喔,讓我們透過真正實作,把之前講的都化為應用上來吧!

Exercism installation

讓我們一步一步來做

  1. 我們進入網站 Exercism

  2. 建立 Exercism 的帳號,Exercism 提供直接用 Github 作為登錄帳號,或是你可以建立一組新的帳號密碼

  3. 點選程式語言 Elixir,第一個練習題將會是很熟悉的 Hello World

  4. 開始之前呢,我們要先確定我們的電腦裡面有 Homebrew,這是一個 Mac 電腦上面的套件管理程式

    對的沒錯,筆者的練習平台是用 Mac 電腦唷

  5. 透過 brew update && brew install exercism 來安裝 exercism cli,這個工具的功能就是負責幫你下載題目到你的電腦上以及把寫好的程式碼上傳到 Exercism

  6. 測試指令 exercism version ,來測試一下安裝是否成功

  7. exercism cli 與您的Exercism 的帳號綁定,讓他們做一個關聯,我們輸入 exercism configure --token=your-token-key,這邊的 your-token-key 可以到網站的 setting page 裡面去找找

  8. 回到練習題 Hello World 的頁面

  9. 頁面右上角的 Begin walk-through 其實也有寫到前面幾點的步驟,偷偷跟你們講其實呢不一定要 Mac,Windows 和 Linux 也可以跑啦

  10. 頁面右手邊有個 Download 連結,複製貼上並在終端機執行,這個連結是用來下載題目,使用編輯器去編輯下載好的檔案,寫出符合題目的答案出來!

  11. 頁面右手邊還有個 Submit 的連結,複製貼上並在終端機執行,這個連結是用來上傳答案的,可以選擇上傳一個檔案或是多個檔案,上傳好之後呢到網站看一下,重新讀取一下,很快地就會看到你的答案被上傳了啦

  12. 完成!

我們今天來做個幾題測試測試,讓大家上手一下喔!

Ex1: Hello World

題目

The classical introductory exercise. Just say “Hello, World!”.

“Hello, World!” is the traditional first program for beginning programming in a new language or environment.

The objectives are simple:

  • Write a function that returns the string “Hello, World!”.
  • Run the test suite and make sure that it succeeds.
  • Submit your solution and check it at the website.

If everything goes well, you will be ready to fetch your first real exercise.

解答

這一題要我們寫一個函式,功能是印出 Hello World 出來

1
2
3
4
5
6
7
8
defmodule HelloWorld do

@spec hello() :: String.t
@spec hello(String.t) :: String.t
def hello(name \\ "World") do
"Hello, #{name}!"
end
end

重點提示

  1. 反斜線 \\ 代表的是函式的預設值(可以參考 Function & Module
  2. 如果要在字串中使用變數要使用 #{} 來包含,比如說 #{1 + 1} 得到的字串會回傳 2,#{name} 會得到變數name的值(可以參考 基本資料型別與運算

Ex2: RNA Transcription

題目

Given a DNA strand, return its RNA complement (per RNA transcription).

Both DNA and RNA strands are a sequence of nucleotides.

The four nucleotides found in DNA are adenine (A), cytosine (C), guanine (G) and thymine (T).

The four nucleotides found in RNA are adenine (A), cytosine (C), guanine (G) and uracil (U).

Given a DNA strand, its transcribed RNA strand is formed by replacing each nucleotide with its complement:

  • G -> C
  • C -> G
  • T -> A
  • A -> U

解答

第二個範例是要做字元的交換

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
defmodule RnaTranscription do
@spec to_rna([char]) :: [char]
def to_rna(dna) do
Enum.map(dna, fn (x) -> change_word(x) end)
end

def change_word(input) do
case input do
?G -> ?C
?C -> ?G
?T -> ?A
?A -> ?U
_ -> input
end
end
end

重點提示

  1. _ 代表是所有其它條件都會進入
  2. ?G 這個問號是一個 construct ,功能為轉換字元成 integer code point(可以參考 1)
  3. Enum.map 將每一個元素都用後面的函式執行過一次並回填其值(可以參考 3)
  4. fn (x) -> change_word(x) end 這是一個匿名函式(可以參考 Function & Module

以上兩題小試身手,透過寫題庫的方式讓自己熟練語法,有沒有回到高中的感覺呢,讓大家熟悉熟悉一下,未來會有更多的練習題分享大家喔

參考資料

  1. https://hexdocs.pm/elixir/String.html

  2. https://exercism.io/

  3. https://hexdocs.pm/elixir/Enum.html#map/2