All SolutionsAll Solutions
⌨️
Run-Length Encoding
Week 34, 2026
Rune by rune scan | greenya | Odin Solutions
package main
import "core:fmt"
main :: proc () {
text := #load("input.txt", string)
state: State
for r in text do state_push(&state, r)
state_print(state)
fmt.println()
}
State :: struct {
rune : rune,
count : int,
}
state_push :: proc (s: ^State, r: rune) {
if s.rune == r {
assert(s.count > 0)
s.count += 1
} else {
state_print(s^)
s^ = { r, 1 }
}
}
state_print :: proc (s: State) {
if s.count > 0 {
fmt.printf("%i%r", s.count, s.rune)
}
}
Python - Two Pointers | BMC | Python Solutions
def ReadFile(fileName:str) -> str:
mainTxt = ""
fileName += '.txt' if fileName[-4:] != ".txt" else ""
try:
file = open(fileName, 'r')
for line in file:
mainTxt = line.rstrip('\n')
file.close()
except FileNotFoundError as e:
print("File not found")
print(e)
finally:
return mainTxt
def WriteFile(someString: str) -> None:
# Writes a single line only
try:
file = open("ans.txt", 'w')
file.write(someString)
file.close()
except:
print("Error occured while wrirting file.")
finally:
print("WriteFile has ended.")
def toRLE(someString: str) -> str:
listRLE = []
startPtr = 0
checkPtr = 0
lenSS = len(someString)
while startPtr < lenSS:
if someString[startPtr] == someString[checkPtr]:
startPtr += 1
continue
listRLE.append(f"{startPtr-checkPtr}{someString[checkPtr]}")
checkPtr = startPtr
listRLE.append(f"{startPtr-checkPtr}{someString[checkPtr]}")
return "".join(listRLE)
def main() -> None:
x = toRLE(ReadFile("data.txt"))
WriteFile(x)
# blah blah
main()