#coding=gbk

'1. 已知字符串 a = "aAsmr3idd4bgs7Dlsf9eAF",要求如下'

a = "aAsmr3idd4bgs7Dlsf9eAF"


def test11():

   '1.1 请将a字符串的大写改为小写,小写改为大写。'

   print a.swapcase()


def  test12():

   '1.2 请将a字符串的数字取出,并输出成一个新的字符串。'

   b = [ x for x in a if x.isdigit()]

   print ''.join(b)


def  test13():

   '1.3 请统计a字符串出现的每个字母的出现次数(忽略大小写,a与A是同一个字母),\

并输出成一个字典。 例 {"a":4,"b":2}'

   b = a.lower()

   print dict([ (x,b.count(x) ) for x in set(b) ] )


def  test14():

   '1.4 请去除a字符串多次出现的字母,仅留最先出现的一个。例 "abcabb",经过去除后,输出 "abc"'

   a_list = list(a)

   set_list = list(set(a_list))

   set_list.sort(key=a_list.index)

   return ''.join(set_list)


def test15():

   '1.5 请将a字符串反转并输出。例:"abc"的反转是"cba"'

   b = test14()

   print b[-1::-1]


def test16():

   '1.6 去除a字符串内的数字后,请将该字符串里的单词重新排序(a-z),并且重新输\

   出一个排序后的字符 串。(保留大小写,a与A的顺序关系为:A在a前面。例:AaBb)'

   b = sorted(a)

   a_lower_list = []

   a_upper_list = []

   for x in a:

       if x.islower():

            a_lower_list.append(x)

       elif x.isupper():

            a_upper_list.append(x)

       else:

           pass

   for y in a_upper_list:

       y_lower = y.lower()

       if y_lower in a_lower_list:

           a_lower_list.insert(a_lower_list.index(y_lower),y)

   print ''.join(a_lower_list)


def test17():

   '1.7 请判断 "boy"里出现的每一个字母,是否都出现在a字符串里。如果出现,则输出True,否则,则输 出False.'

   #方法1


   for x in 'boy':

       if x in a:

           print True

       else:

           print False


   #方法2

   search = 'boy'

   u = set(a)

   u.update(list(search))

   print len(set(a)) == len(u)


def test18():

   '1.8 要求如1.7,此时的单词判断,由"boy"改为四个,分别是 "boy","girl","bird","dirty",\

请判断如上这4个字符串里的每个字母,是否都出现在a字符串里。'

  #方法1

   search = [ 'boy','girl','bird','dirty' ]

   for x in search:

       if x in set(a):

           print True

       else:

           print False



   #方法2

   search = [ 'boy','girl','bird','dirty' ]

   b  = set(a)

   for x in search:

       b.update(list(x))

   print len(b)  == len(set(a))


def test19():

   '1.9 输出a字符串出现频率最高的字母'

   l = ([(x,a.count(x) ) for x in set(a) ])

   l.sort(key = lambda x:x[1],reverse=True)

   print l[0][0]


def test20():

   '2.在python命令行里,输入import this 以后出现的文档,统计该文档中,"be" "is" "than" 的出现次数。'

   import os

   this = os.popen('python -m this').read()

   this = this.replace('\n','')

   l_this = this.split(' ')

   print [ (x,l_this.count(x)) for x in 'be','is','than' ]


def test30():

   '3.一文件的字节数为 102324123499123,请计算该文件按照kb与mb计算得到的大小。'

   size = 102324123499123

   print 'Size = %skb' % ( size >> 10 )

   print 'Size = %smb' % ( size >> 20 )


def test40():

   '4.已知  a =  [1,2,3,6,8,9,10,14,17],请将该list转换为字符串,例如 "123689101417"'

   a =  [1,2,3,6,8,9,10,14,17]

   print str(a)[1:-1].replace(', ','' )


if  __name__ == '__main__':

   test11()

   test12()

   test13()

   test14()

   test15()

   test16()

   test17()

   test18()

   test19()

   test20()

   test30()

   test40()