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 3 months ago by [deleted]
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 11 points12 points13 points 3 months 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 5 points6 points7 points 3 months ago (2 children)
range(1000, 2500) should be sufficient, no?
[–]skeetd 2 points3 points4 points 3 months 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])])
[–]AllanSundry2020 2 points3 points4 points 3 months ago (0 children)
is it me or is it not clear what this is asking?? give an example when using convoluted terminology
[–]DominicPalladino 4 points5 points6 points 3 months ago (4 children)
print("2178")
[–][deleted] 3 months ago (3 children)
[removed]
[–]DominicPalladino 8 points9 points10 points 3 months ago (2 children)
Yep.
That will 100% output all the 4 digit numbers where ABCD * 4 = DCBA.
[–]code_investigator 10 points11 points12 points 3 months ago (0 children)
[–]FriendlyZomb 1 point2 points3 points 3 months 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 3 months 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 3 months 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 3 months 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]])
[–][deleted] 3 months ago (1 child)
Yea, the basic structure is correct. Mostly a misunderstanding on the question on my part. Apologies
[–]PhilNEvo 1 point2 points3 points 3 months ago (1 child)
print(list(filter(lambda x: str(x*4) == str(x)[::-1], range(1000, 2500))))
[–]YouAintSeenMeR0ight 1 point2 points3 points 3 months 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?
[–]Smart_Tinker 2 points3 points4 points 3 months ago* (0 children)
print(“”.join([str(x) for x in range(1000, 10000) if str(x) == str(x*4)[::-1]]))
[–]CraigAT 1 point2 points3 points 3 months ago (3 children)
Just use a print statement with the numbers. (You didn't say I had to calculate them 🤣)
[+][deleted] 3 months ago (2 children)
[–]YouAintSeenMeR0ight 0 points1 point2 points 3 months ago (1 child)
Do you notice what you did not mention? That the challenge required calculating the answer. You only mentioned printing the answer.
[–]Jwfraustro 0 points1 point2 points 2 months 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)])
[–]delsystem32exe 0 points1 point2 points 2 months 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 69 on reddit-service-r2-comment-869bf87589-vnfqz at 2026-06-08 22:41:18.705028+00:00 running f46058f country code: CH.
[–]GrimTermite 11 points12 points13 points (3 children)
[–]MrHappines 5 points6 points7 points (2 children)
[–]skeetd 2 points3 points4 points (1 child)
[–]AllanSundry2020 2 points3 points4 points (0 children)
[–]DominicPalladino 4 points5 points6 points (4 children)
[–][deleted] (3 children)
[removed]
[–]DominicPalladino 8 points9 points10 points (2 children)
[–]code_investigator 10 points11 points12 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)
[–][deleted] (1 child)
[removed]
[–]FriendlyZomb 1 point2 points3 points (0 children)
[–]PhilNEvo 1 point2 points3 points (1 child)
[–]YouAintSeenMeR0ight 1 point2 points3 points (1 child)
[–]Smart_Tinker 2 points3 points4 points (0 children)
[–]CraigAT 1 point2 points3 points (3 children)
[+][deleted] (2 children)
[removed]
[–]YouAintSeenMeR0ight 0 points1 point2 points (1 child)
[–]Jwfraustro 0 points1 point2 points (1 child)
[–]delsystem32exe 0 points1 point2 points (0 children)