Jump to content

Last list item: Difference between revisions

Line 424:
150 228 243 378
243 378 621</pre>
 
 
=={{header|Python}}==
{{trans|Julia}}
<lang python>""" Rosetta code task: Last list item """
 
def add_least_reduce(lis):
""" Reduce lis: sum least two elements adding sum to list. Will take len(list) - 1 steps """
while len(lis) > 1:
lis.append(lis.pop(lis.index(min(lis))) + lis.pop(lis.index(min(lis))))
print('Interim list:', lis)
return lis
 
LIST = [6, 81, 243, 14, 25, 49, 123, 69, 11]
 
print(LIST, ' ==> ', add_least_reduce(LIST.copy()))
</lang>{{out}}
<pre>
Interim list: [81, 243, 14, 25, 49, 123, 69, 17]
Interim list: [81, 243, 25, 49, 123, 69, 31]
Interim list: [81, 243, 49, 123, 69, 56]
Interim list: [81, 243, 123, 69, 105]
Interim list: [243, 123, 105, 150]
Interim list: [243, 150, 228]
Interim list: [243, 378]
Interim list: [621]
[6, 81, 243, 14, 25, 49, 123, 69, 11] ==> [621]
</pre>
 
=={{header|Raku}}==
4,105

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.