Skip to content

Commit 0abe863

Browse files
author
haozeng
committed
most recent update
1 parent 1981f42 commit 0abe863

File tree

83 files changed

+1996
-117
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1996
-117
lines changed

.DS_Store

6 KB
Binary file not shown.

README.md

Whitespace-only changes.

bc_command.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Basic Idea: The idea is to separate the operators and integers into two arrays and
2+
# handle their operations in the correct order. After each iteration, the size of two arrays will change
3+
# to keep track the latest operations.
4+
5+
# For example, we are given 5*4/2+10, and we seperate it into integers [5,4,2,10] and operators [*, /, +]
6+
# Step 1: 5*4 = 20, integer array became [20, 2, 10]
7+
# Step 2: 20/2 = 10, integer array became [10, 10]
8+
# Step 3: after the multiplicatio and division operations, operator array became [+]
9+
# Step 4: integer array became [20], which is the expected results.
10+
11+
def bc(string)
12+
# initialize the operators and integers array
13+
operators = []
14+
integers = []
15+
16+
# separate integers and operators into different arrays
17+
integers = string.split(/\+|\-|\*|\//).map(&:to_i)
18+
string.each_char do |c|
19+
if c.match /\+|\-|\*|\//
20+
operators << c
21+
end
22+
end
23+
24+
# we want to handle * and / first
25+
count = 0
26+
operators.each_with_index do |o, i|
27+
if o.match /\*|\//
28+
r = integers[i-count].send(o, integers[i+1-count])
29+
integers.delete_at i+1-count
30+
integers.delete_at i-count
31+
integers.insert(i, r)
32+
count += 1
33+
end
34+
end
35+
36+
# remove all * and / operators
37+
operators = operators.select do |o|
38+
!o.match /\*|\//
39+
end
40+
41+
# we want to handle + and -
42+
integers.compact!
43+
count = 0
44+
operators.each_with_index do |o, i|
45+
if o.match /\+|\-/
46+
r = integers[i-count].send(o, integers[i+1-count])
47+
integers.delete_at i+1-count
48+
integers.delete_at i-count
49+
integers.insert(i, r)
50+
count += 1
51+
end
52+
end
53+
54+
integers.compact
55+
end
56+
57+
puts "5*4"
58+
puts bc("5*4")
59+
puts "5*4/2"
60+
puts bc("5*4/2")
61+
puts "5*4/2+10"
62+
puts bc("5*4/2+10")
63+
puts "100/100"
64+
puts bc("100/100")
65+
puts "10+10+10+10"
66+
puts bc("10+10+10+10")
67+
puts "10+10+10+10"
68+
puts bc("10+10+10+10")
69+
puts "10-10"
70+
puts bc("10-10")
71+
puts "10+10*10/10"
72+
puts bc("10+10*10/10")

binary_search_tree_diameter.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# sudo code:
2+
# 1. return 0 if the node is nil
3+
# 2. get the height and diameter from left node.
4+
# 3. get the height and diameter from right node.
5+
# 4. calculate height from previous step.
6+
# 5. return the diameter and its height
7+
require 'pry'
8+
9+
class Node
10+
attr_accessor :value, :left, :right, :name
11+
12+
def initialize(options={})
13+
@value = options[:value]
14+
@name = options[:name]
15+
@left = options[:left] || nil
16+
@right = options[:right] || nil
17+
end
18+
19+
def children?
20+
@left && @right
21+
end
22+
23+
def no_children?
24+
!children?
25+
end
26+
end
27+
28+
def diameter(node)
29+
return [0, 0] if node.nil?
30+
31+
ld, lh = diameter(node.left)
32+
rd, rh = diameter(node.right)
33+
34+
height = [lh, rh].max + 1
35+
36+
[[lh + rh + 1, ld, rd].max, height]
37+
end
38+
39+
root = Node.new({:value => 1, :name => 'root'})
40+
child_1 = Node.new({:value => 2, :name => 'child_1'})
41+
child_2 = Node.new({:value => 3, :name => 'child_2'})
42+
grand_child_1 = Node.new({:value => 4, :name => 'grand_child_1'})
43+
grand_grand_child_1 = Node.new({:value => 5, :name => 'grand_grand_child_1'})
44+
grand_child_1.left = grand_grand_child_1
45+
child_1.left = grand_child_1
46+
root.left = child_1
47+
root.right = child_2
48+
49+
height = diameter(root)
50+
puts height

bridgewater.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Thought Process:
2+
# We want to use a nested hash structure as we iterate the string to save the count as well as the location
3+
# of each word, so total time complexity is O(n), since we are just iterating the total string once.
4+
# Here I simply use ruby to demonstrate the idea and write the program
5+
6+
# 1. Scan through the whole string and find out places where . is being used other than the end of sentence.
7+
# Replace . with {dot} in those places, Note: we want to add appropriate regrex if needed, in this case, it is i.e.
8+
# 2. Split the string after each sentence ending with .
9+
# 3. For each sentence, we want to iterate each word and count the appearance of the word and the index, the location of the word in the string.
10+
# 4, In the last, we iterate the nested hash and print out the word, number of its appearance and its appearing location.
11+
12+
string = 'Given an arbitrary text document written in English, write a program that will generate a '\
13+
'concordance, i.e. an alphabetical list of all word occurrences, labeled with word frequencies.'\
14+
' Bonus: label each word with the sentence numbers in which each occurrence appeared.'
15+
16+
def concordance(string)
17+
# Initialize the hash
18+
concordance = {}
19+
20+
# Replace the . with {dot} using regrex, and in this case it is just i.e.
21+
# Split the sentence
22+
string.gsub!(/(i)\.(e)\./, "i{dot}e{dot}").split('.').each_with_index do |sentence, index|
23+
# Iterate words in the sentence
24+
sentence.gsub(',', '').split(' ').each do |word|
25+
# Change the {dot} back to .
26+
word = word.gsub(/{dot}/, '.').downcase
27+
# Save the index and its count in the nested hash
28+
concordance[word] ||= [0, []]
29+
concordance[word][0] += 1
30+
concordance[word][1] << index + 1
31+
end
32+
end
33+
34+
# Print out the hash based on the format accordingly
35+
concordance.sort_by { |k, _| k }.each do |pair|
36+
puts "#{pair[0]} {#{pair[1][0]}:#{pair[1][1].join(',')}}"
37+
end
38+
end
39+
40+
# run the function
41+
concordance(string)
42+
43+
44+
# Execution instruction:
45+
# ➜ exercises git:(master) ✗ ruby bridgewater.rb
46+
47+
# Output
48+
# ➜ exercises git:(master) ✗ ruby bridgewater.rb
49+
# a {2:1,1}
50+
# all {1:1}
51+
# alphabetical {1:1}
52+
# an {2:1,1}
53+
# appeared {1:2}
54+
# arbitrary {1:1}
55+
# bonus: {1:2}
56+
# concordance {1:1}
57+
# document {1:1}
58+
# each {2:2,2}
59+
# english {1:1}
60+
# frequencies {1:1}
61+
# generate {1:1}
62+
# given {1:1}
63+
# i.e. {1:1}
64+
# in {2:1,2}
65+
# label {1:2}
66+
# labeled {1:1}
67+
# list {1:1}
68+
# numbers {1:2}
69+
# occurrence {1:2}
70+
# occurrences {1:1}
71+
# of {1:1}
72+
# program {1:1}
73+
# sentence {1:2}
74+
# text {1:1}
75+
# that {1:1}
76+
# the {1:2}
77+
# which {1:2}
78+
# will {1:1}
79+
# with {2:1,2}
80+
# word {3:1,1,2}
81+
# write {1:1}
82+
# written {1:1}

bridgewater.txt

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Thought Process:
2+
# We want to use a nested hash structure as we iterate the string to save the count as well as the location
3+
# of each word, so total time complexity is O(n), since we are just iterating the total string once.
4+
# Here I simply use ruby to demonstrate the idea and write the program
5+
6+
# 1. Scan through the whole string and find out places where . is being used other than the end of sentence.
7+
# Replace . with {dot} in those places, Note: we want to add appropriate regrex if needed, in this case, it is i.e.
8+
# 2. Split the string after each sentence ending with .
9+
# 3. For each sentence, we want to iterate each word and count the appearance of the word and the index, the location of the word in the string.
10+
# 4, In the last, we iterate the nested hash and print out the word, number of its appearance and its appearing location.
11+
12+
string = 'Given an arbitrary text document written in English, write a program that will generate a '\
13+
'concordance, i.e. an alphabetical list of all word occurrences, labeled with word frequencies.'\
14+
' Bonus: label each word with the sentence numbers in which each occurrence appeared.'
15+
16+
def concordance(string)
17+
# Initialize the hash
18+
concordance = {}
19+
20+
# Replace the . with {dot} using regrex, and in this case it is just i.e.
21+
# Split the sentence
22+
string.gsub!(/(i)\.(e)\./, "i{dot}e{dot}").split('.').each_with_index do |sentence, index|
23+
# Iterate words in the sentence
24+
sentence.gsub(',', '').split(' ').each do |word|
25+
# Change the {dot} back to .
26+
word = word.gsub(/{dot}/, '.').downcase
27+
# Save the index and its count in the nested hash
28+
concordance[word] ||= [0, []]
29+
concordance[word][0] += 1
30+
concordance[word][1] << index + 1
31+
end
32+
end
33+
34+
# Print out the hash based on the format accordingly
35+
concordance.sort_by { |k, _| k }.each do |pair|
36+
puts "#{pair[0]} {#{pair[1][0]}:#{pair[1][1].join(',')}}"
37+
end
38+
end
39+
40+
# run the function
41+
concordance(string)
42+
43+
44+
# Execution instruction:
45+
# ➜ exercises git:(master) ✗ ruby bridgewater.rb
46+
47+
# Output
48+
# ➜ exercises git:(master) ✗ ruby bridgewater.rb
49+
# a {2:1,1}
50+
# all {1:1}
51+
# alphabetical {1:1}
52+
# an {2:1,1}
53+
# appeared {1:2}
54+
# arbitrary {1:1}
55+
# bonus: {1:2}
56+
# concordance {1:1}
57+
# document {1:1}
58+
# each {2:2,2}
59+
# english {1:1}
60+
# frequencies {1:1}
61+
# generate {1:1}
62+
# given {1:1}
63+
# i.e. {1:1}
64+
# in {2:1,2}
65+
# label {1:2}
66+
# labeled {1:1}
67+
# list {1:1}
68+
# numbers {1:2}
69+
# occurrence {1:2}
70+
# occurrences {1:1}
71+
# of {1:1}
72+
# program {1:1}
73+
# sentence {1:2}
74+
# text {1:1}
75+
# that {1:1}
76+
# the {1:2}
77+
# which {1:2}
78+
# will {1:1}
79+
# with {2:1,2}
80+
# word {3:1,1,2}
81+
# write {1:1}
82+
# written {1:1}
13.7 KB
Binary file not shown.
28 KB
Binary file not shown.

0 commit comments

Comments
 (0)