Repository navigation
Expand file tree
/
Copy pathtest_code.txt
More file actions
119 lines (93 loc) · 3.51 KB
/
Copy pathtest_code.txt
File metadata and controls
119 lines (93 loc) · 3.51 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
// Statistical operations
let data: array<float> = [1.0, 2.0, 3.0, 4.0, 5.0];
data.mean();
print("Mean:", data_mean);
// Test other operations
data.median();
print("Median:", data_median);
data.variance();
print("Variance:", data_variance);
data.stddev();
print("Standard Deviation:", data_stddev);
// Linear regression
let x: array<float> = [1.0, 2.0, 3.0, 4.0, 5.0];
let y: array<float> = [2.1, 4.0, 6.3, 8.0, 9.9];
x.linreg(y);
print("Linear Regression Results:");
print("Slope:", x_slope);
print("Intercept:", x_intercept);
print("R-squared:", x_r_squared);
// Matrix operations
let matrix1: array<array<float>> = [[1.0, 2.0], [3.0, 4.0]];
let matrix2: array<array<float>> = [[5.0, 6.0], [7.0, 8.0]];
matrix1.add(matrix2);
print("Matrix Addition Result:", matrix1_add);
matrix1.multiply(matrix2);
print("Matrix Multiplication Result:", matrix1_multiply);
matrix1.invert();
print("Matrix Inversion Result:", matrix1_invert);
matrix1.transpose();
print("Matrix Transpose Result:", matrix1_transpose);
// Pattern matching example
let value: int = 42;
match value {
case 0 => print("Value is zero");
case 42 => print("Value is forty two");
case _ => print("Value is something else");
}
// Array pattern matching
let arr: array<int> = [1, 2, 3];
match arr {
case [1, 2, 3] => print("Array matches [1, 2, 3]");
case _ => print("Array does not match");
}
// Array rotation and shifting
let nums: array<int> = [1, 2, 3, 4, 5];
nums.rotate(1);
print("Rotated Array (by 1):", nums_rotate);
nums.rotate(2);
print("Rotated Array (by 2):", nums_rotate);
nums.rotate(3);
print("Rotated Array (by 3):", nums_rotate);
nums.shift(2);
print("Shifted Array (by 2):", nums_shift);
let numbers: array<int> = [1, 2, 3, 4, 5];
numbers.filter(x => x % 2 != 0);
print("Filtered (even numbers):", numbers_filter);
numbers.filter(x => x > 2);
print("Filtered (greater than 2):", numbers_filter);
let numbers: array<int> = [1, 2, 3, 4, 5];
numbers.map(x => x * 2);
print("Mapped Array (doubled values):", numbers_map);
numbers.map(x => x + 10);
print("Mapped Array (incremented by 10):", numbers_map);
// "Mary Had a Little Lamb" (complete)
let melody1: array<float> = [3,2,1,2,3,3,3, 2,2,2, 3,5,5, 3,2,1,2,3,3,3, 3,2,2,3,2,1];
print("Playing Mary Had a Little Lamb...");
melody1.play();
// "Twinkle Twinkle Little Star" (complete)
let melody2: array<float> = [0,0,4,4,5,5,4, 3,3,2,2,1,1,0, 4,4,3,3,2,2,1, 4,4,3,3,2,2,1, 0,0,4,4,5,5,4, 3,3,2,2,1,1,0];
print("Playing Twinkle Twinkle...");
melody2.play();
// "Row Row Row Your Boat" (complete with rounds)
let melody3: array<float> = [0,0,0,1,2, 2,1,2,3,4, 7,7,7,4,4,4, 2,2,2,1,1,1, 4,4,4,2,2,2, 0,0,0,1,2, 2,1,2,3,4];
print("Playing Row Row Row Your Boat...");
melody3.play();
// "Hot Cross Buns" (complete with repeats)
let melody4: array<float> = [3,2,1, 3,2,1, 1,1,1,1, 2,2,2,2, 3,2,1, 3,2,1, 3,2,1, 1,1,1,1, 2,2,2,2, 3,2,1];
print("Playing Hot Cross Buns...");
melody4.play();
// "Frère Jacques" (complete with repeats)
let melody5: array<float> = [0,1,2,0, 0,1,2,0, 2,3,4, 2,3,4, 4,5,4,3,2,0, 4,5,4,3,2,0, 0,4,0, 0,4,0, 0,1,2,0, 0,1,2,0];
print("Playing Frère Jacques...");
melody5.play();
// "London Bridge" (complete)
let melody6: array<float> = [4,5,4,3,2,3,4, 2,3,4,3,4,5, 4,5,4,3,2,3,4, 2,4,0, 4,5,4,3,2,3,4, 2,3,4,3,4,5, 4,5,4,3,2,3,4, 2,4,0];
print("Playing London Bridge...");
melody6.play();
// "Baa Baa Black Sheep" (complete)
let melody7: array<float> = [0,0,4,4,5,5,4, 3,3,2,2,1,1,0, 4,4,3,3,2, 4,4,3,3,2, 0,0,4,4,5,5,4, 3,3,2,2,1,1,0];
print("Playing Baa Baa Black Sheep...");
melody7.play();
"""