Tutte le sfide
Facile 2 risolte 100.0% di accettazione

Count Positive Numbers

Count the values greater than zero in a list.

Cosa imparerai

  • Loop over a list of numbers
  • Count values that match a condition
  • Ignore zero and negatives

Problema

Mostra traduzione

Count how many numbers in the list are greater than zero. Zero is neither positive nor negative.

Implement count_positives(numbers).

Input

numbers: The list of integers.

Output

Return the count.

Vincoli

0 ≤ length ≤ 100,000; each value fits in a 64-bit integer

Esempi

Esempio 1
Input:
numbers = [3, -1, 0, 7, 2]

Output atteso:
expected = 3
Input:
std::vector<long long> numbers = {3, -1, 0, 7, 2};

Output atteso:
long long expected = 3;
Input:
long[] numbers = new long[] {3, -1, 0, 7, 2};

Output atteso:
long expected = 3;

Spiegazione

3, 7 and 2 are positive: 3.

Esempio 2
Input:
numbers = [0, 0, -5]

Output atteso:
expected = 0
Input:
std::vector<long long> numbers = {0, 0, -5};

Output atteso:
long long expected = 0;
Input:
long[] numbers = new long[] {0, 0, -5};

Output atteso:
long expected = 0;

Spiegazione

No value is above zero: 0.

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

Only numbers greater than zero should be counted.

Suggerimento 2

The task is just a filtered count over the list.

Suggerimento 3

Do not include zero, since it is neither positive nor negative.

Complessità attesa

O(n) time, O(1) extra space

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 LMLaura Mahlow Python 798 ms - 5/5 Vedi il codice
2 RCRiccardo Corti Python 809 ms - 5/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 2 soluzioni complete sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le revisioni dell'IA. Accedi e risolvi la sfida per partecipare alla discussione.