Skip to content

Instantly share code, notes, and snippets.

@tansengming
Created October 13, 2016 13:01
Show Gist options
  • Save tansengming/62bb0cbae732ac3bd61273e902513a07 to your computer and use it in GitHub Desktop.
Save tansengming/62bb0cbae732ac3bd61273e902513a07 to your computer and use it in GitHub Desktop.
look it crackles and pops
#! /usr/bin/env ruby
sum = ->(top, bottom) { top.zip(bottom).map{|array| array.inject('', :+) } }
mask = ->(top, bottom) { top.zip(bottom).map{|mask_layer, bottom_layer| mask_layer.empty? ? bottom_layer : mask_layer } }
list = (1..100).to_a
crackles = list.map{|num| (num % 3) == 0 ? 'Crackle' : '' }
pops = list.map{|num| (num % 5) == 0 ? 'Pop' : '' }
# ['', '', 'Crackle', '', '' , ...]
# SUM
# ['', '', '' , '', 'Pop', ...]
# ||
# ['', '', 'Crackle', '', 'Pop', ...]
crackle_pops = sum[crackles, pops]
# ['', '', 'Crackle', '', 'Pop', ...]
# MASK
# [1 , 2 , 3 , 4 , 5 , ...]
# ||
# [1 , 2 , 'Crackle', 4 , 'Pop', ...]
result = mask[crackle_pops, list]
puts result.join(', ')
# `ruby crackle_pop.rb test` to run
if ARGV.size == 1 && ARGV.first == 'test'
require 'test/unit'
extend Test::Unit::Assertions
assert_equal 1, result[0]
assert_equal 'Crackle', result[2]
assert_equal 'Pop', result[4]
assert_equal 'CracklePop',result[14]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment