Monday, May 13, 2019

Canonical QA offsite meeting之Python 練習

5月初部門舉辦了一次offsite meeting,主題是學習Python。

內部同事兼講師,教材準備得很好,上課內容又豐富,所以當然要把回家作業完成。

Exercise 1

Write a function that takes a string and returns the inverted string (e.g. Taiwan → nawiaT). This function must use a for loop.

Exercise 2

Same as exercise 1, but using the easiest/shortest way (hint: the answer is somewhere in the previous sections.

Exercise 3

Dictionary containing all Ubuntu releases. The key is the release number (e.g. “18.04”) and the value is the release codename (e.g. “Bionic Beaver”):

  • Using Python code:return the release codenames for versions released in October
  • find how many releases were released neither in April nor October
  • return the version number for versions whose codename contains a “k”

Exercise 4

Write a function that reads the content of a text file and returns a dictionary with the number of occurrences for each letter of the alphabet

以下是我寫的code:

Exercise 1

def revert(word):
    for i in range(len(word)-1, -1, -1):
        print(word[i],end="")

revert("I love Taiwan")

Exercise 2

string="Taiwan"
print(string[::-1])

Exercise 3

Ubuntu={ "4.10": "Warty Warthog", "5.04": "Hoary Hedgehog", "5.10": "Breezy Badger", "6.06": "Dapper Drake", "6.10": "Edgy Eft", "7.04": "Feisty Fawn", "7.10": "Gutsy Gibbon", "8.04": "Hardy Heron", "8.10": "Intrepid Ibex", "9.04": "Jaunty Jackalope", "9.10": "Karmic Koala", "10.04": "Lucid Lynx", "10.10": "Maverick Meerkat", "11.04": "Natty Narwhal", "11.10": "Oneiric Ocelot", "12.04": "Precise Pangolin", "12.10": "Quantal Quetzal", "13.04": "Raring Ringtail", "13.10": "Saucy Salamander", "14.04": "Trusty Tahr", "14.10": "Utopic Unicorn", "15.04": "Vivid Vervet", "15.10": "Wily Werewolf", "16.04": "Xenial Xerus", "16.10": "Yakkety Yak", "17.04": "Zesty Zapus", "17.10": "Artful Aardvark", "18.04": "Bionic Beaver", "18.10": "Cosmic Cuttlefish", "19.04": "Disco Dingo" } for number in Ubuntu.keys(): if number.split(".")[1]=="10": print("OCT=",Ubuntu[number]) for number in Ubuntu.keys(): if number.split(".")[1]!="10" and number.split(".")[1]!="04": print("Neither in APR or OCT:",Ubuntu[number]) for codename in Ubuntu.values(): if "k" in codename.lower(): print("Codename contains a K or k:",codename)

Exercise 4

LIST=[]
for w in range(ord('a'), ord('z') + 1):
    LIST.append(chr(w))

ACCOUNT=[]
for i in range(0,26):
    ACCOUNT.append(0)

with open('/usr/share/doc/python3/copyright','r') as f:
    for alphabet in f.read():
        for i in range(0,26):
            if alphabet.lower()==LIST[i]:
                ACCOUNT[i]=ACCOUNT[i]+1
                break


dictionary=dict(zip(LIST,ACCOUNT))
print(dictionary)

老實說我有一段時間沒寫程式了,但稍微學習了一下Python後,它的語法很簡潔好懂,果然是受歡迎的程式語言之一呀!

No comments: