0

Working on a codefights problem. Got stuck on the solution, or rather, solving the way codefights wanted me too. Here is the solution

 def listBeautifier(a):
     res = a[:]
     while res and res[0] != res[-1]:
          a, *res, d = res
return res*

I did some printing of the result to try and understand what's going on and some experimentation to see how it works. From what I gathered, printing out various tests, the first item of res get's assigned to a and the last to d (which remain unused). Then, res is unpacked and each element of res is reassigned as the remaining bit of the list. I tried the following code

 def listBeautifier(a):
     res = a[:]
     while res and res[0] != res[-1]:
          a,c, *res, d = res
return res*

which threw an error so I gather this must be done symmetrically. What is python doing exactly? I see what's happening but not how.

vaultah
  • 44,105
  • 12
  • 114
  • 143
RhythmInk
  • 513
  • 1
  • 4
  • 18
  • 1
    What is the error? No, there is no requirement for symmetry. Try `a, c, *res, d = [1, 2, 3, 4, 5, 6, 7]`. – ayhan Dec 28 '17 at 00:07
  • 1
    The second just needs at least 3 elements in `res` which random input won't guarantee. The first just requires at least 2, which is guaranteed by the while condition. – user2390182 Dec 28 '17 at 00:12

0 Answers0