Here are a few example programs written in SauravCode:
1. Find the Maximum Value in a List
function findmax list
maxvalue = list.index
for element in list
if element > maxvalue
maxvalue = element
end
return maxvalue
end
# Example usage
list = 1 3 2 8 5
result = findmax list
# result 8
2. DFS for Traversing a Graph
function dfs graph start visited
visited.add start
for neighbor in graph.start
if not visited.contains neighbor
dfs graph neighbor visited
end
end
end
# Example usage
graph = map
graph.put 1 list 2 3
graph.put 2 list 4
graph.put 3 list 5
graph.put 4 list
graph.put 5 list
visited = set
start = 1
dfs graph start visited
# visited will contain 1 2 3 4 5
3. Sliding Window: Maximum Sum of k Consecutive Elements
function maxsumkconsecutive list k
maxsum = 0
currentsum = 0
for i = 0 to k - 1
currentsum = currentsum + list.i
end
maxsum = currentsum
for i = k to list.size - 1
l = i - k
currentsum = currentsum - list.l + list.i
currentmax = max maxsum currentsum
maxsum = currentmax
end
return maxsum
end
# Example usage
list = 2 1 5 1 3 2
k = 3
result = maxsumkconsecutive list k
# result 9