Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soma /Sum #1

Open
anabneri opened this issue Apr 27, 2020 · 7 comments
Open

Soma /Sum #1

anabneri opened this issue Apr 27, 2020 · 7 comments
Assignees

Comments

@anabneri
Copy link
Collaborator

@anabneri anabneri commented Apr 27, 2020

[pt-br] Dada uma matriz de números inteiros, encontre a soma de seus elementos.

Por exemplo, se a matriz ar = [1, 2, 3], 1 + 2 + 3 = 6, então retorna 6

Descrição da função

Complete uma função que deve retornar a soma dos elementos da matriz como um número inteiro.
simpleArraySum possui o(s) seguinte(s) parâmetro(s):

  • ar: uma matriz de números inteiros

Exemplo de Entrada

6
1 2 3 4 10 11

Exemplo de saída
31

...

[en-us] Given an array of integers, find the sum of its elements.

For example, if the matrix ar = [1, 2, 3], 1 + 2 + 3 = 6, then returns 6

Description of function

Complete a function that should return the sum of the matrix elements as an integer.
simpleArraySum has the following parameter (s):

  • ar: an array of integers

Entry example

6
1 2 3 4 10 11

Example output
31

@anabneri anabneri self-assigned this Apr 27, 2020
@anabneri anabneri changed the title Soma De Matrizes/ArraySum Soma /Sum Apr 28, 2020
@paulocordeiro
Copy link

@paulocordeiro paulocordeiro commented Apr 28, 2020

// throw NullPointerException if ar is null
 int simpleArraySum(int[] ar) {
    return Arrays.stream(ar).reduce(0, Integer::sum);
}
@anabneri
Copy link
Collaborator Author

@anabneri anabneri commented Apr 28, 2020

// throw NullPointerException if ar is null
 int simpleArraySum(int[] ar) {
    return Arrays.stream(ar).reduce(0, Integer::sum);
}

--> Consegue mandar o link do código?

@paulocordeiro
Copy link

@paulocordeiro paulocordeiro commented Apr 28, 2020

// throw NullPointerException if ar is null
 int simpleArraySum(int[] ar) {
    return Arrays.stream(ar).reduce(0, Integer::sum);
}

--> Consegue mandar o link do código?

https://gist.github.com/paulocordeiro/85951bf633f3ff9c3a4a225fc70806c4

@mferreira17
Copy link

@mferreira17 mferreira17 commented Apr 28, 2020

Groovy pode? Se sim, seria apenas
int sumOfElemsInArr(def arr){ /*should throw error if arr == null*/ return arr.sum() }

@danielle8farias
Copy link
Contributor

@danielle8farias danielle8farias commented Apr 28, 2020

Minha solução, em Python:

#função print retorna uma string na tela
print('Escolhendo o tamanho do vetor:')
#input() recebe como string dado digitado
#int() convertendo string para tipo inteiro
#atribuindo valor a variável num
num = int(input('Digite um número inteiro: '))
#criando vetor
lista = []
#inicializando variável contadora
i = 1
#inicializando soma
soma = 0
#range vai até um valor a menos do parâmetro, por isso num+1
#laço que começa em 1 e vai até num
for i in range(i, num+1):
    #i assume valores de 1 a num; conforme a execução do laço
    #atribuindo a variável os elementos do vetor
    elemento = int(input(f'Digite o {i}º número: '))
    #somando os valores de cada elemento passado
    soma += elemento
    #adicionando os elementos a lista(vetor)
    lista.append(elemento)
#função print vazia não retorna nada; pula uma linha
print()
#função print retorna uma string formatada na tela
print(f'Vetor: {lista}')
print(f'Soma dos elementos do vetor: {soma}')
@leticiacamposs2
Copy link
Contributor

@leticiacamposs2 leticiacamposs2 commented Jun 23, 2020

Minha solução em Javascript

function simpleArraySum(ar) {
    const sum = (accumulator, currentValue) => accumulator + currentValue;
    return console.log(ar.reduce(sum));
}

Repositório:
https://github.com/leticiacamposs2/hackerrank-algorithms-js/blob/master/01-simple-array-sum.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.