Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wenjie Fang - Recursion-tracing - Octos #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions mystery-methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ def mystery1(n)
end
end

# Q1: mystery1(5) => 15
# Q2: mystery1(10) => 155
# Q3: mystery1(0) => inifinite loop / stack overflow error
# Time Complexity: O(n)
# Space Complexity: O(n)

def mystery2(n)
if n < 10
return n
Expand All @@ -15,6 +21,13 @@ def mystery2(n)
end
end

# Q1: mystery2(123) => 6
# Q2: mystery2(9005) => 14
# Q3: mystery2(-123) => -123
# Time Complexity: O(n)
# Space Complexity: O(n)
# Condition: return n if n > -10 || n < 10

def mystery3(n)
if n == 0
return 100
Expand All @@ -28,6 +41,13 @@ def mystery3(n)
end
end

# Q1: mystery3(1) => 100
# Q2: mystery3(13) => 100
# Q3: mystery3(-6) => 200
# Time Complexity: O(n)
# Space Complexity: O(n)


def mystery4(b,e)
if e == 0
return 1
Expand All @@ -36,6 +56,12 @@ def mystery4(b,e)
end
end

# Q1: mystery4(10, 2) => 100
# Q2: mystery4(4, 3) => 64
# Q3: mystery4(5, 0) => 1
# Time Complexity: O(e)
# Space Complexity: O(e)

def mystery5(s)
if s.length == 0
return ""
Expand All @@ -44,6 +70,13 @@ def mystery5(s)
end
end

# Q1: mystery5('hi') => '**'
# Q1: mystery5('') => ''
# Q1: mystery5('Hi, there!') => '**********'
# Time Complexity: 0(s.length)
# Space Complexity: O(s.length)
# return '*' + mystery5(s[1..-1]) if s[0] is a letter

def mystery6(s)
if s == nil || s.length == 0
return ""
Expand All @@ -56,6 +89,15 @@ def mystery6(s)
end
end


# Q1: mystery6('goodnight moon') => ' moon goodnight'
# Q2: mystery6('Ada Developer Academy') => ' Academy Developer Ada'
# Q3: mystery6('Hi, there!') => ' there! Hi,'
# Time Complexity: O(n) - n is the lenggth of s
# Space Complexity: O(n) - n is the lenggth of s
# Added fun:


def mystery7(word)
if word.length < 2
return true
Expand All @@ -65,3 +107,9 @@ def mystery7(word)
return mystery7(word[1..-2])
end
end

# Q1: mystery7('cupcake') => false
# Q2: mystery7('detected') => false
# Q3: mystery7('eye') => true
# Time Complexity: O(n) - n is the length of word
# Space Complexity: O(n) - n is the length of word