Reply To: Programming
The mystery of the silver bullet › Forums › The Intelligence Room › Programming › Reply To: Programming
@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