1 Environments

1.1 Language

Python3.6

1.2 how to run

make sure you've install the python3.6. then you can just type the following commands

python solution1.py

2 Questions

2.1 第一題

import unittest


def reverse_str(value):
    return value[::-1]


def reverse_words(words):
    return ' '.join([word[::-1] for word in words.split(' ')])


class TestCase(unittest.TestCase):
    def test_reverse_str(self):
        value = 'abc'
        expected = 'cba'

        self.assertEqual(reverse_str(value), expected)

    def test_reverse_words(self):
        words = "flipped class room is important"
        expected = "deppilf ssalc moor si tnatropmi"

        self.assertEqual(reverse_words(words), expected)


if __name__ == '__main__':
    unittest.main()

2.2 第二題

import unittest


def remove_three_and_five_multiple_without_common(num):
    if num < 1:
        raise ValueError("invalid input")
    return len(
        tuple(
            filter(lambda x: not ((x % 3 == 0 or x % 5 == 0) and x % 15 != 0),
                   range(1, num + 1))))


class TestCase(unittest.TestCase):
    def test_remove_three_and_five_multiple_without_common(self):
        num = 15
        expected = 9

        self.assertEqual(remove_three_and_five_multiple_without_common(num),
                         expected)

    def test_edge_case(self):
        num = 1
        expected = 1

        self.assertEqual(remove_three_and_five_multiple_without_common(num),
                         expected)

    def test_invalid_case(self):
        num = 0

        with self.assertRaises(ValueError):
            remove_three_and_five_multiple_without_common(num)


if __name__ == '__main__':
    unittest.main()

2.3 第三題

基本上可以把題目想成用 truth table 來表達,今天看到三的袋子各有標籤,如下 然後我們可以來個排列組合一下實際袋子情況內容,來各字填上對或錯,再來繼續推論題目

實際情況 只裝鉛筆 只裝原子筆 混合
鉛原混 F F F
鉛混原 F T T
原混鉛 T T T
原鉛混 T T F
混鉛原 T T T
混原鉛 T F T

實際只有這六種可能性(3!),那今天能夠符合題目所敘述的標籤全貼錯的狀況,只有表中我全為true的情況(true代表有符合標籤是錯的情況, false代表,標籤是對的,那就不符合題目要求)

兩種情況我挑選出來是下面這樣

  • 原混鉛
  • 混鉛原

基本上答案就是這樣了,當然也可以將這狀況來比對看看

那我們今天從混和的去挑,因為他標籤是錯的,所以代表他一定是只有鉛筆或原子筆,今天假設是鉛筆,那就是符合 原混鉛 這狀況,因為指裝原子筆只剩下混合的可能性了,要不然他標籤會是對的,無法符合題目敘述!

另外一個允許的狀況也可以套套看,一樣的意思最後都會符合題目的敘述。

2.4 第四題

今天可以將題目變成數學表達式

三人出的錢 = 餐點的錢 + 服務生暗摃的錢

900 = 900 + 0 => 這是一開始的

900 = 750 + 150 => 服務生發現可以退費150

900 = 750 + 60 + 90 => 服務生想要暗槓 60

900 - 90 = 750 + 60

3*(300-30) = 750 + 60

810 = 750 + 60 (90元已經被移出去這個等式了,等量公理兩邊相減)

所以今天題目敘述詭異的地方是, 270*3 卻又去加 60 想要等於 900 這段,因為今天出出去的錢總數已經變成 810元了。所以拿810去加60有點意義不明,是故意要錯誤引導。