Python 入門指南 V2.00 - 單元 20 範例及練習程式碼



encrypt.py


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from random import shuffle
 
# 定義 Encrypt 類別
class Encrypt:
   # 建立 Encrypt 物件同時建立密碼表
   def __init__(self, str=None):
      # 設定 code
      if str == None:
         self.code = [chr(i) for i in
                      range(97, 123)]
         shuffle(self.code)
      else:
         self.code = list(str)
 
      # 設定 alph
      self.alph = [chr(i) for i in
                   range(97, 123)]
 
   # 回傳密碼表字串
   def __str__(self):
      code = "".join(self.code)
      return "code: " + code
 
   # 編碼的方法
   def toEncode(self, str):
      # 暫存編碼結果的字串
      result = ""
 
      # 利用迴圈走完參數字串的所有字元
      for i in str:
         # 判斷該字元是否為英文小寫字母
         # 若是英文小寫字母就進行編碼轉換
         if i in self.code:
            j = self.alph.index(i)
            result += self.code[j]
         else:
            result += i
 
      # 結束回傳編碼過的字串
      return result
 
   # 解碼的方法
   def toDecode(self, str):
      # 暫存解碼結果的字串
      result = ""
 
      # 利用迴圈走完參數字串的所有字元
      for i in str:
         # 判斷該字元是否為英文小寫字母
         # 若是英文小寫字母就進行解碼轉換
         if i in self.code:
            j = self.code.index(i)
            result += self.alph[j]
         else:
            result += i
 
      # 結束回傳解碼過的字串
      return result
 
# 測試部分
if __name__ == '__main__':
   e = Encrypt()
   print()
   print(e)
   s1 = "There is no spoon."
   print("Input : " + s1)
   s2 = e.toEncode(s1)
   print("Encode: " + s2)
   s3 = e.toDecode(s2)
   print("Decode: " + s3)
   print()
  
# 檔名: encrypt.py
# 作者: Kaiching Chang
# 時間: July, 2014

exercise2001.py


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from random import shuffle
 
class GuessGame:
   def __init__(self, digit=None):
      if digit == None or digit < 3 or digit > 6:
         self.length = 4
      else:
         self.length = digit
 
      self.set_game();
 
   def set_game(self):
      while True:
         self.answer = [chr(i) for i in range(48, 58)]
         shuffle(self.answer)
         self.answer = self.answer[0:4]
         if self.answer[0] != "0":
            break
       
      self.times = 0
      self.a = 0
      self.b = 0
 
if __name__ == "__main__"
   g = GuessGame(6)
   print(g.answer)
   g = GuessGame(5)
   print(g.answer)
   g = GuessGame(4)
   print(g.answer)
   g = GuessGame(3)
   print(g.answer)
  
# 檔名: exercise2001.py
# 作者: Kaiching Chang
# 時間: July, 2014

exercise2002.py


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from random import shuffle
 
class GuessGame:
   def __init__(self, digit=None):
      if digit == None or digit < 3 or digit > 6:
         self.length = 4
      else:
         self.length = digit
 
      self.set_game();
 
   def set_game(self):
      while True:
         self.answer = [chr(i) for i in range(48, 58)]
         shuffle(self.answer)
         self.answer = self.answer[0:self.length]
         if self.answer[0] != "0":
            break
       
      self.times = 0
      self.a = 0
      self.b = 0
 
if __name__ == "__main__"
   g = GuessGame(6)
   print(g.answer)
   g = GuessGame(5)
   print(g.answer)
   g = GuessGame(4)
   print(g.answer)
   g = GuessGame(3)
   print(g.answer)
  
# 檔名: exercise2002.py
# 作者: Kaiching Chang
# 時間: July, 2014

the end

沒有留言: