2016-09-01 3 views
-2

私は、学習サイトのための関数を書くことに取り組んでいます code wars私の主な問題は、私が与えられた数をループするときに私が望む5つの整数を割り当てることができないということです。私はそれはにあまりにも多くの桁数を割り当てに掛けているように私はそう目的の変数を配列に割り当てるのに問題があります。

digits = 33333339999990000000 
ray_a, ray_b = [] 
ray_a = digits[start_count, end_count] 
ray_b = digits[start_count + 1, end_count+1] 

ようですが、何らかの理由でオーバーループされるようになっ

arr = [1, 2, 3, 4, 5, 6] 
arr[1..4] #=> [2, 3, 4, 5] 

ような配列を割り当てることができるruby-docから理解します配列

次のように実際の問題への指示だけでなく、私の試みである...




次の6桁の数字では、283910

91は2桁の最大のシーケンスです。

与えられた数の中で最大の5桁の数値を返すように、ソリューションを完成させます。数字は、数字のみの文字列として渡されます。 5桁の整数を返します。渡される数字は1000桁にもなります。

私はあなたがray_aray_bを設定する方法が間違っている333333399999.9億

def solution(digits) 
    ray_a = [] # ray_a & ray_b are to be compaired 
    ray_b = [] 

    total_counter = 1 
    start_count = 0 
    end_count = 4 
    answer = 0 

    digits = digits.to_s.split('') #turns digits into an array of integers 
    digits.map!{|x| x.to_i } 


    while total_counter<=digits.length do #carries out as many loops as the variable "digits" has digits 

     ray_a = digits[start_count, end_count] #sets ray_a & ray_b to the numbers to be compaired 
     ray_b = digits[start_count + 1, end_count+1] 

    # the above block is where my problem is. At any time each array should have a maximum of 5 digits, 
    # instead using the example variable I am passing in as many as 13 digits are assigned 

    # I found the fatal flaw in my code with the debugging block 
    # puts "ray_a" 
    # print ray_a 


    ray_a.map{|x| x.to_s} #this block turns ray_a & ray_b into indivudal integers that are not arrays. 
    ray_a = ray_a.join('') 
    ray_b.map{|x| x.to_s} 
    ray_b = ray_b.join('') 
    ray_a = ray_a.to_i 
    ray_b = ray_b.to_i 



    if ray_a > ray_b # this block determines the answer 
      if ray_a > answer 
      answer = ray_a 
    end 
     end 



    start_count +=1 #incriments all the counters 
    end_count +=1 
    total_counter+=1 
    ray_a = []# resets both arrays to be redefined in the next loop 
    ray_b = [] 
end 
return answer 

end 


solution(33333339999990000000) 
+2

この膨大なコードを読んだ後でも、あなたの問題が何であるか分かりません。問題の詳細** **を最小限に抑えることができますか? – tadman

+0

http://stackoverflow.com/help/mcve – orde

+0

私は以前この問題を抱えていたと思います。かなり確かに彼は5桁のシーケンスを見つけようとしていて、数字の文字列の中で最大の5桁の数字を作り出しています。 – davidhu2000

答えて

2
ray_a = digits[start_count, end_count] 
ray_b = digits[start_count + 1, end_count+1] 

に渡された私のテストのために私の注釈付きのコード...。あなたがやっていることは、あなたがend_countをインクリメントしているので、あなたは常に、あなたの配列の長さを変更する理由です

ray_a = digits[starting index, length] 

です。あなたがする必要がどのような

ray_a = digits[start_count..end_count] 

または

ray_a = digits[start_count, 5] 

であり、あなたは、変数を削除することができます。

関連する問題