Прокодка раунд 2 |
1 | def someFunc(array): iterations = len(array) - 1 for i in range(iterations): for j in range(iterations-i): if array[j] > array[j+1]: array[j], array[j+1] = array[j+1], array[j] |
2 | static int[] SomeFunc(int[] mas) { int temp; for (int i = 0; i < mas.Length; i++) { for (int j = i + 1; j < mas.Length; j++) { if (mas[i] > mas[j]) { temp = mas[i]; mas[i] = mas[j]; mas[j] = temp; } } } return mas; } |
3 | begin for i:=1 to n-1 do begin for j:=1 to n-i do begin if m[j]>m[j+1] then begin k:=m[j]; m[j]:=m[j+1]; m[j+1]:=k; end; end; end; for i:=1 to n do Write(m[i], ' '); end. |
4 | void someFunc(int* num, int size) { for (int i = 0; i < size - 1; i++) { for (int j = (size - 1); j > i; j--) { if (num[j - 1] > num[j]) { int temp = num[j - 1]; num[j - 1] = num[j]; num[j] = temp; } } } } |
5 | const someFunc = (arr) => { for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr.length - i; j++) { if (arr[j] > arr[j + 1]) { let tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } } return arr; } |
6 | |
7 | double second_func(double y) { double l = 0, r = 1e9; while (r - l > 1e-6) { double m = (l + r) / 2; double cur = m * m; if (cur < y) { l = m; } else if (cur > y) { r = m; } else { return m; } } return l; } |
8 | def second_func(arr, x): low = 0 high = len(arr) - 1 mid = 0 while low <= high: mid = (high + low) // 2 if arr[mid] < x: low = mid + 1 elif arr[mid] > x: high = mid - 1 else: return mid return -1 print(second_func([1,2,3,4,5,6,7], 4)) |
9 | const secondFunc = (data, target, start, end) => { if(end < 1) return data[0]; const middle = Math.floor((start + (end - start)/2); if (target == data[middle].svgX) return data[middle]; if (end — 1 === start) return Math.abs(data[start].svgX — target) > Math.abs(data[end].svgX — target) ? data[end] : data[start]; if (target > data[middle].svgX) return secondFunc(data,target,middle,end); if (target < data[middle].svgX) return secondFunc(data,target,start,middle); } let closestPoint = secondFunc(data,target, 0, data.length-1) |
10 | static int SecondFunc(int[] array, int searchedValue, int first, int last) { if (first > last) { return -1; } var middleValue = array[middle]; if (middleValue == searchedValue) { return middle; } else { if (middleValue > searchedValue) { return SecondFunc(array, searchedValue, first, middle - 1); } else { return SecondFunc(array, searchedValue, middle + 1, last); } } } |
11 | function secondFunc( $list, $val ) { $lowIndex = 0; $hightIndex = sizeof( $list ) - 1; while( $lowIndex <= $hightIndex ) { $middleIndex = round( ( $lowIndex + $hightIndex ) / 2 ); $test = $list[ $middleIndex ]; if( $test == $val ) { return $middleIndex; } else if( $test > $val ) { $hightIndex = $middleIndex - 1; } else { $lowIndex = $middleIndex + 1; } } return 'не найдено'; } |
Комментарии