Skip to content

Commit c0a70ec

Browse files
authored
Merge pull request #70 from victorKariuki/main
Hisabati package update
2 parents 7732a4b + b2de172 commit c0a70ec

File tree

5 files changed

+453
-209
lines changed

5 files changed

+453
-209
lines changed

examples/Astart.nr

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/*############ A*(A-star) Algorithm ##############
2+
3+
By @VictorKariuki
4+
5+
https://github.com/VictorKariuki
6+
7+
################################################*/
8+
9+
10+
// create a list of numbers
11+
fanya list = unda(first,last,interval){
12+
fanya list = [first];
13+
fanya i = first + interval;
14+
wakati(i < last){
15+
list.sukuma(i)
16+
i+=interval;
17+
}
18+
rudisha list;
19+
}
20+
21+
// Maths functions
22+
// find the absolute value of a number
23+
fanya abs_namba = unda(namba){
24+
kama(namba < 0){
25+
rudisha -1 * namba;
26+
}
27+
28+
rudisha namba;
29+
}
30+
31+
// square a number
32+
fanya square = unda(n, i, j){
33+
fanya kati = (i+j)/2;
34+
fanya mul = kati * kati;
35+
fanya abs_diff = abs_namba(mul-n);
36+
37+
kama (mul == n || abs_diff < 0.00001){
38+
rudisha kati;
39+
}au kama(mul < n){
40+
rudisha square(n,kati,j)
41+
}au{
42+
rudisha square(n,i,kati)
43+
}
44+
}
45+
46+
// find the square root of a number
47+
fanya sqrt = unda(namba){
48+
kwa i ktk list(0,namba,1) {
49+
kama((i*i )== namba){
50+
rudisha i;
51+
}au kama ((i*i )> namba){
52+
rudisha square(namba,i-1,i)
53+
}
54+
}
55+
}
56+
57+
// Main function
58+
fanya aStar = unda(start, goal) {
59+
// Initialize the open and closed lists
60+
fanya openList = [start];
61+
fanya closedList = [];
62+
63+
fanya reconstructPath = unda(node) {
64+
fanya path = [node];
65+
wakati (node["parent"]) {
66+
path = [node["parent"]] + path;
67+
node = node["parent"];
68+
}
69+
rudisha path;
70+
}
71+
72+
fanya heuristic = unda(node1, node2) {
73+
// Calculate the Euclidean distance between the nodes' positions
74+
fanya dx = node1["x"] - node2["x"];
75+
fanya dy = node1["y"] - node2["y"];
76+
rudisha sqrt(dx * dx + dy * dy);
77+
}
78+
79+
fanya findMinNode = unda(openList) {
80+
fanya i = 1;
81+
fanya minNode = openList[0];
82+
83+
wakati (i < openList.idadi()) {
84+
fanya node = openList[i];
85+
kama (node["f"] < minNode["f"]) {
86+
minNode = node;
87+
}
88+
i++
89+
}
90+
91+
rudisha minNode;
92+
}
93+
94+
fanya removeNodeFromArray = unda(array, node) {
95+
fanya newArray = [];
96+
fanya i = 1;
97+
wakati (i < array.idadi()) {
98+
kama (array[i] != node) {
99+
newArray.sukuma(array[i]);
100+
}
101+
i++;
102+
}
103+
rudisha newArray;
104+
}
105+
106+
fanya urefu = unda(node1, node2) {
107+
// Assume all edges have a cost of 1
108+
rudisha 1;
109+
}
110+
111+
// Initialize the g and f scores of the starting node
112+
start["g"] = 0;
113+
start["f"] = start["g"] + heuristic(start, goal);
114+
115+
116+
117+
// Start the search loop
118+
wakati (openList.idadi() > 0) {
119+
// Find the node with the lowest f score in the open list
120+
fanya current = findMinNode(openList);
121+
122+
// Check kama the goal node has been reached
123+
kama (current == goal) {
124+
rudisha reconstructPath(current);
125+
}
126+
127+
// Move the current node from the open to the closed list
128+
openList = removeNodeFromArray(openList, current);
129+
130+
closedList.sukuma(current);
131+
132+
// Explore the neighbors of the current node
133+
kwa neighbor ktk current["neighbors"] {
134+
// Skip neighbors that are in the closed list
135+
kama (neighbor ktk closedList) {
136+
endelea
137+
}
138+
139+
// Calculate the tentative g score of the neighbor
140+
fanya tentativeG = start["g"] + urefu(current, neighbor);
141+
142+
// Check kama the neighbor is in the open list
143+
fanya tentativeIsBetter = sikweli;
144+
kama (!(neighbor ktk openList)) {
145+
openList.sukuma(neighbor);
146+
tentativeIsBetter = kweli;
147+
} au kama (tentativeG < neighbor["g"]) {
148+
tentativeIsBetter = kweli;
149+
}
150+
151+
// Update the neighbor's g score kama the tentative score is better
152+
kama (tentativeIsBetter) {
153+
neighbor["g"] = tentativeG;
154+
neighbor["f"] = neighbor["g"] + heuristic(neighbor, goal);
155+
neighbor["parent"] = current;
156+
}
157+
}
158+
}
159+
160+
// kama the open list is empty, no path was found
161+
rudisha tupu;
162+
}
163+
164+
// Define the nodes of the graph
165+
fanya nodeA = { "x": 0, "y": 0, "neighbors": [] };
166+
fanya nodeB = { "x": 1, "y": 2, "neighbors": [] };
167+
fanya nodeC = { "x": 3, "y": 1, "neighbors": [] };
168+
fanya nodeD = { "x": 4, "y": 3, "neighbors": [] };
169+
170+
// Define the edges between the nodes
171+
nodeA["neighbors"] = [nodeB];
172+
nodeB["neighbors"] = [nodeA, nodeC];
173+
nodeC["neighbors"] = [nodeB, nodeD];
174+
nodeD["neighbors"] = [nodeC];
175+
176+
// Call the A* function with the start and goal nodes and the heuristic and distance functions
177+
//fanya path = aStar(nodeA, nodeC);
178+
179+
andika(nodeA);

examples/reduce.nr

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
fanya reduce = unda(iterator, callback, initialValue) {
2+
fanya accumulator = initialValue;
3+
4+
kwa thamani ktk iterator {
5+
accumulator = callback(accumulator, thamani);
6+
}
7+
8+
rudisha accumulator;
9+
}
10+
11+
fanya list = [1,2,3,4,5];
12+
fanya employees = [{"salary":120},{"salary":135},{"salary":140}]
13+
14+
fanya sum = unda(acc,value){
15+
rudisha acc + value;
16+
}
17+
18+
fanya mul = unda(acc,value){
19+
rudisha acc * value;
20+
}
21+
22+
fanya sumSalo = unda(acc,value){
23+
rudisha acc + value["salary"];
24+
}
25+
26+
fanya sumSaloWithTax = unda(acc,value){
27+
rudisha acc + (value["salary"] * (1-0.34));
28+
}
29+
30+
andika(reduce(list,sum,0))
31+
andika(reduce(list,mul,1))
32+
33+
andika(reduce(employees,sumSalo,0))
34+
andika(reduce(employees,sumSaloWithTax,0))

examples/sarufi.nr

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
tumia mtandao
22
tumia jsoni
33
pakeji sarufi {
4-
andaa = unda(file) {
5-
config = fungua(file)
6-
configString = config.soma()
7-
configDict = jsoni.dikodi(configString)
8-
clientID = configDict["client_id"]
9-
clientSecret = configDict["client_secret"]
10-
params = {"client_id": clientID, "client_secret": clientSecret}
11-
tokenString = mtandao.tuma(yuareli="https://api.sarufi.io/api/access_token", mwili=params)
12-
tokenDict = jsoni.dikodi(tokenString)
13-
@.token = tokenDict["access_token"]
14-
@.Auth = "Bearer " + @.token
15-
}
4+
andaa = unda(file) {
5+
config = fungua(file)
6+
configString = config.soma()
7+
configDict = jsoni.dikodi(configString)
8+
clientID = configDict["client_id"]
9+
clientSecret = configDict["client_secret"]
10+
params = {"client_id": clientID, "client_secret": clientSecret}
11+
tokenString = mtandao.tuma(yuareli="https://api.sarufi.io/api/access_token", mwili=params)
12+
tokenDict = jsoni.dikodi(tokenString)
13+
@.token = tokenDict["access_token"]
14+
@.Auth = "Bearer " + @.token
15+
}
1616

17-
tokenYangu = unda() {
18-
rudisha @.token
19-
}
17+
tokenYangu = unda() {
18+
rudisha @.token
19+
}
2020

21-
tengenezaChatbot = unda(data) {
22-
majibu = mtandao.tuma(yuareli="https://api.sarufi.io/chatbot", vichwa={"Authorization": @.Auth}, mwili = data)
23-
rudisha majibu
24-
}
21+
tengenezaChatbot = unda(data) {
22+
majibu = mtandao.tuma(yuareli="https://api.sarufi.io/chatbot", vichwa={"Authorization": @.Auth}, mwili = data)
23+
rudisha majibu
24+
}
2525

26-
pataChatbotZote = unda() {
27-
majibu = mtandao.peruzi(yuareli="https://api.sarufi.io/chatbots", vichwa={"Authorization": @.Auth})
28-
rudisha majibu
29-
}
30-
}
26+
pataChatbotZote = unda() {
27+
majibu = mtandao.peruzi(yuareli="https://api.sarufi.io/chatbots", vichwa={"Authorization": @.Auth})
28+
rudisha majibu
29+
}
30+
}
3131

3232

0 commit comments

Comments
 (0)