Add subs and grph

This commit is contained in:
Alberto Venturini 2024-09-04 06:54:24 +02:00
parent b340f05ef4
commit 49127ae4ef
7 changed files with 174 additions and 1 deletions

2
07_subs/sample.txt Normal file
View file

@ -0,0 +1,2 @@
GATATATGCATATACTT
ATAT

29
07_subs/subs.nim Normal file
View file

@ -0,0 +1,29 @@
import os
import std/streams
import std/sequtils
from std/strutils import join
proc findNeedleInHaystack(s, t: string): seq[int] =
var occurrences = newSeq[int]()
for i in 0 ..< s.len:
var j = 0
while j < t.len and (i+j) < s.len and s[i+j] == t[j]:
inc j
if j == t.len:
occurrences.add(i+1)
return occurrences
proc parseStream(stream: Stream): (string, string) =
let s = readLine(stream)
let t = readLine(stream)
return (s, t)
let fileName = paramStr(1)
let fileStream = newFileStream(fileName)
if not isNil(fileStream):
let (s, t) = parseStream(fileStream)
let occurrences = findNeedleInHaystack(s, t)
echo occurrences.mapIt($it).join(" ")
fileStream.close()