How Does Append() Work In This Code Snippet? Confused With A Particular Variable
Can someone explain this code to me? More specifically, the part about lead.append([sum1 - sum2 , 1]) and print(ans,ans).
I do not understand the "1" in "lead.append([sum1 - sum2 , 1])"
I, also, do not understand the "1" and "0" in print(ans,ans).
lead =  sum1 , sum2 = 0 , 0 for i in range(int(input())): a1 , a2 = map(int, input().split()) sum1 += a1 sum2 += a2 if sum1>sum2: lead.append([sum1 - sum2 , 1]) else: lead.append([sum2 - sum1 , 2]) ans = max(lead) print(ans,ans)
lead.append([sum2 - sum1 , 2])
This is appending to the list 'lead' the value inside the brackets.
[sum2 - sum1, 2] <- inside the brackets
The 2 or 1 you are referring to is inside a list itself. So you would be appending these entries to the list. The code seems to check which of sum1/sum2 is greater and then subtracts the lesser one, adding the corresponding value to the list 'lead'.
The last line, assuming that all items in the list are lists themselves, should print the 2nd value of the sublist, then the 1st value of the sublist.
In : lead = [[100, 2], [200, 1], [400, 3]] In : max(lead) Out: [400, 3] In : ans = max(lead) In : print(ans, ans) 3 400
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module