Skip to main content
The National Cipher Challenge

Programming

Viewing 4 posts - 46 through 49 (of 49 total)
  • Author
    Posts
  • #98469
    _madness_
    Participant

    Looking at it, I realize that I called factorial() twice for each iteration, which is too inefficient. Here, I fixed it:

    def nth_permutation(n,objects):
        perm = []
        n -= 1
        while len(objects) > 0:
            k = factorial(len(objects)-1)
            m = int(n/k)
            n -= m * k
            perm.append(objects[m])
            objects.pop(m)
        return perm
    

    @ByteInBits, you said

    Normally I would smile and let it pass but I have to take issue with you
    to come to some understanding.

    In post #98171 you state:
    @ByteInBits, your MD5 sum is for the NEXT permutation. By my example, you should start with 1 and not 0.

    This make you incorrect with the entry number!!
    (standard practice doese not start at 1, and you never stated the deviation)

    But I did. When I said:

    Notice that I listed them in a sort of numerical order: 123, 132, 213, 231, 312, 321. There are 3! = 6 of them. If I were to ask you what is the fourth permutation of 1, 2, 3, you should answer “2, 3, 1”. OK, got it?

    I was very careful about making sure the list started at 1, so I said “fourth” instead of “third”. I even stopped to ask if everyone understood before I went on.

    Never wager against a Sicilian when death is on the line.

    #98468
    robb
    Participant

    @ByteInBits PROGRAMMING QUEST #4 #98187 and #98369

    Sorting the list of random numbers was an interesting challenge in this task; for example, the correct ordering of 6531, 65 in set 4. Here’s how I tackled that part:

    def cnvrtsort(s):
    #convert set s to a list with string values sorted in descending order
    sstr = [str(elem) for elem in s]
    sl = list(sorted(sstr, reverse=True))
    sc =compare(sl)
    #Combine sc to get a single number and print
    sf=”.join(sc)
    print(sf)

    def compare(s):
    #compare consecutive elements and swap if needed
    for i in range(0,len(s)-1):
    if s[i][:len(s[i+1])] == s[i+1]:
    a = s[i]
    b = s[i+1]
    rmv=a[len(str(b)):]

    if s[i+1]>rmv:
    #swap order
    s[i] = b
    s[i+1]=a
    return s

    #98486
    ByteInBits
    Participant

    @_madness_ Thank you, and sorry for my misunderstanding of your narrative which now make sense. @rob also helped get me there.

    #98494
    _madness_
    Participant

    @ByteInBits, It’s all cool.

Viewing 4 posts - 46 through 49 (of 49 total)
  • You must be logged in to reply to this topic.
Report a problem