[VicPiMakers Projects] C++ solution
Mark G.
vpm at palaceofretention.ca
Sat Oct 10 16:41:58 PDT 2020
On 2020-10-06 8:47 p.m., James Briante wrote:
> Congratulations Mark for beingĀ first to finish Challenge1!
>
> Jim
I found a bug in my solution, so I have an update to the code.
% make
clang++ -std=c++17 -o bb byte_banging.cpp
% ./bb
Input: 72, 111, 63, 85, 61, 56, 118, 121, 61, 69, 63, 61
Output #1: Sum: 941
Output #2: Average (quotient), remainder: 78, 5
Output #3: Even numbers: 72, 56, 118
Output #4: Min, at: 56, 6
Output #5: Repeats: 5, 61, 61, 61, 63, 63
Output #6: Short int (16 bits): 18543
Output #7: Sorted: 56, 61, 61, 61, 63, 63, 69, 72, 85, 111, 118, 121
Output #8: Ho?U=8vy=E?= (sorted: 8===??EHUovy)
Output #9: HOUVYE
Output #10: Key: 6
BIOPSY
Code attached.
-------------- 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' };
int key = letters.at(0) - message.at(0);
print_output_and_number(output_number++);
std::cout << "Key: " << key << std::endl;
int offset {'A'};
int message_letter {'0'};
for (const auto letter : letters) {
message_letter = offset + (((letter - offset) - key + 26) % 26);
//std::cout << (letter - offset) << " " << ((letter - offset) - key) << " " << (((letter - offset) - key) % 26) << std::endl;
//std::cout << (char)letter << " " << offset << std::endl;;
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