-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinarySearch.cpp
More file actions
115 lines (102 loc) · 2.25 KB
/
binarySearch.cpp
File metadata and controls
115 lines (102 loc) · 2.25 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <vector>
#include <fstream>
using std::cout;
using std::endl;
// problem 1
template <typename T>
int binSearch(T arr[], int begin, int end, T target)
{
int mid = 0;
while(begin < end)
{
mid = begin + (end - begin)/2;
if(arr[mid] == target)
return mid;
else if(arr[mid] < target)
begin = mid + 1;
else
end = mid;
}
return -1; //no find
}
// problem 2
template <typename T>
void reverse(T* arr, int begin, int end)
{
int i = begin;
int j = end - 1;
while(i < j)
{
std::swap(arr[i], arr[j]);
i++; j--;
}
}
template <typename T>
void revrot(T* arr, int rotpos, int len)
{
reverse(arr, 0, rotpos);
reverse(arr, rotpos, len);
reverse(arr, 0, len);
}
//problem 3
void hadleAnagrams(string inFile, string outFile)
{
ifstream in(inFile);
string str, temp;
typedef pair<string, string> StrPair;
vector<StrPair> vec;
while (in>>str)
{
temp = str;
sort(temp.begin(), temp.end());
vec.push_back(make_pair(temp, str));
}
in.close();
sort(vec.begin(), vec.end(), [](const StrPair& left, const StrPair& right) { return left.first < right.first; } );
ofstream out(outFile);
string tagStr;
for_each(vec.begin(), vec.end(), [&](const StrPair& element){
if(tagStr.empty()){
out<<element.first<<": "<<element.second;
tagStr = element.first;
}
else{
if(element.first == tagStr)
out<<" "<<element.second;
else{
out<<endl;
out<<element.first<<": "<<element.second;
tagStr = element.first;
}
}
});
out.close();
}
int main()
{
// test problem 1
constexpr int N = 10;
int arr[N];
int i = 1;
std::for_each(arr, arr + N, [&](int& v) {v = i++;} );
assert(4 == binSearch(arr, 0, N, 5));
assert(-1 == binSearch(arr, 0, N, 100));
assert(5 == binSearch(arr, 0, N, 6));
// test problem 2
char str[100];
memset(str, '\0', 100);
strcpy(str, "abcdefghijk");
int len = strlen(str);
revrot(str, 3, len);
cout<<str<<endl;
// test problem 3
//input file: pans pots opt snap stop tops
//output file: anps: pans snap
// opst: pots stop tops
// opt: opt
hadleAnagrams(string("wy.txt"), string("a.txt"));
}