Fl4m3Ph03n1x

Fl4m3Ph03n1x

Ceaser cypher exercise on Exercism

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 0 and 26 . Using a key of 0 or 26 will 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 is ROT13 .

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 ?

Most Liked

gregvaughn

gregvaughn

FYI: I had some fun golfing this problem a few years ago Caesar Cipher ElixirGolf · GitHub

gregvaughn

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 shift value instead of subtracting it.

Where Next?

Popular in Chat/Questions Top

lgmfred
Hello, I want to get started with elixir today. Having learnt the basics of Erlang (my first ever language) and seen what it’s can do, I’...
New
Allyedge
Hey, I want to learn Elixir OTP and I wanted to know if there are any good resources that teach it. I found some web pages, but none of t...
New
Lincxx
Hello, I’m sure this has been asked a bunch, but where to start. I do prefer vodeos over books, but recently I have found books to be mor...
New
shansiddiqui94
Hello, I have an interview coming up and I seem to have forgotten important concepts of Elixir. So I was wondering if you guys know of a...
New
armanm
I know zero downtime deployment can mean different things depending on your application and what your users can tolerate so expectations ...
New
xgilarb
Hi there, I’m interested in using Elixir because of the rumors about the reliability of the Phoenix framework, and surprisingly, Elixir’...
New
New
Sujit
Hi Team From the title, I am entirely new to programming and i am interested in learning elixir but not sure where to start. Can someone...
New
InkFlo
Hi everyone, This year I’m graduated from Bachelor Degree (in computer science) from France (not really a bachelor, the exact term is “L...
New
Nopp
Hello there, i have a lot to read and to learn, but are there some books, that are focussing on how i “should” plan the architecture of ...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement