A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A, C, G and T have impact factors of 1, 2, 3 and 4, respectively. You are going to answer several queries of the form: What is the minimal impact factor of nucleotides contained in a particular part of the given DNA sequence?
The DNA sequence is given as a non-empty string S = S[0]S[1]...S[N-1] consisting of N characters. There are M queries, which are given in non-empty arrays P and Q, each consisting of M integers. The K-th query (0 ≤ K < M) requires you to find the minimal impact factor of nucleotides contained in the DNA sequence between positions P[K] and Q[K] (inclusive).
For example, consider string S = CAGCCTA and arrays P, Q such that:
P[0] = 2 Q[0] = 4 P[1] = 5 Q[1] = 5 P[2] = 0 Q[2] = 6The answers to these M = 3 queries are as follows:
- The part of the DNA between positions 2 and 4 contains nucleotides G and C (twice), whose impact factors are 3 and 2 respectively, so the answer is 2.
- The part between positions 5 and 5 contains a single nucleotide T, whose impact factor is 4, so the answer is 4.
- The part between positions 0 and 6 (the whole string) contains all nucleotides, in particular nucleotide A whose impact factor is 1, so the answer is 1.
Write a function:
def solution(s, p, q)
that, given a non-empty string S consisting of N characters and two non-empty arrays P and Q consisting of M integers, returns an array consisting of M integers specifying the consecutive answers to all queries.
Result array should be returned as an array of integers.
For example, given the string S = CAGCCTA and arrays P, Q such that:
P[0] = 2 Q[0] = 4 P[1] = 5 Q[1] = 5 P[2] = 0 Q[2] = 6the function should return the values [2, 4, 1], as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- M is an integer within the range [1..50,000];
- each element of arrays P and Q is an integer within the range [0..N - 1];
- P[K] ≤ Q[K], where 0 ≤ K < M;
- string S consists only of upper-case English letters A, C, G, T.
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length, { A: 0, C: 0, G: 0, T: 0 })
# Iterating through each character, increment
# the counter for that character at that position.
string.split('').each_with_index do |char, i|
running_total = prefix_sums[i][char.to_sym]
prefix_sums[i][char.to_sym] = running_total + 1
end
prefix_sums.each_with_index do |p,i|
puts "i: #{i}, p: #{p.join {|k, v| k + ' ' + v}"
end
user.rb:21: syntax error, unexpected tSTRING_BEG, expecting tSTRING_DEND user.rb:62: syntax error, unexpected tCONSTANT, expecting tSTRING_DEND STDERR.puts "Invalid result type, Integer expecte... ^ user.rb:62: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '(' ...d result type, Integer expected, #{inp.class} found" ... ^ user.rb:63: syntax error, unexpected '\n', expecting :: or '[' or '.' user.rb:66: syntax error, unexpected keyword_end, expecting tSTRING_DEND user.rb:147: syntax error, unexpected end-of-input, expecting tSTRING_DEND
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length, { A: 0, C: 0, G: 0, T: 0 })
# Iterating through each character, increment
# the counter for that character at that position.
string.split('').each_with_index do |char, i|
running_total = prefix_sums[i][char.to_sym]
prefix_sums[i][char.to_sym] = running_total + 1
end
prefix_sums.each_with_index do |p,i|
puts "i: #{i}, p: #{p.join {|k, v| k + ' ' + v}}"
end
user.rb:147: syntax error, unexpected end-of-input, expecting keyword_end
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length, { A: 0, C: 0, G: 0, T: 0 })
# Iterating through each character, increment
# the counter for that character at that position.
string.split('').each_with_index do |char, i|
running_total = prefix_sums[i][char.to_sym]
prefix_sums[i][char.to_sym] = running_total + 1
end
prefix_sums.each_with_index do |p,i|
puts "i: #{i}, p: #{p}"
end
user.rb:147: syntax error, unexpected end-of-input, expecting keyword_end
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length, { A: 0, C: 0, G: 0, T: 0 })
# Iterating through each character, increment
# the counter for that character at that position.
string.split('').each_with_index do |char, i|
running_total = prefix_sums[i][char.to_sym]
prefix_sums[i][char.to_sym] = running_total + 1
end
prefix_sums.each_with_index do |p, i|
puts "i: #{i}, p: #{p}"
end
user.rb:147: syntax error, unexpected end-of-input, expecting keyword_end
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length, { A: 0, C: 0, G: 0, T: 0 })
# Iterating through each character, increment
# the counter for that character at that position.
string.split('').each_with_index do |char, i|
running_total = prefix_sums[i][char.to_sym]
prefix_sums[i][char.to_sym] = running_total + 1
end
prefix_sums.each_with_index do |p, i|
puts "i: #{i}, p: #{p}"
end
end
Invalid result type, Integer expected, Hash foundstdout:
i: 0, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 1, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 2, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 3, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 4, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 5, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 6, p: {:A=>2, :C=>3, :G=>1, :T=>1}
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length, { A: 0, C: 0, G: 0, T: 0 })
# Iterating through each character, increment
# the counter for that character at that position.
string.split('').each_with_index do |char, i|
running_total = prefix_sums[i][char.to_sym]
prefix_sums[i + 1][char.to_sym] = running_total + 1
end
prefix_sums.each_with_index do |p, i|
puts "i: #{i}, p: #{p}"
end
end
user.rb:17:in `block in solution': undefined method `[]=' for nil:NilClass (NoMethodError) from user.rb:15:in `each' from user.rb:15:in `each_with_index' from user.rb:15:in `solution' from user.rb:146:in `<main>'
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length, { A: 0, C: 0, G: 0, T: 0 })
# Iterating through each character, increment
# the counter for that character at that position.
string.split('').each_with_index do |char, i|
running_total = prefix_sums[i - 1][char.to_sym]
prefix_sums[i][char.to_sym] = running_total + 1
end
prefix_sums.each_with_index do |p, i|
puts "i: #{i}, p: #{p}"
end
end
Invalid result type, Integer expected, Hash foundstdout:
i: 0, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 1, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 2, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 3, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 4, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 5, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 6, p: {:A=>2, :C=>3, :G=>1, :T=>1}
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length, { A: 0, C: 0, G: 0, T: 0 })
# Iterating through each character, increment
# the counter for that character at that position.
string.split('').each_with_index do |char, i|
running_total = prefix_sums[i - 1][char.to_sym] || 0
prefix_sums[i][char.to_sym] = running_total + 1
end
prefix_sums.each_with_index do |p, i|
puts "i: #{i}, p: #{p}"
end
end
Invalid result type, Integer expected, Hash foundstdout:
i: 0, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 1, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 2, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 3, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 4, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 5, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 6, p: {:A=>2, :C=>3, :G=>1, :T=>1}
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length, { A: 0, C: 0, G: 0, T: 0 })
# Iterating through each character, increment
# the counter for that character at that position.
string.split('').each_with_index do |char, i|
puts "char: #{char}"
running_total = prefix_sums[i - 1][char.to_sym] || 0
prefix_sums[i][char.to_sym] = running_total + 1
end
prefix_sums.each_with_index do |p, i|
puts "i: #{i}, p: #{p}"
end
end
Invalid result type, Integer expected, Hash foundstdout:
char: C char: A char: G char: C char: C char: T char: A i: 0, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 1, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 2, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 3, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 4, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 5, p: {:A=>2, :C=>3, :G=>1, :T=>1} i: 6, p: {:A=>2, :C=>3, :G=>1, :T=>1}
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterating through each character, increment
# the counter for that character at that position.
string.split('').each_with_index do |char, i|
puts "char: #{char}"
running_total = prefix_sums[i - 1][char.to_sym] || 0
prefix_sums[i][char.to_sym] = running_total + 1
end
prefix_sums.each_with_index do |p, i|
puts "i: #{i}, p: #{p}"
end
end
Invalid result type, Integer expected, Hash foundstdout:
char: C char: A char: G char: C char: C char: T char: A i: 0, p: {:A=>0, :C=>1, :G=>0, :T=>0} i: 1, p: {:A=>1, :C=>0, :G=>0, :T=>0} i: 2, p: {:A=>0, :C=>0, :G=>1, :T=>0} i: 3, p: {:A=>0, :C=>1, :G=>0, :T=>0} i: 4, p: {:A=>0, :C=>2, :G=>0, :T=>0} i: 5, p: {:A=>0, :C=>0, :G=>0, :T=>1} i: 6, p: {:A=>1, :C=>0, :G=>0, :T=>0}
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterating through each character, increment
# the counter for that character at that position.
string.split('').each_with_index do |char, i|
puts "char: #{char}"
running_total = prefix_sums[i - 1][char.to_sym]
prefix_sums[i][char.to_sym] = running_total + 1
end
prefix_sums.each_with_index do |p, i|
puts "i: #{i}, p: #{p}"
end
end
Invalid result type, Integer expected, Hash foundstdout:
char: C char: A char: G char: C char: C char: T char: A i: 0, p: {:A=>0, :C=>1, :G=>0, :T=>0} i: 1, p: {:A=>1, :C=>0, :G=>0, :T=>0} i: 2, p: {:A=>0, :C=>0, :G=>1, :T=>0} i: 3, p: {:A=>0, :C=>1, :G=>0, :T=>0} i: 4, p: {:A=>0, :C=>2, :G=>0, :T=>0} i: 5, p: {:A=>0, :C=>0, :G=>0, :T=>1} i: 6, p: {:A=>1, :C=>0, :G=>0, :T=>0}
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterating through each character, increment
# the counter for that character at that position.
string.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
prefix_sums[i][char.to_sym] += 1
end
prefix_sums.each_with_index do |p, i|
puts "i: #{i}, p: #{p}"
end
end
Invalid result type, Integer expected, Hash foundstdout:
i: 0, p: {:A=>0, :C=>1, :G=>0, :T=>0} i: 1, p: {:A=>1, :C=>1, :G=>0, :T=>0} i: 2, p: {:A=>1, :C=>1, :G=>1, :T=>0} i: 3, p: {:A=>1, :C=>2, :G=>1, :T=>0} i: 4, p: {:A=>1, :C=>3, :G=>1, :T=>0} i: 5, p: {:A=>1, :C=>3, :G=>1, :T=>1} i: 6, p: {:A=>2, :C=>3, :G=>1, :T=>1}
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
string.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether the
# character occured by looking at the total for
# that character at the beginning and end of the
# slice.
slice_start.each_with_index.map do |s, i|
e = slice_end[i]
prefix_sums[0].each do |char, v|
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
break s if (prefix_sums[s] - prefix_sums[e]) > 0
end
end
end
user.rb:37:in `block (2 levels) in solution': undefined method `-' for {:A=>1, :C=>1, :G=>1, :T=>0}:Hash (NoMethodError) from user.rb:33:in `each' from user.rb:33:in `block in solution' from user.rb:30:in `each' from user.rb:30:in `each_with_index' from user.rb:30:in `each' from user.rb:30:in `map' from user.rb:30:in `solution' from user.rb:163:in `<main>'
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
string.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether the
# character occured by looking at the total for
# that character at the beginning and end of the
# slice.
slice_start.each_with_index.map do |s, i|
e = slice_end[i]
prefix_sums[0].each do |char, v|
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
break s if (prefix_sums[s][char] - prefix_sums[e][char]) > 0
end
end
end
Invalid result type, Integer expected, Hash found
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
string.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether the
# character occured by looking at the total for
# that character at the beginning and end of the
# slice.
r = slice_start.each_with_index.map do |s, i|
e = slice_end[i]
prefix_sums[0].each do |char, v|
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
break s if (prefix_sums[s][char] - prefix_sums[e][char]) > 0
end
end
puts r
r
end
Invalid result type, Integer expected, Hash foundstdout:
{:A=>0, :C=>1, :G=>0, :T=>0} {:A=>0, :C=>1, :G=>0, :T=>0} {:A=>0, :C=>1, :G=>0, :T=>0}
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
string.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
chars.detect do |char|
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
(prefix_sums[s][char] - prefix_sums[e][char]) > 0
end
end
puts r
r
end
user.rb:38:in `[]': no implicit conversion from nil to integer (TypeError) from user.rb:38:in `block (2 levels) in solution' from user.rb:34:in `each' from user.rb:34:in `detect' from user.rb:34:in `block in solution' from user.rb:30:in `each' from user.rb:30:in `map' from user.rb:30:in `solution' from user.rb:167:in `<main>'
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
string.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
chars.detect do |char|
puts "char: #{char}"
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
(prefix_sums[s][char] - prefix_sums[e][char]) > 0
end
end
puts r
r
end
user.rb:39:in `[]': no implicit conversion from nil to integer (TypeError) from user.rb:39:in `block (2 levels) in solution' from user.rb:34:in `each' from user.rb:34:in `detect' from user.rb:34:in `block in solution' from user.rb:30:in `each' from user.rb:30:in `map' from user.rb:30:in `solution' from user.rb:168:in `<main>'stdout:
char: A char: C char: G char: T char: A char: C char: G char: T char: A char: C char: G char: T char: A
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
string.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
chars.detect do |char|
puts "char: #{char}, #{char.inspect}, #{char.class}"
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
(prefix_sums[s][char] - prefix_sums[e][char]) > 0
end
end
puts r
r
end
user.rb:39:in `[]': no implicit conversion from nil to integer (TypeError) from user.rb:39:in `block (2 levels) in solution' from user.rb:34:in `each' from user.rb:34:in `detect' from user.rb:34:in `block in solution' from user.rb:30:in `each' from user.rb:30:in `map' from user.rb:30:in `solution' from user.rb:168:in `<main>'stdout:
char: A, :A, Symbol char: C, :C, Symbol char: G, :G, Symbol char: T, :T, Symbol char: A, :A, Symbol char: C, :C, Symbol char: G, :G, Symbol char: T, :T, Symbol char: A, :A, Symbol char: C, :C, Symbol char: G, :G, Symbol char: T, :T, Symbol char: A, :A, Symbol
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
string.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
chars.detect do |char|
puts "char: #{char}, #{char.inspect}, #{char.class}"
puts "slice s: #{prefix_sums[s][char]}"
puts "slice e: #{prefix_sums[e][char]}"
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
(prefix_sums[s][char] - prefix_sums[e][char]) > 0
end
end
puts r
r
end
user.rb:36:in `[]': no implicit conversion from nil to integer (TypeError) from user.rb:36:in `block (2 levels) in solution' from user.rb:34:in `each' from user.rb:34:in `detect' from user.rb:34:in `block in solution' from user.rb:30:in `each' from user.rb:30:in `map' from user.rb:30:in `solution' from user.rb:170:in `<main>'stdout:
char: A, :A, Symbol slice s: 1 slice e: 1 char: C, :C, Symbol slice s: 1 slice e: 3 char: G, :G, Symbol slice s: 1 slice e: 1 char: T, :T, Symbol slice s: 0 slice e: 0 char: A, :A, Symbol slice s: 1 slice e: 1 char: C, :C, Symbol slice s: 3 slice e: 3 char: G, :G, Symbol slice s: 1 slice e: 1 char: T, :T, Symbol slice s: 1 slice e: 1 char: A, :A, Symbol slice s: 0 slice e: 2 char: C, :C, Symbol slice s: 1 slice e: 3 char: G, :G, Symbol slice s: 0 slice e: 1 char: T, :T, Symbol slice s: 0 slice e: 1 char: A, :A, Symbol
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
string.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
chars.detect do |char|
puts "char: #{char}, #{char.inspect}, #{char.class}"
puts "slice s: #{prefix_sums[s][char]}"
puts "slice e: #{prefix_sums[e][char]}"
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
(prefix_sums[e][char] - prefix_sums[s][char]) > 0
end
end
puts r
r
end
user.rb:36:in `[]': no implicit conversion from nil to integer (TypeError) from user.rb:36:in `block (2 levels) in solution' from user.rb:34:in `each' from user.rb:34:in `detect' from user.rb:34:in `block in solution' from user.rb:30:in `each' from user.rb:30:in `map' from user.rb:30:in `solution' from user.rb:170:in `<main>'stdout:
char: A, :A, Symbol slice s: 1 slice e: 1 char: C, :C, Symbol slice s: 1 slice e: 3 char: A, :A, Symbol slice s: 1 slice e: 1 char: C, :C, Symbol slice s: 3 slice e: 3 char: G, :G, Symbol slice s: 1 slice e: 1 char: T, :T, Symbol slice s: 1 slice e: 1 char: A, :A, Symbol slice s: 0 slice e: 2 char: A, :A, Symbol
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
string.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
chars.detect do |char|
puts "char: #{char}, #{char.inspect}, #{char.class}"
puts "slice s: #{prefix_sums[s][char]}"
puts "slice e: #{prefix_sums[e][char]}"
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
(prefix_sums[e][char] - prefix_sums[s][char]) > 0
end
end
puts r
r
end
user.rb:45:in `solution': undefined local variable or method `r' for main:Object (NameError) from user.rb:170:in `<main>'stdout:
char: A, :A, Symbol slice s: 1 slice e: 1 char: C, :C, Symbol slice s: 1 slice e: 3 char: A, :A, Symbol slice s: 1 slice e: 1 char: C, :C, Symbol slice s: 3 slice e: 3 char: G, :G, Symbol slice s: 1 slice e: 1 char: T, :T, Symbol slice s: 1 slice e: 1 char: A, :A, Symbol slice s: 0 slice e: 2
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
string.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
chars.detect do |char|
puts "char: #{char}, #{char.inspect}, #{char.class}"
puts "slice s: #{prefix_sums[s][char]}"
puts "slice e: #{prefix_sums[e][char]}"
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
(prefix_sums[e][char] - prefix_sums[s][char]) > 0
end
end
end
Invalid result type, Integer expected, Symbol foundstdout:
char: A, :A, Symbol slice s: 1 slice e: 1 char: C, :C, Symbol slice s: 1 slice e: 3 char: A, :A, Symbol slice s: 1 slice e: 1 char: C, :C, Symbol slice s: 3 slice e: 3 char: G, :G, Symbol slice s: 1 slice e: 1 char: T, :T, Symbol slice s: 1 slice e: 1 char: A, :A, Symbol slice s: 0 slice e: 2
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
string.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
lowest_char = chars.detect do |char|
puts "char: #{char}, #{char.inspect}, #{char.class}"
puts "slice s: #{prefix_sums[s][char]}"
puts "slice e: #{prefix_sums[e][char]}"
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
(prefix_sums[e][char] - prefix_sums[s][char]) > 0
end
chars.index(lowest_char) + 1
end
end
user.rb:44:in `block in solution': undefined method `+' for nil:NilClass (NoMethodError) from user.rb:30:in `each' from user.rb:30:in `map' from user.rb:30:in `solution' from user.rb:169:in `<main>'stdout:
char: A, :A, Symbol slice s: 1 slice e: 1 char: C, :C, Symbol slice s: 1 slice e: 3 char: A, :A, Symbol slice s: 1 slice e: 1 char: C, :C, Symbol slice s: 3 slice e: 3 char: G, :G, Symbol slice s: 1 slice e: 1 char: T, :T, Symbol slice s: 1 slice e: 1
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
string.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
lowest_char = chars.detect do |char|
puts "char: #{char}, #{char.inspect}, #{char.class}"
puts "slice s: #{prefix_sums[s][char]}"
puts "slice e: #{prefix_sums[e][char]}"
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
(prefix_sums[e][char] - prefix_sums[s][char]) > 0
end
chars.index(lowest_char)
end
end
Invalid result type, Integer expected, NilClass foundstdout:
char: A, :A, Symbol slice s: 1 slice e: 1 char: C, :C, Symbol slice s: 1 slice e: 3 char: A, :A, Symbol slice s: 1 slice e: 1 char: C, :C, Symbol slice s: 3 slice e: 3 char: G, :G, Symbol slice s: 1 slice e: 1 char: T, :T, Symbol slice s: 1 slice e: 1 char: A, :A, Symbol slice s: 0 slice e: 2
def solution(string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
string.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
lowest_char = chars.detect do |char|
puts "char: #{char}, #{char.inspect}, #{char.class}"
puts "slice s: #{prefix_sums[s]}"
puts "slice e: #{prefix_sums[e]}"
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
(prefix_sums[e][char] - prefix_sums[s][char]) > 0
end
puts "lowest_char: #{lowest_char}"
chars.index(lowest_char)
end
end
Invalid result type, Integer expected, NilClass foundstdout:
char: A, :A, Symbol slice s: {:A=>1, :C=>1, :G=>1, :T=>0} slice e: {:A=>1, :C=>3, :G=>1, :T=>0} char: C, :C, Symbol slice s: {:A=>1, :C=>1, :G=>1, :T=>0} slice e: {:A=>1, :C=>3, :G=>1, :T=>0} lowest_char: C char: A, :A, Symbol slice s: {:A=>1, :C=>3, :G=>1, :T=>1} slice e: {:A=>1, :C=>3, :G=>1, :T=>1} char: C, :C, Symbol slice s: {:A=>1, :C=>3, :G=>1, :T=>1} slice e: {:A=>1, :C=>3, :G=>1, :T=>1} char: G, :G, Symbol slice s: {:A=>1, :C=>3, :G=>1, :T=>1} slice e: {:A=>1, :C=>3, :G=>1, :T=>1} char: T, :T, Symbol slice s: {:A=>1, :C=>3, :G=>1, :T=>1} slice e: {:A=>1, :C=>3, :G=>1, :T=>1} lowest_char: char: A, :A, Symbol slice s: {:A=>0, :C=>1, :G=>0, :T=>0} slice e: {:A=>2, :C=>3, :G=>1, :T=>1} lowest_char: A
def solution(input_string, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(string.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_string.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
lowest_char = chars.detect do |char|
puts "char: #{char}, #{char.inspect}, #{char.class}"
puts "slice: #{s}..#{e} (...#{input_string[s..e]}...)"
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
(prefix_sums[e][char] - prefix_sums[s][char]) > 0
end
puts "lowest_char: #{lowest_char}"
chars.index(lowest_char)
end
end
user.rb:12:in `solution': undefined local variable or method `string' for main:Object (NameError) from user.rb:169:in `<main>'
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
lowest_char = chars.detect do |char|
puts "char: #{char}, #{char.inspect}, #{char.class}"
puts "slice: #{s}..#{e} (...#{input_str[s..e]}...)"
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
(prefix_sums[e][char] - prefix_sums[s][char]) > 0
end
puts "lowest_char: #{lowest_char}"
chars.index(lowest_char)
end
end
Invalid result type, Integer expected, NilClass foundstdout:
char: A, :A, Symbol slice: 2..4 (...GCC...) char: C, :C, Symbol slice: 2..4 (...GCC...) lowest_char: C char: A, :A, Symbol slice: 5..5 (...T...) char: C, :C, Symbol slice: 5..5 (...T...) char: G, :G, Symbol slice: 5..5 (...T...) char: T, :T, Symbol slice: 5..5 (...T...) lowest_char: char: A, :A, Symbol slice: 0..6 (...CAGCCTA...) lowest_char: A
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
lowest_char = chars.detect do |char|
puts "char: #{char}, #{char.inspect}, #{char.class}"
puts " slice: #{s}..#{e} (...#{input_str[s..e]}...)"
puts " start: #{prefix_sums[s][char]}"
puts " end: #{prefix_sums[e][char]}"
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
(prefix_sums[e][char] - prefix_sums[s][char]) > 0
end
puts "lowest_char: #{lowest_char}"
chars.index(lowest_char)
end
end
Invalid result type, Integer expected, NilClass foundstdout:
char: A, :A, Symbol slice: 2..4 (...GCC...) start: 1 end: 1 char: C, :C, Symbol slice: 2..4 (...GCC...) start: 1 end: 3 lowest_char: C char: A, :A, Symbol slice: 5..5 (...T...) start: 1 end: 1 char: C, :C, Symbol slice: 5..5 (...T...) start: 3 end: 3 char: G, :G, Symbol slice: 5..5 (...T...) start: 1 end: 1 char: T, :T, Symbol slice: 5..5 (...T...) start: 1 end: 1 lowest_char: char: A, :A, Symbol slice: 0..6 (...CAGCCTA...) start: 0 end: 2 lowest_char: A
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
lowest_char = chars.detect do |char|
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
(prefix_sums[e + 1][char] - prefix_sums[s][char]) > 0
end
puts "lowest_char: #{lowest_char}"
chars.index(lowest_char)
end
end
user.rb:38:in `block (2 levels) in solution': undefined method `[]' for nil:NilClass (NoMethodError) from user.rb:34:in `each' from user.rb:34:in `detect' from user.rb:34:in `block in solution' from user.rb:30:in `each' from user.rb:30:in `map' from user.rb:30:in `solution' from user.rb:167:in `<main>'stdout:
lowest_char: C lowest_char: A
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
lowest_char = chars.detect do |char|
return char if s == e && prefix_sums[s][char] == prefix_sums[e][char]
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
(prefix_sums[e][char] - prefix_sums[s][char]) > 0
end
chars.index(lowest_char)
end
end
Invalid result type, Integer expected, String found
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
lowest_char = chars.detect do |char|
return char if s == e && prefix_sums[s][char] == prefix_sums[e][char]
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
(prefix_sums[e][char] - prefix_sums[s][char]) > 0
end
low = chars.index(lowest_char)
puts low
low
end
end
Invalid result type, Integer expected, String foundstdout:
1
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
lowest_char = chars.detect do |char|
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
occurences = prefix_sums[e][char] - prefix_sums[s][char]
if s == e
occurences >= 0
else
occurences > 0
end
end
low = chars.index(lowest_char)
puts low
low
end
end
1 0 0
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
lowest_char = chars.detect do |char|
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
occurences = prefix_sums[e][char] - prefix_sums[s][char]
if s == e
occurences >= 0
else
occurences > 0
end
end
low = chars.index(lowest_char)
puts low
low + 1
end
end
1 0 0
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
lowest_char = chars.detect do |char|
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
occurences = prefix_sums[e][char] - prefix_sums[s][char]
if s == e
occurences >= 0
else
occurences > 0
end
end
low = chars.index(lowest_char)
low + 1
end
end
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
lowest_char = chars.detect do |char|
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
occurences = prefix_sums[e][char] - prefix_sums[s][char]
puts prefix_sums[s][char]
puts prefix_sums[e][char]
if s == e
occurences >= 0
else
occurences > 0
end
end
low = chars.index(lowest_char)
low + 1
end
end
1 1 1 3 1 1 0 2
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
lowest_char = chars.detect do |char|
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
occurences = prefix_sums[e][char] - prefix_sums[s][char]
puts prefix_sums[s]
puts prefix_sums[e]
if s == e
occurences >= 0
else
occurences > 0
end
end
low = chars.index(lowest_char)
low + 1
end
end
{:A=>1, :C=>1, :G=>1, :T=>0} {:A=>1, :C=>3, :G=>1, :T=>0} {:A=>1, :C=>1, :G=>1, :T=>0} {:A=>1, :C=>3, :G=>1, :T=>0} {:A=>1, :C=>3, :G=>1, :T=>1} {:A=>1, :C=>3, :G=>1, :T=>1} {:A=>0, :C=>1, :G=>0, :T=>0} {:A=>2, :C=>3, :G=>1, :T=>1}
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
puts prefix_sums[s]
puts prefix_sums[e]
lowest_char = chars.detect do |char|
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
occurences = prefix_sums[e][char] - prefix_sums[s][char]
if s == e
occurences >= 0
else
occurences > 0
end
end
low = chars.index(lowest_char)
low + 1
end
end
{:A=>1, :C=>1, :G=>1, :T=>0} {:A=>1, :C=>3, :G=>1, :T=>0} {:A=>1, :C=>3, :G=>1, :T=>1} {:A=>1, :C=>3, :G=>1, :T=>1} {:A=>0, :C=>1, :G=>0, :T=>0} {:A=>2, :C=>3, :G=>1, :T=>1}
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
puts prefix_sums[s]
puts prefix_sums[e]
lowest_char = chars.detect do |char|
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
occurences = prefix_sums[e][char] - prefix_sums[s][char]
if s == e
occurences >= 0
else
occurences > 0
end
end
low = chars.index(lowest_char)
low + 1
end
end
user.rb:33:in `[]': no implicit conversion from nil to integer (TypeError) from user.rb:33:in `block in solution' from user.rb:30:in `each' from user.rb:30:in `map' from user.rb:30:in `solution' from user.rb:173:in `<main>'stdout:
{:A=>1, :C=>1, :G=>1, :T=>0} {:A=>1, :C=>3, :G=>1, :T=>0} {:A=>1, :C=>3, :G=>1, :T=>1} {:A=>1, :C=>3, :G=>1, :T=>1} {:A=>0, :C=>1, :G=>0, :T=>0} {:A=>2, :C=>3, :G=>1, :T=>1}
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
puts prefix_sums[s]
puts prefix_sums[e]
lowest_char = chars.detect do |char|
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
occurences = prefix_sums[e][char] - prefix_sums[s][char]
if s == e
occurences >= 0
else
occurences > 0
end
end
low = chars.index(lowest_char)
low + 1
end
end
{:A=>1, :C=>1, :G=>1, :T=>0} {:A=>1, :C=>3, :G=>1, :T=>0} {:A=>1, :C=>3, :G=>1, :T=>1} {:A=>1, :C=>3, :G=>1, :T=>1} {:A=>0, :C=>1, :G=>0, :T=>0} {:A=>2, :C=>3, :G=>1, :T=>1}
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
puts prefix_sums[i]
end
# For each slice, we can now look up whether each
# character occured and find the lowest.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
# puts prefix_sums[s]
# puts prefix_sums[e]
lowest_char = chars.detect do |char|
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
occurences = prefix_sums[e][char] - prefix_sums[s][char]
if s == e
occurences >= 0
else
occurences > 0
end
end
low = chars.index(lowest_char)
low + 1
end
end
{:A=>0, :C=>1, :G=>0, :T=>0} {:A=>1, :C=>1, :G=>0, :T=>0} {:A=>1, :C=>1, :G=>1, :T=>0} {:A=>1, :C=>2, :G=>1, :T=>0} {:A=>1, :C=>3, :G=>1, :T=>0} {:A=>1, :C=>3, :G=>1, :T=>1} {:A=>2, :C=>3, :G=>1, :T=>1}
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# We now have an array of hashes that looks
# something like this:
#
# {:A=>0, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>1, :T=>0}
# {:A=>1, :C=>2, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>1}
# {:A=>2, :C=>3, :G=>1, :T=>1}
# For each slice, we can now look up whether each
# character occured.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
if s == e
return chars.index(prefix_sums[s][char]) + 1
end
lowest_char = chars.detect do |char|
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
occurences = prefix_sums[e][char] - prefix_sums[s][char]
occurences > 0
end
chars.index(lowest_char) + 1
end
end
user.rb:46:in `block in solution': undefined local variable or method `char' for main:Object (NameError) from user.rb:41:in `each' from user.rb:41:in `map' from user.rb:41:in `solution' from user.rb:182:in `<main>'
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# We now have an array of hashes that looks
# something like this:
#
# {:A=>0, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>1, :T=>0}
# {:A=>1, :C=>2, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>1}
# {:A=>2, :C=>3, :G=>1, :T=>1}
# For each slice, we can now look up whether each
# character occured.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
if s == e
return chars.index(input_str.to_sym) + 1
end
lowest_char = chars.detect do |char|
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
occurences = prefix_sums[e][char] - prefix_sums[s][char]
occurences > 0
end
chars.index(lowest_char) + 1
end
end
user.rb:46:in `block in solution': undefined method `+' for nil:NilClass (NoMethodError) from user.rb:41:in `each' from user.rb:41:in `map' from user.rb:41:in `solution' from user.rb:182:in `<main>'
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# We now have an array of hashes that looks
# something like this:
#
# {:A=>0, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>1, :T=>0}
# {:A=>1, :C=>2, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>1}
# {:A=>2, :C=>3, :G=>1, :T=>1}
# For each slice, we can now look up whether each
# character occured.
chars = prefix_sums[0].keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
if s == e
return chars.index(input_str[s].to_sym) + 1
end
lowest_char = chars.detect do |char|
# Subtracting the number of occurences at the
# end of the range from the same number at the
# start tells us if the character occured.
occurences = prefix_sums[e][char] - prefix_sums[s][char]
occurences > 0
end
chars.index(lowest_char) + 1
end
end
user.rb:162:in `_codility_save_result': undefined method `length' for 4:Fixnum (NoMethodError) from user.rb:183:in `<main>'
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
char_values = { A: 1, C: 2, G: 3, T: 4 }
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# We now have an array of hashes that looks
# something like this:
#
# {:A=>0, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>1, :T=>0}
# {:A=>1, :C=>2, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>1}
# {:A=>2, :C=>3, :G=>1, :T=>1}
# For each slice, we can now look up whether each
# character occured.
chars = char_values.keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
if s == e
return char_values(input_str[s].to_sym)
end
lowest_char = chars.detect do |char|
occurences = prefix_sums[e][char] - prefix_sums[s][char]
occurences > 0
end
char_values(lowest_char)
end
end
user.rb:55:in `block in solution': undefined method `char_values' for main:Object (NoMethodError) from user.rb:42:in `each' from user.rb:42:in `map' from user.rb:42:in `solution' from user.rb:180:in `<main>'
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
char_values = { A: 1, C: 2, G: 3, T: 4 }
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# We now have an array of hashes that looks
# something like this:
#
# {:A=>0, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>1, :T=>0}
# {:A=>1, :C=>2, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>1}
# {:A=>2, :C=>3, :G=>1, :T=>1}
# For each slice, we can now look up whether each
# character occured.
chars = char_values.keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
if s == e
return char_values(input_str[s].to_sym)
end
lowest_char = chars.detect do |char|
occurences = prefix_sums[e][char] - prefix_sums[s][char]
occurences > 0
end
char_values[lowest_char]
end
end
user.rb:47:in `block in solution': undefined method `char_values' for main:Object (NoMethodError) from user.rb:42:in `each' from user.rb:42:in `map' from user.rb:42:in `solution' from user.rb:180:in `<main>'
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
char_values = { A: 1, C: 2, G: 3, T: 4 }
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# We now have an array of hashes that looks
# something like this:
#
# {:A=>0, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>1, :T=>0}
# {:A=>1, :C=>2, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>1}
# {:A=>2, :C=>3, :G=>1, :T=>1}
# For each slice, we can now look up whether each
# character occured.
chars = char_values.keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
if s == e
return char_values[input_str[s].to_sym]
end
lowest_char = chars.detect do |char|
occurences = prefix_sums[e][char] - prefix_sums[s][char]
occurences > 0
end
char_values[lowest_char]
end
end
user.rb:160:in `_codility_save_result': undefined method `length' for 4:Fixnum (NoMethodError) from user.rb:181:in `<main>'
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice in
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
char_values = { A: 1, C: 2, G: 3, T: 4 }
# Initialise an array to hold the number of
# times each character occurs at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# We now have an array of hashes that looks
# something like this:
#
# {:A=>0, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>1, :T=>0}
# {:A=>1, :C=>2, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>1}
# {:A=>2, :C=>3, :G=>1, :T=>1}
# For each slice, we can now look up whether each
# character occured.
chars = char_values.keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
if s == e
next char_values[input_str[s].to_sym]
end
lowest_char = chars.detect do |char|
occurences = prefix_sums[e][char] - prefix_sums[s][char]
occurences > 0
end
char_values[lowest_char]
end
end
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
char_values = { A: 1, C: 2, G: 3, T: 4 }
# Initialise an array to hold the number of
# times each character has occured at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# We now have an array of hashes that looks
# something like this:
#
# {:A=>0, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>1, :T=>0}
# {:A=>1, :C=>2, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>1}
# {:A=>2, :C=>3, :G=>1, :T=>1}
# For each slice, we can now look up whether each
# character occured in the character range.
chars = char_values.keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
if s == e
# If the start and end of the slice are the same,
# we can just return the value at that position.
next char_values[input_str[s].to_sym]
end
lowest_char = chars.detect do |char|
occurences = prefix_sums[e][char] - prefix_sums[s][char]
occurences > 0
end
char_values[lowest_char]
end
end
def solution(input_str, slice_start, slice_end)
# The challenge here is to find a way to return
# the minimum value present in an array slice
# without having to iterate through the entire
# slice – i.e. in constant O(1) time.
m = slice_start.length - 1
char_values = { A: 1, C: 2, G: 3, T: 4 }
# Initialise an array to hold the number of
# times each character has occured at each point
# in the original string. It's just a running
# total for each character.
prefix_sums = Array.new(input_str.length) { { A: 0, C: 0, G: 0, T: 0 } }
# Iterate through the original string,
# incrementing the counters.
input_str.split('').each_with_index do |char, i|
prefix_sums[i].each do |k, v|
# Set each value at this index to whatever
# it was last index, so the counters grow.
prefix_sums[i][k] = prefix_sums[i - 1][k]
end
# Increment the counter for this character.
prefix_sums[i][char.to_sym] += 1
end
# We now have an array of hashes that looks
# something like this:
#
# {:A=>0, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>0, :T=>0}
# {:A=>1, :C=>1, :G=>1, :T=>0}
# {:A=>1, :C=>2, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>0}
# {:A=>1, :C=>3, :G=>1, :T=>1}
# {:A=>2, :C=>3, :G=>1, :T=>1}
# For each slice, we can now look up whether each
# character occured in the character range.
chars = char_values.keys
(0..m).map do |i|
s = slice_start[i]
e = slice_end[i]
if s == e
# If the start and end of the slice are the same,
# we can just return the value at that position.
next char_values[input_str[s].to_sym]
end
lowest_char = chars.detect do |char|
occurences = prefix_sums[e][char] - prefix_sums[s][char]
occurences > 0
end
char_values[lowest_char]
end
end
The following issues have been detected: wrong answers.