Skip to content

Instantly share code, notes, and snippets.

@kenzo0107
Last active November 24, 2019 15:31
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kenzo0107/7d43a2ff956336ed04efdc36cbc2800a to your computer and use it in GitHub Desktop.
def div_hash_inject(r)
[10_000, 5_000, 2_000, 1_000, 500, 100, 50, 10, 5, 1].inject(Hash.new(0)){|h, d|
a, r = r.divmod(d)
h[d] = a
h
}
end
p div_hash_inject(123_456) # {10000=>12, 5000=>0, 2000=>1, 1000=>1, 500=>0, 100=>4, 50=>1, 10=>0, 5=>1, 1=>1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment