Skip to content

Instantly share code, notes, and snippets.

@JustSnow
Created March 24, 2020 15:49
Show Gist options
  • Save JustSnow/01535bb36c0817d7dfc5ccb6b09c06e3 to your computer and use it in GitHub Desktop.
Save JustSnow/01535bb36c0817d7dfc5ccb6b09c06e3 to your computer and use it in GitHub Desktop.
Test task for invisible trust
# 1
Array(1..100).each do |number|
case
when (number % 3).zero? && (number % 5).zero? then puts 'HiBy'
when (number % 3).zero? then puts 'Hi'
when (number % 5).zero? then puts 'By'
else puts number
end
end
def random_string letters_count = 5
o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten
(0..letters_count).map { o[rand(o.length)] }.join
end
# 2
string = random_string 50
new_string = ''
string.each_char do |char|
if /[[:lower:]]/ =~ char
new_string += char.capitalize
else
new_string += char.downcase
end
end
puts "String before: #{string}"
puts "New string after: #{new_string}"
# 3
first_random_dict = random_string 5
second_random_dict = random_string 5
salt = random_string 5
true_string = first_random_dict + second_random_dict + salt
dict = [first_random_dict, second_random_dict]
third_random_dict = random_string 5
false_string = salt + third_random_dict
def segmentable? string, dict
fit_flag = false
dict.each do |pattern|
break if fit_flag
fit_flag = string.include? pattern
end
"Can be segmented: #{fit_flag}"
end
puts segmentable? true_string, dict
puts segmentable? false_string, dict
# 4
def getMaxSubSum array
maxSumStartIndex = 0
maxSumLastIndex = 0
maxSum = 0
lastSumStartIndex = 0
lastSum = 0
array.each_with_index do |item, index|
lastSum += item
if lastSum < item
lastSum = item
lastSumStartIndex = index
end
if maxSum < lastSum
maxSumStartIndex = lastSumStartIndex
maxSumLastIndex = index
maxSum = lastSum
end
end
array.slice(maxSumStartIndex, (maxSumLastIndex - maxSumStartIndex + 1))
end
p getMaxSubSum([-1, -13, -2, 1, -3, 4, -1, 2, 1, -5, 4]).inspect
#5
example_array = [[2], [3 ,4], [6, 5 ,7], [4, 1 ,8,3]] # 2 + 3 + 5 + 1 = 11
one_more_example_array = [[2, 1], [8, 9], [-1, 2, 3], [10, 15, 4]] # 1 + 8 -1 + 4 = 12
def find_shortest_path_to_end array
sum = 0
array.each do |sub_array|
sum += sub_array.min
end
p "Minimal sum to the end: #{sum}"
end
find_shortest_path_to_end example_array
find_shortest_path_to_end one_more_example_array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment