python problem

Hello,

I have this problem a = [1, 11, 21, 1211, 111221
Now I need the length of the 30 number.

So i thought this would solve that:


getal = "1"
vorig = getal[0] 
aantal = 0
uitkomst2 = 0 
uitkomst = ""
einde = 30 
hoevaak = 1
while hoevaak < 30:
    for nummers in getal:
        if nummers == vorig :
            aantal = aantal + 1
            vorig = nummers
        else:
            getal2 = str(aantal)
            uitkomst = uitkomst + getal2 + vorig
            aantal = 1 
            vorig = nummers
    getal2 = str(aantal)
    uitkomst = uitkomst + getal2 + vorig
    uitkomst2 = uitkomst2 + len(uitkomst)
    hoevaak = hoevaak + 1
print uitkomst

So the answer is 78
But he real answer is 5805

What am I doing wrong ?

Roelof

I won’t solve your problem for you but my hint to you is to put a print statement inside the loop so you can see what is happening at each step. Hopefully you will see what the issue is.

Hello,

I already did that and the first 5 numbers are allright.
I think I made a thinking error.

I assume this :

1 will be 11
11 wil be 21
12 will be 1122
21 will be 1211
111 will be 31
222 will be 32

Is this the right ?

Roelof

Only you would know, you have the formula for getting the next number in the series, no?

Oke,

That’s right

But I found with google that It can be two solutions.
One is the Morris puzzle
Other is the row of Conway.

And I hope you can say that I understood the Morris solution.

Roelof

No you haven’t understood how to get the sequence correctly.

You were given your first 5 so what is the 6th(Well 30th really and my quick googling didn’t get 5805 for the length of the number either)?(1, 11, 21, 1211, 111221)

To highlight you mentioned these as the first 5…
(1,11,12,21,111,222)

P.S I found the sequence based around google searches for conway and morris.

No,

The first five are 1, 11, 21, 1211, 111221

So the sixth is according to me 312211

Roelof

Correct so now write a program to find the 30th and count the length.

Hint if you actually find the sequence name you’ll find people have done the home work programming isn’t my strong point but I do have the answer.

I found it twice one using lambda and one using a relatively simple function, sadly I bailed out at expanding it and just did a wc on the 30th to find the length.

Hello,

I wrote a programm. See first post but that gave me a wrong answer
So i try to figure out what I have done wrong.

Roelof

The answer is well wrong all numbers in the sequence are made from 123 as far as I’m aware.(Edit well less than you showed it would seem sometimes other digits do pop up but they didn’t stand out to me in the seq to 30 with a seed as 1)

You have the full range of digits from my quick look. Answer that gives you 79

112131415161718191101111121131141151161171181191201211221231241251261271281291

As Ken said throw in some print statements from my quick look the else isn’t even being triggered.

P.S.
Both solutions I’ve found seem to be using the length of the string to equate the next number any way. My crude glancing reckons you’re not doing enough with the len of the number to equate the next number in the sequence. I also suspect not enough nesting to equate it.

Oke,

Im not find the whole solution.

But this I understand.

1 => 11
2 => 12
11 => 21
21 = 2 - 1 so => 12 - 11 = 1211
1211 = 12 - 11 = 1 - 2 - 11 = 111221
111221 = 11 - 12 - 21 => 11 - 1 -2 - 2 -1 => 21 - 11 - 12 - 12 - 11 = 2111121211

Am i on the right track now ?

Roelof

You understand how to generate the sequence but you’re generating many sequences with different seeds you’ve been given the first 5 for your sequence you have the formula the sequence you’re interested in is…

1, 11, 21, 1211, 111221, 312211 … (Till you get to the 30th number generated in this sequence)

Your example isn’t a sequence. Where does 2 come from? Presuming a slip up whilst you’re getting the answer right I’m not sure whether you understand the how…
1211 => 111221
1211 = 11 12 2*1

You gave me the 6th in sequence …
312211
111221 = 31 22 1*1

maybe that will help finding the name of the sequence is really going to help I’m not sure you’ve found that out yet.

Hello

I thought Morris sequente.

So the next is :

111221 = 31 22 11
312211 = 1
3 13 22 21
13132211 = 1
1 13 13 22 11
11132211 = 13 13 22 21
13132221

etc.

Hmm, not find out how to make a python programm of that if I understand it right now.

Roelof

Missed the ref yes it is the Morris number sequence.

But you’re still not understanding the formula.
312211 = 13 13 22 21 = 13132221

Should be
312211 = 13’s 11’s 22’s 21’s (Maybe the 's will help) = 13112221

Really not quite sure what you’re doing to be honest.
Look-and-say sequence - Wikipedia, the free encyclopedia

Oke,

That was a typo.

So now find out how I can use vim as python editor.
I installed gvim and did all the steps from this page : VIM as Python IDE | Alain M. Lafon

But still no luck.

Roelof

I’d imagine your problem is sourcing the vimrc iirc gvim won’t use vimrc but can be told how to start with .vimrc or put them in .gvimrc (gvim --help). The instructions look pretty valid but been a while since I did it.

Hello,

I tried that.
But it won’t work.
I will make a new topic for it.

Roelof

On 2010-10-19, snowman1967 <snowman1967@no-mx.forums.opensuse.org> wrote:
>
> Hello,
>
> I have this problem a = [1, 11, 21, 1211, 111221
> Now I need the length of the 30 number.
>
> So i thought this would solve that:
>
> Code:
> --------------------
>
> getal = “1”
> vorig = getal[0]
> aantal = 0
> uitkomst2 = 0
> uitkomst = “”
> einde = 30
> hoevaak = 1
> while hoevaak < 30:
> for nummers in getal:
> if nummers == vorig :
> aantal = aantal + 1
> vorig = nummers
> else:
> getal2 = str(aantal)
> uitkomst = uitkomst + getal2 + vorig
> aantal = 1
> vorig = nummers
> getal2 = str(aantal)
> uitkomst = uitkomst + getal2 + vorig
> uitkomst2 = uitkomst2 + len(uitkomst)
> hoevaak = hoevaak + 1
> print uitkomst
>
> --------------------
>
>
> So the answer is 78
> But he real answer is 5805
>
> What am I doing wrong ?

According to mine, the answer is 5808

Here is what I tried:

#! /usr/bin/python
import string

maxnum=30

num=0
start=“1”
getal=start

while num<=maxnum:
print num, ‘) ‘, len(getal)
vorige=getal[0]
out=’’
for c in getal:
if c!=vorige:
out=out+’ ’
vorige=c
out=out+c

getal=’’
for c in string.split(out, ’ '):
getal=getal+str(len(c))+c[0]

num+=1


When in doubt, use brute force.
– Ken Thompson

Hello,

Thank you for your solution.
But I don’t see what’s wrong with my solution.

Anyone who can give me a hint.

Roelof

On 2010-10-21, snowman1967 <snowman1967@no-mx.forums.opensuse.org> wrote:
> Thank you for your solution.
> But I don’t see what’s wrong with my solution.
>
> Anyone who can give me a hint.

It would help if we had a proper indentation.

Is this how the code is supposed to look ?

#! /usr/bin/python
getal = “1”
vorig = getal[0]
aantal = 0
uitkomst2 = 0
uitkomst = “”
einde = 30
hoevaak = 1

while hoevaak < 30:
for nummers in getal:
if nummers == vorig :
aantal = aantal + 1
vorig = nummers
else:
getal2 = str(aantal)
uitkomst = uitkomst + getal2 + vorig
aantal = 1
vorig = nummers
getal2 = str(aantal)
uitkomst = uitkomst + getal2 + vorig
uitkomst2 = uitkomst2 + len(uitkomst)
hoevaak = hoevaak + 1
print uitkomst


When in doubt, use brute force.
– Ken Thompson