Advertisement
Narzew

Kodowanie z dowolnym przesunieciem

Jan 17th, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.64 KB | None | 0 0
  1. require 'jcode' # Usun jesli uzywasz Ruby 1.9
  2. def zadany_element(x,y)
  3.         count = 0
  4.                 x.each{|z|
  5.                         return count if y == z
  6.                         count += 1
  7.                 }
  8.                 return 0 # 0 zamiast false, by zapobiec bledom
  9. end
  10. $alfabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
  11. def przesun(x,y)
  12.                 result = ''
  13.                 x = x.upcase
  14.                 x.each_char{|z|
  15.                         result << $alfabet[(zadany_element($alfabet,z)+y)%26]
  16.                 }
  17.                 return result
  18. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement