048-Python测试题03

第四份2014-2015

1a.

True True False True

‘is’ judges if the a is the same object to b, a and b point to same memory space which is ‘Tom’, so a is b, a==b, c and d are two different Person instances, they point to different memory space and they have different id, so c is d returns False, c.name and d.name is the same.

 

1b.

[[‘A’, ‘b’], 1, 2, 3]

blist is a shallow copy of alist, alist and blist have different id and point to different memory space, but alist[0] and blist[0] have same address of [‘a’,’b’] list, so when blist[0][0]=’A’, alist[0][0] will be changed to ‘A’, so we have above result.

 

1c.

[1,2,3,4,5,6]

__add__() is a special method defined in class G which allows ‘+’ operator to invocate it. so g+[1,2,3] invocates __add__(self,[1,2,3]), so self.l is [1,2,3], and then +[..], because 2 and 3 are already in the self.l, so self.l is ...

 

1d.

[4,4,4,4,4]

a is [0,1,2,3,4], b is created from a, b append elements from a[-1] to a[0],so b is [4,3,2,1,0], elements of c are based on a[i]+b[i],and i are 0,1,2,3,4 respectively, so c[0] is a[0]+b[0] which is 0+4, and c[1] is a[1]+b[1] which is 1+3, c[4] is 4+0, so c is 4,4,4,4,4

 

2a.

A regular expression is a special sequence of characters that are used to find and match a set of strings.

 

2b.

a string whose length is 4 and the first position is a lowercase character, second position is a uppercase character, third and fourth position are digit.

a string which contain a or b for zero or more times and contain c for one or more times, like c,ac,bc,aac,abc,bbc,

a string whose first seven characters are word character(alphbet,digit or ‘_’),and the last position is a digit.

 

2c.

  1. Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABDHJLNPUWZ]{2}

 

2d.

def sort_postcodes(postcodes):

    codes = {}

    postcodes.sort()

    print(postcodes)

    for item in postcodes:

        code = item.split(' ')

        key = code[0]

        value = code[1]

        if key in codes:

            codes[key].append(value)

        else:

            codes[key] = [value]

    list = []

    for k, v in codes.items():

        arr = []

        for item in v:

            str = k + ' ' + item

            arr.append(str)

            arr.sort()

        list.append(arr)

    return list

 

 

ps = ['vn5 2jh', 'cf4 8uw', 'h3r 9lh', 'bn1 3ha', 'cf4 1ad', 'vn5 3hh', 'cf4 7wn', 'h3r 2aa']

codes = sort_postcodes(ps)

print(codes)

 

 

3a.

tree’s nonlinear data structure allows us to implement faster algorithms and organizing data into groups

  1. computer file system uses tree structure to group files into directories.
  2. binary search tree provides fast search by storing data into binary tree.

 

3b.

diagram

 

3c.

there are two loops in the algorithm, in the worst case, 1 loops n times, 2 loops n times, so it is 2*n times, so the running time is O(n).

 

3d.

def find_larger(tree,root,val):

num=0

if root>val:

return getT(tree,root)

else:

return find_larger(tree,root.rightchild,val)

 

 

 

 

 

 

 

第五份2013-2014

  1. a

[0,12,24,36]

alist is from 0 to 19 and the interval is 2 so alist is [0,2,4,6,8,10,12,14,16,18], the elements of blist is based on 2*x, x is 0,6,12,18 respectively, so 2*x is 0,12,24,36, so blist is [0,12,24,36]

 

  1. b

20

before g() method, a is a global field, but inside the g() method, a is a local field, so a=b will not change a, so a is still 20, so the output is 20.

 

  1. c

[('tower', 'glow')]

re (\w+) captures two words before ‘, grow, ‘, and \w means word characters(alphabet, number and ‘_’ ) so the first word is tower, second word is glow. so the output is above result.

 

  1. d

[[],[1],[2,2],[3,3,3],[4,4,4,4],[5,5,5,5,5],[6,6,6,6,6,6],[7,7,7,7,7,7,7]]

in the first iteration, alist append [0]*0, and in the second, [1]*1, when i is 7, alist append [7]*7,and i += 1, so i is 8, and it breaks the loop. so alist is above.

 

  1. e

[] [5,4,3,2,1]

func() is a recursive function, it takes element of x out and y append the element, and when x is empty, it returns, so finally x is [], y is [5,4,3,2,1]

 

  1. a

string is immutable and list is mutable.

an immutable data object, once created, cannot be modified

Immutability means some objects could not be changed, for example, tuple could not be changed, so tuple is immutale.

string is an immutable data type in python, each char can be accessed, but item assignment using indexing is not supported, for example

word=’hello’

word[0]=’H’

this is not supported

list is a mutable data type ,you can modify an individual item or sub-list using indexing assignment or slicing assignment.

 

2b

class Author:

def __init__(self,id,name,birth,book_id_list):

self.id=id

self.name=name

self.birth=birth

self.book_id_list=book_id_list

class Book:

def __init__(self,id,title,pub,isbn,author_id_list):

self.title=title

self.pub=pub

self.isbn=isbn

self.author_id_list=author_id_list

author01=Author(0,‘Alice’,’1980-01-01’)

author_list=[author01]

book01=Book(‘Apple’,’2000’,’123’,[0])

 

 

 

 

 

 

第六份2012-2013

1.

(a is not empty, so it returns True in the ‘if a:’ statement, so the output is

a is not None.

 

2.

hello False

A_id_1 is the id of the memory space of “Hello”, after a=’h’+a[1:], a point at a different memory space which is ‘hello’. So output is

 

3.

In the for loop, i is 10,15,20, j is 0,1 in first loop, j is 0,1,2 in second loop, j is 0,1,2,3 in third loop. So the output is

10: 2 2

15: 3 3 3

20: 4 4 4 4

 

4.

X is -10 to 9, y is -10 to 9, radius is 0 to 100 without 100. So the output is

(0,0) 0

x is -10 to 10, y is -10 to 10, radius is 0 to 99,

 

5.

After fun1(), a doesn’t change because the a inside fun1() is a local variable, after fun2(), b is changed to be 200, because b is global b, and a is changed to be 200 too, because a and b point to the same memory space, so the output is

A = 100, b = 200

 

6.

The length of mylist is 4 so return 1000+fun([100,10,1]) which return 100+fun([10,1]), which return 10+fun([1]), which return 1, so the output is

1111

 

  1. a

def getInfo(str):

Info={}

arr=str.split(‘,’)

for item in arr:

keyvalue=item.split(‘:’)

Key=keyvalue[0]

Value=keyvalue[1]

Info.append(key,value)

Return info

=======================

def getInfo(str):

    info = {}

    arr = str.split(',')

    for item in arr:

        keyvalue = item.strip().split(':')

        key = keyvalue[0]

        value = keyvalue[1]

        info[key] = value

    return info

 

 

a = getInfo('a:aaa, b:bbb')

print(a)

 

 

b.

def getInfo(str):

name=re.search(‘Name:[a-zA-Z]+,’,str)

age=re.search(‘Age:[0-9]+,’,str)

address=re.search(‘Address:[a-zA-Z]+,’,str)

city=re.search(‘City:[a-zA-Z]+,’,str)

phone=re.search(‘Age:[0-9]+,’,str)

Info={

‘Phone’:phone,

‘City’:city,

‘age:age,

‘name’:name,

‘address’:address

}

return info

def getInfo(str):

    list=re.findall('\w*:\w*',str)

    kv={}

    for item in list:

        split = item.split(':')

        kv[split[0]]=split[1]

    return kv

  1. a

 

b.

[14,[7,[],[8,[],[9,[],[]]]],[18,[],[23,[20,[],[]],[]]]]

 

c.

def getNodes(list,arr):

For item in arr:

If item.isDigit():

List.append(item)

Return list

Else:

getNodes(list,item)

def get_list(arr):

    list = []

    for item in arr:

        if type(item) is int:

            list.append(item)

        else:

            list.extend(get_list(item))

    return list

 

 

arr = [1, 2, [2, 3, [3, [], [3, 3, [3, [], []]]]]]

print(get_list(arr))

 

 

D.

..

 

3e.

n+n  2*n  O(n)

 

 

4a.

 

 

发布了1081 篇原创文章 · 获赞 42 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/qq_33781658/article/details/103952125