Array List - Xem lại lần làm thử - BK-LMS
Array List - Xem lại lần làm thử - BK-LMS
Câu hỏi 1
Đúng
Implement methods ensureCapacity, add, size in template class ArrayList representing the array list with type T with the
initialized frame. The description of each method is given in the code.
template <class T>
class ArrayList {
protected:
T* data; // dynamic array to store the list's items
int capacity; // size of the dynamic array
int count; // number of items stored in the array
public:
ArrayList(){capacity = 5; count = 0; data = new T[5];}
For example:
Test Result
ArrayList<int> arr; [19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
int size = 20; 20
Reset answer
1 template<class T>
2 ▼ void ArrayList<T>::ensureCapacity(int cap){
3 ▼ /*
4 if cap > capacity:
5 new_capacity = capacity * 1.5;
6 create new array with new_capacity
7 else: do nothing
8 */
9
10 ▼ if (cap > capacity) {
11 int newCapacity = capacity * 1.5;
12 ▼ try {
13 T* newData = new T[newCapacity];
14
15 ▼ for (int i = 0; i < count; ++i) {
16 newData[i] = data[i];
17 }
18
18
19 delete[] data;
20 data = newData;
21 capacity = newCapacity;
22 }
23 ▼ catch (std::bad_alloc& e) {
24 throw;
25 }
26 }
27 }
28
29 template <class T>
30 ▼ void ArrayList<T>::add(T e) {
31 ensureCapacity(count + 1);
32 data[count] = e;
33 ++count;
34 }
35
36 template<class T>
37 ▼ void ArrayList<T>::add(int index, T e) {
38 ▼ /*
39 Insert an element into the array at given index.
40 if index is invalid:
41 throw std::out_of_range("the input index is out of range!");
42 */
43 ▼ if (index < 0 || index > count) {
44 throw std::out_of_range("the input index is out of range!");
45 }
46 ensureCapacity(count + 1);
47 ▼ for (int i = count; i > index; --i) {
48 data[i] = data[i - 1];
49 }
50 data[index] = e;
51 ++count;
52 }
53
54 template<class T>
55 ▼ int ArrayList<T>::size() {
56 /* Return the length (size) of the array */
57 return count;
58 }
59
ArrayList<int> arr; [19, 18, 17, 16, 15, 14, 13, 12, 11, [19, 18, 17, 16, 15, 14, 13, 12,
int size = 20; 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
20 0]
for(int index = 0; index < 20
size; index++){
arr.add(0, index);
}
Đúng
Marks for this submission: 1,00/1,00.
Câu hỏi 2
Đúng
Implement methods removeAt, removeItem, clear in template class ArrayList representing the singly linked list with type T
with the initialized frame. The description of each method is given in the code.
template <class T>
class ArrayList {
protected:
T* data; // dynamic array to store the list's items
int capacity; // size of the dynamic array
int count; // number of items stored in the array
public:
ArrayList(){capacity = 5; count = 0; data = new T[5];}
~ArrayList(){ delete[] data; }
};
For example:
Test Result
Reset answer
1 template<class T>
2 ▼ T ArrayList<T>::removeAt(int index){
3 ▼ /*
4 Remove element at index and return removed value
5 if index is invalid:
6 throw std::out_of_range("index is out of range");
7 */
8 ▼ if (index < 0 || index >= count) {
9 throw std::out_of_range("index is out of range");
10 }
11 T removedItem = data[index];
12 ▼ for (int i = index; i < count - 1; i++) {
13 data[i] = data[i + 1];
14 }
15 --count;
16 T *newData = new T[capacity];
17 ▼ for (int i = 0; i < count; i++) {
18 newData[i] = data[i];
19 }
20 delete[] data;
21 data = newData;
22 return removedItem;
23
24 }
25
26 template<class T>
27 ▼ bool ArrayList<T>::removeItem(T item){
28 /* Remove the first apperance of item in array and return true, otherwise return false */
29
30 ▼ for (int i = 0; i < count; i++) {
31 ▼ if (data[i] == item) {
32 removeAt(i);
33 return true;
34 }
35 }
36 return false;
37 }
38
39 template<class T>
40 ▼ void ArrayList<T>::clear(){
41 ▼ /*
42 Delete array if array is not NULL
43 Create new array with: size = 0, capacity = 5
44 */
45 if(data != nullptr)
46 delete[] data;
47 data = nullptr;
48 count = 0;
49 capacity = 5;
50 data = new T[capacity];
51 }
52
Test Expected Got
Đúng
Marks for this submission: 1,00/1,00.
Câu hỏi 3
Đúng
Implement methods Get, set, clear, empty, indexOf, contains in template class ArrayList representing the array list with type
T with the initialized frame. The description of each method is given in the code.
template <class T>
class ArrayList {
protected:
T* data; // dynamic array to store the list's items
int capacity; // size of the dynamic array
int count; // number of items stored in the array
public:
void set(int index, T e); //set the index position in the list with the value e
int indexOf(T item); //get the first index of item in the list, else return -1
bool contains(T item); //check if the item is in the list
T removeAt(int index);
bool removeItem(T item);
};
Notice: You just have to implement the methods: set, get, clear, empty, indexOf, contains. Other methods have been
implemented already.
For example:
Test Result
Test Result
Answer:
1 template <class T>
2 int ArrayList<T>::indexOf(T item)
3 ▼ {
4 ▼ for (int i = 0; i < count; i++) {
5 ▼ if (data[i] == item) {
6 return i;
7 }
8 }
9 return -1;
10 }
11
12 template <class T>
13 T ArrayList<T>::get(int index)
14 ▼ {
15 ▼ if (index < 0 || index >= count) {
16 throw std::out_of_range("Index is out of range");
17 }
18 return data[index];
19 }
20
21 template <class T>
22 ▼ void ArrayList<T>::clear(){
23 ▼ /*
24 Delete array if array is not NULL
25 Create new array with: size = 0, capacity = 5
26 */
27 if(data != nullptr)
28 delete[] data;
29 data = nullptr;
30 count = 0;
31 capacity = 5;
32 data = new int[capacity];
33 }
34
35 template <class T>
36 bool ArrayList<T>::empty()
37 ▼ {
38 return count == 0;
39 }
40
41 template <class T>
42 bool ArrayList<T>::contains(T item)
43 ▼ {
44 return indexOf(item) != -1;
45 }
46
47 template <class T>
48 ▼ void ArrayList<T>::set(int index, T e){
49 data[index] = e;
50 }//set the index position in the list with the value e
Test Expected Got
Câu hỏi 4
Đúng
The function returns the array after applying all operation in operations.
Note:
- The iostream, and vector libraries have been included and namespace std is being used. No other libraries are allowed.
- You can write helper functions.
For example:
Test Result
Reset answer
1
2 ▼ vector<int> updateArrayPerRange(vector<int>& nums, vector<vector<int>>& operations) {
3 ▼ for(const auto& operation : operations){
4 ▼ for(int j = operation[0]; j <= operation[1]; ++j){
5 nums[j] = nums[j] + operation[2];
6 }
7 }
8 return nums;
9 }
vector<int> nums {13, 0, 6, 9, 14, 16}; [21, 8, 14, 9, 14, [21, 8, 14, 9,
vector<vector<int>> operations {{5, 5, 16}, {3, 4, 0}, {0, 2, 8}}; 32] 14, 32]
printVector(updateArrayPerRange(nums, operations));
vector<int> nums {19, 4, 3, 2, 16, 3, 17, 8, 18, 12}; [32, 28, 36, 41, [32, 28, 36,
vector<vector<int>> operations {{0, 3, 4}, {2, 5, 12}, {3, 6, 6}, 51, 61, 36, 21, 31, 41, 51, 61, 36,
{5, 8, 5}, {8, 9, 8}, {0, 5, 9}, {1, 7, 8}, {1, 1, 3}, {5, 5, 18}}; 20] 21, 31, 20]
printVector(updateArrayPerRange(nums, operations));
Đúng
Marks for this submission: 1,00/1,00.
Câu hỏi 5
Đúng
The function returns if all the 1s appear consecutively in nums. If nums does not contain any elements, please return true
Note:
- The iostream and vector libraries have been included and namespace std are being used. No other libraries are allowed.
- You can write helper functions.
- Do not use global variables in your code.
For example:
Test Result
Reset answer
Đúng
Marks for this submission: 1,00/1,00.
Câu hỏi 6
Đúng
The prices of all cars of a car shop have been saved as an array called N. Each element of the array N is the price of each
car in shop. A person, with the amount of money k want to buy as much cars as possible.
Where nums is the array N, length is the size of this array and k is the amount of money the person has. Find the maximum
cars this person can buy with his money, and return that number.
Example:
The result is 3, he can buy the cars having index 1, 2, 3 (first index is 0).
Note: The library iostream, 'algorithm' and using namespace std have been used. You can add other functions but you are not
allowed to add other libraries.
For example:
Test Result
Reset answer
Đúng
Marks for this submission: 1,00/1,00.
Câu hỏi 7
Đúng
The function returns the smallest index i such that the sum of the numbers to the left of i is equal to the sum of the numbers
to the right.
If no such index exists, return -1.
Note:
- The iostream and vector libraries have been included and namespace std is being used. No other libraries are allowed.
- You can write helper functions.
For example:
Test Result
Reset answer
Đúng
Marks for this submission: 1,00/1,00.
Câu hỏi 8
Đúng
The function returns the length of the longest subarray where all words share the same first letter.
Note:
- The iostream and vector libraries have been included and namespace std is being used. No other libraries are allowed.
- You can write helper functions.
For example:
Test Result
Reset answer
Đúng
Marks for this submission: 1,00/1,00.