首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

CUCS Project 小贴士

编写者

孙振豪

ICS 31

1. Project Tips:

7.12

从题目中我们可以知道a jiffy = 1/100 second, 所以我们能得到一个 100 jiffies = 1 second的一个公式。 我们的input 是seconds 输出要jiffies 所以只是一个单位转换的问题所以我们可以直接输出例如input n *100(比例)就是我们要的output了

7.13

这个function input 是一个年份 因为有两个判断标准所以我们要判断他是不是century year 判断的标准就是用input 比如说 Y%100 看是不是=0如果是的话就说明是century year 然后如果是century year我们就需要用标准400 来判断他是不是leap year y%400 如果=0的话那么就是然后得出相应的output 至于不是century year 的话我们就用 y%4 来判断是不是leap year

7.14

这个题我们要swap 两个input integer 我们在function 里面只需要定义一个中间量 比如 c,

我们可以先让c等于a的值 这样a的值就存起来了, 所以我们再让a=b这样就完成了一半的swap然后 b的值怎么办呢 我们可以让b=c这样就完成了a,b两个input的互换。 我们可以同时return 出a,b但是这个时候要注意 return 出来的是一个tuple index它的值得到a,b

7.15

这个题我们其实可以分成两个部分一个是input number 0的时候。 让number 0的时候我们 先判断number 的数量。 因为我们要用最少的coins 所以要先判断最大的面值也就是dollars 从example 中我们能判断出1 dollar相当于数字100 然后依次往下判断当number>一个面值的时候记录那个相应面值的数量然后从input中减去相应的数量然后再判断判断到number=0为止。至于最后output的要求就判断下面值对应数量是>1还是=1. 如果>1就复数就好啦。整体可以用一个while loop +if 判断number 然后判断出来立即输出结果+\n

7.16

我们这个function 可以先读一个数字,就是我们要loop的次数,也就是进来的几个int。然后,我们可以在最开始设置一个state ,也就是记录是odd 还是even 。当第一遍读的时候就把state 变成even 或者odd ,然后当后面有出入的时候立马就结束loop, 然后打出neither 的message 。至于到最后如果全是even 或者odd的,那就在function的最后打出state 的状态就好了。body 是一个for loop 里面加上if 来判定 每一个数字的状态

7.17

这个题首先从input里面得到一个数字我们可以name 成n ,然后我们把后面读到的东西都放在一个list 里面,然后当读取到最后一个数字的时候再loop这个list里面的数字来看是不是小于等于它的,如果是就output

ICS 32A

1. Project Tips:

在这个project中,首先我们要安装python-twitter,按照官方的说明即可:https://python-twitter.readthedocs.io/en/latest/installation.html

然后我们还需要申请Twitter的developer,需要去https://apps.twitter.com 申请成功后创建一个新的application并记录两个API keys和两个tokens。之后要先创建一个python-twitter的api instance:api = twitter.Api(consumer_key='consumer_key',consumer_secret='consumer_secret', access_token_key='access_token', access_token_secret=‘access_token_secret’), 接下来就可以正式开始了。

第一个def filterTimeline(keyword): 想要获取自己的timeline可以使用api.GetUserTimeline()。需要注意的是,它的返回值是list of status,每个Status可以用status.AsDict()转换成dict,这里转换完的dict如果看起来比较混乱的话可以在https://jsonformatter.curiousconcept.com 上进行formatter,然后就能很简单的找到text的位置。最后,可以如果text里不包含keyword就可以把它放到一个list里。

第二个def commonWord(username): 还是用api.GetUserTimeline()只不过parameter里要写screen_name(注意不是user_id)。这里的count有一个200的限制,所以还要进行循环。像上一题一样拿到所有的text后可以开始进行搜索,把所有除掉common words的词加入一个dict,value是出现次数(或者是list of tuple,这样搜索最大值时简单一些)。

第三个def searchArea(keyword, location): 这里需要使用的是api. GetSearch()。parameter里需要有term和geocode,geocode里注意要加上5miles的限制。除此之外还有一个七天的限制,需要用到python的datetime:

import datetime

然后使用datetime.datetime.now().year可以得到现在的年份,月份和日期可以稍加修改得到。最后再转换成他需要的YYYY-MM-DD形式。

其他参考:

python-twitter API document: https://python-twitter.readthedocs.io/en/latest/twitter.html#module-twitter.api (比较重要,建议仔细看看,主要是GetUserTimeline和GetSearch)

Alex的note:https://www.ics.uci.edu/~thornton/ics32a/Notes/WebAPIs/ (不过这个project并不需要我们自己parse url或者load json,python-twitter已经帮我们都做好了)。

编写者

江啸

编写者

朱德明

ICS 33

1. 本周Lecture note总结:

类的继承Inheritance

继承作为面向对象编程中一种重要的工具,提供了类在不含有重复内容的情况下实现相同或者相似功能的能力。

As one of the most important concepts in OOP, inheritance allows programmers to have the same or similar methods and attributes in a new class inherited from another.

继承自其它类的新类被称为派生类(子类),继承自的类被称为基类(父类)。程序可以在派生类可以在不重复声明的情况下,对子类的实例调用其基类的方法和属性。

The class inherited from a class is called subclass, and the class that can inherit attributes and behavior methods by another class is called subclass. Programmers and can call the methods and attributes that have been declared in the superclass without declaring them in the subclass.

-继承可以有一个或者多个基类

-类默认继承自object

-派生类在使用isinstance()方法检测出的类型包括本身和所有基类

-派生类可以用__base__方法查看自己的基类

-继承自父类的方法可以被重新声明

-a subclass can have one or more superclass(in python)

-the methods inherited from a superclass can be rewrite in the subclass

-all classed are inherited from the class ’object’ by default

-the type returned by isinstance() includes the class itself and all the superclass of it

-the superclass of a subclass can be examined bychecking __base__

继承范例:

Inheritance Example:

class A:

def test(self):

print('test function')

class B(A): #新建类B继承类A,类A中的属性全部被类B继承,即使B中没有声明

#new class B inherited from class A

pass

b1 = B() #类B的实例可引用父类B的'test'方法

#instance of B calls the method “test()”of its superclass A

b1.test()

方法重写:

Method override:

class A:

def test(self):

print('test function')

class B(A): #新建类B继承类A,类A中的属性全部被类B继承

#new class B inherited from class A

def test(self):

print('test function B')

pass

b1 = B() #类B的实例可引用父类B的'test'方法 但自身类下也有重名的’test‘方法 自身优先

#when a instance of B calls “test”, the new “test” will be used before the old “test” of A

b1.test()

#运行结果 Output

#test function B

多继承:

Multiple inheritance:

class A:

def test():

print("A")

class B:

def test():

print("B")

class C(A,B):

pass

C.test()

#运行结果 Output:

#A

__mro__

每个类中都会包含一个__mro__用来保存继承过的方法和属性

mro 代表 method resolution order

在类被实例化的时候生成

根据C3 linearization algorithm被生成

every class contains a attribute called __mro__ to save the methods and attributes inherited

mro stands for method resolution order

__mro__ will be created during the instantiation

__mro__ will be created based on C3 linearization algorithm

多继承寻找特定方法 :

Method searching in multiple inheritance:

先在实例的类中找

实例中没找到去基类中找

先从继承列表左边开始找

如果子类和所有基类中都没有则报错

The class of the instance will be search at first

Then the superclass of the instance

Superclass closer to the left-hand side will be examined first

Throws exception if nothing is found in the class itself or in its superclasses

多继承可能导致非法继承的情况:

Cases of illegal multiple inheritance:

https://www.ics.uci.edu/~pattis/ICS-33/lectures/inheritanceii.pdf

2.Project Tips(难点总结,常见困难):

这个程序主要内容内容是编写一个可以解析一个函数调用中实际传进来的参数类型和函数本身要求的类型是否一致的类(Check_Annotation)。这个类是一个decorator类,可以解析所有以decorator方式传进来的函数。大致思路是在main中函数被调用,传入检查是否一致的类(Check_Annotation)中:

@Check_Annotation

def f(x : [int]): pass

f(1)

类的__call__函数会在传入的时候被调用,所以应该在这里将函数本身的参数类型解析:

(使用__annotations__,或者inspect.signature(),_f为传入的函数)

self._f.__annotations__

输出:{'x': }

inspect.signature(self._f)

输出:(x:int)

同时将传入的参数解析(通过__call__的*args, **kargs)

f_signature = inspect.signature(self._f)

bound_f_signature = f_signature.bind(*args,**kargs)

之后将得到的两组类型进行比较(通过Check_All_OK和Check_Any_OK),可以根据题目要求写多个函数分别用来检查不同的情况。(list,set,dict.....)函数形参满足某一类型的,把实参传入对应检查类型的函数来比较。例如:

def check_sequence(kind, kind_text):

def check_dict(kind, kind_text):

def check_set(kind, kind_text):

The main purpose of the programme is to have a class that can check whether the arguments and parameters match when the method is called. The class should be a decorator. The method for testing should be declared in main and then be delivered to the class (Check_Annotation).

@Check_Annotation

def f(x : [int]): pass

f(1)

The method __call__ will be called when the function is delivered to the class. Then we can parse the arguments and parameters here.

Parameters:

(Use __annotations__,or inspect.signature(),_f is method sent to the class)

self._f.__annotations__

Output:{'x': }

inspect.signature(self._f)

Output:(x:int)

Arguments:

(Use *args, **kargs of method __call__)

f_signature = inspect.signature(self._f)

bound_f_signature = f_signature.bind(*args,**kargs)

At last, use Check_All_OK and Check_Any_OK to compare arguments and parameters.

3.列出将要使用的functions:

decorator函数和类

inspect.signature

__annotations__

*args, **kargs的用法

4.翻译以上function对应的documentation:

decorator函数和类

在函数声明前加入一行@name(参数列表)

则函数被调用时会被传入名称为name的函数或类中

详解链接:

https://www.codementor.io/sheena/advanced-use-python-decorators-class-function-du107nxsv

inspect.signature和__annotations__

inspect.signature()可以被用来获得函数的形参

__annotations__具有同样的功能(格式不同)

详解链接:

https://docs.python.org/3/library/inspect.html

*args, **kargs的用法

当函数的参数不确定时(非负整数个),可以使用*args 和**kwargs

*args 没有key值,**kwargs有key值。

*args可以当作可容纳多个变量组成的list

**kwargs可以当作容纳多个key和value的dictionary

*args应该在**kwargs之前

详解链接:

https://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20181115G1FTUZ00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券