-2

In this snippet,what for loop does?

a = input().split()
if (all([int(i) > 0 for i in a]) and any([(i) == (i)[::-1]] for i in a)):
    print(True)
else:
    print(False)

Edit: There is a solution,you can check it out! How do Python's any and all functions work?

Berke Balcı
  • 81
  • 10
  • 2
    but you do understand the `any` part? – DeepSpace Jun 13 '21 at 16:31
  • 1
    You are looking for the term `List Comprehension`. – quamrana Jun 13 '21 at 16:32
  • 2
    `a` is a list of strings integers. `(all([int(i) > 0 for i in a])` checks if all the numbers in this list are greater than *0*. `any([(i) == (i)[::-1]] for i in a))` if `a` is equal to reversed `a` – Buddy Bob Jun 13 '21 at 16:32
  • 1
    Please go through the [intro tour](https://stackoverflow.com/tour), the [help center](https://stackoverflow.com/help) and [how to ask a good question](https://stackoverflow.com/help/how-to-ask) to see how this site works and to help you improve your current and future questions, which can help you get better answers. "Explain this code block to me" is off-topic for Stack Overflow, as is "teach me this basic programming tool". "Show me how to solve this coding problem?" is off-topic for Stack Overflow. – Prune Jun 13 '21 at 16:52
  • 1
    See [How much research](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) and the [Question Checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). It seems that you need to look up the `any` and `all` commands. Most tutorials will also show you *list comprehensions* and *generators*, the common ways to express arguments to those functions. – Prune Jun 13 '21 at 16:53
  • @DeepSpace yes. – Berke Balcı Jun 13 '21 at 17:04
  • @quamrana Thanks I'll look for it – Berke Balcı Jun 13 '21 at 17:05
  • @BerkeBalcı but they are the exact same construct, just using a different operation on `i`. I don't see how it's possible to understand one but not the other – DeepSpace Jun 13 '21 at 21:15

1 Answers1

2

A structure like

[atom for atom in iterable if condition(atom)]

is a List Comprehension


Further-

any() returns True if at least one of the members of an iterable passed to it is Truthy

Together, this creates a list where each value is conditionally True and then returns whether at least one value in that new list is Truthy

The structure you provide actually does a lot more than it needs to, because the any() could be made redundant (a non-empty list is Truthy, and the condition x>0 guarantees that any entries are Truthy)

The second condition (after the and) seems to be some attempt to find palindromes by checking if the member is equal to itself reversed

>>> "1234"[::-1]
'4321'

Finally the statement itself is Truthy or Falsey, so there's no need for an if at all, and it can be directly used/displayed immediately when found

Here's an enterprise version

import sys

def test_substrings(sub_strings):
    for atom in sub_strings:
        if int(atom) <= 0:      # ValueError for non-int
            return False
        if atom != atom[::-1]:  # check for palindrome
            return False
    return True

sub_strings = input("enter a list of integers separated by spaces: ").split()

try:
    result = test_substrings(sub_strings)
except ValueError as ex:
    sys.exit("invalid entry (expected an integer): {}".format(
        repr(ex)))

print(str(result))

and a compact version

print(bool([x for x in input().split() if int(x) > 0 and x==x[::-1]]))

Beware: your example and mine will accept an empty input ("") as True

ti7
  • 16,375
  • 6
  • 40
  • 68