Programming
The mystery of the silver bullet › Forums › The Intelligence Room › Programming
- This topic has 47 replies, 12 voices, and was last updated 1 week, 4 days ago by _madness_.
-
AuthorPosts
-
14th November 2024 at 12:27 pm #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.
14th November 2024 at 5:05 pm #98468robbParticipant@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 s14th November 2024 at 10:49 pm #98486ByteInBitsParticipant@_madness_ Thank you, and sorry for my misunderstanding of your narrative which now make sense. @rob also helped get me there.
28th November 2024 at 11:23 am #98494_madness_Participant@ByteInBits, It’s all cool.
4th December 2024 at 11:20 am #98717_madness_ParticipantIf anyone is interested in something almost as much fun as a seriated Polybius cipher, this programming challenge may or may not be hard. I don’t know. The challenge is to find a Pythagorean triple (x^2 + y^2 = z^2) where each number has more than 100 digits. To show you that it might be easy, here is one:
5448316437483445534218637633951372103721472133084302800181196711700804618365736182804131733102336602000582925889704780559079553695392126450638360766858239249449202384020719285919431179599065584204861405445366919281541626876
590954136213084844483425568023560779816003827176678161335509531818673831091741858041079741290341646336920339282126397662570865581372141104223408019010508444113688638386186080310786949311722657164655061901989505154837335335001963211536629992088337093907
590954136213084844483425568023560779816003827176678161335534647263497664985816974232810815094373455008058948195003268779709416780329753566378275824425812622524697460051934566226668094855294530020882890470426060467302586456555110110607191094703028457845
But then again, maybe it’s hard.
What’s harder? Constructing examples or checking they are right?! Harry
-
AuthorPosts
- You must be logged in to reply to this topic.