<commit_before>module VigenereCipher where

import Data.Char (ord, chr)
import Prelude

-- 1. Pick a keyword

-- 2. Match chars of message against 
--    chars of the keyword

-- 3. Index of keyword char matched against
--    the message char is the amount by which
--    to shift the message char

rotate :: Char 
       -> (String, Int, [Int], [Int], [Int], String) 
       -> (String, Int, [Int], [Int], [Int], String)
rotate c (keyword, index, modAcc, firstCodedOrds, finalCodedOrds, acc) = 
  (keyword, newIndex, shiftedOrd : modAcc, firstCodedOrd : firstCodedOrds, finalCodedOrd : finalCodedOrds, encodedChar : acc)
  where 
    newIndex = index + 1
    shiftedOrd = fromIntegral $ mod (ord $ keyword !! (mod index (length keyword))) 26
    --     shiftedOrd = ord $ keyword !! (mod index (length keyword))
    wrapOrd n = n - (ord 'z' - ord 'a') - 1
    charMod n
      | n > ord 'z' = wrapOrd n
      | otherwise   = n
    firstCodedOrd = (ord c) + shiftedOrd
    finalCodedOrd = charMod firstCodedOrd        
    encodedChar = 
      if ord c <= (ord 'z') || ord c >= (ord 'a')  
      then chr finalCodedOrd -- chr . finalCodedOrd -- charMod firstCodedOrd-- $ (ord c) + shiftedOrd
      else c -- don't encode if nonalpha

keyword = "brodybtest"
message = "vulka fenryka to tizca"

(_, ix, mods, firstCodedOrds, finalCodedOrds, codeMessage) = 
  foldr rotate (keyword, 0, [], [], [], "") message

shiftsInRange :: [Int] -> Bool
shiftsInRange = all (\x -> x <= 25 && x >= 0)

rangeAlpha :: [Int] -> Bool
rangeAlpha = all (\x -> x == 32 || (x <= (ord 'z') && x >= (ord 'a')))<commit_msg>Add notion of encode and decode<commit_after>module VigenereCipher where

import Data.Char (ord, chr)
import Prelude

-- 1. Pick a keyword

-- 2. Match chars of message against 
--    chars of the keyword

-- 3. Index of keyword char matched against
--    the message char is the amount by which
--    to shift the message char

data Direction = Backward | Forward

encode :: String
       -> String
       -> (Direction, String, Int, [Int], [Int], [Int], String)
encode keyword message = 
  foldr rotate (Forward, keyword, 0, [], [], [], "") message  

decode :: String
       -> String
       -> (Direction, String, Int, [Int], [Int], [Int], String)
decode keyword message = 
  foldr rotate (Backward, keyword, 0, [], [], [], "") message  
     
rotate :: Char 
       -> (Direction, String, Int, [Int], [Int], [Int], String) 
       -> (Direction, String, Int, [Int], [Int], [Int], String)
rotate c (direction, keyword, index, modAcc, firstCodedOrds, finalCodedOrds, acc) = 
  (direction, keyword, newIndex, shiftedOrd : modAcc, firstCodedOrd(direction) : firstCodedOrds, finalCodedOrd : finalCodedOrds, encodedChar : acc)
  where 
    newIndex = index + 1
    shiftedOrd = fromIntegral $ mod (ord $ keyword !! (mod index (length keyword))) 26
    wrapOrd n = n - (ord 'z' - ord 'a') - 1
    charMod n
      | n > ord 'z' = wrapOrd n
      | otherwise   = n
    firstCodedOrd Forward = (ord c) + shiftedOrd
    firstCodedOrd Backward = (ord c) - shiftedOrd
    finalCodedOrd = charMod (firstCodedOrd direction)
    encodedChar = 
      if ord c <= (ord 'z') || ord c >= (ord 'a')  
      then chr finalCodedOrd
      else c -- don't encode if nonalpha

keyword = "brodybtest"
message = "vulka fenryka to tizca"

(_, _, _, codeMods, firstCodedOrds, finalCodedOrds, codedMessage) = 
  encode keyword message

(_, _, _, decodeMods, firstDecodedOrds, finalDecodedOrds, decodedMessage) = 
  decode keyword codedMessage

shiftsInRange :: [Int] -> Bool
shiftsInRange = all (\x -> x <= 25 && x >= 0)

rangeAlpha :: [Int] -> Bool
rangeAlpha = all (\x -> x == 32 || (x <= (ord 'z') && x >= (ord 'a')))
{'fraction_non_alphanumeric': 0.13933315261588505, 'fraction_numerical': 0.007319056654920032, 'mean_word_length': 2.967741935483871, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2873067', 'n_tokens_mistral': 1245, 'n_tokens_neox': 1184, 'n_words': 421}