Skip to content

Commit

Permalink
Merge pull request #3 from qianglise/main
Browse files Browse the repository at this point in the history
change order of episodees and assign numbers to exercises
  • Loading branch information
code4yonglei authored Sep 19, 2024
2 parents 6dbdf83 + 1a7dee6 commit 74d8790
Show file tree
Hide file tree
Showing 98 changed files with 1,693 additions and 17 deletions.
13 changes: 13 additions & 0 deletions content/code/04_automata-cxx/CMakeLists.txt
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
8 changes: 8 additions & 0 deletions content/code/04_automata-cxx/external/CMakeLists.txt
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}
)
8 changes: 8 additions & 0 deletions content/code/04_automata-cxx/external/conversion.cpp
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 content/code/04_automata-cxx/external/conversion.hpp
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);
13 changes: 13 additions & 0 deletions content/code/04_automata-cxx/solution/CMakeLists.txt
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 content/code/04_automata-cxx/solution/external/CMakeLists.txt
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 content/code/04_automata-cxx/solution/external/conversion.cpp
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 content/code/04_automata-cxx/solution/external/conversion.hpp
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);
15 changes: 15 additions & 0 deletions content/code/04_automata-cxx/solution/src/CMakeLists.txt
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 content/code/04_automata-cxx/solution/src/evolution/CMakeLists.txt
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 content/code/04_automata-cxx/solution/src/evolution/evolution.cpp
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;
}
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 content/code/04_automata-cxx/solution/src/initial/CMakeLists.txt
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 content/code/04_automata-cxx/solution/src/initial/initial.cpp
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 content/code/04_automata-cxx/solution/src/initial/initial.hpp
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 content/code/04_automata-cxx/solution/src/io/CMakeLists.txt
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}
)
12 changes: 12 additions & 0 deletions content/code/04_automata-cxx/solution/src/io/io.cpp
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;
}
5 changes: 5 additions & 0 deletions content/code/04_automata-cxx/solution/src/io/io.hpp
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);
34 changes: 34 additions & 0 deletions content/code/04_automata-cxx/solution/src/main.cpp
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 content/code/04_automata-cxx/solution/src/parser/CMakeLists.txt
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 content/code/04_automata-cxx/solution/src/parser/parser.cpp
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);
}
5 changes: 5 additions & 0 deletions content/code/04_automata-cxx/solution/src/parser/parser.hpp
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[]);
8 changes: 8 additions & 0 deletions content/code/04_automata-cxx/src/CMakeLists.txt
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(...)
5 changes: 5 additions & 0 deletions content/code/04_automata-cxx/src/evolution/CMakeLists.txt
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(...)
24 changes: 24 additions & 0 deletions content/code/04_automata-cxx/src/evolution/evolution.cpp
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 content/code/04_automata-cxx/src/evolution/evolution.hpp
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);
5 changes: 5 additions & 0 deletions content/code/04_automata-cxx/src/initial/CMakeLists.txt
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(...)
14 changes: 14 additions & 0 deletions content/code/04_automata-cxx/src/initial/initial.cpp
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 content/code/04_automata-cxx/src/initial/initial.hpp
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);
5 changes: 5 additions & 0 deletions content/code/04_automata-cxx/src/io/CMakeLists.txt
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(...)
12 changes: 12 additions & 0 deletions content/code/04_automata-cxx/src/io/io.cpp
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;
}
5 changes: 5 additions & 0 deletions content/code/04_automata-cxx/src/io/io.hpp
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);
Loading

0 comments on commit 74d8790

Please sign in to comment.