crypto-square: initial commit

This commit is contained in:
Dmitry Kokorin 2016-04-07 15:41:41 +03:00
parent 32aef4e595
commit 5f232d2fe3
3 changed files with 239 additions and 0 deletions

View file

@ -0,0 +1,59 @@
# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)
# Basic CMake project
cmake_minimum_required(VERSION 2.8.11)
# Name the project after the exercise
project(${exercise} CXX)
# Locate Boost libraries: unit_test_framework, date_time and regex
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.55 REQUIRED COMPONENTS unit_test_framework date_time regex)
# Enable C++11 features on gcc/clang
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
set(CMAKE_CXX_FLAGS "-std=c++11")
endif()
# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
add_definitions(-DEXERCISM_RUN_ALL_TESTS)
endif()
# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})
# Implementation could be only a header
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
set(exercise_cpp ${file}.cpp)
else()
set(exercise_cpp "")
endif()
# Include a test helper header if it exists
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/require_equal_containers.h)
set(test_helper require_equal_containers.h)
else()
set(test_helper "")
endif()
# Build executable from sources and headers
add_executable(${exercise} ${file}_test.cpp ${test_helper} ${exercise_cpp} ${file}.h)
# We need boost includes
target_include_directories(${exercise} PRIVATE ${Boost_INCLUDE_DIRS})
# We need boost libraries
target_link_libraries(${exercise} ${Boost_LIBRARIES})
# Tell MSVC not to warn us about unchecked iterators in debug builds
if(${MSVC})
set_target_properties(${exercise} PROPERTIES
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS)
endif()
# Run the tests on every build
add_custom_command(TARGET ${exercise} POST_BUILD COMMAND ${exercise})

108
cpp/crypto-square/README.md Normal file
View file

@ -0,0 +1,108 @@
# Crypto Square
Implement the classic method for composing secret messages called a square code.
The input is first normalized: The spaces and punctuation are removed
from the English text and the message is downcased.
Then, the normalized characters are broken into rows. These rows can be
regarded as forming a rectangle when printed with intervening newlines.
For example, the sentence
> If man was meant to stay on the ground god would have given us roots
is 54 characters long.
Broken into 8-character columns, it yields 7 rows.
Those 7 rows produce this rectangle when printed one per line:
```plain
ifmanwas
meanttos
tayonthe
groundgo
dwouldha
vegivenu
sroots
```
The coded message is obtained by reading down the columns going left to
right.
For example, the message above is coded as:
```plain
imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau
```
Write a program that, given an English text, outputs the encoded version
of that text.
The size of the square (number of columns) should be decided by the
length of the message.
If the message is a length that creates a perfect square (e.g. 4, 9, 16,
25, 36, etc), use that number of columns.
If the message doesn't fit neatly into a square, choose the number of
columns that corresponds to the smallest square that is larger than the
number of characters in the message.
For example, a message 4 characters long should use a 2 x 2 square. A
message that is 81 characters long would use a square that is 9 columns
wide.
A message between 5 and 8 characters long should use a rectangle 3
characters wide.
Output the encoded text grouped by column.
For example:
- "Have a nice day. Feed the dog & chill out!"
- Normalizes to: "haveanicedayfeedthedogchillout"
- Which has length: 30
- And splits into 5 6-character rows:
- "havean"
- "iceday"
- "feedth"
- "edogch"
- "illout"
- Which yields a ciphertext beginning: "hifei acedl v…"
## Getting Started
Make sure you have read [the C++ page](http://exercism.io/languages/cpp) on
exercism.io. This covers the basic information on setting up the development
environment expected by the exercises.
## Passing the Tests
Get the first test compiling, linking and passing by following the [three
rules of test-driven development](http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd).
Create just enough structure by declaring namespaces, functions, classes,
etc., to satisfy any compiler errors and get the test to fail. Then write
just enough code to get the test to pass. Once you've done that,
uncomment the next test by moving the following line past the next test.
```C++
#if defined(EXERCISM_RUN_ALL_TESTS)
```
This may result in compile errors as new constructs may be invoked that
you haven't yet declared or defined. Again, fix the compile errors minimally
to get a failing test, then change the code minimally to pass the test,
refactor your implementation for readability and expressiveness and then
go on to the next test.
Try to use standard C++11 facilities in preference to writing your own
low-level algorithms or facilities by hand. [CppReference](http://en.cppreference.com/)
is a wiki reference to the C++ language and standard library. If you
are new to C++, but have programmed in C, beware of
[C traps and pitfalls](http://www.slideshare.net/LegalizeAdulthood/c-traps-and-pitfalls-for-c-programmers).
## Source
J Dalbey's Programming Practice problems [view source](http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html)

View file

@ -0,0 +1,72 @@
#include "crypto_square.h"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(normalize_strange_characters)
{
BOOST_REQUIRE_EQUAL("splunk", crypto_square::cipher("s#$%^&plunk").normalize_plain_text());
}
#if defined(EXERCISM_RUN_ALL_TESTS)
BOOST_AUTO_TEST_CASE(normalize_numbers)
{
BOOST_REQUIRE_EQUAL("123go", crypto_square::cipher("1, 2, 3 GO!").normalize_plain_text());
}
BOOST_AUTO_TEST_CASE(size_of_small_square)
{
BOOST_REQUIRE_EQUAL(2U, crypto_square::cipher("1234").size());
}
BOOST_AUTO_TEST_CASE(size_of_slightly_larger_square)
{
BOOST_REQUIRE_EQUAL(3U, crypto_square::cipher("123456789").size());
}
BOOST_AUTO_TEST_CASE(size_of_non_perfect_square)
{
BOOST_REQUIRE_EQUAL(4U, crypto_square::cipher("123456789abc").size());
}
BOOST_AUTO_TEST_CASE(plain_text_segments_from_phrase)
{
const std::vector<std::string> expected{"neverv", "exthin", "eheart", "withid", "lewoes"};
const auto actual = crypto_square::cipher("Never vex thine heart with idle woes").plain_text_segments();
BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end());
}
BOOST_AUTO_TEST_CASE(plain_text_segments_from_complex_phrase)
{
const std::vector<std::string> expected{"zomg", "zomb", "ies"};
const auto actual = crypto_square::cipher("ZOMG! ZOMBIES!!!").plain_text_segments();
BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end());
}
BOOST_AUTO_TEST_CASE(cipher_text_short_phrase)
{
BOOST_REQUIRE_EQUAL("tasneyinicdsmiohooelntuillibsuuml",
crypto_square::cipher("Time is an illusion. Lunchtime doubly so.").cipher_text());
}
BOOST_AUTO_TEST_CASE(cipher_text_long_phrase)
{
BOOST_REQUIRE_EQUAL("wneiaweoreneawssciliprerlneoidktcms",
crypto_square::cipher("We all know interspecies romance is weird.").cipher_text());
}
BOOST_AUTO_TEST_CASE(normalized_cipher_text1)
{
BOOST_REQUIRE_EQUAL("msemoa anindn inndla etltsh ui",
crypto_square::cipher("Madness, and then illumination.").normalized_cipher_text());
}
BOOST_AUTO_TEST_CASE(normalized_cipher_text2)
{
BOOST_REQUIRE_EQUAL("vrela epems etpao oirpo",
crypto_square::cipher("Vampires are people too!").normalized_cipher_text());
}
#endif