Skip to content
This repository was archived by the owner on Mar 1, 2022. It is now read-only.

Commit 88a46b3

Browse files
author
ivan-ristovic
committed
Week 10
1 parent 90c6cd9 commit 88a46b3

File tree

18 files changed

+386
-0
lines changed

18 files changed

+386
-0
lines changed

10-cpp-range/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
range-v3-0.11.0

10-cpp-range/01_count_adj_eq/Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
PROGRAM=01_count_adj_eq.out
2+
CXX = g++
3+
CXXFLAGS = -g -std=c++14 -Wall -I../range-v3/include
4+
5+
$(PROGRAM): main.o
6+
$(CXX) -o $(PROGRAM) main.o
7+
8+
.PHONY: clean dist
9+
10+
clean:
11+
-rm *.o $(PROGRAM) *core
12+
13+
dist: clean
14+
-tar -chvj -C .. -f ../$(PROGRAM).tar.bz2 $(PROGRAM)
15+
16+

10-cpp-range/01_count_adj_eq/main.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Zadatak: Napisati sablonsku funkciju count_adj_equals
2+
// koja vraca broj uzastopnih jednakih elemenata kolekcije.
3+
4+
#include <iostream>
5+
#include <string>
6+
#include <numeric>
7+
#include <vector>
8+
9+
// Mozemo iskoristiti std::inner_product funkciju
10+
// Operacije * i + cemo zameniti sa == i +
11+
// Dakle, za kolekcije xs i ys, racunamo:
12+
// (x1 == y1) + (x2 == y2) + ...
13+
// Mi zapravo poredimo kolekciju samu sa sobom
14+
// pa ce ys biti isto xs samo bez prvog elementa
15+
// Pocetna vrednost (akumulator) je 0
16+
template <typename T>
17+
int count_adj_equals(const T& xs)
18+
{
19+
return std::inner_product(
20+
std::cbegin(xs), std::cend(xs) - 1,
21+
std::cbegin(xs) + 1,
22+
0,
23+
// Mozemo koristiti auto takodje
24+
[](const int& x, const int &y) { return x + y; },
25+
// Ovde ne znamo tip elemenata kolekcije, pa stoga auto
26+
[](const auto& x, const auto &y) { return x == y ? 1 : 0; }
27+
);
28+
}
29+
30+
int main(int argc, char *argv[])
31+
{
32+
const std::string text = "Hooloovoo";
33+
34+
std::cerr << text << ": " << count_adj_equals(text) << std::endl;
35+
36+
const std::vector<double> numbers{ -1.0, 2.36, 65.4, 65.4, 65.4, -1.0, 0.0, 5.4 };
37+
38+
std::cerr << "numbers : " << count_adj_equals(numbers) << std::endl;
39+
40+
return 0;
41+
}

10-cpp-range/02_is_sorted/Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
PROGRAM=02_is_sorted.out
2+
CXX = g++
3+
CXXFLAGS = -g -std=c++14 -Wall -I../range-v3/include
4+
5+
$(PROGRAM): main.o
6+
$(CXX) -o $(PROGRAM) main.o
7+
8+
.PHONY: clean dist
9+
10+
clean:
11+
-rm *.o $(PROGRAM) *core
12+
13+
dist: clean
14+
-tar -chvj -C .. -f ../$(PROGRAM).tar.bz2 $(PROGRAM)
15+
16+

10-cpp-range/02_is_sorted/main.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Zadatak: Napisati sablonsku funkciju is_sorted
2+
// koja proverava da li je data kolekcija sortirana.
3+
4+
#include <iostream>
5+
#include <string>
6+
#include <numeric>
7+
#include <vector>
8+
9+
// Mozemo iskoristiti std::inner_product funkciju
10+
// Operacije * i + cemo zameniti sa <= i &&
11+
// Dakle, za kolekcije xs i ys, racunamo:
12+
// (x1 <= y1) && (x2 <= y2) && ...
13+
// Mi zapravo poredimo kolekciju samu sa sobom
14+
// pa ce ys biti isto xs samo bez prvog elementa
15+
// Pocetna vrednost (akumulator) je true
16+
template <typename T>
17+
bool is_sorted(const T& xs)
18+
{
19+
return std::inner_product(
20+
std::cbegin(xs), std::cend(xs) - 1,
21+
std::cbegin(xs) + 1,
22+
true,
23+
// Mozemo koristiti auto takodje
24+
[](const bool &x, const bool &y) { return x && y; },
25+
// Ovde ne znamo tip elemenata kolekcije, pa stoga auto
26+
[](const auto &x, const auto &y) { return x <= y; }
27+
);
28+
}
29+
30+
int main(int argc, char *argv[])
31+
{
32+
const std::string text = "Hooloovoo";
33+
std::cerr << text << ": " << is_sorted(text) << std::endl;
34+
35+
const std::string word = "Almost";
36+
std::cerr << word << ": " << is_sorted(word) << std::endl;
37+
38+
const std::vector<int> numbers{ 1, 2, 3, 3, 4, 5, 6, 6, 7 };
39+
std::cerr << "numbers: " << is_sorted(word) << std::endl;
40+
41+
return 0;
42+
}

10-cpp-range/03_for_each/Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
PROGRAM=04_for_each.out
2+
CXX = g++
3+
CXXFLAGS = -g -std=c++14 -Wall -I../range-v3-0.11.0/include
4+
5+
$(PROGRAM): main.o
6+
$(CXX) -o $(PROGRAM) main.o
7+
8+
.PHONY: clean dist
9+
10+
clean:
11+
-rm *.o $(PROGRAM) *core
12+
13+
dist: clean
14+
-tar -chvj -C .. -f ../$(PROGRAM).tar.bz2 $(PROGRAM)
15+
16+

10-cpp-range/03_for_each/main.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <functional>
4+
#include <range/v3/view.hpp>
5+
6+
using namespace ranges::v3;
7+
using namespace std::placeholders;
8+
9+
int main(int argc, char *argv[])
10+
{
11+
std::vector<int> xs = { -1, -3, -5, 1, 3, 5};
12+
13+
// Pravimo pogled koji ce se sastojati od tekstualnih
14+
// reprezentacija apsolutnih vrednosti brojeva iz xs,
15+
// ali samo onih za koje vazi da je x < 6
16+
auto results =
17+
xs | view::transform(abs)
18+
| view::filter(std::bind(std::less<>(), _1, 6))
19+
| view::transform([] (auto value) { return std::to_string(value); });
20+
21+
for (auto value: results) {
22+
std::cout << value << std::endl;
23+
}
24+
25+
return 0;
26+
}

10-cpp-range/04_filtered_sum/Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
PROGRAM=03_filtered_sum.out
2+
CXX = g++
3+
CXXFLAGS = -g -std=c++14 -Wall -I../range-v3-0.11.0/include
4+
5+
$(PROGRAM): main.o
6+
$(CXX) -o $(PROGRAM) main.o
7+
8+
.PHONY: clean dist
9+
10+
clean:
11+
-rm *.o $(PROGRAM) *core
12+
13+
dist: clean
14+
-tar -chvj -C .. -f ../$(PROGRAM).tar.bz2 $(PROGRAM)
15+
16+

10-cpp-range/04_filtered_sum/main.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <range/v3/numeric/accumulate.hpp>
4+
#include <range/v3/view.hpp>
5+
6+
using namespace ranges::v3;
7+
8+
template <typename Predicate, typename Collection, typename Accumulator>
9+
Accumulator filtered_sum(Predicate predicate, Accumulator init, const Collection &xs)
10+
{
11+
return accumulate(xs | view::filter(predicate), init);
12+
}
13+
14+
int main(int argc, char *argv[])
15+
{
16+
std::vector<int> xs = { -1, -3, -5, 1, 3, 5};
17+
18+
std::cout << filtered_sum(
19+
[] (auto val) { return val > 0; },
20+
0,
21+
xs)
22+
<< std::endl;
23+
24+
return 0;
25+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
PROGRAM=05_filter_by_index.out
2+
CXX = g++
3+
CXXFLAGS = -g -std=c++1z -Wall -I../range-v3-0.11.0/include
4+
5+
$(PROGRAM): main.o
6+
$(CXX) -o $(PROGRAM) main.o
7+
8+
.PHONY: clean dist
9+
10+
clean:
11+
-rm *.o $(PROGRAM) *core
12+
13+
dist: clean
14+
-tar -chvj -C .. -f ../$(PROGRAM).tar.bz2 $(PROGRAM)
15+
16+
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <functional>
4+
#include <range/v3/view.hpp>
5+
6+
using namespace ranges::v3;
7+
8+
bool index_filter(size_t index) {
9+
return index % 3 != 0;
10+
}
11+
12+
int main(int argc, char *argv[])
13+
{
14+
std::vector<int> xs = { -1, -3, -5, 1, 3, 5};
15+
16+
// Zipujemo xs sa listom svih prirodnih brojeva,
17+
// filtriramo po indeksu, posle toga zaboravljamo indeks.
18+
auto results = view::zip(xs, view::ints(1, unreachable))
19+
| view::filter([] (auto value) { return index_filter(value.second); })
20+
| view::transform([] (auto value) { return value.first; });
21+
22+
for (auto value: results)
23+
std::cout << value << std::endl;
24+
25+
return 0;
26+
}

10-cpp-range/06_word_count/Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
PROGRAM=06_word_count.out
2+
CXX = g++
3+
CXXFLAGS = -g -std=c++1z -Wall -I../range-v3-0.11.0/include
4+
5+
$(PROGRAM): main.o
6+
$(CXX) -o $(PROGRAM) main.o
7+
8+
.PHONY: clean dist
9+
10+
clean:
11+
-rm *.o $(PROGRAM) *core
12+
13+
dist: clean
14+
-tar -chvj -C .. -f ../$(PROGRAM).tar.bz2 $(PROGRAM)
15+
16+

10-cpp-range/06_word_count/hare.txt

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
This is the story of the hare who lost his spectacles.
2+
3+
Owl loved to rest quietly whilst no one was watching.
4+
Sitting on a fence one day,
5+
He was surprised when suddenly a kangaroo ran close by.
6+
Now this may not seem strange, but when Owl overheard Kangaroo whisper to no one in
7+
Particular,
8+
"The hare has lost his spectacles," well, he began to wonder.
9+
Presently, the moon appeared from behind a cloud and there, lying on the grass was hare.
10+
In the stream that flowed by the grass a newt.
11+
And sitting astride a twig of a bush a bee. Ostensibly motionless, the hare was trembling with
12+
Excitement, for without his spectacles he was completely helpless.
13+
Where were his spectacles?
14+
Could someone have stolen them?
15+
Had he mislaid them?
16+
What was he to do?
17+
Bee wanted to help, and thinking he had the answer began:
18+
"You probably ate them thinking they were a carrot."
19+
"No!" interrupted Owl, who was wise.
20+
"I have good eye-sight, insight, and foresight. How could an intelligent hare make such a silly mistake?"
21+
But all this time, Owl had been sitting on the fence, scowling!
22+
Kangaroo were hopping mad at this sort of talk.
23+
She thought herself far superior in intelligence to the others.
24+
She was their leader, their guru.
25+
She had the answer: "Hare, you must go in search of the optician."
26+
But then she realized that Hare was completely helpless without his spectacles.
27+
And so, Kangaroo loudly proclaimed, "I can't send Hare in search of anything!"
28+
"You can guru, you can!" shouted Newt.
29+
"You can send him with Owl."
30+
But Owl had gone to sleep.
31+
Newt knew too much to be stopped by so small a problem
32+
"You can take him in your pouch."
33+
But alas, Hare was much too big to fit into
34+
Kangaroo's pouch.
35+
All this time, it had been quite plain to hare that the others knew nothing about spectacles.
36+
37+
As for all their tempting ideas, well Hare didn't care.
38+
The lost spectacles were his own affair.
39+
And after all, Hare did have a spare a-pair. A-pair.

10-cpp-range/06_word_count/main.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <functional>
4+
5+
#include <range/v3/view.hpp>
6+
#include <range/v3/action.hpp>
7+
#include <range/v3/range/conversion.hpp>
8+
#include <range/v3/view/istream.hpp>
9+
10+
using namespace ranges::v3;
11+
12+
std::string string_to_lower(const std::string &s) {
13+
return s | view::transform(tolower) | to<std::string>();
14+
}
15+
16+
std::string string_only_alnum(const std::string &s) {
17+
return s | view::filter(isalnum) | to<std::string>();
18+
}
19+
20+
int main(int argc, char *argv[])
21+
{
22+
const int n = argc <= 1
23+
? 10
24+
: atoi(argv[1]);
25+
26+
const auto words =
27+
// Kolekcija reci
28+
istream_range<std::string>(std::cin)
29+
30+
// Ne zelimo da imamo velika slova
31+
| view::transform(string_to_lower)
32+
33+
// Ne zelimo da imamo bilo sta osim slova i cifara
34+
| view::transform(string_only_alnum)
35+
36+
// Uklanjamo prazne stringove iz kolekcije
37+
| view::remove_if(&std::string::empty)
38+
39+
// Da bismo mogli da sortiramo, potrebna nam je
40+
// random-access kolekcija
41+
| to_vector
42+
| action::sort;
43+
44+
45+
const auto results =
46+
words
47+
48+
// Grupisemo iste reci
49+
| view::group_by(std::equal_to<>())
50+
51+
// Potrebna nam je rec, i koliko puta se ponovila
52+
| view::transform([] (const auto &group) {
53+
const auto begin = std::begin(group);
54+
const auto end = std::end(group);
55+
const int size = distance(begin, end);
56+
const std::string word = *begin;
57+
58+
return std::make_pair(size, word);
59+
})
60+
61+
// Sortiramo rezultat
62+
| to_vector | action::sort;
63+
64+
65+
for (auto value: results | view::reverse // Sortirano je u rastucem poretku, obrcemo
66+
| view::take(n) // Uzimamo prvih `n` rezultata
67+
) {
68+
std::cout << value.first << " " << value.second << std::endl;
69+
}
70+
71+
return 0;
72+
}

10-cpp-range/06_word_count/main.plist

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>clang_version</key>
6+
<string>clang version 5.0.0 (trunk 304107)</string>
7+
<key>files</key>
8+
<array>
9+
</array>
10+
<key>diagnostics</key>
11+
<array>
12+
</array>
13+
</dict>
14+
</plist>

0 commit comments

Comments
 (0)