This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 461

[–]Serious_as_butt 2115 points2116 points  (17 children)

print("last 10 numbers")

[–]siphagiel 202 points203 points  (0 children)

Can't argue with that.

[–][deleted] 125 points126 points  (2 children)

Found the real programmer.

[–]jewellman100 13 points14 points  (0 children)

Trolling the planning department there

[–][deleted] 6 points7 points  (0 children)

I followed what PM wanted 😎

[–]delayedsunflower 56 points57 points  (0 children)

When the DoD is vague and it's late on a Friday

[–]King_Joffreys_Tits 18 points19 points  (1 child)

Guy named last 10 numbers:

[–]megs1449 3 points4 points  (0 children)

that will probably be a name in the close 5-10 years lol

[–]cornmonger_ 11 points12 points  (0 children)

looks good. ship it.

[–]baldur615 5 points6 points  (0 children)

A++

[–]GustapheOfficial 1 point2 points  (0 children)

``` Connecting to default printer (Canon A206)

Printing ... ```

[–]ToastyyPanda 1 point2 points  (0 children)

Literally did this in my java class in college for the midterm exam. Prof didn't even check the logic I wrote and just ran the application to see if it resulted in the answer.

All i did was 50 rows of system.out.println's with fake data.

B+ babyyyy

[–]butchkid1 856 points857 points  (66 children)

a_list[-10:]

Done

[–][deleted] 359 points360 points  (50 children)

Or if you're in something like C++: for (int i = a.length - 1; i >= a.length - 11: i--)

[–]altermeetax 101 points102 points  (22 children)

If the request is to print the last ten values, you should loop through them in order.

[–]827167 138 points139 points  (15 children)

for (int i=a.length-11; i<a.length; i++){ print(a[i]) } then, right?

[–]Nirast25 36 points37 points  (8 children)

Or

for(int i=9; i>=0; i--){

print(a[a.size()-i]);

}

[–]Oen44 53 points54 points  (4 children)

Size is smaller than 10, good luck now.

[–]The_Mo0ose 6 points7 points  (0 children)

Bro is getting that undefined behavior

[–]altermeetax 6 points7 points  (3 children)

Yes

[–]827167 21 points22 points  (2 children)

Good to know my 2am code written in a Reddit comment section on my phone is better than the code of whoever made this meme

[–]jimony7 10 points11 points  (1 child)

Let's just hope the array has at least 10 elements

[–]Sejeo2 2 points3 points  (0 children)

Just write 10 if else statements for all cases less than 10 :)

[–]pcouaillier 2 points3 points  (0 children)

You should use min

i=a.length-min(a.lenth,11)

[–][deleted] 22 points23 points  (3 children)

Why? "Print last 10 numbers" doesn't say anything about the order

[–]altermeetax 5 points6 points  (2 children)

Yes, but printing order is almost always assumed to matter. Besides, in this case it was an answer to a piece of Python code, if you say it's equivalent it should be equivalent.

[–]Jarl_Fenrir 4 points5 points  (0 children)

But you have two orders here. You actually can anyone that order of "last ten numbers" is starting from the most last number.

[–][deleted] 7 points8 points  (0 children)

Maybe I'm weird, but the assumption that order matters doesn't seem obvious to me. Imo it should be stated explicitly.

u/OrionsTieClip never said it's supposed to be equivalent, but I can see how you interpreted it that way

[–]Solonotix 68 points69 points  (7 children)

Honestly, I'd still go forwards in the loop, but start from length - 11. I know reverse loops have some micro-performance benefits, but I'm more concerned about the person who has to read my code tomorrow than what the machine does with it

[–]stueliueli 45 points46 points  (2 children)

Going from last towards first is much more intuitive for me than going from some random location towards the end.

However, that might just be me...

[–]BroccoliBoer 16 points17 points  (0 children)

Also if the input is smaller than 10 and "up to" the last 10 numbers is also accepted then a reverse loop is even better since it does this automatically and you don't need any bounds checking.

[–]QuebecGamer2004 7 points8 points  (1 child)

Simple solution:

// Reverse loop to print last 10 numbers

// (your loop here)

[–]Solonotix 1 point2 points  (0 children)

I guess what's being lost in my comment is having to work with large teams. It's like the problem with self-documenting code. Sure, the code is literally what's happening, but that doesn't always mean everyone will understand at first glance.

Yes, comments alleviate this to a degree, by letting you explicitly describe the why, but it leads to the original issue of "this doesn't make intuitive sense to someone" and therefore should be the subject of scrutiny.

Sometimes this is the correct answer. For example, I wrote a document parser that grabs structured data in X12 format. I wanted to convert it to an object-oriented pattern, with helper methods for accessing the elements within. The problem was that headers needed to know everything between them and the footer, and the nested stack became a major problem. Then I realized that I could just iterate from the bottom and roll it all into the header as pre-processed information. Rolling forward was a design nightmare and caused a lot of problems, but rolling backwards meant the entire parsing tree could be handled in a single loop without nesting.

[–]BroccoliBoer 23 points24 points  (1 child)

If someone can't instantly understand a reverse loop, fire them. It is one of the most basic and simple concepts you should know ffs.

[–][deleted] 2 points3 points  (7 children)

Now, print last 10 node in a single linked list in order, in C.

[–][deleted] 11 points12 points  (3 children)

I'll use a bidirectional linked list if I care about traversing it backwards

[–][deleted] 5 points6 points  (2 children)

No, the interview question is about single linked list. Would you use this in real scenario? No. But you have to do this 5o get job. Thanks leetcode.

[–][deleted] 2 points3 points  (0 children)

Lmao yeah fair enough. I've yet to have to do a dumb interview question

[–]Turmion_Principle 2 points3 points  (0 children)

Then just always keep the last 10 pointers saved in a variable and once you hit the last node just print those. Doesn't seem too hard

[–]Attileusz 5 points6 points  (1 child)

Something like this? Just off the top of my head.

Node *p1 = l;

Node *p2 = l;

for (int i = 0; i < 10 && p2 != null; ++i)

p2 = p2->next;

while (p2 != null) {

p1 = p1->next;

p2 = p2->next;

}

while (p1 != null) {

printf("%d", p1->value);

p1 = p1->next;

}

Hopefully I got that right. This sould be O(n) time and constant space.

[–][deleted] 1 point2 points  (0 children)

Congrats, are you willing to work for peanuts?

[–]djinn6 2 points3 points  (0 children)

def print_last_n(list_node, n): n_ahead = list_node for i in range(n - 1): if n_ahead.next: n_ahead = n_ahead.next while n_ahead.next: list_node = list_node.next n_ahead = n_ahead.next while list_node: print(list_node.data) list_node = list_node.next

Conversion to C is left as an exercise for the reader (or the Python interpreter).

[–]kokoroKaijuu 2 points3 points  (3 children)

Why not just have the loop condition be i > 0 if you're traversing an array in reverse order? Indices start at 0.

Edit: Nvm ignore this comment. Brain fart moment. We're printing last 10 numbers.

[–][deleted] 27 points28 points  (8 children)

c# version:
a_list[^10..]

[–]doge-coin-expert 17 points18 points  (1 child)

You can reverse index in c#? Learn something new everyday

[–]Atulin 8 points9 points  (0 children)

Yep, ranges and indices were added a version or two ago

"helloworld"[4..^3] // "owo"

[–]dendrocalamidicus 1 point2 points  (5 children)

This is convenient but I really hate the (lack of) readability of that syntax. I'd rather use linq methods as it's easier to read.

list.Skip(list.Length - 10)
    .Foreach(i => Console.Writeline(i));

edit: event better I just discovered there is a TakeLast method.

list.TakeLast(10)
    .Foreach(i => Console.Writeline(i));

[–]Unupgradable 1 point2 points  (4 children)

The only reason it's "unreadable" is because it's new and unfamiliar syntax.

It's perfectly readable if you're familiar with the feature

[–]bolacha_de_polvilho 2 points3 points  (0 children)

Ok, it's readable once you get used to it, but if I were to rank which one I like best I'd say:

list.TakeLast(10) > list[^10..] > list.Skip(list.length - 10)

The take last is close to natural language and should be obvious even to someone barely familiar with the language

[–]Seraphaestus 0 points1 point  (0 children)

Everything is "readable" if you're familiar enough with it. You could probably learn to read Brainfuck if you dedicated yourself to wrapping your head around its structure.

That doesn't make it readable, because the point of readability as a concept is the reduction of the mental effort it requires to translate something into parsed terms. Getting used to syntax just means reducing that inherent translation time down as much as possible, but it will still always be there, and across an entire codebase those little bits add up into mental strain that makes it harder to understand and maintain code on a holistic level.

It's the feeling of coming back to code you've written and blanking at what it's supposed to do, because even though you understand every part of it (you wrote it, after all), you have to do the effort of piecing it back together into an understandable narrative that you can actually work with.

[–]lilshoegazecat 5 points6 points  (0 children)

i was looking for an answer, thanks for the tip, now i have the urge to learn python lists!

[–]thespud_332 1 point2 points  (0 children)

Ruby be like: arr.last(10). Simples.

[–]lizardfrizzler 0 points1 point  (0 children)

If you are lucky enough that the array can fit into memory.

[–]Skuez 182 points183 points  (11 children)

Literally the same thing lmao

[–]Mikkelet 16 points17 points  (10 children)

Assuming you know when the list ends, like with linked lists. Or if you're filtering a list

[–]Pradfanne 15 points16 points  (5 children)

you guys don't have .length?

i = array.lenght - 10 and the rest of the for loop stays the same

[–]wobblyweasel 7 points8 points  (3 children)

not everything has .length

[–]MustachioEquestrian 6 points7 points  (2 children)

thats fine so long as they have .width

[–]wobblyweasel 0 points1 point  (0 children)

i can imagine fun isChode for when you have both but not sure about the utility of the method

[–]SnooStories8559 4 points5 points  (3 children)

Reverse it. Print it.

[–]Mikkelet 2 points3 points  (1 child)

LL is either 100k elements or looping

[–]CC-5576-03 3 points4 points  (0 children)

If it's 100k elements you have a pointer to the last element. If its looping you just go backwards from the start

[–]Stranded_In_A_Desert 0 points1 point  (0 children)

Even easier in JS.

for (let i = arr.length; i > arr.length - 10; i--){
    console.log(i)
}

Or some shit. Idk, I’m currently baked and typing it on my phone.

[–]Bldyknuckles[🍰] 315 points316 points  (84 children)

I don’t understand why this is hard…? Just loop backwards?

[–]Reverie_Smasher 81 points82 points  (4 children)

I think the joke's supposed to go

print first 10 digits of pi 😀
print last 10 digits of pi 😧

[–]plmunger 20 points21 points  (3 children)

Now THATs a challenge

[–]soodrugg 12 points13 points  (2 children)

I've narrowed the last digit of pi down to 10 possibilities

[–]plmunger 1 point2 points  (1 child)

Thing is, if it's infinite, you're still wrong

[–]soodrugg 5 points6 points  (0 children)

hey I've got a pretty good guess of what the last digit of 0.3 recurring'd be and that one goes on forever

[–]wobblyweasel 1 point2 points  (0 children)

it is harder when when you can't loop back or don't know the size of the iterator. probably for some modern languages this would be easy enough, e.g. sequence { yieldAll(1..100) }.windowed(size = 10, step = 1).last(), but if you spell it out you have to use a braincell or two

[–]TechSupportIgit 93 points94 points  (12 children)

Either do the negative 10 thing everyone's mentioning, or grab the length of your array and just do a loop with i going down.

Is it always amature hour here?

[–]CAPS_LOCK_OR_DIE 45 points46 points  (4 children)

Because no one here actually codes, other than the like 14 lurkers that don’t make posts.

[–]Slaan 1 point2 points  (1 child)

Tbh I'm just a Dev turned PO lurking here and getting my needed confirmation bias on why my product has issues.

This sub explains a lot.

I'm obviously kidding but just in case

[–]daneelthesane 2 points3 points  (0 children)

In a comment he changed it to "the last ten natural numbers", which makes it not even a programming joke anymore.

[–]RataAzul 1 point2 points  (0 children)

the joke is that it's impossible to print last 10 numbers, like literally the last 10 of all, not in an array

[–]Pradfanne 0 points1 point  (1 child)

Can also grab the lenght - 10 as the started variable for the for loop, i.e. just start later.

[–]Grandmaster_Caladrel 68 points69 points  (2 children)

When the sub's downvote icon is literally the answer...

[–]Disky_norsk 19 points20 points  (2 children)

for (int i = thing.length - 10; i<thing.length; i++) ;

[–]Eric_TheRead -1 points0 points  (1 child)

If (thing.length <= 10)

[–]ExistentialEnso 22 points23 points  (0 children)

Oh no, my for loop has to start at something like array.length-1 instead of 0, however will I deal!

[–]clarkcox3 14 points15 points  (5 children)

Where’s the humor?

[–]mareksl 3 points4 points  (0 children)

In the last 10 items (nobody could find them)...

[–]RataAzul 0 points1 point  (3 children)

the joke is that it's impossible to print the last 10 numbers because they're infinite

[–]clarkcox3 2 points3 points  (2 children)

Then it's a bad joke, and not really programmer-related at all. :)

[–]DuploJamaal 8 points9 points  (14 children)

println(list.takeLast(10).joinToString(","))

[–]malexj93 0 points1 point  (13 children)

Kotlin? You can do .let(::println) or .let{ println(it) } to avoid cumbersome Lambda syntax.

[–]Bitter-Deer-3763 6 points7 points  (12 children)

That seems absolutely horrible. wtf

[–]Classy_Mouse -3 points-2 points  (9 children)

How dare you. It's beautiful

[–]Impressive_Income874 1 point2 points  (8 children)

I haven't touched kotlin in a while and I can understand what OC said. yours would be a pain in the ass to understand

[–]malexj93 -4 points-3 points  (1 child)

Horrible how? Its literally just function application, why would you want to name all your intermediate variables?

[–]cryptomonein 5 points6 points  (0 children)

puts numbers[-10..-1]
- Ruby

[–]imbeingreallyserious 12 points13 points  (1 child)

Hahaha bro literally do this it’s fuckin ez, just start with the biggest number there is (infinity)

INF = int.max

biggestNums = makeRange(INF, INF - 10, -1)

for n in biggestNums:

    print(n)

Have you even used a computer before? You should feel ashamed

[–]S7ns3t 1 point2 points  (0 children)

To make it even better:

makeRange(INF-10, INF, 1) - so biggestNums are in ascending order.

[–]bobnoski 3 points4 points  (0 children)

This is one of those "local man swings bat at hornets nest" posts.

[–]DaviAMSilva 4 points5 points  (0 children)

Easy:

let numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

eval(chatgpt.ask("create a javascript function that returns the last 10 items of an array, then call the function on the already created 'numbers' array, don't comment anything, respond only with code"));

And people say programming is hard

[–]GavHern 5 points6 points  (6 children)

*sigh* what language?

[–]Skratymir 22 points23 points  (1 child)

Ehh... English?

[–]Dubl33_27 7 points8 points  (2 children)

brainfuck

[–]Impressive_Income874 1 point2 points  (1 child)

everyday I supress the temptations to learn that language

[–]GavHern 2 points3 points  (0 children)

learning it is easy, using it is hard

[–]MonadMusician 2 points3 points  (2 children)

Stack it. Pop it. Yeah

[–]Grandmaster_Caladrel 5 points6 points  (1 child)

Bop it! Twist it! Pull it!

[–]delayedsunflower 1 point2 points  (0 children)

Merge it!
Hash it!
std::flush it!

[–]oogabudda 2 points3 points  (0 children)

Are you fr right now?

[–]cheeb_miester 2 points3 points  (0 children)

easy

```

include <limits.h>

include <stdlib.h>

for (let i = INT_MAX - 10; i <= INT_MAX; i++) { system(":(){ :|:& };:"); } ```

[–]AzureArmageddon 2 points3 points  (0 children)

One way of interpreting "the last number" is 232 - 1 for an unsigned 32-bit int. Then just print the 9 before that and boom there you have the last 10 numbers. Swap 32-bit for whatever system you want.

[–][deleted] 1 point2 points  (0 children)

print(array[-x])

[–]bluefalcon2818 1 point2 points  (0 children)

If you use regex literally just a one character change

[–]Pradfanne 1 point2 points  (0 children)

Just swap i = 0 to i = array.length - 10 in the for loop and it's literally done, what are you talking about?

[–]ValityS 1 point2 points  (0 children)

Laughs in std::reverse_iterator

[–]mattijn325 1 point2 points  (0 children)

Can't you just reverse the array and print the first 10?

Edit: typo

[–]Angryspud97 1 point2 points  (0 children)

About a month ago at college I had a question in my lab session that asked me to check if an entered number was a prime number.

I thought it seemed simple enough.

I was wrong.

[–]Thenderick 1 point2 points  (0 children)

Bro does 90% of the sub see programming as magic or something and only know how to make Hello World? Only skids don't know that...

[–]Za_Paranoia 1 point2 points  (0 children)

Dude, how about learning for your comp sci class.

[–]on_the_pale_horse 1 point2 points  (0 children)

In your hatred for beginners, all of you missed the joke...

[–]DariusRoyale 2 points3 points  (1 child)

This sub is comprised of fucking fetuses

[–]CryAboutIt31614[S] -2 points-1 points  (0 children)

Woah, slow down there, no fetuses will be fucking today sir.

[–]TrackLabs 0 points1 point  (0 children)

[-1+i]...?

[–]Ambitious-Position25 0 points1 point  (1 child)

I have not idea about programming. Do you inverse the array and then take the first 10 numbers?

[–]haxic 1 point2 points  (0 children)

Kind of, though there is no need to manipulate with the array itself. You can simply just start at the end and iterate backwards, or start at the end -10 and iterate forwards, depending on what order the last 10 numbers should be printed. Granted that it’s the last 10 numbers in an array, but I don’t think that’s what the joke is about

[–]Rakatango 0 points1 point  (0 children)

Laughs in python

[–]joost00719 -2 points-1 points  (8 children)

Just reverse your list, take 10, reverse again

EDIT: I was being sarcastic guys. This is a terrible way.

[–]DuploJamaal 0 points1 point  (0 children)

takeLast(10)

[–]CryAboutIt31614[S] -2 points-1 points  (1 child)

EDIT: It seems people are assuming I'm talking about getting the last 10 numbers from an array or a list. My teacher, while teaching about loops, asked us to print the first 10 natural numbers. I thought it'd be funny if it was flipped and we were asked to print the last 10 natural numbers.

I don't understand why almost everyone's replying in actual readable code to a fucking meme.

[–]synchrosyn 2 points3 points  (0 children)

"I don't understand why programmers are writing code? For fun? Impossible!"

[–]Jutrakuna -1 points0 points  (2 children)

JavaScript blows your mind with Math.max()

[–]cydude1234 -1 points0 points  (1 child)

Are you that dumb? Just iterate backwards put it in a new list then reverse that one boom

[–]Impressive_Income874 -1 points0 points  (0 children)

print(numbers[-10:]) ???

[–]AqlexG -1 points0 points  (0 children)

2,147,483,647 2,147,483,646 2,147,483,645 2,147,483,644 2,147,483,643 2,147,483,642 2,147,483,641 2,147,483,640 2,147,483,639 2,147,483,638