Skip to content

Commit a3d1176

Browse files
committed
added recursion test
1 parent 76ceb33 commit a3d1176

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

testRecursion.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import random, timeit
2+
3+
def getLine(syllablesRemaining, s):
4+
while syllablesRemaining > 0:
5+
toGet = random.randint(1,syllablesRemaining)
6+
syllablesRemaining -= toGet
7+
idx = random.randint(0,len(words[toGet])-1)
8+
s += words[toGet][idx]
9+
return s
10+
11+
def getLineRecursive(syllablesRemaining, s):
12+
s = ""
13+
while syllablesRemaining > 0:
14+
toGet = random.randint(1,syllablesRemaining)
15+
syllablesRemaining -= toGet
16+
idx = random.randint(0,len(words[toGet])-1)
17+
s += words[toGet][idx]
18+
return s
19+
20+
fIn = open('HaikuSource.txt','r')
21+
lineNum = 186524
22+
words = [[],[],[],[],[],[],[]]
23+
#Initalize arrays
24+
while lineNum > 0:
25+
lineNum -= 1
26+
s = fIn.readline()
27+
s = s.replace('\n','')
28+
syllables = int(s[-1])
29+
s = s[:-1] #keep the space at the end
30+
if syllables < 8:
31+
words[syllables-1].append(s)
32+
33+
#Timer for iterative solution
34+
iterative = timeit.Timer('getLine(syllables, s)', setup='syllables = 5; s = ""')#Yikes, semicolons!
35+
#Timer for recursive solution
36+
recursive = timeit.Timer('getLineRecursive(syllables, s)', setup='syllables = 5; s = ""')
37+
38+

0 commit comments

Comments
 (0)