Skip to content

Instantly share code, notes, and snippets.

@corneliusellen
Created May 30, 2018 16:32
Show Gist options
  • Save corneliusellen/18d608bcdbb036638375ac04cd7f8592 to your computer and use it in GitHub Desktop.
Save corneliusellen/18d608bcdbb036638375ac04cd7f8592 to your computer and use it in GitHub Desktop.
require 'pry'
x = [1, 2, 3, 4, 5]
############
sum = 0
for current_number in x do
sum += current_number
end
puts sum
############
sum = 0
count = 0
while count < x.length do
sum += x[count]
count += 1
end
puts sum
############
@sum = 0
@count = 0
def recursion_sum(array)
unless @count == 5
@sum += array[@count]
@count += 1
recursion_sum(array)
end
end
recursion_sum(x)
puts @sum
var x = [1, 2, 3, 4, 5]
///////
var sum = 0
for(i = 0; i < x.length; i++){
sum += x[i]
}
console.log(sum)
///////
var sum = 0
var count = 0
while(count < 5) {
sum += x[count]
count += 1
}
console.log(sum)
require 'pry'
letters = ["a", "b", "c"]
numbers = [1, 2, 3]
puts letters.zip(numbers).flatten
require 'pry'
fibonacci = [0, 1]
count = 0
while count < 100 do
next_number = fibonacci[count] + fibonacci[count + 1]
fibonacci << next_number
count += 1
end
puts fibonacci
require 'pry'
random = [50, 2, 50, 9]
95052
95502
more_than_1_digit = {
}
more_than_1_digit_count = Hash.new(0)
random.each do |num|
if num.to_s.length > 1
more_than_1_digit[num.to_s.chars.first.to_i] = num
end
end
random.each do |num|
if num.to_s.length > 1
more_than_1_digit_count[num.to_s.chars.first.to_i] += 1
end
end
first_digits = random.map do |num|
num.to_s.chars.first.to_i
end
largest_sum = []
first_digits.sort.reverse.map do |num|
unless more_than_1_digit[num] == nil
largest_sum << more_than_1_digit[num]
else
largest_sum << num
end
end
largest_sum.each do |num|
num
end
binding.pry
puts largest_sum.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment