Writing a string in a spiral

后端 未结 11 982
别那么骄傲
别那么骄傲 2020-12-30 11:29

I had recently participated in coding competion sponsored by an company and there was this one question which I did not understood, as to what was it asking.

Here i

11条回答
  •  说谎
    说谎 (楼主)
    2020-12-30 11:58

    I wrote an implementation in Python, which I think, shows a workmanlike approach. Although you tagged your question Java, sometimes I think it's wise to prototype and learn about problems, especially "interview questions" in a very high level dynamic language. The language itself is like pseudo-code that runs, and this helps you understand the shape of the problem, or the shape of the question.

    The clues are there in the way the person has asked the question:

    • There is a nice box like shape of letters. A good place to start is to ask yourself, how do I write code that makes a box of letters.
    • What data structure (array with an x+(y*c) lookup or, 2d array) will I store the box of letters in.
    • How do I put them in and how do I get them out?

    From the point where you break it down into smaller pieces, you should be able to understand the question, and then begin to formulate an answer.

    It could probably be done in many fewer lines of code but I felt like doing it as linearly as possible. Here's a not-very-good-at-python answer:

    from math import *
    import pprint
    
    directions = [ (0,1),(1,0),(0,-1),(-1,0) ]  
    
    def store_spiral(array,s,l):
        direction = 0
        dx = directions[direction][0]
        dy = directions[direction][1]
        x=0
        y=0
        for c in s:
            array[x][y] = c
            x = x +dx
            y = y +dy
            if (x >= l) or (y >= l) or (x<0) or (y<0):
                x=x-dx
                y=y-dy
                direction = (direction + 1) % 4
                dx = directions[direction][0]
                dy = directions[direction][1]            
                x=x+dx
                y=y+dy
            elif (array[x][y]!=''):
                x=x-dx
                y=y-dy
                direction = (direction + 1) % 4
                dx = directions[direction][0]
                dy = directions[direction][1]  
                x=x+dx
                y=y+dy
    
    
    def convert(s):
        l = len(s)
        sl = int(ceil(sqrt(l)))
        # make empty 2d array of [ ['','', ... ], .... ]
        ar2 = [['' for i in range(sl)] for j in range(sl)]
        store_spiral(ar2,s,sl)
        x=''
        for ar in ar2:
            for l in ar:
                x=x+l
        return x
    
    a = convert("paypalisthefastersaferwaytosendmoney")
    
    print a
    

    And here's an idea how to make a cooler version, but it would require generating the series of values called 'limits' here, which is the "length of a walk before you turn".

    from math import *
    import pprint
    
    # directions = East,South,West,North
    directions = [ (0,1),(1,0),(0,-1),(-1,0) ]
    
    x=0
    y=-1
    
    def store_spiral(array,s,l):
        global x
        global y
        direction = 0
        dx = directions[direction][0]
        dy = directions[direction][1]
        d=0
        n=0
        limits=[5,4,4,3,3,2,2,1,1,1,1]
        limit=limits[n]
        for c in s:
            d=d+1
            x=x+dx
            y=y+dy
            array[y+(x*l)]=c
            if d>limit and (limit>0):
                direction = (direction + 1) % 4
                dx = directions[direction][0]
                dy = directions[direction][1]
                n=n+1
                if n>=len(limits):
                    break
                limit=limits[n]
                d=0        
    
    
    
    def convert(s):
        l = len(s)
        sl = int(ceil(sqrt(l)))
        # make empty 2d array of [ ['','', ... ], .... ]
        ar = ['*' for i in range(l)]
        #try:
        store_spiral(ar,s,sl)
        #except:
        #  pass
        x=''
        n=0
        for l in ar:
                x=x+l
                print l,
                n=n+1
                if n==sl:
                    n=0
                    print
        return x
    
    a = convert("paypalisthefastersaferwaytosendmoney")
    
    print
    
    print 'result: ',a
    

提交回复
热议问题