046-Python测试题01

01.

a=[]
print(a)
if a:
    print('yes')
else:
    print('no')

02.

a='hello'
b=a
c=a[:]
d=a[0:]
print(id(a))
print(id(b))
print(id(c))
print(id(d))

a=[1,2,3]
b=a
c=a[:]
d=a[0:]
print(id(a))
print(id(b))
print(id(c))
print(id(d))

扫描二维码关注公众号,回复: 10245226 查看本文章

03

a='hello'
b='hello'
print(id(a))
print(id(b))

04

a='hello'
b='h'+a[1:]
c='h'+a[1:]
d='hello'
print(id(a))
print(id(b))
print(id(c))
print(id(d))

05

import random
for i in range(5):
    a=random.randint(0,1)
    print(a)
for i in range(5):
    a=random.randrange(0,1)
    print(a)

Question 1

  1.  

In my opinion, binary search tree(BST) is suitable, because BST can support fast search, and it is faster than the list. For example, we have 7 values like 1,2,3,4,5,6,7 in sorted list, unsorted list and binary search tree.

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

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

   4

 2   6

1 3  5  7

So we have to search 1,2,3,4,5,6,7 times to search 1-7 in sorted list,

We have to search 7,1,6,2,5,3,4 times to search 1-7 in unsorted list,

We have to search 3,2,3,1,3,2,3 times to search 1-7 in binary search list.

 

  1.  

A binary search tree is a binary tree whose nodes contain comparable objects and are organized as values.

The height of the tree is maximum depth of a leaf node, so the height of Figure 1 is 3.

The length is 1.

The pre-order traversal is A-B-D-C-E-G-F-H-I.

The in-order traversal is B-D-A-G-E-C-H-F-I.

The post-order traversal is D-B-G-E-H-I-F-C-A.

 

 

Question 2

  1. The count is 12.
  2. 15,11
  3. 4

 

 

Question 3

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

B.

  1. A string contains 4 characters, the first character is from a to z...
  2. a or b for zero or n times, and c for one or more times.
  3. A letter for 7 times and a digit.

 

C.

def search_by_id(student_id):

    for item in records.values():

        if item[ID] == student_id:

            return item[NAME]

return None

D.

The function can be seen as

for item in range(N):

sequence of statements

So the complexity is O(N)

 

 

 

1.

a)

Both sorted list and binary search tree could be

suitable.

Using an unsorted list, it is fast and simple to store data

(O(1)), but the search operation takes O(n).

Using a sorted list, storing a particular data item into a

sorted list may take O(logn) time, but the search

operation is faster, which takes O(logn) time using

binary search.

Binary Search Tree has performance similar to the

sorted list. Assume the binary search tree is a balanced

tree, and then both its data store and search methods

take O(logn) time. However, if the tree is not balanced,

the worst case could be O(n). Since Binary Search Tree

is a non-linear approach, it is more flexible regarding

memory usage.

b)

i. A binary search tree is a binary tree. Each node in

the tree satisfies the following requirements:

· The data in the node is larger than all data

values stored in the nodes of its left subtree.

· The data in the node is less than or equal to all

data values stored in the nodes of its right

subtree.

· Both its left and right subtrees are binary

search trees.

ii.

a) The height of the tree is 3

b) The length of the path between ‘A’ and ‘C’ is

1.

c) D, B, G, E, H, I, F, C, A

2.

a- Output is 12

When assigning check1 to check2, we create a second

reference to the same list. Changes to check2 affects

check1. When assigning the slice of all elements in

check1 to check3, we are creating a full copy of

check1 which can be modified independently (i.e, any

change in check3 will not affect check1).

So, while checking check1 ‘Code’ gets matched and

count increases to 1, but Mcq does gets matched since

its available only in check3.

Now checking check2 here also ‘Code’ gets matched

resulting in count value to 2.

Finally, while checking check3 which is separate than

both check1 and check2 here only Mcq gets matched and

count becomes 12.

b- output is below:

LETTERS 15

DIGITS 11

Explanation: d={"DIGITS":0, "LETTERS":0} # this line

initializing the dictionaries that will be the counter for the

DIGITS and LETTERS

Looping through the String as it is sequential type, and check

if the character is Digit or letter using if statement and

increment the counter each time.

c- output is: 4

Explanation: count is a global variable, and since it is

accessible from with the function by calling “global count”

the value should update to 7. However, because the function

was not called in the first place the count variable will

remain as it is.

3.

a. A regular expression is a special sequence of

characters that are used to find and match a set of

strings.

b.

i. r’[a-z][A-Z][0-9][0-9]’ describes a sequence of 4

characters: a lowercase letter, a uppercase

followed by two digits.

ii. r’[ab]*c+’ describes a sequence that contains 0 or

more characters of ‘a’ and ‘b’, followed by 1 or

more ‘c’s.

iii. r’\w{7}\d’ describes a sequence of 8 characters.

The first seven characters have to be word

character (alphabet, digit or ‘_’), and the last

character has to be a digit.

c.

def search_by_id(student_id):

for key, value in records.items():

if value[ID]==student_id:

return key

return None

Time complexity : O (n)

 

 

 

 

 

 

 

 

 

 

 

 

 

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

猜你喜欢

转载自blog.csdn.net/qq_33781658/article/details/103932982
今日推荐