What programming language you start with really all depends on where you want to go with programming/coding. The great thing about this field is that there are an absolute abundance of smaller fields that you can go into, all using programming in their own unique ways. For web applications, a good start would be with HTML and later moving your way through CSS, JavaScript, JQuery, PHP, SQL, and any of the JavaScript libraries. Ruby is also a popular choice, so I would recommend checking that out too. For more scientific fields or areas with more machine learning and A.I., Python is generally a great place to start as it is widely used in that field of study. C++ is also a very useful language to know for that, but it can be a little more challenging for beginners. For game and application design, languages such as C#, C, Swift, Kotlin, and Java are most often used for that.
Description
Permutation madness
A group of programs has gotten together in a line and have started dancing
They appear to have 3 dance moves.
Try Spin that's a good trick, the right end swaps up most left keeing their order
Exchange Two programs appear to swap depending on the numbers given
Partner Two programs that know eachother swaps
Input description
a list of programs in their initial order
First you will be given a string of characters, each character is an individual program
On the next line you will get a list of moves split by ,
The moves work as following:
Spin is given as SN where N is a positive integer. This moves N programs from the right up front, keeping their order
Exchange is given as xA/B where A and B are the positions of two programs that will swap positions
Partner is given as pA/B where A and B refer to the original positions of the programs and swaps them whereever they currently are
Output description
The output is the final order the list of programs stand in after they are done dancing
Input example
abcde
s1,x3/4,p4/1
Everything is 0 indexed
Output example
abcde (initial)
eabcd (s1)
eabdc (x3/4)
baedc (p4/1)
You only have to return the final state
Challenge Input
abcde
s1,x3/4,p4/1
.
dbagcfe
s4,s5,x5/3,x5/6,s5,s3,x0/3,x3/6,x6/0,x2/3,x3/5,s5,s5,s5,s1,s5,s3,s3,x2/3,x1/0,s1,s1,s1,s4,x1/3,x4/2,x5/1,x6/0,s2,x2/1
too long to show
too long to show
Challenge Output
baedc
abcdefg
eojfmbpkldghncia
<Figure the last one out yourself>
Solution
in C++
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
namespace {
struct Spin { size_t amount; };
struct Exchange { size_t fst, snd; };
struct Partner { size_t fst, snd; };
using Move = std::variant<Spin, Exchange, Partner>;
Move asMove(std::string_view sv) {
size_t a, b;
if (std::sscanf(sv.data(), "s%zu", &a) == 1) {
return Spin{a};
} else if (std::sscanf(sv.data(), "x%zu/%zu", &a, &b) == 2) {
return Exchange{a, b};
} else if (std::sscanf(sv.data(), "p%zu/%zu", &a, &b) == 2) {
return Partner{a, b};
} else {
throw std::runtime_error("bad move: " + std::string(sv));
}
}
auto splitOnComma(std::string_view sv) {
auto numCommas = std::count(sv.begin(), sv.end(), ',');
std::vector<std::string_view> result;
result.reserve(numCommas + 1);
for (auto start = sv.begin(), end = start;
start < sv.end();
start = end + 1) {
end = std::find(start, sv.end(), ',');
auto len = std::distance(start, end);
result.emplace_back(start, len);
}
return result;
}
auto asMoves(std::string_view sv) {
std::vector<Move> result;
auto tokens = splitOnComma(sv);
result.reserve(tokens.size());
std::transform(
tokens.begin(), tokens.end(), std::back_inserter(result), asMove);
return result;
}
template<typename C>
struct MoveVisitor {
const C& input;
C result;
MoveVisitor(const C& c) : input(c), result(c) {}
void operator()(Spin s) {
auto n_first = result.end() - s.amount;
std::rotate(result.begin(), n_first, result.end());
}
void operator()(Exchange e) {
std::swap(result[e.fst], result[e.snd]);
}
void operator()(Partner p) {
auto from = std::find(result.begin(), result.end(), input[p.fst]);
auto to = std::find(result.begin(), result.end(), input[p.snd]);
std::swap(*from, *to);
}
};
template<typename C>
C permute(const C& input, const std::vector<Move>& moves) {
auto mv = MoveVisitor(input);
for (auto m : moves) {
std::visit(mv, m);
}
return mv.result;
}
}
int main() {
std::string programs;
std::string movesLine;
std::getline(std::cin, programs);
std::getline(std::cin, movesLine);
auto moves = asMoves(movesLine);
std::puts(permute(programs, moves).c_str());
}
Permutation madness
A group of programs has gotten together in a line and have started dancing
They appear to have 3 dance moves.
Try Spin that's a good trick, the right end swaps up most left keeing their order
Exchange Two programs appear to swap depending on the numbers given
Partner Two programs that know eachother swaps
Input description
a list of programs in their initial order
First you will be given a string of characters, each character is an individual program
On the next line you will get a list of moves split by ,
The moves work as following:
Spin is given as SN where N is a positive integer. This moves N programs from the right up front, keeping their order
Exchange is given as xA/B where A and B are the positions of two programs that will swap positions
Partner is given as pA/B where A and B refer to the original positions of the programs and swaps them whereever they currently are
Output description
The output is the final order the list of programs stand in after they are done dancing
Input example
abcde
s1,x3/4,p4/1
Everything is 0 indexed
Output example
abcde (initial)
eabcd (s1)
eabdc (x3/4)
baedc (p4/1)
You only have to return the final state
Challenge Input
abcde
s1,x3/4,p4/1
.
dbagcfe
s4,s5,x5/3,x5/6,s5,s3,x0/3,x3/6,x6/0,x2/3,x3/5,s5,s5,s5,s1,s5,s3,s3,x2/3,x1/0,s1,s1,s1,s4,x1/3,x4/2,x5/1,x6/0,s2,x2/1
too long to show
too long to show
Challenge Output
baedc
abcdefg
eojfmbpkldghncia
<Figure the last one out yourself>
Solution
in C++
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
namespace {
struct Spin { size_t amount; };
struct Exchange { size_t fst, snd; };
struct Partner { size_t fst, snd; };
using Move = std::variant<Spin, Exchange, Partner>;
Move asMove(std::string_view sv) {
size_t a, b;
if (std::sscanf(sv.data(), "s%zu", &a) == 1) {
return Spin{a};
} else if (std::sscanf(sv.data(), "x%zu/%zu", &a, &b) == 2) {
return Exchange{a, b};
} else if (std::sscanf(sv.data(), "p%zu/%zu", &a, &b) == 2) {
return Partner{a, b};
} else {
throw std::runtime_error("bad move: " + std::string(sv));
}
}
auto splitOnComma(std::string_view sv) {
auto numCommas = std::count(sv.begin(), sv.end(), ',');
std::vector<std::string_view> result;
result.reserve(numCommas + 1);
for (auto start = sv.begin(), end = start;
start < sv.end();
start = end + 1) {
end = std::find(start, sv.end(), ',');
auto len = std::distance(start, end);
result.emplace_back(start, len);
}
return result;
}
auto asMoves(std::string_view sv) {
std::vector<Move> result;
auto tokens = splitOnComma(sv);
result.reserve(tokens.size());
std::transform(
tokens.begin(), tokens.end(), std::back_inserter(result), asMove);
return result;
}
template<typename C>
struct MoveVisitor {
const C& input;
C result;
MoveVisitor(const C& c) : input(c), result(c) {}
void operator()(Spin s) {
auto n_first = result.end() - s.amount;
std::rotate(result.begin(), n_first, result.end());
}
void operator()(Exchange e) {
std::swap(result[e.fst], result[e.snd]);
}
void operator()(Partner p) {
auto from = std::find(result.begin(), result.end(), input[p.fst]);
auto to = std::find(result.begin(), result.end(), input[p.snd]);
std::swap(*from, *to);
}
};
template<typename C>
C permute(const C& input, const std::vector<Move>& moves) {
auto mv = MoveVisitor(input);
for (auto m : moves) {
std::visit(mv, m);
}
return mv.result;
}
}
int main() {
std::string programs;
std::string movesLine;
std::getline(std::cin, programs);
std::getline(std::cin, movesLine);
auto moves = asMoves(movesLine);
std::puts(permute(programs, moves).c_str());
}
Comments
Post a Comment