#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Frustrated because of this problem


    Can't really figure out what's wrong with my code. Let me first explain what the problem is:

    I take a string consisting of 0s and 1s, and then return a string as output which contains a 0 inserted right after a pattern consisting of 5 consecutive 1s.

    For example, let say my string is 1111111. Then, since the first 5 are all 1s, a 0 should be inserted right after that pattern. Then, my output string should be 11111011.

    Here's my code for the same:

    python Code:
     
    num = 1111111
    s = str(num)
    li = list(s)
    count = 0
    for e in li:
        while count<5:
            if e == 1:
                count+=1
            count = 0
        ind = li.find(e)
        temp = li[0:ind]
        temp.append(0)
        ans = temp + li[ind:]
    print(ans)


    My code never executes for some reason, and I can't find out why. Since I am new to python, I am not yet fully comfortable with the indentations and the syntax. Can you guys please help me find the solution for my problem?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    436
    Rep Power
    67
    Originally Posted by Leeuwnhawk
    My code never executes for some reason, and I can't find out why.
    Actually it executes all right but doesn’t do what you expect it do. There are many reasons for this.

    First let me say that the easiest way to debug your code is to add print() calls to check that your variables have the values you expect them to have. For instance, after the first three lines your variable “li” holds a list of STRINGS (['1', '1', ...]) but your comparison on line 8 expect a number. “if e == 1” is always False because the string '1' is never equal to the number 1.

    Second, your “while” loop shouldn’t be a loop at all; you’d actually need an “if” on line 7. As it stands your code takes the first element in the list and enters into an endless loop: the counter is never incremented since “e” always holds the value of the first element in the list and that’s a STRING '1', not 1. And even if it were incremented, you would set it 0 at the end of the loop with nothing else done.

    Try to correct these first and then let’s see.
    Last edited by SuperOscar; July 30th, 2013 at 12:52 PM.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    491
    Rep Power
    33
    Code:
    if e == 1:
    On the line before this statement print the types, i.e.
    Code:
    print type(e), type(1)
    if e == 1:
    You are comparing strings and integers. You have to either convert "e" to an integer or compare it to "1", a string. Also there should be an else to reset the counter or "1110110" will give a false positive. Note that the while statement has been removed and I will let you figure out why (print the variable if you don't know):
    Code:
        for e in li:
            if e == 1:  ## correct this so it works
                count+=1
                if count == 5:
                    ## append zero
                    count = 0  ## allow for 6 ones in a row
            else:
                count = 0
    Last edited by dwblas; July 30th, 2013 at 01:05 PM.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    Also, depending on how you run this script, you might be well advised to put in a statement that prevents Python from exiting and thereby closing the window (this can happen so fast it may appear that nothing happened). I use:

    raw_input("done")
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    I made some modifications, although I still couldn't get the desired output. Here's my modified code:

    python Code:
     
    def frame(arr):
        n = len(arr)
        one = []
        count = 0
        two = []
        for i in range(0, n-1):
            if arr[i] == 1:
                count+=1
                if count == 5:
                    one = arr[0:i+1]
                    two = arr[i+1:]
                    return one+[0]+two
                else:
                    count = 0
        return arr   
     
    print(frame([1, 1, 1, 1, 1, 1, 0, 1]))
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    436
    Rep Power
    67
    Originally Posted by Leeuwnhawk
    python Code:
     
            if arr[i] == 1:
                count+=1
                if count == 5:
                    one = arr[0:i+1]
                    two = arr[i+1:]
                    return one+[0]+two
                else:
                    count = 0
    Okay, let’s see what happens here.

    After finding 1, you first increase the counter: count is now 1.

    Then, since 1 is not 5, you immediately set it back to 0. The counter can thus never get > 1!

    Also, if it ever got to 5 (which can’t happen), you would immediately return from the function after only the first sequence of 5 consecutive 1’s. Instead of “return” you should put the result in a variable and only “return” once, at the end of the function.

    Note: DON’T modify “arr” while looping through it, unless you really know how to do it. Use a separate variable. Performance-wise this creates a problem, it’s both slow and wastes memory, but for now this is simpler.

    Comments on this post

    • Leeuwnhawk agrees
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    491
    Rep Power
    33
    You do not have to do the list slicing. Just iterate over the string and create an output list.
    Code:
    num = 1111111
    s = str(num)
    count = 0
    output = []
    for e in s:
        output.append(e)
        if int(e) == 1:
            count+=1
            if count == 5:
                output.append('0')    ## append zero
                count = 0  ## allow for 6 ones in a row
        else:
            count = 0 
    print(output)

    Comments on this post

    • Leeuwnhawk agrees
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by dwblas
    You do not have to do the list slicing. Just iterate over the string and create an output list.
    Code:
    num = 1111111
    s = str(num)
    count = 0
    output = []
    for e in s:
        output.append(e)
        if int(e) == 1:
            count+=1
            if count == 5:
                output.append('0')    ## append zero
                count = 0  ## allow for 6 ones in a row
        else:
            count = 0 
    print(output)
    This worked for me, and finally understood where I was going wrong with my code. Thanks a ton!!

IMN logo majestic logo threadwatch logo seochat tools logo