-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path011_is_sorted.sio
More file actions
81 lines (71 loc) · 1.7 KB
/
011_is_sorted.sio
File metadata and controls
81 lines (71 loc) · 1.7 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
//@ run-pass
// HumanEval 011: Is Sorted
//
// Check if an integer array is sorted in non-decreasing order.
// Returns 1 if sorted, 0 otherwise.
fn is_sorted(arr: [i64; 256], n: i64) -> i64 with Mut, Panic {
if n <= 1 { return 1 }
var i: i64 = 0
while i < n - 1 {
if arr[i] > arr[i + 1] {
return 0
}
i = i + 1
}
1
}
fn main() -> i64 with IO, Mut, Panic, Div {
// Test 1: sorted array [1, 2, 3, 4, 5]
var a1: [i64; 256] = [0; 256]
a1[0] = 1
a1[1] = 2
a1[2] = 3
a1[3] = 4
a1[4] = 5
assert(is_sorted(a1, 5) == 1)
// Test 2: unsorted array [3, 1, 2]
var a2: [i64; 256] = [0; 256]
a2[0] = 3
a2[1] = 1
a2[2] = 2
assert(is_sorted(a2, 3) == 0)
// Test 3: single element
var a3: [i64; 256] = [0; 256]
a3[0] = 42
assert(is_sorted(a3, 1) == 1)
// Test 4: empty array
var a4: [i64; 256] = [0; 256]
assert(is_sorted(a4, 0) == 1)
// Test 5: all equal [5, 5, 5, 5]
var a5: [i64; 256] = [0; 256]
a5[0] = 5
a5[1] = 5
a5[2] = 5
a5[3] = 5
assert(is_sorted(a5, 4) == 1)
// Test 6: descending [5, 4, 3, 2, 1]
var a6: [i64; 256] = [0; 256]
a6[0] = 5
a6[1] = 4
a6[2] = 3
a6[3] = 2
a6[4] = 1
assert(is_sorted(a6, 5) == 0)
// Test 7: sorted with negatives [-3, -1, 0, 4, 7]
var a7: [i64; 256] = [0; 256]
a7[0] = 0 - 3
a7[1] = 0 - 1
a7[2] = 0
a7[3] = 4
a7[4] = 7
assert(is_sorted(a7, 5) == 1)
// Test 8: unsorted at the end [1, 2, 3, 0]
var a8: [i64; 256] = [0; 256]
a8[0] = 1
a8[1] = 2
a8[2] = 3
a8[3] = 0
assert(is_sorted(a8, 4) == 0)
println("011_is_sorted: ALL TESTS PASSED")
0
}