python作业8

1.以下程序输出结果
a = 1
def fun(a):
    a = 2
fun(a)
print (a)  
 
a = []
def fun(a):
    a.append(1)
fun(a)
print(a)
 
#1 [1]

2.请简要说明什么是类变量,什么是实例变量,并观察以下程序的输出结果

 
class Person:
    name="aaa"
 
     
p1=Person()
p2=Person()
p1.name="bbb"
print(p1.name)  
print(p2.name) 
print(Person.name) 
 
#实例变量是对于每个实例都独有的数据,而类变量是该类所有实例共享的属性
和方法
#bbb aaa aaa

3.以下语句有什么弊端,name是元祖的时候,程序会是什么样的结果,如何避免

"hi there %s" % name
#弊端:name只能是单个字符串
#会报类型错误:不是所有在字符串格式化期间转换的参数,用format函数'hi there {}'.format(name)

4.阅读下面的代码,写出A0,A1至An的最终值。

A0 = dict(zip(('a','b','c','d','e'),(1,2,3,4,5))) A1 = range(10) A2 = [i for i in A1 if i in A0] A3 = [A0[s] for s in A0] A4 = [i for i in A1 if i in A3] A5 = {i:i*i for i in A1} A6 = [[i,i*i] for i in A1]

#A0 = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5}
#A1 = range(0,10)
#A2 = []
#A3 = [1,2,3,4,5]
#A4 = [1,2,3,4,5]
#A5 = {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
#A6 = [[0, 0],[1, 1],[2, 4],[3, 9],[4, 16],[5, 25],[6, 36],[7, 49],[8, 64],[9, 81]]

5.你如何管理不同版本的代码?

6.下面代码会输出什么:

def f(x,l=[]):
    for i in range(x):
        l.append(i*i)
    print(l)
f(2)
f(3,[3,2,1])
f(3)
 
#[0, 1] [3, 2, 1, 0, 1, 4] [0, 1, 0, 1, 4]

7.这两个参数是什么意思:*args**kwargs?我们为什么要使用它们?

#可变参数和关键字参数
#如果我们不确定要往函数中传入多少个参数,或者我们想往函数中以列表和元
组的形式传参数时,那就使要用*args
#果我们不知道要往函数中传入多少个关键词参数,或者想传入字典的值作为关
键词参数时,那就要使用**kwargs

8.阅读下面的代码,它的输出结果是什么?

class A(object):
    def go(self):
        print "go A go!"
    def stop(self):
        print "stop A stop!"
    def pause(self):
        raise Exception("Not Implemented")
class B(A):
    def go(self):
        super(B, self).go()
        print "go B go!"
class C(A):
    def go(self):
        super(C, self).go()
        print "go C go!"
    def stop(self):
        super(C, self).stop()
        print "stop C stop!"
class D(B,C):
    def go(self):
        super(D, self).go()
        print "go D go!"
    def stop(self):
        super(D, self).stop()
        print "stop D stop!"
    def pause(self):
        print "wait D wait!"
class E(B,C): pass
a = A()
b = B()
c = C()
d = D()
e = E()
# 说明下列代码的输出结果
a.go()
b.go()
c.go()
d.go()
e.go()
a.stop()
b.stop()
c.stop()
d.stop()
e.stop()
a.pause()
b.pause()
c.pause()
d.pause()
e.pause()
 
#go A go!

#go A go!
#go B go!

#go A go!
#go C go!

#go A go!
#go C go!
#go B go!
#go D go!

#go A go!
#go C go!
#go B go!

#stop A stop!

#stop A stop!

#stop A stop!
#stop C stop!

#stop A stop!
#stop C stop!
#stop D stop!

#stop A stop!
#stop C stop!

#异常

#异常

#异常

#wait D wait!

#异常

9.请写出一段Python代码实现删除一个list里面的重复元素

def delre(l):
    l = list(set(l))
    return l

10.单引号,双引号,三引号的区别

单引号和双引号定义字符串,三引号加注释

11.写一个函数, 输入一个字符串, 返回倒序排列的结果

输入: string_reverse(‘abcdef') , 返回: ‘fedcba',写出你能想到的多种方法

def string_reverse(s):
    s = ','.join(s)
    l = s.split(',')
    l.reverse()
    s = ''.join(l)
    return s
s = 'abcdefg'
print(string_reverse(s))

 12. 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在。利用当前我们所学知识,尝试使得所创建的类是单实例模式

import functools

def decorator(f):
    @functools.wraps(f)
    def wrapper(*args, **kw):
        global flag
    
        print(flag)
        if flag == False:
            return 0
        flag = False
        return f(*args, **kw)
    return wrapper

@decorator
class Oneo(object):
    pass

flag = True
d = Oneo()
print(type(d))
a = Oneo()
print(type(a))

编程练习

1.定义一个点(Point)类和直线(Line)类,使用 getLen 方法可以获得直线的长度。`?^$q=:dI

提示:

    • 设点 A(X1,Y1)、点 B(X2,Y2),则两点构成的直线长度 |AB| = √((x1-x2)2+(y1-y2)2)
    • Python 中计算开根号可使用 math 模块中的 sqrt 函数
    • 直线需有两点构成,因此初始化时需有两个点(Point)对象作为参数
import math
class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Line(object):
    def __init__(self, a, b):
        self.a = a
        self.b = b
    def getlen(self):
        l = math.sqrt((a.x-b.x)**2 + (a.y-b.y)**2)
        print(l)
        return l

a = Point(1,2)
b = Point(2,1)
c = Line(a,b)
c.getlen()

2.列表[1,2,3,4,5],请使用map()函数输出[1,4,9,16,25],并使用列表推导式提取出大于10的数,最终输出[16,25]

l = [1,2,3,4,5]
def square(x):
    x = x**2
    return x
l = list(map(square,l))
print(l)
print([i for i in l if i > 10])

3.

坦克

某次战役中,为便于信息交互,我军侦察部门将此次战役的关键高地坐标设定为(x=0,y=0)并规定,每向东增加100米,x加1,每向北增加100米,y加1。同时,我军情报部门也破译了敌军向坦克发送的指挥信号,其中有三种信号(L,R,M)用于控制坦克的运动,L 和 R 分别表示使令坦克向左、向右转向,M 表示令坦克直线开进100米,其它信号如T用于时间同步,P用于位置较准。

一日,我军侦察兵发现了敌军的一辆坦克,侦察兵立即将坦克所在坐标(P, Q)及坦克前进方向(W:西,E:东,N:北,S:南)发送给指挥部,同时启动信号接收器,将坦克接收到的信号实时同步发往指挥部,指挥部根据这些信息得以实时掌控了该坦克的位置,并使用榴弹炮精准地击毁了该坦克。

请设计合理的数据结构和算法,根据坦克接收到的信号,推断出坦克所在的位置。
设计时请考虑可能的扩展情况,并体现出您的设计风格。

假设,侦察兵发送给指挥部的信息如下:
坦克坐标:(11,39)
坦克运行方向:W
坦克接收到的信号为:MTMPRPMTMLMRPRMTPLMMTLMRRMP
其位置应该是(9,43),运动方向为E

class Tank(object):
    def __init__(self, xlobal, ylobal, direction):
        self.xlobal = xlobal
        self.ylobal = ylobal
        self.direction = direction


    def count(self, signal):

        for i in signal:
            if i == 'T' or i == 'P':
                pass
            elif i == 'M' and self.direction == 'W':
                self.xlobal -= 1
            elif i == 'M' and self.direction == 'E':
                self.xlobal += 1
            elif i == 'M' and self.direction == 'N':
                self.ylobal += 1
            elif i == 'M' and self.direction == 'S':
                self.ylobal -+ 1

            elif i == 'L' and self.direction == 'W':
                self.direction = 'S'

            elif i == 'L' and self.direction == 'S':
                self.direction = 'E'

            elif i == 'L' and self.direction == 'E':
                self.direction = 'N'

            elif i == 'L' and self.direction == 'N':
                self.direction = 'W'

            elif i == 'R' and self.direction == 'W':
                self.direction = 'N'

            elif i == 'R' and self.direction == 'N':
                self.direction = 'E'

            elif i == 'R' and self.direction == 'E':
                self.direction = 'S'

            elif i == 'R' and self.direction == 'S':
                self.direction = 'W'

            else:
                pass

    def res(self):
        print('目标位置为({},{}),运动方向为{}'.format(self.xlobal, self.ylobal, self.direction))


t1 = Tank(11,39,'W')
t1.count('MTMPRPMTMLMRPRMTPLMMTLMRRMP')
t1.res()

猜你喜欢

转载自www.cnblogs.com/tangyun966/p/10130972.html