博文

目前显示的是 九月, 2017的博文

INF551 HW2

STATS.PY import sys,copy,json jFile=json.load(open(sys.argv[1])) sumOfHow,sumOfHowMany,sumOfHowMuch,sumOfWhat,sumOfWhen,sumOfWhere,sumOfWhich,sumOfWho,sumOfWhom=0,0,0,0,0,0,0,0,0 # index01=0 # while index01<len(jFile['data']): #     for answer in jFile['data'][index]['paragraphs'][] # #     index01+=1 for cataloge in jFile['data']:     for contextQas in cataloge['paragraphs']:         for qas in contextQas['qas']:             qas['question'].strip()             if qas['question'].startswith('How many') or qas['question'].startswith('how many'):                 sumOfHowMany+=1             elif qas['question'].startswith('How much') or qas['question'].startswit...

412. Fizz Buzz

class Solution(object):     def fizzBuzz(self, n):         """         :type n: int         :rtype: List[str]         """         i=1         y=list()         while i<=n:             y.append(i)             if y[i-1]%15==0:                 y[i-1]='FizzBuzz'             elif y[i-1]%3==0:                 y[i-1]='Fizz'             elif y[i-1]%5==0:                 y[i-1]='Buzz'             i+=1         return map(str,y)                               又秒了一题,注意range...

463. Island Perimeter

class Solution(object):     def islandPerimeter(self, grid):         """         :type grid: List[List[int]]         :rtype: int         """         closeLine=0         n=0         sum1=0         while n<len(grid):             k=0             while k<len(grid[n]):                 if grid[n][k]==1:                     sum1+=4                     if k>0 and grid[n][k]==grid[n][k-1]:                         closeLine+=2                 k+=1             q=0     ...

575. Distribute Candies

哈哈终于体会到秒题的快感了!不过这道题也就是小学奥数题的难度。 class Solution(object):     def distributeCandies(self, candies):         """         :type candies: List[int]         :rtype: int         """         b=len(set(candies))         return min(b,len(candies)/2)       只打败了19%的人2333. 注意: 计算set和list元素个数请使用len()函数,List.count是用来接收一个特定参数求list里面这个参数出现次数的函数。 class Solution (object) : def distributeCandies (self, candies) : """ :type candies: List[int] :rtype: int """ kind = set(candies) if len(kind) > len(candies)/ 2 : return len(candies)/ 2 else : return len(kind) 这是最快的答案,感觉差不多么……但是速度是我的两倍

557. Reverse Words in a String III

第一版: class Solution(object):     def reverseWords(self, s):         """         :type s: str         :rtype: str         """         result=s.split(" ")         output=list()         for n in result:             output.append(n[::-1])         return " ".join(output) 第二版: class Solution(object):     def reverseWords(self, s):         """         :type s: str         :rtype: str         """         result=s.split(" ")         j=0         for n in result:             result[j]=n[::-1]             j+=1         return " ".join(result) 第二版的速度快了...

Python Tips, Sept-Oct, 17

待解决:工厂函数?生成器?闭包?类属性?动态语言?强弱类型? 1.3.x 里面,两个int除法返回一个float,2.x里面是正常情况。 2.深拷贝是完全创造了一个新的对象,浅拷贝创造了一个新对象,但是新对象里面的子对象还是指向原来子对象。 http://blog.csdn.net/u014647208/article/details/77683545 3.set和dict的唯一区别仅在于没有存储对应的value,但是,set的原理和dict一样,所以,同样不可以放入可变对象,因为无法判断两个可变对象是否相等,也就无法保证set内部“不会有重复元素”。而且set和dict的key的子元素(子元素的子元素的子元素的子元素的……也不行,即化简之后的key必须为不可变元素)也必须为不可变的 4.pass可以啥也不做,可以加在任何地方: pass 还可以用在其他语句里,比如: if age >= 18 : pass 缺少了 pass ,代码运行就会有语法错误。 5.bar = [] if bar is None else bar 三元表达式,可以用于函数设置默认参数,注意,函数的默认参数在函数声明的时候就被初始化了。 6.关于声明函数的默认参数,可以参见如下代码: INPUT: def test(a=[]):     a.append(":-)")     return a q=test() print("test函数第一次在没有给定参数被调用时",q,id(q)) w=test([1]) e=test() r=test([2]) t=test() print("test函数在没有给定参数被调用三次后",q,id(q),'\n',w,id(w),'\n',e,id(e),'\n',r,id(r),'\n',t,id(t),'\n') OUTPUT: test函数第一次在没有给定参数被调用时 [':-)'] 54701312 test函数在没有给定参数被调用三次后 [':-)...

500. Keyboard Row

python只在数字类型之间有隐式转换,不能隐式的从数字转到string。 str1.find(str2),找到就返回位置,否则返回-1 class Solution(object):     def findWords(self, words):         """         :type words: List[str]         :rtype: List[str]         """         a='qwertyuiopQWERTYUIIOP'         b='asdfghjklASDFGHJKL'         c='zxcvbnmZXCVBNM'         test=list()         outcome=list()         n=0         ifDel=False         for word in words:             j=0             for alf in word:                 if alf in a:                     test.append('a')                    ...