-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunique2.cpp
More file actions
113 lines (94 loc) · 2.8 KB
/
unique2.cpp
File metadata and controls
113 lines (94 loc) · 2.8 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
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <vector>
using std::vector;
vector<int> push_front(vector<int> V, int x);
bool expSum(vector<int> V, int x);
vector<int> unique(vector<int> V);
int main(void)
{
vector<int> V = {2,2,3,3,3,4,5,5,7};
/*
int x;
while (cin >> x) {
V.push_back(x);
}
*/
vector<int> R = push_front(V, 1);
bool found = expSum(R, 3);
for (size_t i = 0; i < R.size(); i++) {
cout << R[i] << " ";
}
cout << endl;
vector<int> S = unique(R);
for (size_t i = 0; i < S.size(); i++) {
cout << S[i] << " ";
}
cout << endl;
cout << found << endl;
return 0;
}
/* TODO: write a function called "push_front(V,x)" for a vector, which
* adds parameter x to index 0 of vector V by moving all the other values
* to higher indexes. (This should show you why inserting elements is only
* efficient at the *end* of a vector.) */
vector<int> push_front(vector<int> V, int x) {
V.push_back(x);
size_t n = V.size();
//Swap with last element
for (size_t i = 0; i < n; i++) {
int temp = V[i];
V[i] = V[V.size() - 1];
V[V.size() - 1] = temp;
}
return V;
}
/* TODO: write a function that takes a vector V (of integers) and a single
* integer x, and returns a boolean value indicating whether or not x can
* be expressed as the sum of two elements of V. (Let's say you can't use
* the same index twice, so you must find i =/= j such that x = V[i]+V[j].) */
bool expSum(vector<int> V, int x) {
for (size_t i = 0; i < V.size(); i++) {
for (size_t j = 0; j < V.size(); j++) {
if (i != j) {
if (x == V[i] + V[j]) {
return true;
}
}
}
}
return false;
}
/* TODO: write a function that takes a vector V of integers which
* is *sorted*, and produces a vector of the unique integers from V.
* E.g., if V = {1,2,2,3,3,3,4}, then the result should
* be {1,2,3,4}. Ideally, modify the vector *in-place*.
* That is, modify the vector V that is given to the function directly,
* and don't create any new vectors. As a warm up: return a new vector with
* the
* result.
* */
vector<int> unique(vector<int> V) {
size_t n = V.size();
for (size_t i = 1; i < n; i++) {
//If equal to prev term
if (V[i] == V[i-1]) {
//Swap
int temp = V[i-1];
V[i-1] = V[i];
V[i] = temp;
}
}
return V;
}
/* TODO: write a *binary search* on a sorted vector. The idea is to
* kind of emulate the process you use to find a particular page in a book:
* 1. open the book to some page in the middle.
* 2. if the page number was too high, open to the middle of the pages to the
* left; if it was too low, open to the middle of the pages to the right
* 3. continue as above until you found the right page.
*
* This might be a little challenging. Ask questions if you get stuck.
* */