generated from ENCCS/sphinx-lesson-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from qianglise/main
change order of episodees and assign numbers to exercises
- Loading branch information
Showing
98 changed files
with
1,693 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
cmake_minimum_required(VERSION 3.18) | ||
|
||
project(automata-cxx LANGUAGES CXX) | ||
|
||
set(CMAKE_CXX_STANDARD 14) | ||
set(CMAKE_CXX_EXTENSIONS OFF) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
||
# defines targets and sources | ||
# FIXME add src folder | ||
|
||
# contains an "external" library we will link to | ||
# FIXME add external folder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# FIXME create library from sources in this folder | ||
add_library(...) | ||
|
||
# add this folder to include directories for the project | ||
target_include_directories(conversion | ||
PUBLIC | ||
${CMAKE_CURRENT_LIST_DIR} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include "conversion.hpp" | ||
|
||
#include <bitset> | ||
#include <string> | ||
|
||
std::string binary_representation(const int decimal) { | ||
return std::bitset<8>(decimal).to_string(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
std::string binary_representation(const int decimal); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
cmake_minimum_required(VERSION 3.18) | ||
|
||
project(automata-cxx LANGUAGES CXX) | ||
|
||
set(CMAKE_CXX_STANDARD 14) | ||
set(CMAKE_CXX_EXTENSIONS OFF) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
||
# defines targets and sources | ||
add_subdirectory(src) | ||
|
||
# contains an "external" library we will link to | ||
add_subdirectory(external) |
13 changes: 13 additions & 0 deletions
13
content/code/04_automata-cxx/solution/external/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
add_library(conversion) | ||
|
||
target_sources(conversion | ||
PRIVATE | ||
conversion.cpp | ||
PUBLIC | ||
conversion.hpp | ||
) | ||
|
||
target_include_directories(conversion | ||
PUBLIC | ||
${CMAKE_CURRENT_LIST_DIR} | ||
) |
8 changes: 8 additions & 0 deletions
8
content/code/04_automata-cxx/solution/external/conversion.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include "conversion.hpp" | ||
|
||
#include <bitset> | ||
#include <string> | ||
|
||
std::string binary_representation(const int decimal) { | ||
return std::bitset<8>(decimal).to_string(); | ||
} |
5 changes: 5 additions & 0 deletions
5
content/code/04_automata-cxx/solution/external/conversion.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
std::string binary_representation(const int decimal); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
add_executable(automata main.cpp) | ||
|
||
add_subdirectory(evolution) | ||
add_subdirectory(initial) | ||
add_subdirectory(io) | ||
add_subdirectory(parser) | ||
|
||
target_link_libraries(automata | ||
PRIVATE | ||
conversion | ||
evolution | ||
initial | ||
io | ||
parser | ||
) |
13 changes: 13 additions & 0 deletions
13
content/code/04_automata-cxx/solution/src/evolution/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
add_library(evolution) | ||
|
||
target_sources(evolution | ||
PRIVATE | ||
evolution.cpp | ||
PUBLIC | ||
evolution.hpp | ||
) | ||
|
||
target_include_directories(evolution | ||
PUBLIC | ||
${CMAKE_CURRENT_LIST_DIR} | ||
) |
24 changes: 24 additions & 0 deletions
24
content/code/04_automata-cxx/solution/src/evolution/evolution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "evolution.hpp" | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
std::vector<int> evolve(const std::vector<int> row, const std::string rule_binary) { | ||
std::vector<int> result; | ||
|
||
for (auto i = 0; i < row.size(); ++i) { | ||
|
||
auto left = (i == 0 ? row.size() : i) - 1; | ||
auto center = i; | ||
auto right = (i + 1) % row.size(); | ||
|
||
auto ancestors = 4 * row[left] + 2 * row[center] + 1 * row[right]; | ||
ancestors = 7 - ancestors; | ||
|
||
auto new_state = std::stoi(rule_binary.substr(ancestors, 1)); | ||
|
||
result.push_back(new_state); | ||
} | ||
|
||
return result; | ||
} |
6 changes: 6 additions & 0 deletions
6
content/code/04_automata-cxx/solution/src/evolution/evolution.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
std::vector<int> evolve(const std::vector<int> row, const std::string rule_binary); |
13 changes: 13 additions & 0 deletions
13
content/code/04_automata-cxx/solution/src/initial/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
add_library(initial) | ||
|
||
target_sources(initial | ||
PRIVATE | ||
initial.cpp | ||
PUBLIC | ||
initial.hpp | ||
) | ||
|
||
target_include_directories(initial | ||
PUBLIC | ||
${CMAKE_CURRENT_LIST_DIR} | ||
) |
14 changes: 14 additions & 0 deletions
14
content/code/04_automata-cxx/solution/src/initial/initial.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "initial.hpp" | ||
|
||
#include <vector> | ||
|
||
std::vector<int> initial_distribution(const int length) { | ||
|
||
// we start with a vector which is zeroed out | ||
std::vector<int> result(length, 0); | ||
|
||
// more or less in the middle we place a living cell | ||
result[length / 2] = 1; | ||
|
||
return result; | ||
} |
5 changes: 5 additions & 0 deletions
5
content/code/04_automata-cxx/solution/src/initial/initial.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
std::vector<int> initial_distribution(const int length); |
13 changes: 13 additions & 0 deletions
13
content/code/04_automata-cxx/solution/src/io/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
add_library(io) | ||
|
||
target_sources(io | ||
PRIVATE | ||
io.cpp | ||
PUBLIC | ||
io.hpp | ||
) | ||
|
||
target_include_directories(io | ||
PUBLIC | ||
${CMAKE_CURRENT_LIST_DIR} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include "io.hpp" | ||
|
||
#include <algorithm> | ||
#include <iostream> | ||
#include <vector> | ||
|
||
void print_row(const std::vector<int> row) { | ||
std::for_each(row.begin(), row.end(), [](int const &value) { | ||
std::cout << (value == 1 ? '*' : ' '); | ||
}); | ||
std::cout << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
void print_row(const std::vector<int> row); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "conversion.hpp" | ||
#include "evolution.hpp" | ||
#include "initial.hpp" | ||
#include "io.hpp" | ||
#include "parser.hpp" | ||
|
||
#include <iostream> | ||
|
||
int main(int argc, char *argv[]) { | ||
|
||
// parse arguments | ||
int length, num_steps, rule_decimal; | ||
std::tie(length, num_steps, rule_decimal) = parse_arguments(argc, argv); | ||
|
||
// print information about parameters | ||
std::cout << "length: " << length << std::endl; | ||
std::cout << "number of steps: " << num_steps << std::endl; | ||
std::cout << "rule: " << rule_decimal << std::endl; | ||
|
||
// obtain binary representation for the rule | ||
std::string rule_binary = binary_representation(rule_decimal); | ||
|
||
// create initial distribution | ||
std::vector<int> row = initial_distribution(length); | ||
|
||
// print initial configuration | ||
print_row(row); | ||
|
||
// the system evolves, print each step | ||
for (int step = 0; step < num_steps; step++) { | ||
row = evolve(row, rule_binary); | ||
print_row(row); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
content/code/04_automata-cxx/solution/src/parser/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
add_library(parser) | ||
|
||
target_sources(parser | ||
PRIVATE | ||
parser.cpp | ||
PUBLIC | ||
parser.hpp | ||
) | ||
|
||
target_include_directories(parser | ||
PUBLIC | ||
${CMAKE_CURRENT_LIST_DIR} | ||
) |
15 changes: 15 additions & 0 deletions
15
content/code/04_automata-cxx/solution/src/parser/parser.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include "parser.hpp" | ||
|
||
#include <cassert> | ||
#include <string> | ||
#include <tuple> | ||
|
||
std::tuple<int, int, int> parse_arguments(int argc, char *argv[]) { | ||
assert(argc == 4 && "program called with wrong number of arguments"); | ||
|
||
auto length = std::stoi(argv[1]); | ||
auto num_steps = std::stoi(argv[2]); | ||
auto rule_decimal = std::stoi(argv[3]); | ||
|
||
return std::make_tuple(length, num_steps, rule_decimal); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#include <tuple> | ||
|
||
std::tuple<int, int, int> parse_arguments(int argc, char *argv[]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# FIXME add automata executable from main.f90 | ||
add_executable(...) | ||
|
||
# FIXME add subdirectories for each sub-library | ||
add_subdirectory(...) | ||
|
||
# FIXME link libraries to the executable | ||
target_link_libraries(...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# FIXME create library from the sources in this folder | ||
add_library(...) | ||
|
||
# FIXME add this folder to include directories for this project | ||
target_include_directories(...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "evolution.hpp" | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
std::vector<int> evolve(const std::vector<int> row, const std::string rule_binary) { | ||
std::vector<int> result; | ||
|
||
for (auto i = 0; i < row.size(); ++i) { | ||
|
||
auto left = (i == 0 ? row.size() : i) - 1; | ||
auto center = i; | ||
auto right = (i + 1) % row.size(); | ||
|
||
auto ancestors = 4 * row[left] + 2 * row[center] + 1 * row[right]; | ||
ancestors = 7 - ancestors; | ||
|
||
auto new_state = std::stoi(rule_binary.substr(ancestors, 1)); | ||
|
||
result.push_back(new_state); | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
std::vector<int> evolve(const std::vector<int> row, const std::string rule_binary); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# FIXME create library from the sources in this folder | ||
add_library(...) | ||
|
||
# FIXME add this folder to include directories for this project | ||
target_include_directories(...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "initial.hpp" | ||
|
||
#include <vector> | ||
|
||
std::vector<int> initial_distribution(const int length) { | ||
|
||
// we start with a vector which is zeroed out | ||
std::vector<int> result(length, 0); | ||
|
||
// more or less in the middle we place a living cell | ||
result[length / 2] = 1; | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
std::vector<int> initial_distribution(const int length); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# FIXME create library from the sources in this folder | ||
add_library(...) | ||
|
||
# FIXME add this folder to include directories for this project | ||
target_include_directories(...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include "io.hpp" | ||
|
||
#include <algorithm> | ||
#include <iostream> | ||
#include <vector> | ||
|
||
void print_row(const std::vector<int> row) { | ||
std::for_each(row.begin(), row.end(), [](int const &value) { | ||
std::cout << (value == 1 ? '*' : ' '); | ||
}); | ||
std::cout << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
void print_row(const std::vector<int> row); |
Oops, something went wrong.