Bob is a lackadaisical teenager. In conversation, his responses are very limited.
Bob answers ‘Sure.’ if you ask him a question.
He answers ‘Whoa, chill out!’ if you yell at him.
He answers ‘Calm down, I know what I’m doing!’ if you yell a question at him.
He says ‘Fine. Be that way!’ if you address him without actually saying anything.
He answers ‘Whatever.’ to anything else.
Bob’s conversational partner is a purist when it comes to written communication and always follows normal rules regarding sentence punctuation in English.
解答
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
defmoduleBobdo defhey(input) do conddo String.trim(input) == "" -> "Fine. Be that way!" shouting?(input) && question?(input) -> "Calm down, I know what I'm doing!" shouting?(input) -> "Whoa, chill out!" question?(input) -> "Sure." true -> "Whatever." end end
Recite the lyrics to that beloved classic, that field-trip favorite: 99 Bottles of Beer on the Wall.
Note that not all verses are identical.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
99 bottles of beer on the wall, 99 bottles of beer. Take one down and pass it around, 98 bottles of beer on the wall.
98 bottles of beer on the wall, 98 bottles of beer. Take one down and pass it around, 97 bottles of beer on the wall.
97 bottles of beer on the wall, 97 bottles of beer. Take one down and pass it around, 96 bottles of beer on the wall. ... 2 bottles of beer on the wall, 2 bottles of beer. Take one down and pass it around, 1 bottle of beer on the wall.
1 bottle of beer on the wall, 1 bottle of beer. Take it down and pass it around, no more bottles of beer on the wall.
No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.
For bonus points
Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:
Remove as much duplication as you possibly can.
Optimize for readability, even if it means introducing duplication.
If you’ve removed all the duplication, do you have a lot of conditionals? Try replacing the conditionals with polymorphism, if it applies in this language. How readable is it?
Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it?
defmoduleBeerSongdo @doc""" Get a single verse of the beer song """ @spec verse(integer) :: String.t() defverse(0) do """ No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall. """ end
defverse(1) do """ 1 bottle of beer on the wall, 1 bottle of beer. Take it down and pass it around, no more bottles of beer on the wall. """ end
defverse(number) do """ #{number} bottles of beer on the wall, #{number} bottles of beer. Take one down and pass it around, #{number - 1} bottles of beer on the wall. """ end
@doc""" Get the entire beer song for a given range of numbers of bottles. """ @spec lyrics(Range.t()) :: String.t() deflyrics(default \\99..0) do default |> Enum.map(&verse/1) |> Enum.join("\n") end end
重點提示
Enum.map 的定義如下
map(enumerable, fun)
Returns a list where each element is the result of invoking fun on each corresponding element of enumerable.
For maps, the function expects a key-value tuple.
Range 是個 enumerable,所以我們可以使用 map 這個函式,針對裡面所有的元素都來執行 fun,這邊也是一個典型的利用自我遞迴來取代迴圈的例子喔