Quest #10: Caesar Salad
This article is part of the Weekly Quest series, where each week we present a unique programming challenge designed specifically for the Q language. There are two challenge categories, the shortest code and the fastest execution time. The chosen winners will have their solutions featured in the following week's Quest. The tests will be executed using Kdb+ 4.1.
The response must be in a comment to this article, not a reply to another comment, it should be an assignable function i.e.
f:{x+1}
In case two functions have the same performance or length, the winner will be the first one to publish it. You can publish as many as you want, so don't be afraid of giving it a second thought!
Winners of the previous Quest
The winners of the previous Quest are:
In the category of the shortest code is Attila V. with the following function:
(#)1_((-)."J"$(|:\)desc"0"^4$$:)\
In the category of the fastest execution is Sujoy R. with the following function:
(#)1_{(-)."J"$((|)a;a:{x(<)x}"0"|4$($)x)}\
In the category of the first submitted valid solution the winner is Mark Street with the following function:
{-1+count{(-)."J"$(desc;asc)@\:"0"^4$string x}\[6174<>;x]}
This Week's Quest: Caesar Salad
Yesterday, me and some friends were playing some videogames together when we suddenly got hungry. We were debating which food to order when my vegan friend Aitor Tilla got angry at us for wanting to get a Caesar Salad. Could you help us with ciphering our messages so that Aitor doesn't notice whenever we order one next time?
A Caesar cipher, also known as a shift cipher, is one of the simplest encryption techniques. In a Caesar cipher, each letter in the plaintext is replaced by a letter some fixed number of positions away in the alphabet, effectively "shifting" the alphabet.
Write a function that:
- Takes a single integer argument representing the size of the shift. It could be positive or negative.
- Takes a character vector argument representing the plaintext message.
- Returns a result with the same shape as the right argument representing the encrypted message.
The initial alphabet will be " ABCDEFGHIJKLMNOPQRSTUVWXYZ". Notice the whitespace at the beginning.
You can assume that the plaintext message will contain only uppercase characters and whitespaces, and will always be more than one character long.
Input:
- The number of shifted characters as an integer.
- A string to cipher.
Output:
The ciphered string.
Example 1:
Input:
- shift: 1
- string: "HELLO"
Recommended by LinkedIn
Output:
- ciphered string: "IFMMP"
Explanation:
Our initial alphabet gets shifted one position, so H becomes I, E becomes F, et-cetera.
Example 2:
Input:
- shift: 1
- string: "ZOO"
Output:
- ciphered string: " PP"
Explanation:
Our initial alphabet gets shifted five positions. Notice how the Z becomes a whitespace.
Example 3:
Input:
- shift: -1
- string: "IFMMP"
Output:
- (de)ciphered string: "HELLO"
Explanation:
Our initial alphabet gets shifted back 1 position, making it decipher a 1 shift ciphered word.
Write your Q function as a reply to this article.
q)f[0;"HELLO"]
"HELLO"
Let your Q expertise shine, and may the most elegant solution prevail!
Happy qoding!
is this one ok? f:{ a x+(a:" ",.Q.A)?y}
f:{l[x+(l:" ",.Q.A)?y]}
f:{s(x+s?y)mod count s:.Q.A,” “}
Not a function, but works for non-negative ints: ![" ",.Q.A;.Q.A," "]/
Tried to avoid rotate. f:{"c"$?[within[;(65;90)]'[x+a];x+a:"i"$y;32]}