Introduction:
Il y a quelques jours, j'ai lu ce billet avec le même titre quand je l'ai trouvé dans la HNQ. Dans cette question, on discute de la revendication du président-candidat Bernie Sanders, qui a affirmé ce qui suit:
Aujourd'hui, les 26 milliardaires les plus riches du monde, 26, possèdent autant de richesses que les 3,8 milliards les plus pauvres de la planète, soit la moitié de la population mondiale.
Lien vers la vidéo
est vrai ou pas. Veuillez vous rendre à la question elle-même pour y trouver des réponses et des discussions.
En ce qui concerne le défi réel basé sur cette revendication:
Défi:
Deux entrées: une liste numérique triée par ordre décroissant et un nombre (où est ).
Sortie: le plus long sous-liste de suffixe possible de dont la somme totale est à la somme des premiers valeurs dans la liste .
Exemple:
Entrées: = [500,200,150,150,125,100,75,75,55,50,40,30,30,20,10,10,8,5,5,5,3,2,2,1,0,-2,-3]
et .
Sortie:[125,100,75,75,55,50,40,30,30,20,10,10,8,5,5,5,3,2,2,1,0,-2,-3]
Pourquoi?
Les valeurs de la liste ( [500,200]
) totalisent 700
. Si nous prenons tous les suffixes des nombres restants, ainsi que leurs sommes:
Suffix: Sum:
[-3] -3
[-2,-3] -5
[0,-2,-3] -5
[1,0,-2,-3] -4
[2,1,0,-2,-3] -2
[2,2,1,0,-2,-3] 0
[3,2,2,1,0,-2,-3] 3
[5,3,2,2,1,0,-2,-3] 8
[5,5,3,2,2,1,0,-2,-3] 13
[5,5,5,3,2,2,1,0,-2,-3] 18
[5,5,5,5,3,2,2,1,0,-2,-3] 23
[10,5,5,5,5,3,2,2,1,0,-2,-3] 33
[10,10,5,5,5,5,3,2,2,1,0,-2,-3] 43
[20,10,10,5,5,5,5,3,2,2,1,0,-2,-3] 63
[30,20,10,10,5,5,5,5,3,2,2,1,0,-2,-3] 93
[30,30,20,10,10,5,5,5,5,3,2,2,1,0,-2,-3] 123
[40,30,30,20,10,10,5,5,5,5,3,2,2,1,0,-2,-3] 163
[50,40,30,30,20,10,10,5,5,5,5,3,2,2,1,0,-2,-3] 213
[55,50,40,30,30,20,10,10,5,5,5,5,3,2,2,1,0,-2,-3] 268
[75,55,50,40,30,30,20,10,10,5,5,5,5,3,2,2,1,0,-2,-3] 343
[75,75,55,50,40,30,30,20,10,10,5,5,5,5,3,2,2,1,0,-2,-3] 418
[100,75,75,55,50,40,30,30,20,10,10,5,5,5,5,3,2,2,1,0,-2,-3] 518
[125,100,75,75,55,50,40,30,30,20,10,10,5,5,5,5,3,2,2,1,0,-2,-3] 643
[150,125,100,75,75,55,50,40,30,30,20,10,10,5,5,5,5,3,2,2,1,0,-2,-3] 793
[150,150,125,100,75,75,55,50,40,30,30,20,10,10,5,5,5,5,3,2,2,1,0,-2,-3] 943
Le suffixe le plus long qui a une somme inférieure ou égale à 700
est [125,100,75,75,55,50,40,30,30,20,10,10,8,5,5,5,3,2,2,1,0,-2,-3]
avec une somme de 643
, voilà notre résultat.
Règles du challenge:
- Les valeurs dans le premier préfixe ne sont pas comptées dans le suffixe de sortie. Par exemple, les entrées =
[10,5,5,3]
et auraient pour résultat[5,3]
, et non[5,5,3]
. - I / O est flexible. Vous pouvez entrer sous forme de liste / flux / tableau d'entiers / décimales / chaînes, une seule chaîne délimitée, un par un via STDIN, etc. Vous pouvez également générer une liste / flux / tableau d'entiers / décimales / chaînes, imprimer / retourner une chaîne délimitée, imprimer un numéro sur chaque nouvelle ligne, etc. Votre appel.
- La sortie est garantie d'être non vide. Ainsi, vous n’avez pas à traiter de cas de tests tels que =
[-5,-10,-13]
et ce qui donne[]
. - Si vous le souhaitez, l’entrée et / ou la sortie peuvent aussi bien être dans l’ordre croissant que dans l’ordre décroissant.
Règles générales:
- C'est du code-golf , donc la réponse la plus courte en octets est gagnante.
Ne laissez pas les langues de code-golf vous décourager de poster des réponses avec des langues autres que le code de golf. Essayez de trouver une réponse aussi courte que possible à n'importe quel langage de programmation. - Les règles standard s'appliquent à votre réponse avec les règles d'E / S par défaut . Vous êtes donc autorisé à utiliser STDIN / STDOUT, fonctions / méthode avec les paramètres appropriés et des programmes complets de type de retour, de type renvoyé. Ton appel.
- Les failles par défaut sont interdites.
- Si possible, veuillez ajouter un lien avec un test pour votre code (c.-à-d. TIO ).
- En outre, l'ajout d'une explication de votre réponse est fortement recommandé.
Cas de test:
Inputs: L=[500,200,150,150,125,100,75,75,55,50,40,30,30,20,10,10,8,5,5,5,3,2,2,1,0,-2,-3], n=2
Output: [125,100,75,75,55,50,40,30,30,20,10,10,8,5,5,5,3,2,2,1,0,-2,-3]
Inputs: L=[10,5,5,3], n=2
Output: [5,3]
Inputs: L=[7,2,1,-2,-4,-5,-10,-12], n=7
Output: [-12]
Inputs: L=[30,20,10,0,-10,-20,-30], n=1
Output: [20,10,0,-10,-20,-30]
Inputs: L=[100,35,25,15,5,5,5,5,5,5,5,5,5,5,5,5,5], n=1
Output: [15,5,5,5,5,5,5,5,5,5,5,5,5,5]
Inputs: L=[0,-5,-10,-15], n=2
Output: [-10,-15]
Inputs: L=[1000,999,998,900,800,766,525,525,400,340,120,110,80,77,33,12,0,-15,-45,-250], n=2
Output: [525,525,400,340,120,110,80,77,33,12,0,-15,-45,-250]
Inputs: L=[10,5,5], n=1
Output: [5,5]
[131000000000, 96500000000, 82500000000, 76000000000, (7.7 billion more entries)]
:: pL = [-5,-10,-13]
etn=2
ayant abouti[]
. " croissant si vous le souhaitez), il n’ya donc[1,2,3]
pas de liste d’entrée valide pour commencer (à moins que vous ne choisissiez une entrée croissante, auquel cas ce[1,2]
serait le résultat).Réponses:
C # (compilateur interactif Visual C #) ,
8881696863 octets-4 octets grâce à LiefdeWen
Essayez-le en ligne!
la source
+b
in theSkip
call; it's redundant to check the firstn
list, but I think it's still correct.EXAPUNKS (2 EXAs, 30 Instructions, 594-byte solution file)
I've wanted to try a code golf challenge in EXAPUNKS for a while, and you looked like the best fit I could find, so, congrats!
Input via files 200 and 201, for L and n respectively. Output via a new file. L and the output are in ascending order.
Basically, XA sums the last n values in L, then sends it to XB. It then seeks to the beginning of L and sends each value one-by-one to XB. XB first receives the total from XA and stores it in register X. It then receives values one-by-one from XA, deducting the new value from X, and writing those values to the output file until X < 0.
XA
XB
JavaScript for the level here
la source
Python 2, 60 bytes
Try it online!
Explanation:
First takes the sum of the first
n
items.Then the sum of each sublist is compared to this sum. As soon as one is not larger, we stop.
Then the resulting (longest) sublist is printed.
la source
05AB1E,
1412 bytesSaved 2 bytes thanks to Grimy
Try it online!
Explanation
la source
.$.sʒO²¹£O>‹}θ
. :)|
overwrote thelast-input
, interesting.|
here makes the result of|
become the last input instead of what was actually the last input.J, 18 bytes
Try it online!
Explanation:
A dyadic verb, taking
n
as its left argument andL
- as its right argument.la source
Ruby,
4743 bytesTakes an ascending list as input. Removesn items from the end of the array to get the initial sum, then continue removing items until the remaining elements' sum is less than the initial sum.
-4 bytes by reading the spec more closely.
Try it online!
la source
R, 53
55bytes@Giuseppe saved me 2 bytes changing the way remove was done
Try it online! Takes the input as descending and outputs in ascending as allowed by rule 4.
Y
is the rev ofL
with 1:n removed using0:-n
Y
where cumulative sum is less then equal to the sum ofL[X]
la source
X
using-(1:n)
but of course that was the same size, so left itJavaScript (ES6), 66 bytes
Takes input as
(a)(n)
with the list in ascending order.Try it online!
Commented
la source
Python 2, 59 bytes
Also compatible with Python 3
Try it online!
Explanation
The sum of the suffix is compared to half of the sum of the whole list. If the sum of the suffix is smaller or equal, the suffix is returned. If not, the function is called recursively with the first item of the suffix popped out.
la source
Pyth,
1615 bytesTry it online!
The input list is expected to be sorted in ascending order, rather than descending as is used in the examples.
It's at times like this when I really wish Pyth had a single token operator to access the second input more than once (
E
evaluates the next line of input, but repeated calls discard the previous value read).Edit: saved 1 byte thanks to MrXcoder
la source
JavaScript, 64 bytes
Try it online!
la source
Stax, 12 bytes
Run and debug it at staxlang.xyz!
Nicer version
Unpacked (14 bytes) and explanation:
By consensus, I can leave this result on the stack. Stax will attempt an implicit print, which might fail, but appending an
m
to the unpacked program lets you see the output nicely.Looks like
{ ... }j
is the same as{ ... fh
. Huh. Edit: That's not quite the case; the only the former will stop when it gets a truthy result, possibly avoiding side effects or a fatal error later on.la source
APL+WIN, 27 bytes
Prompts for input of L then n.
Try it online! Courtesy of Dyalog Classic
la source
Japt, 16 bytes
Try it
la source
Jelly,
1312 bytesTry it online!
Thanks to @JonathanAllan for saving a byte!
A dyadic link taking the list of valuesL as left argument and the number n as right.
Explanation
la source
ṫḊÐƤS>¥ÞḣS¥Ḣ
Gaia, 12 bytes
Try it online!
I think there's a byte I can golf if I get the stack just right.
la source
Haskell, 46 bytes
Displeased with how this looks; hope I’m just missing some obvious golfs.
Try it online!
I tried getting the prefix and suffix using
splitAt
and pattern matching in a guard, but that turned out to be 3 bytes more. Planning on trying to convert to a recursive function with guards later to see if that lowers the byte count.Explanation
What I refer to as "the prefix" is the first
n
elements and "the suffix" is the rest of the list.la source
APL (Dyalog Unicode),
2321 bytesSBCSAnonymous prefix lambda, takingn as left argument and L as right argument.
Try it online!
{
…}
"dfn";⍺
(leftmost Greek letter) and⍵
(rightmost Greek letter):⌽⍵
reverse+\
prefix sums of that(
…)<
Boolean mask where less than:⍺↑⍵
take the first+/
sum those⊥⍨
count trailing truths⍺⌈
the maximum of⍵↓⍨
drop that many elements from the front ofla source
MATL,
1312 bytes1 byte saved thanks to @Giuseppe, based on the answer by @MickyT.
Output is in ascending order.
Try it online! Or verify all test cases.
Explanation
Consider inputs
2
and[500,200,150,150,125,100,75,75,55,50,40,30,30,20,10,10,8,5,5,5,3,2,2,1,0,-2,-3]
.la source
PowerShell,
9997 bytesTry it online!
Takes list in ascending order, output is descending (because it was easier to compare to the test cases :^))
Goes through the list
backwardsforwards, comparing the accumulator to the lastn
entries added together (via gluing them together with+
s and passing the resulting string toinvoke-expression
). The Until loop needed additional logic to handle going into the Rich Neighborhood because it wouldn't stop if we're still not richer than the Rich Guys until we churn through the entire list.Unrolled:
la source
Retina 0.8.2, 99 bytes
Try it online! Link only includes some test cases; I could get it to work in some cases with negative numbers at a cost of 12 bytes (in particular the first
n
entries inL
still need to be positive; theoretically I could probably only require the sum of the firstn
entries to be positive). Explanation:Convert to unary.
Insert a marker at the start of
L
.Move it down the list
n
times, summing as we go. This deletesn
but its comma remains.Create an entry for each suffix of
L
.Replace the middle with a copy of the suffix.
Sum the copy of the suffix.
Take the first entry where the suffix sum does not exceed the prefix sum.
Delete the sums.
Convert to decimal.
Delete the trailing comma that came before
n
.la source
r
option, so I've now linked it in with some test cases.Charcoal, 17 bytes
Try it online! Link is to verbose version of code. Explanation:
la source
Red, 63 bytes
Try it online!
la source
PowerShell, 86 bytes
Try it online!
Unrolled:
la source
MathGolf, 13 bytes
Try it online!
Explanation
Takes input as
n L
The reason why this works is that in the first step, we actually divide the list into two overlapping parts. As an example,
L = [4, 3, 2, 1], n = 2
will split up the list as[3, 2, 1]
and[4, 3]
. The reason for having an extra element in the first list is that in the loop, the first thing that happens is a discard. If an extra element was not prepended, the cases where the output should be the entire rest of the list would fail.la source
Wolfram Language (Mathematica), 40 bytes
Try it online!
la source
Clojure,
6675 bytesSadly there doesn't seem to be a shorter idiom for the total sum of a sequence.
Edit: Oh when adding examples to the Try it online! link I noticed that the original answer gave wrong results when many negative numbers were present.
The
doseq
uses "keys" destructuring so it should be somewhat clear which data ends up in which symbol.#(...)
is an anonymous function, here I'm binding it to the symbolf
:Output:
la source
APL(NARS), 32 chars, 64 bytes
test and comments:
I reported wrongly the byte length...
la source
MS SQL Server 2017, 271 bytes
I know that using a more relation-like table to store the input data can make the code more concise, but using the character data type to store the numeric list and the
STRING_SPLIT
function, I get shorter theBuild Schema
part :)More readable version:
Try it on SQL Fiddle!
la source
Python 3.8 (pre-release), 59 bytes
Output is in ascending Order
Try it online!
la source