Website Summary Files Commits Branches @commit @diff @stats
path: root / pract_KN / pract. 13 / solutions.cpp
blob: 5d782210abd3bb66641b9dca51c32f7556a59cd8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include<fstream>

bool isEven(int n) {
	return (n % 2) == 0;
}

void plusFive(int& n) {
	n += 5;
}
// task one
void filter(int* arr, int length, bool (*pred)(int)) {
	for (int i = 0; i < length; i++) {
		if (!pred(arr[i]))
			arr[i] = -1;
	}
}

// task two
void map(int* arr, int length, void (*func)(int&)) {
	for (int i = 0; i < length; i++) {
		func(arr[i]);
	}
}

// task three:
size_t replaceEveryFiveWithEight(size_t number) {
	if (number == 0)
		return 0;

	int lastDigit = number % 10;
	if (lastDigit == 5)
		return 10 * replaceEveryFiveWithEight(number / 10) + 8;

	return 10 * replaceEveryFiveWithEight(number / 10) + lastDigit;
	// 12 * 10 + 8
}

// task four
int binarySearch(const int arr[], int start, int end, int searchedElement) {
	if (start > end)
		return -1;

	int mid = start + (end - start) / 2;
	if (arr[mid] == searchedElement)
		return mid;
	else if (arr[mid] > searchedElement)
		return binarySearch(arr, start, mid - 1, searchedElement);
	else
		return binarySearch(arr, mid + 1, end, searchedElement);
}

// task five
bool isValidIndex(bool** grid, int start, int end, int w, int h) {
	return start >= 0 && end >= 0 && start < h&& end < w && (grid[start][end] == 0);
}

bool isThereAPath(bool** grid, int startX, int startY, int endX, int endY, const int w, const int h) {
	if (startX == endX && startY == endY)
		return true;

	if (!isValidIndex(grid, startX, startY, w, h))
		return false;

	grid[startX][startY] = true;

	return isThereAPath(grid, startX + 1, startY, endX, endY, w, h) ||
		isThereAPath(grid, startX - 1, startY, endX, endY, w, h) ||
		isThereAPath(grid, startX, startY + 1, endX, endY, w, h) ||
		isThereAPath(grid, startX, startY - 1, endX, endY, w, h);
}

// task six
bool isValidIndex(int** grid, int sX, int sY, int w, int h) {
	return sX >= 0 && sY >= 0 && sX < h && sY < w;
}

int biggestPath(int** grid, int startX, int startY, int width, int heigth) {
	if (!isValidIndex(grid, startX, startY, width, heigth))
		return 0;

	return grid[startX][startY] + std::max(biggestPath(grid, startX + 1, startY, width, heigth), biggestPath(grid, startX, startY + 1, width, heigth));
}

int main() {
	int arr[12] = { 1, 5, 8, 12, 16, 22, 34, 45,69, 77,101, 420 };
	std::cout << binarySearch(arr, 0, 11, 8) << std::endl;
	std::cout << binarySearch(arr, 0, 11, 9);
}