Funny word generator

Back to General discussions forum

padraig_beag     2015-06-30 20:25:22

Hi, I'm trying to solve this exercise and have reached a bit of a blank wall. Here is my code attempt. Bear in mind I am fairlynew to programming , so excuse me if the code looks a bit silly. This is what I came up with anyway

def nthmem(a, c, m, x0, n):# as solved in linear congruental generator problem

n1 = 0
while n1 != n:
    xnext = (a * x0 + c) % m
    x0 = xnext
    n1 = n1 + 1

return xnext

nums = [4, 5, 6]# sample data for each in nums:

word = ""
con = "bcdfghjklmnprstvwxz"
vow = "aeiou";  
a = 445     # 
c = 700001  # given 3 values to use in exercise
m = 2097152 #
x0 = 0 # seed value
n = each

data = [] #will be used to hold individual digits

for n in range(1, n + 1):
    data.append(nthmem(a, c, m, x0, n)) 

if len(data) % 2  == 0:# handles words of even length
    for i in range(0, len(data), 2):
        word = word + con[data[i] % 19] + vow[data[i + 1] % 5]

else:
    for i in range(0, len(data) - 1, 2):# handles words of odd length
        word = word + con[data[i] % 19] + vow[data[i + 1] % 5]

    word = word + con[data[len(data) - 1] % 19]

print word,

The problem is that the output gives

fami famiw famiwo

instead of the expected output from the example.

As far as I can see, there is something wrong with the way the random is being generated. If it is possible to give a little hint which might set me on the right track, I would appreciate it.

Thanks

Guy Gervais     2015-06-30 22:40:42
User avatar

You must not restart the LCG between each word. Once the LCG starts giving values, you have to keep it going until the end of that input.

padraig_beag     2015-07-01 21:21:30

Thanks a lot, I can now see where I was going wrong in using the lcg . I still have to figure out how to automate getting the output list of funny words. I can do it manually ok and get the words, but it is very tedious. As I said I'm fairly new at programming, so have to basically figure out how to break up a list of integers into a bunch of individual lists, each with length of the required words

Thanks again!

ancastillov     2022-10-27 22:46:09

I got the same problem, i think the exercise is not writting on the beeter way to explain

Please login and solve 5 problems to be able to post at forum