Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. ### 1. 请填写下列Python代码运行后, b3和b4的结果
  2.  
  3. ```python
  4. b1 = [1, 2, 3]
  5. b2 = [2, 3, 4]
  6. b3 = [val for val in b1 if val in b2]
  7. b4 = [val for val in b1 if val not in b2]
  8. b3 = [2, 3] # 求交集
  9. b4 = [1] # 求差集
  10. ```
  11.  
  12. ### 2. 假设 condition = True 或 False
  13.  
  14. 以下两条语句是否等价 ?
  15.  
  16. ```python
  17. result = 1 if condition else 0
  18. result = condition and 1 or 0
  19. ```
  20. 什么情况下不等价 ?
  21.  
  22. 以所给的例子来说是等价的,但需要注意如下情况,
  23. ```python
  24. result = 0 if condition else 1
  25. result = condition and 0 or 1
  26. ```
  27. 此时不等价,第二个三元表达式返回的永远是1
  28.  
  29.  
  30. ### 3. 类
  31.  
  32. ```python
  33. class Foo(object):
  34. def __init__(self):
  35. pass
  36. ```
  37.  
  38. 对于 `class FooExt(Foo)`, 请比较以下两种构造函数写法的异同
  39.  
  40. ```python
  41. def __init__(self):
  42. Foo.__init__(self)
  43.  
  44. def __init__(self):
  45. super(FooExt, self).__init__()
  46. ```
  47. 对于单继承没有太大的区别,对于多重继承,super涉及mro(顺序查找),调用继承的第一个父类的初始化方法
  48.  
  49.  
  50. ### 4. 请实现以下功能
  51.  
  52. 读入一个文本文件,统计并输出其中出现频率最高的10个英文单词及其出现的频次。文本文件中均为英文单词,单词之间通过一个,或.分割。
  53. ```python
  54. import re
  55. from collections import Counter
  56. content = open('workspace/outsourcing/models.py').read() # 读入文件内容
  57. words = re.split(',|\.', content) # 按,和.分词
  58. counter = Counter(words) # 计数
  59. for word, count in sorted(counter.items(), key=lambda (x, y): y, reverse=True)[:10]: # 倒序并取前十个元素
  60. print word, count
  61. ```
  62.  
  63.  
  64. ### 5. 请实现一个函数,判断一个字符串中的括号是否匹配。
  65.  
  66. 给出一个字符串,其中只包含括号(大中小括号“()[]{}”),括号可以任意嵌套。如果同样的左右括号成对出现并且嵌套正确,那么认为它是匹配的。例如:
  67.  
  68. ```
  69. () -> TRUE (匹配)
  70. [()] -> TRUE (匹配,括号可以嵌套)
  71. ()() -> TRUE (匹配,括号可以并列排列)
  72. ({}([])) -> TRUE (匹配,括号可以任意嵌套,大括号不必在外)
  73. ) -> FALSE (不匹配,缺少左括号)
  74. (} -> FALSE (不匹配,左右括号不一样)
  75. {)(} -> FALSE (不匹配,左右括号相反)
  76. ```
  77. list具备栈的功能,对([{进行入栈,遇到)]}时,检查栈顶元素是否匹配,以及完成最后list是否为空
  78. ```python
  79. def check(content):
  80. lst = []
  81. for i in content:
  82. if i in ['(', '[', '{']:
  83. lst.append(i)
  84. else:
  85. if i == ')':
  86. if not lst[-1] == '(':
  87. return False
  88. elif i == ']':
  89. if not lst[-1] == '[':
  90. return False
  91. elif i == '}':
  92. if not lst[-1] == '{':
  93. return False
  94. lst.pop()
  95. if not lst:
  96. return True
  97. return False
  98. ```
  99.  
  100. ## 6. Microsoft Excel use letters to denote column labels. Write a function to convert that from and to numerical representation.
  101.  
  102. Example
  103.  
  104. ```
  105. input: A
  106. output: 1
  107.  
  108. input: Z
  109. output: 26
  110.  
  111. input: AA
  112. output: 27
  113. ```
  114.  
  115. ```python
  116. letter2num_dict = dict(zip(map(chr, range(65, 91)), range(0, 26))) #对应成字典
  117. def to_num(label):
  118. num = 0
  119. for i, letter in enumerate(label[::-1]): # 倒序遍历
  120. num += 26**i+letter2num_dict.get(letter) # 26进制
  121. return num
  122. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement