2017-03-13 3 views
-1
def uncompress(to_uncompress) 
    dictionary = (0..255).to_a.map { |element| element.chr } 
    output = Array.new 
    current = to_uncompress.shift 
    output << current 
    to_uncompress.each_char do |index| 
    previous = current 
    current = index 
    if current < dictionary.length 
     s = dictionary[current] 
     output << s 
     dictionary << dictionary[previous] + s[0] 
    else 
     s = dictionary[previous] 
     output << s 
     dictionary << s 
    end 
    end 
    output.shift 
    output 
end 

[97、98、257、256]のためには、圧縮されたアレイとするとき、それは未定義のメソッド`シフトをスロー解凍メソッドを呼び出しています' で以下のための「[97、98、257、256]」:文字列(NoMethodError)未定義のメソッド `シフト ' "[97、98、257、256]"

+2

'[97,98,257,256]'は配列です。はい。しかし、あなたは[97、98、257、256] "を持っています。これはまったく別の問題です。 –

+0

「uncompress」と呼ばれる行には、実際に関連するコードの行だけが残っています。 –

答えて

2

あなたはそれがJSONデータ可能性があり、あるいは少なくともそれはJSONパーサーを扱うことができるものだということのような文字列リテラル与えられている場合:

to_uncompress = JSON.parse(to_uncompress) 

これは適切な配列です。 JSONモジュールを読み込むには、ファイルの先頭にrequire 'json'が必要な場合があります。

+0

@Adityaそれが機能していることを確認できたら、それを受け入れたものとしてマークして、他の人がそれが良い解決であることを知るようにします。共有に役立ちます。 – tadman

関連する問題