
Staff Engineer @ZenBusinessInc | Tech, Mechanical Keyboards and Cats 💻⌨️😼 | Ich spreche nur ein bisschen Deutsch, aber lerne ich schnell! 🍻
- Tweets743
- Following1K
- Followers187
- Likes3.1K
![Ruby code to find the depth of a binary tree. Code: def max_depth(root) return depth_first_search(root, 0) end def depth_first_traversal(node, level) return level if node == nil left = depth_first_traversal(node.left, level + 1) right = depth_first_traversal(node.right, level + 1) return [left, right].max end](https://pbs.twimg.com/media/E8jqWFAWQAkahjA.jpg?name=small&format=webp)
ALT Ruby code to find the depth of a binary tree. Code: def max_depth(root) return depth_first_search(root, 0) end def depth_first_traversal(node, level) return level if node == nil left = depth_first_traversal(node.left, level + 1) right = depth_first_traversal(node.right, level + 1) return [left, right].max end
![Ruby anagram checker Code: # Given two strings s and t, return true if t is an anagram of s, and false otherwise. def is_anagram(s, t) if s.length != t.length return false end # convert both into a char: count hash s_hash = string_to_hash(s) t_hash = string_to_hash(t) # check the counts of the characters compare_hashes(s_hash, t_hash) end def string_to_hash(s) hash = {} s.each_char do |c| count = 0 if hash.include?(c) count = hash[c] end hash[c] = count + 1 end hash end def compare_hashes(s_hash, t_hash) s_hash.each_pair do |s_key, s_value| t_value = t_hash[s_key] if s_value != t_value return false end end true end](https://pbs.twimg.com/media/E8S0e16XMAQ4yPz.jpg?name=small&format=webp)
ALT Ruby anagram checker Code: # Given two strings s and t, return true if t is an anagram of s, and false otherwise. def is_anagram(s, t) if s.length != t.length return false end # convert both into a char: count hash s_hash = string_to_hash(s) t_hash = string_to_hash(t) # check the counts of the characters compare_hashes(s_hash, t_hash) end def string_to_hash(s) hash = {} s.each_char do |c| count = 0 if hash.include?(c) count = hash[c] end hash[c] = count + 1 end hash end def compare_hashes(s_hash, t_hash) s_hash.each_pair do |s_key, s_value| t_value = t_hash[s_key] if s_value != t_value return false end end true end
![A (probably) poorly written ruby function for finding if an array contains duplicates. I’m not sure the right way to put code in alt text, let me know if there’s a better way. The Code: def contains_duplicate(nums) dict = {} for i in 0..nums.length do letter = nums[i] if dict.key?(letter) return true end dict[letter] = 1 end return false end](https://pbs.twimg.com/media/E8C5EIBWUAMus2F.jpg?name=small&format=webp)
ALT A (probably) poorly written ruby function for finding if an array contains duplicates. I’m not sure the right way to put code in alt text, let me know if there’s a better way. The Code: def contains_duplicate(nums) dict = {} for i in 0..nums.length do letter = nums[i] if dict.key?(letter) return true end dict[letter] = 1 end return false end



![def string_to_hash(s) hash = Hash.new(0) s.each_char do |c| hash[c] += 1 end hash end](https://pbs.twimg.com/media/E8UP7PZX0AILZ2v.jpg?name=small&format=webp)



