0% found this document useful (0 votes)
28 views

Array List - LAB1

Array List_ LAB1

Uploaded by

vohuynhanhth79
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Array List - LAB1

Array List_ LAB1

Uploaded by

vohuynhanhth79
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS

Trạng thái Đã xong


Bắt đầu vào lúc Thứ Ba, 24 tháng 9 2024, 9:30 PM
Kết thúc lúc Thứ Tư, 25 tháng 9 2024, 4:25 PM
Thời gian thực 18 giờ 55 phút
hiện
Điểm 7,00/7,00
Điểm 10,00 trên 10,00 (100%)

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 1/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS

Câu hỏi 1
Đúng

Đạt điểm 1,00 trên 1,00

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];}

~ArrayList(){ delete[] data; }


void add(T e);
void add(int index, T e);
int size();
void ensureCapacity(int index);
};

For example:

Test Result

ArrayList<int> arr; [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


int size = 10; 10

for(int index = 0; index < size; index++){


arr.add(index);
}

cout << arr.toString() << '\n';


cout << arr.size();

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

for(int index = 0; index < size; index++){


arr.add(0, index);
}

cout << arr.toString() << '\n';


cout << arr.size() << '\n';
arr.ensureCapacity(5);

Answer: (penalty regime: 0, 0, 0, 0, 0, 100 %)

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){
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 2/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
11 int new_capacity = capacity * 1.5;
12 T *tmp = new T[new_capacity];
13 ▼ for(int i = 0; i < count; i++){
14 tmp[i] = data[i];
15 }
16 delete [] data;
17 data = tmp;
18 capacity = new_capacity;
19 }
20
21 }
22
23 template <class T>
24 ▼ void ArrayList<T>::add(T e) {
25 /* Insert an element into the end of the array. */
26 count++;
27 ensureCapacity(count);
28 data[count - 1] = e;
29
30 }
31
32 template<class T>
33 ▼ void ArrayList<T>::add(int index, T e) {
34 ▼ /*
35 Insert an element into the array at given index.
36 if index is invalid:
37 throw std::out_of_range("the input index is out of range!");
38 */
39 count++;
40 ensureCapacity(count);
41 ▼ for (int i = size() - 1; i > index; i--){
42 data[i] = data[i - 1];
43 }
44 data[index] = e;
45
46 }
47
48 template<class T>
49 ▼ int ArrayList<T>::size() {
50 /* Return the length (size) of the array */
51 return count;
52 }
53

Test Expected Got

 ArrayList<int> arr; [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 


int size = 10; 10 10

for(int index = 0; index <


size; index++){
arr.add(index);
}

cout << arr.toString() << '\n';


cout << arr.size();

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 3/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS

Test Expected Got

 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);
}

cout << arr.toString() << '\n';


cout << arr.size() << '\n';
arr.ensureCapacity(5);

Passed all tests! 

Đúng
Marks for this submission: 1,00/1,00.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 4/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS

Câu hỏi 2
Đúng

Đạt điểm 1,00 trên 1,00

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; }

void add(T e);


void add(int index, T e);
int size();
bool empty();
void clear();
T get(int index);
void set(int index, T e);
int indexOf(T item);
bool contains(T item);
T removeAt(int index);
bool removeItem(T item);

void ensureCapacity(int index);

};

For example:

Test Result

ArrayList<int> arr; [1, 2, 3, 4, 5, 6, 7, 8, 9]


9
for (int i = 0; i < 10; ++i) {
arr.add(i);
}
arr.removeAt(0);

cout << arr.toString() << '\n';


cout << arr.size();

ArrayList<int> arr; [0, 1, 2, 3, 4, 5, 6, 7, 8]


9
for (int i = 0; i < 10; ++i) {

arr.add(i);
}
arr.removeAt(9);

cout << arr.toString() << '\n';


cout << arr.size();

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 5/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS

Test Result

ArrayList<int> arr; [0, 1, 2, 3, 4, 6, 7, 8, 9]


9
for (int i = 0; i < 10; ++i) {
arr.add(i);
}
arr.removeAt(5);

cout << arr.toString() << '\n';


cout << arr.size();

Answer: (penalty regime: 0, 0, 0, 0, 0, 100 %)

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 >= count || index < 0 || count == 0){
9 throw std::out_of_range("The index is out of range!");
10 }
11
12 count--;
13 ensureCapacity(count);
14 T removedItem = data[index];
15
16 for(int i = index; i < size(); i++)
17 ▼ {
18 data[i] = data[i + 1];
19 }
20 return removedItem;
21 }
22
23 template<class T>
24 ▼ bool ArrayList<T>::removeItem(T item){
25 /* Remove the first apperance of item in array and return true, otherwise return
26 ▼ for(int i = 0; i < count; i++){
27 ▼ if(data[i] == item){
28 ▼ for(int j = i; j < count - 1; j++){
29 data[j] = data[j + 1];
30 }
31 count--;
32 return true;
33 }
34 }
35 return false;
36 }
37
38 template<class T>
39 ▼ void ArrayList<T>::clear(){
40 ▼ /*
41 Delete array if array is not NULL
42 Create new array with: size = 0, capacity = 5
43 */
44 count = 0;
45 capacity = 5;
46 for (int i = 0; i < count; i++) 
47 ▼ {
48 data[i] = 0;
49 }
50 delete[] this->data;
51 data = NULL;
52 this->data = new T[this->capacity];
53 }
54
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 6/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS

Test Expected Got

 ArrayList<int> arr; [1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 3, 4, 5, 6, 7, 8, 9] 


9 9
for (int i = 0; i < 10; ++i) {
arr.add(i);
}
arr.removeAt(0);

cout << arr.toString() << '\n';


cout << arr.size();

 ArrayList<int> arr; [0, 1, 2, 3, 4, 5, 6, 7, 8] [0, 1, 2, 3, 4, 5, 6, 7, 8] 


9 9
for (int i = 0; i < 10; ++i) {
arr.add(i);
}
arr.removeAt(9);

cout << arr.toString() << '\n';


cout << arr.size();

 ArrayList<int> arr; [0, 1, 2, 3, 4, 6, 7, 8, 9] [0, 1, 2, 3, 4, 6, 7, 8, 9] 


9 9
for (int i = 0; i < 10; ++i) {
arr.add(i);
}
arr.removeAt(5);

cout << arr.toString() << '\n';


cout << arr.size();

Passed all tests! 

Đúng
Marks for this submission: 1,00/1,00.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 7/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS

Câu hỏi 3
Không trả lời

Không chấm điểm

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:

ArrayList(){capacity = 5; count = 0; data = new T[5];}


~ArrayList(){ delete[] data; }

void add(T e);


void add(int index, T e);
int size();
bool empty();
void clear(); //remove data and set the list to the initial condition
T get(int index); //get the element at the index, if the index is out of range, "throw std::out_of_range("index
is out of range");"

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

ArrayList<int> arr; [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


int size = 10; 100
for(int index = 0; index < size; index++){ [100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
arr.add(index); []
} 1
cout << arr.toString() << '\n'; 7
arr.set(0,100); 0
cout << arr.get(0) << '\n';
cout << arr.toString() << '\n';
arr.clear();
cout << arr.toString() << '\n';
cout << arr.empty() << '\n'; 
for(int index = 0; index < size; index++){
arr.add(index);
}
cout << arr.indexOf(7) << '\n';
cout << arr.contains(15) << '\n';

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 8/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS

Test Result

ArrayList<int> arr; Index is out of range


int size = 10;
for(int index = 0; index < size; index++){
arr.add(index);
}
try {
arr.set(10,100);
cout << arr.get(10) << '\n';
}
catch(const std::exception & e){
cout << e.what() << endl;
}

Answer:
1

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 9/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 10/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS

Câu hỏi 4
Đúng

Đạt điểm 1,00 trên 1,00

Given an array of integers nums and a two-dimension array of integers operations.


Each operation in operations is represented in the form {L, R, X}. When applying an operation, all elements with index in
range [L, R] (include L and R) increase by X.
Your task is to implement a function with following prototype:
vector<int> updateArrayPerRange(vector<int>& nums, vector<vector<int>>& operations);

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

vector<int> nums {13, 0, 6, 9, 14, 16}; [21, 8, 14, 9, 14, 32]


vector<vector<int>> operations {{5, 5, 16}, {3, 4, 0}, {0, 2, 8}};
printVector(updateArrayPerRange(nums, operations));

Answer: (penalty regime: 0 %)

Reset answer

1 ▼ vector<int> updateArrayPerRange(vector<int>& nums, vector<vector<int>>& operations) {


2 // STUDENT ANSWER
3 ▼ for(const auto& op : operations){
4 int L = op[0];
5 int R = op[1];
6 int X = op[2];
7 ▼ for(int i = L; i <= R; i++){
8 nums[i] += X;
9 }
10 }
11 return nums;
12 }

Test Expected Got

 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));

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 11/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
Passed all tests! 

Đúng
Marks for this submission: 1,00/1,00.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 12/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS

Câu hỏi 5
Đúng

Đạt điểm 1,00 trên 1,00

Given an array of integers.


Your task is to implement a function with the following prototype:
bool consecutiveOnes(vector<int>& nums);

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

vector<int> nums {0, 1, 1, 1, 9, 8}; 1


cout << consecutiveOnes(nums);

Answer: (penalty regime: 0 %)

Reset answer

1 ▼ bool consecutiveOnes(vector<int>& nums) {


2 // STUDENT ANSWER
3 int n = nums.size();
4 if (n == 0) return true;
5
6 bool meetOne = false;
7 bool space = false;
8
9 ▼ for (int i = 0; i < n; i++) {
10 ▼ if (nums[i] == 1) {
11 if (space) return false;
12 meetOne = true;
13 }
14 else if (meetOne) space = true;
15 }
16 return true;
17 }

Test Expected Got

 vector<int> nums {0, 1, 1, 1, 9, 8}; 1 1 


cout << consecutiveOnes(nums);

 vector<int> nums {}; 1 1  


cout << consecutiveOnes(nums);

Passed all tests! 

Đúng
Marks for this submission: 1,00/1,00.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 13/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 14/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS

Câu hỏi 6
Đúng

Đạt điểm 1,00 trên 1,00

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.

Request: Implement function

buyCar(int* nums, int length, int k);

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:

nums=[90, 30, 20, 40, 50]; k=90;

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

int nums[] = {90,30,40,90,20}; 3


int length = sizeof(nums)/sizeof(nums[0]);
cout << buyCar(nums, length, 90) << "\n";

Answer: (penalty regime: 0 %)

Reset answer

1 ▼ int buyCar(int* nums, int length, int k) {


2
3 ▼ for(int i = 0; i < length - 1; i++){
4 ▼ for(int j = i + 1; j < length; j++){
5 ▼ if(nums[i] > nums[j]){
6 int a = nums[i];
7 nums[i] = nums[j];
8 nums[j] = a;
9 }
10 }
11 }
12
13 int count = 0, sum = 0;
14 ▼ for(int i = 0; i < length; i++){
15 ▼ if(sum + nums[i] <= k){
16 sum += nums[i];
17 count++;
18 }
19 else break;
20 }
21 return count;
22 }

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 15/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS

Test Expected Got

 int nums[] = {90,30,40,90,20}; 3 3 


int length = sizeof(nums)/sizeof(nums[0]);
cout << buyCar(nums, length, 90) << "\n";

Passed all tests! 

Đúng
Marks for this submission: 1,00/1,00.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 16/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS

Câu hỏi 7
Đúng

Đạt điểm 1,00 trên 1,00

Given an array of integers.


Your task is to implement a function with following prototype:

int equalSumIndex(vector<int>& nums);

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

vector<int> nums {3, 5, 2, 7, 6, 4}; 3


cout << equalSumIndex(nums);

Answer: (penalty regime: 0 %)

Reset answer

1 ▼ int equalSumIndex(vector<int>& nums) {


2 // STUDENT ANSWER
3 int n = nums.size();
4
5 int right_sum = 0, left_sum = 0;
6
7 ▼ for(int i = 0; i < n; i++){
8 right_sum += nums[i];
9 }
10
11 ▼ for(int i = 0; i < n; i++){
12 right_sum -= nums[i];
13 if(left_sum == right_sum) return i;
14 left_sum += nums[i];
15 }
16
17 return -1;
18 }

Test Expected Got

 vector<int> nums {3, 5, 2, 7, 6, 4}; 3 3 


cout << equalSumIndex(nums); 
 vector<int> nums {3}; 0 0 
cout << equalSumIndex(nums);

Passed all tests! 

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 17/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
Đúng
Marks for this submission: 1,00/1,00.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 18/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS

Câu hỏi 8
Đúng

Đạt điểm 1,00 trên 1,00

Given an array of strings.


Your task is to implement a function with following prototype:

int longestSublist(vector<string>& words);

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

vector<string> words {"faction", "fight", "and", "are", "attitude"}; 3


cout << longestSublist(words);

Answer: (penalty regime: 0 %)

Reset answer

1 ▼ int longestSublist(vector<string>& words) {


2 // STUDENT ANSWER
3
4 int n = words.size();
5 if(n == 0) return 0;
6 if(n == 1) return 1;
7
8 int res = 1;
9 int maxlen = 1;
10 char curr_wd = words[0][0];
11
12 ▼ for(int i = 1; i < n; i++){
13 char first_wd = words[i][0];
14 if(curr_wd == first_wd) res++;
15 ▼ else{
16 maxlen = max(maxlen, res);
17 res = 1;
18 curr_wd = first_wd;
19 }
20 }
21 return max(maxlen, res);
22 }

Test Expected Got

 vector<string> words {"faction", "fight", "and", "are", "attitude"}; 3 3 


cout << longestSublist(words);

 vector<string> words {}; 0 0 



cout << longestSublist(words);

Passed all tests! 

Đúng
Marks for this submission: 1,00/1,00.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 19/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 20/20

You might also like