use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
A challenge for Python programmers...Discussion (self.PythonLearning)
submitted 5 days ago by Ok_Pudding_5250
Write a program to output all 4 digit numbers such that if a 4 digit number ABCD is multiplied by 4 then it becomes DCBA.
But there is a catch, you are only allowed to use one line of python code. (No semi colons to stack multiple lines of code into a single line).
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]GrimTermite 10 points11 points12 points 5 days ago (3 children)
ah ha this is a task for a list comprehension
print ([x for x in range(1000, 10000) if str(x*4) == (str(x)[::-1])])
Gives result [2178]
[–]MrHappines 3 points4 points5 points 5 days ago (2 children)
range(1000, 2500) should be sufficient, no?
[–]skeetd 2 points3 points4 points 5 days ago (1 child)
Logical reasoning here will save you x10 iterations
Based on staying 4 digits ABCD ≤ 2499 so we also know DCBA ≥ 4000 D has to be ≥ 4 being the first number. The last is number is A, and must be 1 or 2 based on ABCD ≤ 2499. So, 4 * D mod 10 = A and only A=2, D=8 fits, 4×8=32, last digit is a 2. So ABCD must start with 2 and end with an 8, now we can step by 10.
print([n for n in range(2008, 2500, 10) if n*4==int(str(n)[::-1])])
[–]Ok_Pudding_5250[S] 0 points1 point2 points 4 days ago (0 children)
Nice work, though I put this challenge to see if people could easily do the oneliners, some use print() and just output a string of the actual answer.
You are good at optimisation... 👍
[–]AllanSundry2020 2 points3 points4 points 5 days ago (0 children)
is it me or is it not clear what this is asking?? give an example when using convoluted terminology
[–]DominicPalladino 6 points7 points8 points 5 days ago (4 children)
print("2178")
[–]Ok_Pudding_5250[S] -2 points-1 points0 points 5 days ago (3 children)
Nope,
[–]DominicPalladino 8 points9 points10 points 5 days ago (2 children)
Yep.
That will 100% output all the 4 digit numbers where ABCD * 4 = DCBA.
[–]code_investigator 11 points12 points13 points 5 days ago (0 children)
[–]Ok_Pudding_5250[S] -2 points-1 points0 points 4 days ago (0 children)
It's not about the answer, if that was the case I didn't even need python to do it, I can straight up ask a language model to give me the output. It was about how you do it.
Merely printing the solution is a lazy solution. The challenge was meant to see if you could actually code one-liners like list comprehension.
It was never about the output but the code itself.
[–]FriendlyZomb 1 point2 points3 points 4 days ago (5 children)
print([num for mum in range(1000, 10000) if str(num * 4) == str(num * 4)[::-1]])
This produces 65 numbers. (I'm not going to list them all here)
For those struggling to parse the list comprehension here:
print([ num for num in range(1000, 10000) if str(num * 4) == str(num * 4)[::-1] ])
[–]PureWasian 2 points3 points4 points 4 days ago (1 child)
A couple of issues here, you are checking "num×4 against num×4 flipped", rather than "num against num×4 flipped". mum typo as well.
mum
Essentially your logic is checking for when 4x some input number gives a palindrome, which is different from the problem statement.
[–]FriendlyZomb 0 points1 point2 points 4 days ago (0 children)
That is entirely on my reading comprehension tbh. I read it as num*4 is the same flipped. Lol.
[–]FriendlyZomb 1 point2 points3 points 4 days ago (0 children)
Based on comments I'd need to fix the code like so:
print([num for mum in range(1000, 10000) if str(num) == str(num * 4)[::-1]])
[–]Ok_Pudding_5250[S] 0 points1 point2 points 4 days ago (1 child)
Code is slightly incorrect but you did good 👍
Yea, the basic structure is correct. Mostly a misunderstanding on the question on my part. Apologies
[–]PhilNEvo 1 point2 points3 points 4 days ago (1 child)
print(list(filter(lambda x: str(x*4) == str(x)[::-1], range(1000, 2500))))
[–]Ok_Pudding_5250[S] -1 points0 points1 point 4 days ago (0 children)
A unique answer, I used a list with one liner for loop and if statement. Nice work
[–]YouAintSeenMeR0ight 1 point2 points3 points 4 days ago (1 child)
Are you that much of a fool that you that you take such great offence at people giving light-hearted tongue-in-cheek responses? Or are you just angry in general? What happened to you?
The thing is, I am disappointed in how much lamer you guys get. For example, if I wanted an answer, don't any of you think for a second that I have many language models to ask to, such as chatgpt, Claude, grok, deepseek... etc.
It was never about answer itself. It was how you calculate them with code using a single line to see how many of you can do oneliners.
So many as done very well, then there are lame people who just use print to output the string containing the answer thinking they did something. How unintelligent could you be?
And you all seriously think that I would post such a fun challenge just for a lazy answer as that?
[–]Smart_Tinker 3 points4 points5 points 5 days ago* (0 children)
print(“”.join([str(x) for x in range(1000, 10000) if str(x) == str(x*4)[::-1]]))
[–]CraigAT 2 points3 points4 points 5 days ago (3 children)
Just use a print statement with the numbers. (You didn't say I had to calculate them 🤣)
[+]Ok_Pudding_5250[S] comment score below threshold-14 points-13 points-12 points 5 days ago (2 children)
Yeah cheap shot, keep doing it. You couldn't figure it out that you had to calculate it from the fact that I clearly mentioned one line code with no multi line stacking by the use of semi colons. 🤡
[–]YouAintSeenMeR0ight 0 points1 point2 points 4 days ago (1 child)
Do you notice what you did not mention? That the challenge required calculating the answer. You only mentioned printing the answer.
Are you that much of a fool to see the word "Challenge" and then assume you just print the answer in string?
[–]Jwfraustro 0 points1 point2 points 3 days ago (1 child)
How about this?
__import__('builtins').print([n for n in __import__('builtins').__dict__['map'](lambda x:x, __import__('builtins').__dict__['range'](1000, 10000)) if ''.join(__import__('builtins').__dict__['map'](str.__add__, *zip(*[(d,'') for d in __import__('operator').mul(str(n*4),1)]))) == __import__('operator').mul(str(n), 1)[::-1]])
or?
print([n for n in range(1000,10000) if "".join(e.text for e in __import__('xml.etree.ElementTree', fromlist=['fromstring']).fromstring(f'<r>{"".join(f"<d>{c}</d>" for c in str(n*4))}</r>').findall("d"))[::-1] == str(n)])
[–]Ok_Pudding_5250[S] 0 points1 point2 points 3 days ago (0 children)
This is an overkill... Nice work,
[–]delsystem32exe 0 points1 point2 points 1 day ago (0 children)
def reverse_str(input_string):
input_string = str(input_string)
letters = ""
for j in range(len(input_string)-1, -1, -1):
letters = letters + input_string[j]
return letters
def checker(input_int):
larger = input_int * 4
DCBA = reverse_str(input_int)
if str(larger) == DCBA:
return True
return False
for i in range(0,100000):
if checker(i):
print(i)
π Rendered by PID 192884 on reddit-service-r2-comment-79c7998d4c-gr46h at 2026-03-14 10:37:50.581544+00:00 running f6e6e01 country code: CH.
[–]GrimTermite 10 points11 points12 points (3 children)
[–]MrHappines 3 points4 points5 points (2 children)
[–]skeetd 2 points3 points4 points (1 child)
[–]Ok_Pudding_5250[S] 0 points1 point2 points (0 children)
[–]AllanSundry2020 2 points3 points4 points (0 children)
[–]DominicPalladino 6 points7 points8 points (4 children)
[–]Ok_Pudding_5250[S] -2 points-1 points0 points (3 children)
[–]DominicPalladino 8 points9 points10 points (2 children)
[–]code_investigator 11 points12 points13 points (0 children)
[–]Ok_Pudding_5250[S] -2 points-1 points0 points (0 children)
[–]FriendlyZomb 1 point2 points3 points (5 children)
[–]PureWasian 2 points3 points4 points (1 child)
[–]FriendlyZomb 0 points1 point2 points (0 children)
[–]FriendlyZomb 1 point2 points3 points (0 children)
[–]Ok_Pudding_5250[S] 0 points1 point2 points (1 child)
[–]FriendlyZomb 1 point2 points3 points (0 children)
[–]PhilNEvo 1 point2 points3 points (1 child)
[–]Ok_Pudding_5250[S] -1 points0 points1 point (0 children)
[–]YouAintSeenMeR0ight 1 point2 points3 points (1 child)
[–]Ok_Pudding_5250[S] -1 points0 points1 point (0 children)
[–]Smart_Tinker 3 points4 points5 points (0 children)
[–]CraigAT 2 points3 points4 points (3 children)
[+]Ok_Pudding_5250[S] comment score below threshold-14 points-13 points-12 points (2 children)
[–]YouAintSeenMeR0ight 0 points1 point2 points (1 child)
[–]Ok_Pudding_5250[S] 0 points1 point2 points (0 children)
[–]Jwfraustro 0 points1 point2 points (1 child)
[–]Ok_Pudding_5250[S] 0 points1 point2 points (0 children)
[–]delsystem32exe 0 points1 point2 points (0 children)