0

So this is hurting my head, I am not very good with programming obviously. I have,

LetterArray = [a,b,c,d,e,f,g]
NumArray = [1,2,3,4,5,6,7,8,9,10]
ListOfLetters = []

and I want to take an element from NumArray and, starting on LetterArray[0], go up var x amount of times in LetterArray, and add that element (say var y to the array. Then starting on y go up the next number in NumArray, and so on. Then print the ListOfLetters to console.

My goal is for the output to be like this: [a, c, f, c, a, f, e, e, f, a].

I am drawing a blank on how to go about this in code.

5
  • 1
    It's not really clear what you want, could you explain it better? Commented Jun 6, 2012 at 15:20
  • Looks like a job/ interview quiz. Interesting question though. Please post all details. Commented Jun 6, 2012 at 15:41
  • You probably want to do something with Array#slice. Commented Jun 6, 2012 at 16:00
  • Nah i just made it up while i was playing guitar...decided i wanted to use the fib sequence to generate a sequence of notes in E mixolydian... Commented Jun 6, 2012 at 16:02
  • For one, that isn't an array of letters, it's an array of references that are the variables a through g. You probably want strings and thus need to quote them (e.g. "a"). Commented Jun 6, 2012 at 16:35

3 Answers 3

2

Something like this (if I get your requirements right of course)?

letter_array = %w[a b c d e f g]
number_array = [1,2,3,4,5,6,7,8,9,10]
list_of_letters = []

number_array.inject(0) do |offset, delta| 
  list_of_letters << letter_array[offset] 
  (offset + delta) % letter_array.size
end

p list_of_letters #=> ["a", "b", "d", "g", "d", "b", "a", "a", "b", "d"]
Sign up to request clarification or add additional context in comments.

Comments

0

Either I don't understand your problem description, or the example output you showed is wrong from a certain point onwards. Anyway, maybe this gets you started:

letter_array = [*?a..?g]
number_array = *1..10
list_of_letters = []

number_array.inject(0) do |s, n| 
  i = s + n
  list_of_letters << letter_array[i % letter_array.size - 1] 
  i 
end

This produces the output ["a", "c", "f", "c", "a", "g", "g", "a", "c", "f"].

Alternatively you can also first create the indices and then use them (this doesn't require a pre-initialized list_of_letters):

indices = number_array.inject([]) { |a, n| a << (a.last || 0) + n ; a})
list_of_letters = indices.map { |i| letter_array[i%letter_array.size-1] }

2 Comments

I think my example output is wrong. But letter_array = ['E', 'F#', 'G#','A', 'B', 'C#', 'D'] number_array = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597] list_of_letters = [] number_array.inject(0) do |s, n| i = s + n list_of_letters << letter_array[i % letter_array.size - 1] i end p list_of_letters is what i was going for...though, the output gives me ["E", "F#", "A", "D", "B", "C#", "B", "B", "A", "G#", "E", "B", "D", "C#", "D", "D", "E"]
but when I hand count it, I got E, F#, G#, B, E, B, B....etc. I want to start on E, go up 1 to F#, then go up 1 to G#, then up 2, to B....i hope this makes this more clear?
0
ar = ('a'..'g').to_a.cycle #keeps on cycling 
res = []
p 10.times.map do |n|
  n.times{ar.next} #cycle one time too short (the first time n is 0)
  res << ar.next #cycle once more and store
end
p res #=>["a", "c", "f", "c", "a", "g", "g", "a", "c", "f"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.