Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment 02 homework #24

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 63 additions & 4 deletions 02_iterables/homework/wh.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,69 @@
#include <iostream>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <utility>
using namespace std;

int main() {
std::string line;
//Find the lenght of characters in the string
char n[] = "When the sun shines, we will shine together. Told you I will be here forever. Said I will always be your friend. Took an oath, I'ma stick it out to the end. Now that it is raining more than ever. Know that we will still have each other. You can stand under my umbrella. You can stand under my umbrella, ella, ella, eh, eh, eh. Under my umbrella, ella, ella, eh, eh, eh. Under my umbrella, ella, ella, eh, eh, eh. Under my umbrella, ella, ella, eh, eh, eh, eh, eh-eh";

while (std::getline(std::cin, line)) {
std::cout << line << std::endl;
string specials = "*()[]{}/,:;\'\".#€£$%^&*=!";
int len = strlen(n);

// Remove the special characters from the input string (uses '.find' to find in each character from 'n' which ones are special characters. Replaces special character with a space)
for (int i = 0; i < len; i++) {
if (specials.find(n[i]) != string::npos) {
n[i] = ' ';
}
}

// for each character, make the letter lowercase
for(int i = 0; i < len;i++){
n[i] = tolower(n[i]);
}
}
//turn the string into a token and store each word in a vector (each word in a separate line)
char * word;
word = strtok(n, " ");
vector<string>words;

while (word != NULL) {
words.push_back(word);
word = strtok (NULL, " ");
}

//sort the words alphabetically
// sort(words.begin(), words.end());

// Constructing a Map with a string (words) and int
map< string, int> counts;

for (string w: words){
int current_count = counts[w]; //read map
int new_count = current_count + 1; //compute
counts[w] = new_count; //write to map
}

//store the map contents in a vector to sort the sorted_counts using a lambda function as a comparator.
//comparing the int first, if they are not equal, sort them in descending order and higher int comes first on the list.
//if the pairs are equal, sort the string alphabetically
vector <pair<string, int>> sorted_counts(counts.begin(), counts.end());
sort(sorted_counts.begin(), sorted_counts.end(), [](const pair <string, int>& a, const pair<string, int>& b) {
if (a.second != b.second) {
return a.second > b.second;
}
else{
return a.first < b.first;
}
});

//Pair my string and int and write output
for(const auto& word_mapping : sorted_counts) {
cout << word_mapping.first << " " << word_mapping.second << endl;
}

return 0;
}
22 changes: 22 additions & 0 deletions homework_01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>

int main() {
std::string line;
std::cout << "Hello! Enter a positive number: " << std::flush;
while(std::getline(std::cin, line)) {
unsigned int n = std::stoi(line);
unsigned int steps = 0;

while (n != 1) {
steps++;
if (n % 2 == 0) {
n = n / 2;
} else {
n = 3 * n + 1;
}
std::cout << " " << n;
}
std::cout << std::endl;
}
return 0;
}