Advertisement
Guest User

ruby paperfolding algorithm

a guest
Sep 5th, 2015
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.59 KB | None | 0 0
  1. $iterations=2
  2. aArr=[1,1,0]
  3.  
  4. def fold (arr)
  5.  fArr=arr
  6.  hArr=[]
  7.  j=0
  8.   while j<(fArr.length+1)/2 do
  9.    gArr=[]
  10.    gArr.push(fArr.shift,fArr.shift)
  11.  
  12.    switch gArr
  13.     when [1,nil]
  14.       hArr.push(1,1,0)
  15.     when [1,1]
  16.       hArr.push(1,1,0,1)
  17.     when [1,0]
  18.       hArr.push(1,1,0,0)
  19.     when [0,1]
  20.       hArr.push(1,0,0,1)
  21.     when [0,0]
  22.       hArr.push(1,0,0,0)
  23.     when [0,nil]
  24.       hArr.push(1,0,0)
  25.     else
  26.       hArr.push("ERROR")
  27.    end
  28.    j+=1
  29.   end
  30.  
  31.  return hArr.flatten
  32. end
  33.  
  34. i=1
  35. while i<$iterations do
  36.  aArr=fold(aArr)
  37.  i+=1
  38. end
  39.  
  40. aArr.each {|x| print x}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement