Skip to content

Instantly share code, notes, and snippets.

@zwilderrr
Created January 16, 2018 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zwilderrr/6816aacdbd19595b27d6079c6e7f1a13 to your computer and use it in GitHub Desktop.
Save zwilderrr/6816aacdbd19595b27d6079c6e7f1a13 to your computer and use it in GitHub Desktop.
in ruby
def continuous_seq(number)
split_num = number.to_s.chars.map(&:to_i)
num_arr = []
index = 0
while index < split_num.length
chunk = []
repeat = true
while repeat
if split_num[index].odd?
if split_num[index + 1].odd?
chunk << split_num[index]
end
elsif
if split_num[index + 1].even?
chunk << split_num[index]
end
else
split_num << split_num[index]
repeat = false
end
num_arr << chunk
index += 1
end
end
num_arr
end
continuous_seq(234)
def squared_seq(num)
squared_arr = [num]
index = 0
while true
next_num = add_squares(squared_arr[index])
if squared_arr.include?(next_num)
break
else
index += 1
squared_arr.push(next_num)
end
end
squared_arr.length + 1
end
def add_squares(num)
split_num = num.to_s.chars.map(&:to_i)
sum = 0
split_num.each { |num| sum += num ** 2 }
sum
end
squared_seq(19)
def movie_assistant(elapsed, duration, action, *time_spec)
num_elapsed = convert_from_string(elapsed).to_f
num_duration = convert_from_string(duration).to_f
result = 0
if action == "Report Progress"
result = 1 - ((num_duration / num_elapsed).round(2))
elsif action == "Rewind"
optional = time_spec[0]
if optional.is_a? String
result = num_elapsed - convert_from_string(optional)
else
result = num_elapsed - (((optional.to_f) / 100) * num_elapsed)
end
elsif action == "Fast Forward"
if optional.is_a? String
result = convert_from_string(num_elapsed + convert_from_string(optional))
else
result = num_elapsed + (((optional.to_f) / 100) * num_elapsed)
end
end
if result >= num_duration
return "Movie Finished"
elsif result <= 0
return "00:00:00"
else
return convert_to_string(result)
end
end
def convert_from_string(string)
num_arr = string.split(":").map(&:to_i)
seconds = num_arr[0] * 3600 + num_arr[1] * 60 + num_arr[2]
end
def convert_to_string(seconds)
return Time.at(seconds).utc.strftime("%H:%M:%S")
# in longhand:
hours = seconds / 3600
minutes = (seconds - (hours * 3600)) / 60
seconds = (seconds - (hours * 3600) - (minutes * 60))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment