Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange output in a script converted from Python to Ruby, why?
    primarykey
    data
    text
    <p>have tried to convert this <a href="https://gist.github.com/josiahcarlson/5526230" rel="nofollow">gist</a> in ruby, this is my code:</p> <pre><code>$char_map = ('!.' + '0123456789' + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz').scan(/./) $int_map = Hash.new $char_map.each_with_index {|v,k| $int_map[v] = k } $cutoff = ($char_map.count) - 11 # Converts an integer to its text-encoded form. def to_chars(value) if value &lt; $cutoff return $char_map[value] end value -= $cutoff out = [] while value != 0 value, rem = value.divmod($char_map.count) out.push($char_map[rem]) end # handle when value == cutoff if !out out.push($char_map[value]) end out.push($char_map[$cutoff + out.count - 1]) out.reverse! return out.join('') end # Converts characters from the provided string back into their integer # representation, and returns both the desired integer as well as the number # of bytes consumed from the character string (this function can accept a # string that is the result of a concatenation of results from to_chars() ). def to_val(chars) chars = chars.scan(/./) first = $int_map[chars[0]] if first &lt; $cutoff return first, 1 end first -= $cutoff - 1 dec = [] for ch in chars[1..1+first] do dec.push($int_map[ch]) end value = dec.pop() + $cutoff m = $char_map.count while dec != [] value += m * dec.pop() m *= $char_map.count end return value, first + 1 end # Converts a sequence of integers into a string that represents those # integers. def from_sequence(lst) lst.map! {|int| to_chars(int)} return lst.join('') end # Converts a string that rappresents a sequence of integers back into a # a list of integers def from_string(str) out = [] i = 0 while i &lt; str.length this, used = to_val(str[i, str.length - i]) out.push(this) i += used end return out end p to_chars(123456789) p to_val(to_chars(123456789)) p from_string(from_sequence([123456789,4688])) </code></pre> <p>(Sorry for global vars ect... is only for testing, when works fit all in own class)</p> <p>The error is in last line, instead of print back the <code>[123456789, 4688]</code> array print <code>[7901231212, 4688]</code></p> <p>Why? where is the error?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload