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 https://gist.github.com/gvaughn/b295e69b4eb302a4fab5

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

meraj_enigma
Hey, What’s a good resource to learn Microservices in Elixir/Phoenix? Is there any book/docs/Github repos that I can refer to? Thanks, -M
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
shansiddiqui94
Hello all, I recently did my first app in Phoenix and Liveview, many thanks to all the users who assisted me. I found that the tutorial ...
New
ericdouglas
I think that would be really interesting to have official books created by the community about all kinds of development we can do with El...
New
stevensonmt
I’d like to provide my review of the Elixir Course module from Groxio. I have some criticisms but I’d like to start with the positives. ...
New
jace
I wanted to write a library for interacting with a Web API as a practical way of learning Elixir. However, there seem to be a lot of diff...
New
pdgonzalez872
Do we have a list of academic/research papers: about Elixir/Erlang? that use Elixir/Erlang? about the Beam? If so, could you please po...
New
younes-alouani
I'm studying Phoenix Framework but I want to understand basics first. Which book/mooc explains better the Design of Network-based Softwar...
New
logesh
Could someone provide a learning path for functional programming for who came from oops background.? Thanks in advance
New
shansiddiqui94
Greetings Elixir Developers, My name is Daniel, and I am taking my first step to learn all about the Elixir language. Currently I have a...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement