-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorTriageExercise.sol
More file actions
70 lines (58 loc) · 1.96 KB
/
ErrorTriageExercise.sol
File metadata and controls
70 lines (58 loc) · 1.96 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract ErrorTriageExercise {
/**
* Finds the difference between each uint with it's neighbor (a to b, b to c, etc.)
* and returns a uint array with the absolute integer difference of each pairing.
*/
function diffWithNeighbor(
uint _a,
uint _b,
uint _c,
uint _d
) public pure returns (uint[] memory) {
uint[] memory results = new uint[](3);
// Fix: Need absolute difference, handle when first value is smaller
results[0] = _a > _b ? _a - _b : _b - _a;
results[1] = _b > _c ? _b - _c : _c - _b;
results[2] = _c > _d ? _c - _d : _d - _c;
return results;
}
/**
* Changes the _base by the value of _modifier. Base is always >= 1000. Modifiers can be
* between positive and negative 100;
*/
function applyModifier(
uint _base,
int _modifier
) public pure returns (uint) {
// Fix: Need to convert int to uint properly, handle negative modifiers
if (_modifier >= 0) {
return _base + uint(_modifier);
} else {
return _base - uint(-_modifier);
}
}
/**
* Pop the last element from the supplied array, and return the popped
* value (unlike the built-in function)
*/
uint[] arr;
function popWithReturn() public returns (uint) {
require(arr.length > 0, "Array is empty");
uint index = arr.length - 1;
uint value = arr[index]; // Fix: Store value before deleting
arr.pop(); // Fix: Use pop() instead of delete to actually remove element
return value;
}
// The utility functions below are working as expected
function addToArr(uint _num) public {
arr.push(_num);
}
function getArr() public view returns (uint[] memory) {
return arr;
}
function resetArr() public {
delete arr;
}
}