Algorithm Learning (15th), algorithm Learning
1. Share Price Volatility
Note: If you choose a stock with larger fluctuations, for example, if the initial price of the stock is £ 50 (instead of £ 20), and when Paul sells the stock, his share price has risen to 200, and his 400 shares have a profit of only.
However, when buying and selling, his income is 100, so his real profit is only 96-more than half of the money is earned by the broker!The broker's Commission is 1% of each transaction.
Paul's stock selection rules: if the Commission is 0.5, then the standard stock deviation should be equal to or greater than 2, Paul will choose this stock.
For example, if the price is £ 99 within 7 days and $101 for the other 7 days, the average price is £ 100, and the broker's Commission (per share) is £ 1. The standard deviation is also 1, so these stocks are not worth buying and selling.
Input data:Contains the number of stocks (stock type or name) used for calculation ).
The following row contains the description of the stock-the stock name (four Latin letters) and then 14 values-the price of the stock every day for the past two weeks.
Answer:It should include the names of some stocks whose volatility is large enough to comply with Paul's standards (the same order as they are given in input ).
For example:
input data:2JOOG 99 99 99 99 99 99 99 101 101 101 101 101 101 101GOLD 95 105 95 105 95 105 95 105 95 105 95 105 95 105answer:GOLD
Test data:
23FOTA 20 21 21 21 21 22 23 22 21 20 20 21 21 22PNSN 26 27 28 27 28 26 25 26 24 23 21 20 22 20VDKL 57 54 54 56 55 57 61 64 65 66 66 65 68 67ZEOD 23 22 21 20 21 20 20 21 21 21 20 19 18 18JABA 21 23 26 28 28 28 27 23 27 24 23 24 26 26ASDF 157 160 163 163 166 164 168 171 170 166 165 164 163 164FLNT 23 24 28 25 27 30 29 26 26 23 24 20 17 15YUKA 33 34 33 32 32 33 33 35 33 35 35 33 35 33DALG 79 78 80 81 81 80 82 81 80 82 82 81 83 83GOLD 217 216 216 217 216 216 216 217 218 218 219 220 219 220JOOG 56 53 56 58 56 54 55 58 59 59 58 57 55 53CKCL 68 69 69 71 71 70 69 68 66 66 64 63 61 62WOWY 24 24 27 26 25 24 28 30 33 30 34 33 35 37SLVR 26 28 27 28 27 27 26 25 26 26 24 25 26 27JEOP 23 23 26 29 26 27 23 20 16 18 21 23 19 23BLEP 81 79 81 79 80 79 81 82 79 77 75 76 76 78SUGR 24 23 21 21 20 19 18 16 18 17 15 13 12 11MYTH 25 24 24 25 26 26 25 24 23 24 25 26 27 26FANT 116 114 115 119 119 119 117 118 118 119 123 122 119 115OBAM 24 21 18 20 17 16 16 18 16 14 11 11 9 9SAKK 24 26 26 26 26 24 22 22 22 21 21 19 19 17INSX 26 26 26 28 27 28 26 25 23 21 22 22 23 24IMIX 42 40 40 39 39 41 42 40 38 36 34 36 37 35
The Code is as follows:
1 from math import sqrt 2 test_cases = int (input () 3 4 def Standard_Deviation (attr): # construct standard deviation 5 M = sum (attr)/len (attr) 6 D = [(M-I) ** 2 for I in attr] 7 d = sqrt (sum (D)/len (D )) 8 return d 9 10 for _ in range (test_cases): 11 data = [I for I in input (). split ()] 12 name = data [0] 13 values = [int (I) for I in data [1:] 14 comission = (sum (values) /len (values) * 0.01 # Commission 15 standred = Standard_Deviation (values) 16 if standred/comission> = 4: #17 print (name, end = '') 18 19 output: PNSN VDKL ZEOD JABA FLNT CKCL WOWY SLVR JEOP SUGR MYTH OBAM SAKK INSX IMIX
2. Insertion Sort (insert sorting)
Note: A very common algorithm requires that the number of steps to be moved when each number in the array is sorted by the insert method, that is, the number of elements to be moved.
For example:3 1 2 5
[3], 1, 2, 5 T = 1, it should be inserted before 3[3, 3], 2, 5 part of array starting from 3 is shifted, 1 is overwritten[1, 3], 2, 5 1 is restored from T to the proper place[1, 3], 2, 5 T = 2, it should be inserted before 3[1, 3, 3], 5 part of array starting from 3 is shifted, 2 is overwritten[1, 2, 3], 5 2 is inserted from T to sub-array[1, 2, 3], 5 T = 5, it should be inserted... after all other elements, nice![1, 2, 3, 5] nothing to shift! we just extend sub-array to have 5 in it.
Result:
input data:43 1 2 5answer:1 1 0
Problem description:
You get a set of values to be sorted. Implement the described algorithm and print out the number of elements in the array that are displaced each time the algorithm is passed.
Input data:The size of the array contained in the first row.
The second row contains the array itself (all elements are different ).
Answer:It should contain the number of moving steps, that is, to show how many elements are moved in each transmission.
Test data:
133126 163 189 177 74 30 185 155 102 66 10 179 34 137 123 49 3 65 112 45 190 5 78 119 92 136 118 101 64 82 91 41 156 120 146 106 159 48 172 169 27 7 150 55 108 15 167 153 6 171 32 124 168 62 181 69 125 160 166 20 81 186 131 14 100 165 114 68 12 157 99 21 67 197 96 89 87 61 53 164 191 107 88 98 104 94 79 115 51 178 72 75 180 141 77 29 54 121 18 113 147 140 152 57 193 23 16 71 28 196 43 73 103 127 25 144 26 85 142 80 44 33 50 158 83 139 154 135 111 9 109 187 93
The Code is as follows:
1 test_case = int (input () 2 Arrays = [int (I) for I in input (). split ()] 3 4 def insertion_sort (A): # construct an insert sorting method 5 M = [] # A list 6 for j in range (1, len ()): 7 key = A [j] 8 I = j-1 9 m = 010 while I> = 0 and A [I]> key: 11 A [I + 1] = A [I] 12 I = I-113 m + = 1 # calculate the total number of steps removed, that is, the number of elements transferred 14 M. append (m) # Add to the list 15 A [I + 1] = key16 return M # return the list of all shifts 17 18 for I in insertion_sort (Arrays ): 19 print (I, end = '') 20 21 22 output: 0 0 1 4 5 1 4 6 8 10 2 10 6 8 12 12 12 9 15 0 20 12 10 13 8 12 15 17 17 26 6 12 8 17 6 30 5 6 37 39 11 32 21 41 7 12 46 6 42 19 8 38 3 36 21 12 11 53 37 2 23 58 36 13 32 46 17 40 62 49 0 42 45 46 58 60 15 1 39 49 45 41 48 56 36 70 8 61 60 28 62 84 75 36 90 42 27 30 26 79 1 94 72 94 1 90 72 53 37 101 32 102 68 33 72 97 101 94 24 71 37 28 41 56 125 57 6 71