It is necessary to check whether there are zeros among the given N numbers.

Input Format:

  • Enter the number N, and then N numbers.

Output Format:

  • Print True if there is at least one zero among the numbers entered, or False otherwise.

My decision:

print(any(int(input()) == 0 for _ in range(int(input())))) 

The receiving system does not want to receive it, although it works normally. It was experimentally found out that he does not want to accept because of the for loop.

How to change the program to avoid a cycle?

I tried to connect the library itertools something like this:

 from itertools import chain print(any(int(input()) == 0 in chain(range(int(input()))))) 

But something did not work out at all.

  • Recursion to use? - Herrgott
  • Corrected the text of the question. I'm just not only interested in this. - Nikolai Yarina
  • and what's the problem I do not understand? What recursion does not solve this problem? - Herrgott
  • About recursion, I didn’t quite understand how it can be done with it. - Nikolai Yarina
  • Well, the standard programming method. You make the function checkHaveZeros, to which you pass the array with the parameter, the current index i (by default 0). Then try catch I hope there is in python, in tray ketch you wrap arr [i] if not zero, call checkHaveZeros (arr, i + 1), if ketch means the array is over, return False, if zero, then return True. isThisArrayHasZero = checkHaveZeros (myArr, 0) print (isThisArrayHasZero) - Herrgott

3 answers 3

Coursera and course from HSE. Everything must be done in a functional style. Solved without additional libraries. I do not consider everything to be right. First, read the first line and create iterable from it:

 range(int(input())) 

Next, slip into the map function

 lambda x: int(input()) 

and obtained iterable . And then for the newly obtained iterable we apply the map with a check for equality to 0 and in the final any

    I would do this:

     In [21]: l = [1,2,3,4,5,6] In [22]: any(map(lambda x: x==0, l)) Out[22]: False In [23]: l = [0,1,2,0,3,4,0,5,6] In [24]: any(map(lambda x: x==0, l)) Out[24]: True 

    or a slightly shorter, but slightly “perverted” version - we assume that bool(num) is true for any non-zero number:

     In [33]: l Out[33]: [1, 2, 3, 4, 5, 6] In [34]: not all(map(bool, l)) Out[34]: False In [35]: l = [0,1,2,0,3,4,0,5,6] In [36]: not all(map(bool, l)) Out[36]: True 

    Or quite simply and elegantly (thanks LXA! ):

     In [42]: l = [1,2,3,4,5,6] In [43]: not all(l) Out[43]: False In [44]: l = [0,1,2,0,3,4,0,5,6] In [45]: not all(l) Out[45]: True 
    • one
      I think you can even cut to: not all(l) - LXA
    • @LXA, yes, indeed - it will learn so much more elegantly - thanks! - MaxU

    The easiest and easiest to understand option:

     n=input() #В данной программе не имеет значения, но для тестов необходимо k=0 base=list(map(int,input().split())) #в условии не конкретно объяснилось как производится ввод, но обычно в задачах такого типа ввод идет через пробел for l in base: if l == 0: k+=1 if k == 0: print("False") else: print("True") 
    • written without cycles - Herrgott
    • I realized that the problem is in the cycle, and I thought to do it with the cycle, but on the other hand - Timur Kasimov
    • The checking system wants a single-line code in a functional style in one line - Stress