Author Topic: Loop all possible words algorithm  (Read 26771 times)

0 Members and 2 Guests are viewing this topic.

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Loop all possible words algorithm
« Reply #30 on: August 05, 2011, 03:46:31 pm »
ephan, here is a working python version (fixed your code):
Code: (Python) [Select]

def increment(x):
    """Increments the string x"""
    length_of_string = len(x)
    if length_of_string == 0:
        return "a"
    bt = ord(x[-1:])
    if bt < 122:
        x = x[:-1] + chr(bt+1)
    else:
        x = increment(x[:-1]) + "a"
    return x
   
def main():
    """Displays all possible words using alphabet a-z"""
    a = "a"
   
    for i in range(1000):
        a = increment(a)
        print a
 
if __name__ == "__main__":
    main()

Offline phenomist

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 132
  • Rating: +46/-3
    • View Profile
Re: Loop all possible words algorithm
« Reply #31 on: August 05, 2011, 05:12:17 pm »
Also, pseudocode example for really fast generation:

Code: [Select]
initarray := [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]
mod := initarray
while 1:{
for loopvar from 1 to dim(mod):{
output mod[loopvar]}
newmod := []
for loopvar from 1 to 26:{
temp := initarray[loopvar] + mod
newmod := newmod + temp
}
mod := newmod
}

ok this really kills the memory, whoops
and you'll have really long pauses for when the computer is calculating the next string length

EDIT: figured out a constant-time algorithm, same memory problems though
pseudocoded for viewing pleasure:
Code: [Select]
list := [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
queue := list
while forever{
pop queue => string
print string
concatenate string, list => list2
push list2 into queue
}
« Last Edit: August 06, 2011, 03:59:22 am by phenomist »
Level Designer for Graviter

[Disclaimer: I can't program for my life.]