1. Ruby / Говнокод #13946

    −126

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    #!/usr/bin/ruby
    # encoding=utf8
    size=2
    start=0
    c=['Н', 'Н' ,'Н' ,'Н' ,'Н','Н','Н','Н','Н','Н','Н','Н','Н','Н']
    code_dictionary=Array.new()
    begin_size_code_dictionary=Array.new()
    end_string=String.new()
    code_dictionary=c.uniq.to_a
    decode_dictionary=c.uniq.to_a
    for i in 0...code_dictionary.size
    	begin_size_code_dictionary[i]=i
    end
    c.size.times{
    if !code_dictionary.include?(c[start,size].to_s)#Comparation
    	code_dictionary.push(c[start,size].to_s.chomp)
    	start=start+(code_dictionary.last.size)/2-1
    	size=2
    else 
    	size+=1;
    end
    }
    end_code=Array.new(code_dictionary.size)
    for i in begin_size_code_dictionary.size...code_dictionary.size
    	end_code[i]=code_dictionary.index(code_dictionary[i].chop.chop)+1
    	end_string+=code_dictionary[i].chop.chop
    end
    if (c.size>(end_string.size)/2)
    	end_code.push(code_dictionary.index(c.last)+1)
    end
    end_code=end_code.compact
    puts code_dictionary
    puts "Input: "+c.join
    puts "Output: "+end_code.join
    puts "Compression coef= "+((c.size-end_code.size)*100/c.size).to_s+"%"
    end_decode = Array.new(end_code.size)
    def ololo(end_code,decode_dictionary,end_decode)
    	count=true
    	end_code.each{|elem| 
    	if elem <= decode_dictionary.size && count == true  
    		end_decode[end_code.index(elem)]=decode_dictionary[elem-1]
    	else
    		count = false
    	end
    }
    end
    puts decode_dictionary.size
    def ololo2(end_code,decode_dictionary,end_decode)
    	if decode_dictionary.size == 1 
    		decode_dictionary[1]=Array.new
    		decode_dictionary[1].push(decode_dictionary[0])
    		decode_dictionary[1].push(decode_dictionary[0])
    		decode_dictionary[1]=decode_dictionary[1].join
    	end	
    	temp_array=end_decode.compact.to_s
    	start =0
    	size = 2
    	temp_array.size.times{
    	if !decode_dictionary.include?(temp_array[start,size*2])
    		decode_dictionary.push(temp_array[start,size*2])
    		start=start+decode_dictionary.last.size-2
    		size=2
    	else
    		size+=1
    	end }
    end
    count=true
    ololo(end_code,decode_dictionary,end_decode)
    ololo2(end_code,decode_dictionary,end_decode)
    ololo(end_code,decode_dictionary,end_decode)
    c4=end_decode.size
    while end_decode.compact.size < end_decode.size && c4 > 0
    ololo2(end_code,decode_dictionary,end_decode)
    ololo(end_code,decode_dictionary,end_decode)
    c4-=1
    end
    for i in 0...end_decode.size
    	if end_decode[i] == nil
    		end_decode[i]=decode_dictionary[end_code[i]-1]
    	end
    end
    puts "Decoded: "+end_decode.to_s

    Кривой LZW

    Запостил: Abbath, 15 Октября 2013

    Комментарии (1) RSS

    • Пингвин и рубин - картина маслом

      begin_size_code_dictionary - I _think_it_is_pretty_darn_short_name

      code_dictionary=c.uniq.to_a
      decode_dictionary=c.uniq.to_a

      я два раза не повторяю, два раза не повторяю
      Ответить

    Добавить комментарий