[VicPiMakers Projects] Challenge 1
Mark G.
vpm at palaceofretention.ca
Tue Oct 6 15:01:35 PDT 2020
Hi Jim,
Attached is a C++ program that completes the challenge. It can
be compiled as follows:
clang++ -std=c++17 -o bb byte_banging.cpp
(might work with another compiler).
Mark
-------------- next part --------------
/*
* Program to complete the first programming challenge given
* by Jim B.
* Writtem by Mark G. 2020-10-04
*/
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <map>
#include <cctype>
void print_output_and_number(int number) {
std::cout << "Output #" << number << ": ";
}
int main()
{
std::vector<char> byte_list { 72, 111, 63, 85, 61, 56, 118, 121, 61, 69, 63, 61 };
std::vector<char> sorted_byte_list { byte_list };
std::string comma {""};
std::cout << "Input: ";
for (const auto b : byte_list) {
std::cout << comma;
std::cout << (int)b;
if (comma.length() == 0) comma = ", ";
}
std::cout << std::endl;
int output_number {1};
print_output_and_number(output_number++);
// Sum
unsigned int sum = std::accumulate(byte_list.begin(), byte_list.end(), 0);
std::cout << "Sum: " << sum << std::endl;
// Quotient /average, remainder
unsigned int quotient = sum / byte_list.size();
unsigned int remainder = sum % byte_list.size();
print_output_and_number(output_number++);
std::cout << "Average (quotient), remainder: " << quotient << ", " << remainder << std::endl;
// Even integers
print_output_and_number(output_number++);
std::cout << "Even numbers: ";
comma = "";
for (const auto b : byte_list)
{
if (b % 2 == 0) {
std::cout << comma;
std::cout << (int)b;
} // end if even number
if (comma.length() == 0) comma = ", ";
}
std::cout << std::endl;
// Smallest, position
// Use the min_element algorithm which returns an iterator starting at the
// position of the first smallest element.
std::vector<char>::iterator smallest = std::min_element(byte_list.begin(), byte_list.end());
// Calculate the index of the smallest in our byte_list, using the std::distance function.
unsigned int location = std::distance(byte_list.begin(), smallest);
// Print the smallest value and the (1-based) index of same.
print_output_and_number(output_number++);
std::cout << "Min, at: " << (int)byte_list.at(location) << ", " << location+1 << std::endl;
// Repeats, print total count of all repeated items, followed by the list of them all.
print_output_and_number(output_number++);
std::map<char, int> byte_count_map;
int total_repeats {0};
for (const auto b : byte_list) {
byte_count_map[b]++;
}
for (const auto bc : byte_count_map) {
if (bc.second > 1) total_repeats += bc.second;
}
std::cout << "Repeats: " << total_repeats << ", ";
comma = "";
for (const auto bc : byte_count_map) {
if (bc.second > 1) {
for (int i = 0; i < bc.second; i++) {
std::cout << comma;
std::cout << (int)bc.first;
if (comma.length() == 0) comma = ", ";
}
}
}
std::cout << std::endl;
// Byte merging
print_output_and_number(output_number++);
unsigned short high_low_byte_pair {0};
high_low_byte_pair = (byte_list.at(0)<<8) + byte_list.at(1);
std::cout << "Short int (16 bits): " << high_low_byte_pair << std::endl;
// Sorted
std::make_heap(sorted_byte_list.begin(), sorted_byte_list.end());
std::sort_heap(sorted_byte_list.begin(), sorted_byte_list.end());
print_output_and_number(output_number++);
std::cout << "Sorted: ";
comma = "";
for (const auto &b : sorted_byte_list) {
std::cout << comma;
std::cout << (int)b;
if (comma.length() == 0) comma = ", ";
}
std::cout << std::endl;
// Character output
print_output_and_number(output_number++);
for (const auto b : byte_list) { std::cout << b; }
std::cout << " (sorted: ";
for (const auto b : sorted_byte_list) { std::cout << b; }
std::cout << ")" << std::endl;
// Characters only, converted to uppercase
std::vector<char> letters;
for (const auto b : byte_list) {
if (isalpha(b)) {
letters.emplace_back(toupper(b));
}
}
print_output_and_number(output_number++);
for (const auto letter : letters) { std::cout << letter; }
std::cout << std::endl;
// Key for ceaser cipher.
std::vector<char> message { 'B', 'I', 'O', 'P', 'S', 'Y' };
unsigned int key = letters.at(0) - message.at(0);
print_output_and_number(output_number++);
std::cout << "Key: " << key << std::endl;
unsigned int offset {'A'};
unsigned int message_letter {'0'};
for (const auto letter : letters) {
message_letter = offset + (((letter - offset) - key) % 26);
std::cout << (char)message_letter;
}
std::cout << std::endl;
//for (const auto b : byte_list) { std::cout << (int)b << std::endl; }
return 0;
}
More information about the Projects
mailing list