Repository navigation
Expand file tree
/
Copy patharrays.sol
More file actions
73 lines (62 loc) · 2.15 KB
/
Copy patharrays.sol
File metadata and controls
73 lines (62 loc) · 2.15 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ArraysExercise {
uint[] public numbers = [1,2,3,4,5,6,7,8,9,10];
address[] public senders;
uint[] public timestamps;
// Return the complete numbers array
function getNumbers() public view returns (uint[] memory) {
return numbers;
}
// Reset numbers array to initial value (1-10)
// Using more gas-efficient approach without .push()
function resetNumbers() public {
// Clear the array first
delete numbers;
// Recreate with initial values
numbers = [1,2,3,4,5,6,7,8,9,10];
}
// Append array to existing numbers array
function appendToNumbers(uint[] calldata _toAppend) public {
for (uint i = 0; i < _toAppend.length; i++) {
numbers.push(_toAppend[i]);
}
}
// Save timestamp with caller address
function saveTimestamp(uint _unixTimestamp) public {
senders.push(msg.sender);
timestamps.push(_unixTimestamp);
}
// Filter timestamps after Y2K (January 1, 2000, 12:00am)
// Unix timestamp: 946702800
function afterY2K() public view returns (uint[] memory, address[] memory) {
// First, count how many timestamps are after Y2K
uint count = 0;
for (uint i = 0; i < timestamps.length; i++) {
if (timestamps[i] > 946702800) {
count++;
}
}
// Create arrays with the correct size
uint[] memory filteredTimestamps = new uint[](count);
address[] memory filteredSenders = new address[](count);
// Fill the arrays with filtered data
uint index = 0;
for (uint i = 0; i < timestamps.length; i++) {
if (timestamps[i] > 946702800) {
filteredTimestamps[index] = timestamps[i];
filteredSenders[index] = senders[i];
index++;
}
}
return (filteredTimestamps, filteredSenders);
}
// Reset senders array
function resetSenders() public {
delete senders;
}
// Reset timestamps array
function resetTimestamps() public {
delete timestamps;
}
}