array checksum help please

Back to General discussions forum

Micky Scandal     2016-11-14 03:07:13

Alright this is the first time I've had to do this, but I could really use some help on the array checksum problem. I'm working in ruby, but what I need is more of a pseudo-code answer. I just cant figure out how to write this out. This is what my code looks like right now. I'm just trying to focus on getting the example to run before I move on to the test input so that's why there's only one array in the input. basically what's going on is if I use the test data, everything runs together. basically I'm getting one super long checksum (thats also above the limit) for all the array's in one. now here's the thing. if at all possible I really don't want someone to just post up an answer, because I want to be able to figure it out, but if someone could give me a nudge in the right direction, I would be really grateful!

inp = [3,1,4,1,5,9] seed = 113 limit = 10000007 result = 0 ans = []

inp.each do |val| puts val result = (result + val) * seed puts result result % limit if result >= limit puts result ans << result puts result end

Quandray     2016-11-14 09:31:44
User avatar

Hi Micky,

Getting your code to work with the example data before moving on to the test data is a good way to do things.

Sorry, I can't follow the code you posted because it's all on one line. Please can you either post it again, using "code blocks" so it stays formatted, "Help on Formatting" will tell you how to do this, or, submit your code on the problem page and I'll be able to see your code as an unsuccessful solution.

Micky Scandal     2016-11-15 01:14:32

oh, ok sorry about the formatting, I actually have another bit of code to post here since I'm still having some trouble. I've got it running and it will successfully create a checksum for each individual array, and then it creates one final checksum out of all the other ones, but when I run the code, It keeps saying my answer is wrong even though it's right when I test it against the example data. maybe someone can tell me where I'm going wrong. and hopefully the code comes out right this time...

class Checksum

  def self.create_checksum(input)
    seed = 113
    limit = 10000007
    result = 0
    input.each do |val|
      result = (result + val) * seed
      result = result % limit if result >= limit
    end
  return result
  end
end

chksm = []
# parse the file to create an array of arrays.
input = Array.new(File.read("input.txt").split(" "))
input =input.map {|x| x.split(//)}.map {|e| e.map {|s| s.to_i}}

# gets the checksum of each individual array
input.each {|ary| chksm << Checksum.create_checksum(ary)}
# gets the final checksum from each individual checksum
puts Checksum.create_checksum(chksm)
Quandray     2016-11-15 07:39:44
User avatar

Sorry, but I don't understand what you are trying to do. There is only one array in the input.

Micky Scandal     2016-11-15 20:03:12

Oh, I thought I already posted this. I figured out what I was doing wrong. I misread the directions. I thought I was supposed to convert each value into an array and calculate a checksum on each value then create a final checksum on the checksums I already got. basically i split all the values into a 2D array and got checksums on each individual value then went back and created a checksum of the checksums. I just did way more work than i should have lol. I guess it was a good learning experience though. definitely had to think a little harder to do it the way i did origionally lol. but thank you very much for all the help!

Please login and solve 5 problems to be able to post at forum