Fl4m3Ph03n1x
Background
I am trying to do the typical Ceaser cypher exercise in Elixir. The description of the exercise is as follows:
Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.
The Caesar cipher is a simple shift cipher that relies on transposing all the letters in the alphabet using an integer key between
0and26. Using a key of0or26will always yield the same output due to modular arithmetic. The letter is shifted for as many values as the value of the key.The general notation for rotational ciphers is
ROT + <key>. The most commonly used rotational cipher isROT13.
Code
To achieve this I made some research and came up with the following solution which doesn’t work:
defmodule RotationalCipher do
@doc """
Given a plaintext and amount to shift by, return a rotated string.
Example:
iex> RotationalCipher.rotate("Attack at dawn", 13)
"Nggnpx ng qnja"
"""
@spec rotate(text :: String.t(), shift :: integer) :: String.t()
def rotate(text, shift) do
text
|> String.to_charlist()
|> Enum.map( fn char -> char < 97 || 97 + rem( char - 71 - shift, 26 ) end )
|> to_string()
end
end
For example, if I call RotationalCipher.rotate("a", 1) instead of b, I get z.
Now since the objective here is to work with strings, I am fairly confident I am not converting the string correctly to it’s bitstring equivalent ( https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html ).
I have read the documentation and searched for several functions but I am clearly missing something.
Question
Which bitstring function should I be using ?
Trending in Chat/Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
gregvaughn
I think you have bitstrings and charlists confused. Bitstrings look at binary data at the bit and byte level. What you really want to deal with are charlists, which you are.
I think your bug is that you should add your
shiftvalue instead of subtracting it.Fl4m3Ph03n1x
Ups, you’re correct. A miss calculation from my side
Thanks for noticing !
gregvaughn
FYI: I had some fun golfing this problem a few years ago Caesar Cipher ElixirGolf · GitHub
Fl4m3Ph03n1x
Just out of curiosity, were you the winner of this challenge ?
I took some inspiration from their top solutions, though my solution is still not working xD
gregvaughn
No I was not the winner. I ended up in 2nd place. But there was no prize involved so it was very casual and we were tweeting our solutions. The one in first place was basically a combination of my approach with that of one other person.
kip
Here’s the link to a binary pattern matching approach: How to improve this code? - #3 by kip