this post was submitted on 14 Sep 2024
602 points (98.7% liked)

Programmer Humor

19176 readers
964 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] otter@lemmy.ca 35 points 5 days ago (10 children)

Can anyone explain this one?

[–] Mirodir@discuss.tchncs.de 109 points 5 days ago* (last edited 5 days ago) (3 children)

Sure. You have to solve it from inside out:

  • not()....See comment below for this one, I was tricked ~~is a base function that negates what's inside (turning True to False and vice versa) giving it no parameter returns "True" (because no parameter counts as False)~~
  • str(x) turns x into a string, in this case it turns the boolean True into the text string 'True'
  • min(x) returns the minimal element of an iterable. In this case the character 'T' because capital letters come before non-capital letters, otherwise it would return 'e' (I'm not entirely sure if it uses unicode, ascii or something else to compare characters, but usually capitals have a lower value than non-capitals and otherwise in alphabetical order ascending)
  • ord(x) returns the unicode number of x, in this case turning 'T' into the integer 84
  • range(x) creates an iterable from 0 to x (non-inclusive), in this case you can think of it as the list [0, 1, 2, ....82, 83] (it's technically an object of type range but details...)
  • sum(x) sums up all elements of a list, summing all numbers between 0 and 84 (non-inclusive) is 3486
  • chr(x) is the inverse of ord(x) and returns the character at position x, which, you guessed it, is 'ඞ' at position 3486.

The huge coincidental part is that ඞ lies at a position that can be reached by a cumulative sum of integers between 0 and a given integer. From there on it's only a question of finding a way to feed that integer into chr(sum(range(x)))

[–] JustAnotherKay@lemmy.world 1 points 4 days ago

I'm not entirely sure if it uses Unicode, ASCII or something else...

I think I remember Automate the Boring Stuff with Python explaining that python uses ASCIIbetical order, but it's been a minute since I read that book

load more comments (2 replies)
load more comments (8 replies)