Skip to content
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
65 changes: 65 additions & 0 deletions exercise/01/containers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "containers.h"
#include <unordered_set>
#include <locale>


namespace
{
std::wstring to_lower(std::wstring const& s)
{
std::wstring ret{};
for (auto& chr : s) ret += towlower(chr);
return ret;
}
}

void remove_element(std::vector<int>& v, const int index)
{
v.erase(v.cbegin() + index);
}

void input_element(std::vector<std::wstring>& v, const int index, const std::wstring& value)
{
v.insert(v.cbegin() + index, value);
}

int list_nth_element(const std::list<int>& c, const int index)
{
auto it = c.cbegin();
std::advance(it, index);
return *it;
}

void list_sort_desc(std::list<int>& c)
{
c.sort(std::greater<>());
}

int unique_numbers(std::wistream& ins)
{
std::unordered_set<int> set{};
int n{};

while (ins >> n) set.emplace(n);
return set.size();
}

word_frequency::word_frequency(std::wistream& in)
{
std::wstring s{};
while (in >> s)
{
freq_[to_lower(s)] += 1;
}
}

int word_frequency::frequency(const std::wstring& s)
{
const auto find_s = freq_.find(to_lower(s));
return find_s != freq_.end() ? find_s->second : 0;
}

int word_frequency::count() const noexcept
{
return freq_.size();
}
7 changes: 5 additions & 2 deletions exercise/01/containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <string>
#include <vector>
#include <list>
#include <map>

void remove_element(std::vector<int>& v, int index);
void input_element(std::vector<std::wstring>& v, int index, const std::wstring& value);
Expand All @@ -13,8 +14,10 @@ int unique_numbers(std::wistream&);

class word_frequency
{
private:
std::map<std::wstring, unsigned int> freq_;
public:
word_frequency(std::wistream&);
explicit word_frequency(std::wistream&);
int frequency(const std::wstring& s);
int count();
int count() const noexcept;
};
25 changes: 25 additions & 0 deletions exercise/01/e01.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2050
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "e01", "e01.vcxproj", "{DD752318-EA08-4316-9EFE-C1A42F5526F4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DD752318-EA08-4316-9EFE-C1A42F5526F4}.Debug|x86.ActiveCfg = Debug|Win32
{DD752318-EA08-4316-9EFE-C1A42F5526F4}.Debug|x86.Build.0 = Debug|Win32
{DD752318-EA08-4316-9EFE-C1A42F5526F4}.Release|x86.ActiveCfg = Release|Win32
{DD752318-EA08-4316-9EFE-C1A42F5526F4}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {705A05C1-6D84-4F05-8B4D-E9EA3B8FE694}
EndGlobalSection
EndGlobal
Loading