Skip to content

Instantly share code, notes, and snippets.

@maggit
Last active March 1, 2023 01:41
Show Gist options
  • Save maggit/b355d4e7216da56f1de7 to your computer and use it in GitHub Desktop.
Save maggit/b355d4e7216da56f1de7 to your computer and use it in GitHub Desktop.
Suma los digitos!

#Suma los digitos! Este archivo de texto tiene un montón de números, el reto es sumar todos los numeros!!!

Ejemplo (si el archivo solo tuviera los siguientes numeros):

  159375
  924
  621
  8
  1 + 5 + 9 + 3 + 7 + 5 + 9 + 2 + 4 + 6 + 2 + 1 + 8 = 62

###Suma los números!

Enumerable

Array

String

File

@RamiroAlvaro
Copy link

captura de pantalla 2016-03-14 a las 16 06 28

ramiroalvaro at Ramiro in ~/Documents
$ ruby desafio.rb
El total de la suma es: 18000

@jucavi
Copy link

jucavi commented Mar 18, 2016

suma = 0
File.open("suma_numeros.txt", "r").each_char do |char|
suma += char.to_i
end

puts "#{suma}"

@dloaizan546
Copy link

@dehuelsln

suma = 0
File.open("numeros.txt", "r").each do |line|
line.each_char { |numero| suma += numero.to_i}
end
puts "Total: #{suma}"

@kbzaso
Copy link

kbzaso commented Mar 26, 2016

suma = 0
File.open("numbers.txt", "r").each do |lineas|
lineas.each_char do |x|
suma += x.to_i
end
end

puts suma

@oscartzgz
Copy link

suma_total = 0
File.open("numbers.txt", "r").each_char do |numero|
suma_total += numero.to_i
end
puts "La suma total es: #{suma_total}"

@lubc
Copy link

lubc commented Apr 10, 2016

$ ruby challange.rb numbers.txt

x = 0
File.open(ARGV[0], 'r').each{ |a| a.each_char{ |b| x += b.to_i } }
puts 'x'

-> 18000

@Geresis
Copy link

Geresis commented Apr 13, 2016

total = 0

f = File.open("numbers.txt", "r")
f.each_line do |linea|
linea.each_char do |numero|
total += numero.to_i
end
end

puts "La suma es = #{total}"

@davidses
Copy link

f = File.open("numbers.txt","r")
suma = 0

f.each_char do |char|
    suma += char.to_i
end

puts suma # => 18000

@martong16
Copy link

martong16 commented Jun 11, 2016

No Hace mucho comencé el curso y en el Workshop: Lectura y manipulación de archivos me toque con este reto y quise dar mi aporte a la solución y aquí esta:

suma = 0
def suma_digito(ruta_archivo)
archivo = File.open(ruta_archivo, "r")
archivo.each_char do |linea|
suma += linea.to_i
end
suma = suma_digito("numbers.txt")
puts "la suma total es: #{suma}"

@LuN4t1k0
Copy link

suma = 0
File.readlines("datos.txt").each do |linea|
linea.each_char do |x|
suma += x.to_i
end
end
puts "El total es #{suma}"

@danielveraec
Copy link

Mi solución:
file_name = ARGV[0]
suma = 0
File.readlines(file_name).each{|linea| linea.each_char{|numero| suma += numero.to_i}}
puts "Total sumado: #{suma}"

@mudhappy
Copy link

mudhappy commented Aug 1, 2016

suma = 0
File.readlines("numeros.txt").each do |linea|
    linea.each_char{ |num| suma += num.to_i }
end
puts suma

oh si, 5 lineas 👯

@rmilano
Copy link

rmilano commented Sep 27, 2016

suma = 0
File.readlines("sumar.txt").each_with_index do |linea|
linea.each_char do |c|
suma += c.to_i
end
end

puts suma

@antonyhm25
Copy link

antonyhm25 commented Oct 2, 2016

f = File.open('digitos.txt', 'r')

suma = 0

f.each_char do |letter|
  suma += letter.to_i
end

puts suma

Resultado: 18000

@pfcvik
Copy link

pfcvik commented Oct 4, 2016

`file_name = ARGV[0]

suma = 0

File.readlines(file_name).each_with_index do |linea, linea_num|
linea.each_char do |n|
suma = suma + n.to_i
end
end

puts suma`

@rocanrolpsycho
Copy link

Total 18000 =D!!!

nombre=ARGV[0]
archivo = File.open(nombre,"r")
total = 0
archivo.each do |linea|
temp = linea.split("")
temp.each do |num|
total = num.to_i + total
end
end
puts total

@BeingPedro
Copy link

Aqui les dejo mi solución

suma = 0
File.readlines("numeros.txt").each { |linea|
 linea.each_char { |c| suma += c.to_i } }
puts suma

@erickdbrito
Copy link

file = File.open('digitos.txt', 'r')

suma_digitos = 0

file.each do |linea_numerica|
  linea_numerica.each_char do |digito|
     suma_digitos += digito.to_i
  end
end

puts "Resultado: #{suma_digitos}"

@Jose-code-prog
Copy link

Alguien sabe como puedo hacer la suma pero en vertical? por ejemplo:
15
9
6
8

suma: 38

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment