Python打卡Day17

Question 65

Question

Please write assert statements to verify that every number in the list [2,4,6,8] is even.


Hints

Use "assert expression" to make assertion.


Main author's Solution: Python 2

1li = [2,4,6,8]
2for i in li:
3    assert i%2==0

My Solution: Python 3

1data = [2,4,5,6]
2for i in data:
3    assert i%2 == 0, "{} is not an even number".format(i)

Question 66

Question

Please write a program which accepts basic mathematic expression from console and print the evaluation result.

Example: If the following n is given as input to the program:

135 + 3

Then, the output of the program should be:

138

Hints

Use eval() to evaluate an expression.


Main author's Solution: Python 2

1expression = raw_input()
2print eval(expression)

My Solution: Python 3

1expression = input()
2ans = eval(expression)
3print(ans)

Question 67

Question

Please write a binary search function which searches an item in a sorted list. The function should return the index of element to be searched in the list.


Hints

Use if/elif to deal with conditions.


Main author's Solution: Python 2

 1import math
 2def bin_search(li, element):
 3    bottom = 0
 4    top = len(li)-1
 5    index = -1
 6    while top>=bottom and index==-1:
 7        mid = int(math.floor((top+bottom)/2.0))
 8        if li[mid]==element:
 9            index = mid
10        elif li[mid]>element:
11            top = mid-1
12        else:
13            bottom = mid+1
14
15    return index
16
17li=[2,5,7,9,11,17,222]
18print bin_search(li,11)
19print bin_search(li,12)

My Solution: Python 3

1#to be written

Solution by ulmasovjafarbek: Python 3

 1def binary_search(lst, item):
 2    low = 0
 3    high = len(lst) - 1
 4    
 5    while low <= high:
 6        mid = round((low + high) / 2)
 7        
 8        if lst[mid] == item:
 9            return mid
10        elif lst[mid] > item:
11            high = mid - 1
12        else:
13            low = mid + 1
14    return None
15    
16lst = [1,3,5,7,]
17print(binary_search(lst, 9))   

Solution by AasaiAlangaram: Python 3

 1def binary_search_Ascending(array, target):
 2    lower = 0
 3    upper = len(array)
 4    print('Array Length:',upper)
 5    while lower < upper:
 6        x = (lower + upper) // 2
 7        print('Middle Value:',x)
 8        value = array[x]
 9        if target == value:
10            return x
11        elif target > value:
12            lower = x
13        elif target < value:
14            upper = x
15
16Array = [1,5,8,10,12,13,55,66,73,78,82,85,88,99]
17print('The Value Found at Index:',binary_search_Ascending(Array, 82))

Solution by yuan1z: Python 3

 1idx = 0
 2def bs(num,num_list):
 3    global idx
 4    if (len(num_list) == 1):
 5        if num_list[0] == num:
 6            return idx
 7        else:
 8            return "No exit in the list"
 9    elif num in num_list[:len(num_list)//2]:
10        return bs(num,num_list[:len(num_list)//2])
11    else:
12        idx += len(num_list)//2
13    return bs(num,num_list[len(num_list)//2:])
14
15print(bs(66,[1,5,8,10,12,13,55,66,73,78,82,85,88,99,100]))

Question 68

Question

Please generate a random float where the value is between 10 and 100 using Python module.


Hints

Use random.random() to generate a random float in [0,1].


Main author's Solution: Python 2

1import random
2print random.random()*100

My Solution: Python 3

1import random
2rand_num = random.uniform(10,100)
3print(rand_num)

Question 69

Question

Please generate a random float where the value is between 5 and 95 using Python module.


Hints

Use random.random() to generate a random float in [0,1].


Main author's Solution: Python 2

1import random
2print random.random()*100-5

My Solution: Python 3

1import random
2rand_num = random.uniform(5,95)
3print(rand_num)

go to previous day

go to next day

Discussion