-
Notifications
You must be signed in to change notification settings - Fork 33
Дудин Лев #30
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
base: main
Are you sure you want to change the base?
Дудин Лев #30
Changes from all commits
cef79c3
0a567ad
44f90db
f3e8951
3b68e1b
c30f3e9
6dd37f4
d47d7b7
bfa5f0b
fe31e67
28298fb
dc86645
668c74f
aeb5b2c
194a5a7
0c32ccb
ac27bb0
50876c1
2924be5
83f113d
31b4cd6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,16 @@ | ||
| #include <stdexcept> | ||
|
|
||
| double ApplyOperations(double a, double b, double (*func[])(double x, double y), size_t size) | ||
| { | ||
| long double sum = 0.; | ||
| if (func) | ||
| { | ||
| for (size_t i = 0; i < size; ++i) | ||
| { | ||
| if (*(func+i)) | ||
| sum += func[i](a, b); | ||
| } | ||
| } | ||
|
|
||
| double ApplyOperations(double a, double b /* other arguments */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| return sum; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,17 @@ | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| /* return_type */ FindLastElement(/* ptr_type */ begin, /* ptr_type */ end, /* func_type */ predicate) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| const int* FindLastElement(const int* begin, const int* end, bool predicate(int x)) | ||
| { | ||
| if (predicate && begin && end) | ||
| { | ||
| size_t size = end - begin; | ||
| for (long i = size-1; i>=0; --i) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не хватает пробелов вокрыг |
||
| { | ||
| if (predicate(begin[i])) | ||
| { | ||
| return (begin + i); | ||
| } | ||
| } | ||
| } | ||
| return end; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,70 @@ | ||
| #include <stdexcept> | ||
| #include <iostream> | ||
| #include <string> | ||
|
|
||
|
|
||
| void PrintMemory(int /* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| void PrintMemory(int num, bool is_inverted = false) | ||
| { | ||
|
|
||
| int size = sizeof(int); | ||
| unsigned char *conv = reinterpret_cast<unsigned char *>(&num); // указатель на конкретный бит в инте | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| std::string str; // строка, которую мы выведем | ||
|
|
||
| char dict[] = {"0123456789ABCDEF"}; // массив символов, в которые мы будем переводить | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. выглядит так будто мы решили задействовать много лишней памяти, для dict и для str |
||
|
|
||
| if (is_inverted) //big endian | ||
| { | ||
| conv += size-1; | ||
| for (; conv >= reinterpret_cast<unsigned char *>(&num); --conv) | ||
| { | ||
| str.push_back(dict[*conv >> 4]); | ||
| str.push_back(dict[*conv & 0b00001111]); | ||
| } | ||
| } | ||
|
|
||
| else // little endian | ||
| { | ||
| for (; conv - reinterpret_cast<unsigned char *>(&num) < size; ++conv) | ||
| { | ||
| str.push_back(dict[*conv >> 4]); | ||
| str.push_back(dict[*conv & 0b00001111]); | ||
| } | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. действия в цикле одинаковые, как будто можно было сделать сильно меньше 15 строк кода на данные действия, если переписать |
||
| std::cout << "0x" << str << std::endl; | ||
|
|
||
|
|
||
| } | ||
|
|
||
| void PrintMemory(double /* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| void PrintMemory(double num, bool is_inverted = false) | ||
| { | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лишняя пустая строка |
||
| int size = sizeof(double); | ||
| unsigned char *conv = reinterpret_cast<unsigned char *>(&num); // указатель на конкретный бит в дабле | ||
| std::string str;// строка, которую мы выведем | ||
|
|
||
| char dict[] = {"0123456789ABCDEF"}; // массив символов, в которые мы будем переводить | ||
|
|
||
| if (is_inverted) //big endian | ||
| { | ||
| conv += size-1; | ||
| for (; conv >= reinterpret_cast<unsigned char *>(&num); --conv) | ||
| { | ||
| str.push_back(dict[*conv >> 4]); | ||
| str.push_back(dict[*conv & 0b00001111]); | ||
| } | ||
| } | ||
|
|
||
| else // little endian | ||
| { | ||
| for (; conv - reinterpret_cast<unsigned char *>(&num) < size; ++conv) | ||
| { | ||
| str.push_back(dict[*conv >> 4]); | ||
| str.push_back(dict[*conv & 0b00001111]); | ||
| } | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. решение аналогичное и дублирует код предыдущей функции. Правильнее было общую часть вынести в третью функцию и после получения указателя на unsigned char*, зная sizeof от типа вызывать эту функцию с общим кодом из фукнции с double и с int |
||
|
|
||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лишние строки пустые |
||
| std::cout << "0x" << str << std::endl; | ||
|
|
||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лишние строки пустые |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,55 @@ | ||
| #include <stdexcept> | ||
|
|
||
| char *FindLongestSubsequence(char *begin, char *end, size_t &count) | ||
| { | ||
| count = 0; | ||
| if (begin && end) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это должна быть проверка на особые условия и выход по короткой ветви с nullptr |
||
| { | ||
|
|
||
| /* return_type */ FindLongestSubsequence(/* ptr_type */ begin, /* ptr_type */ end, /* type */ count) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| char *subsequence = nullptr; | ||
| for (long i = 0; i < end - begin; ++i) | ||
| { | ||
| char *newsequence = begin + i; | ||
| size_t t = 1; | ||
| while (*newsequence == *(newsequence + t) && newsequence + t < end) | ||
| { | ||
| ++i; | ||
| ++t; | ||
| } | ||
| if (t > count) | ||
| { | ||
| count = t; | ||
| subsequence = newsequence; | ||
| } | ||
| } | ||
| return subsequence; | ||
| } | ||
| return nullptr; | ||
| } | ||
|
|
||
| const char *FindLongestSubsequence(const char *begin, const char *end, size_t &count) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. полное дублирование кода в двух функциях, хотя используя const_cast можно вызывать данную функцию из другой |
||
| { | ||
| count = 0; | ||
| size_t pos = 0; | ||
| if (begin && end) | ||
| { | ||
|
|
||
| for (long i = 0; i < end - begin; ++i) | ||
| { | ||
| size_t t = 1; | ||
| const char *newsubsequence = begin + i; | ||
| while (*newsubsequence == *(newsubsequence + t) && i < end - begin - 1) | ||
| { | ||
| ++i; | ||
| ++t; | ||
| } | ||
| if (t > count) | ||
| { | ||
| count = t; | ||
| pos = i - t + 1; | ||
| } | ||
| } | ||
| return begin + pos; | ||
| } | ||
| return nullptr; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,51 @@ | ||
| #include <stdexcept> | ||
| #include <iostream> | ||
| #include <cmath> | ||
|
|
||
| void PrintArray(std::nullptr_t, std::nullptr_t) | ||
| { | ||
| std::cout<<"[]\n"; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| template<typename T> | ||
| void PrintArray(T* begin, T* end, size_t limiter = 0) | ||
| { | ||
| if (begin && end && begin != end) | ||
| { | ||
| std::cout << '['; | ||
|
|
||
| size_t len = abs(begin-end); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. отсутствуют пробелы |
||
| if (begin < end) | ||
| { | ||
| for (size_t i = 0; i < len; ++i) | ||
| { | ||
| if (limiter && i != 0 && !(i % limiter)) | ||
| std::cout << "...\n" << ' '; | ||
|
|
||
| i!=(len-1)? ( std::cout << begin[i] << ", "): ( std::cout << begin[i] << "]\n"); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. отсутствуют пробелы с двух сторон от операторов, лишние пробелы после ( |
||
|
|
||
| } | ||
|
|
||
| } | ||
| else | ||
| { | ||
| for (size_t i = 0; i < len; ++i) | ||
| { | ||
| if (limiter && i != 0 && !(i % limiter)) | ||
| std::cout << "...\n" << ' '; | ||
| T *current = begin - i; | ||
| current == end + 1 ? (std::cout << *current) : (std::cout << *current << ", "); | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. дублирование кода,можно было записать компактнее |
||
| std::cout<<"]\n"; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. отсутствуют пробелы вокруг оператора |
||
| } | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. миллион лишних строк |
||
| else | ||
| { | ||
| std::cout<<"[]\n"; | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. опять зачем-то лишний уровень вложенности и короткое условие идет после основного |
||
| } | ||
|
|
||
|
|
||
| void PrintArray(/* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,34 @@ | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| void SwapPtr(/* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
|
|
||
|
|
||
| void SwapPtr(int*& a, int*& b) | ||
| { | ||
| int* c = a; | ||
| a = b; | ||
| b = c; | ||
| } | ||
|
|
||
| void SwapPtr(const int*& a, const int*& b){ | ||
| const int* c = a; | ||
| a = b; | ||
| b = c; | ||
| } | ||
|
|
||
| void SwapPtr(int**& a, int**& b) | ||
| { | ||
| int** c = a; | ||
| a = b; | ||
| b = c; | ||
| } | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лишние функции в решении, можно было обойтись без такого количества функций и без шаблонов |
||
|
|
||
| /* рабочий вариант через template для любого типа данных. на лекции не проходили, просто показать что я нашел и такую штуку | ||
| template<typename T> | ||
| void SwapPtr(T*& a, T*& b){ | ||
| T* c = a; | ||
| a = b; | ||
| b = c; | ||
| } | ||
| */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,38 @@ | ||
| #include <stdexcept> | ||
|
|
||
| #include <vector> | ||
| #include <math.h> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| struct DataStats { | ||
| double avg = 0.0; | ||
| double sd = 0.0; | ||
| }; | ||
|
|
||
| /* return_type */ CalculateDataStats(/* args */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| struct DataStats CalculateDataStats(std::vector<int> data) { | ||
|
|
||
| struct DataStats output; | ||
| if (data.empty()){ | ||
| output.avg = 0.; | ||
| output.sd = 0.; | ||
| return output; | ||
| } | ||
|
|
||
| /* | ||
| стандартная девиация считается как sqrt(сумма((i - avg)^2)/size) | ||
| если разложим квадратное уравнение: sqrt(сумма(i^2 - 2*i*avg + avg^2)/size)=> | ||
| sqrt((сумма(i^2)+ сумма(- 2* i*avg)+сумма(avg^2))/size) - его имплементируем | ||
| */ | ||
|
|
||
| double first_num = 0.; | ||
| double second_num= 0.; | ||
|
|
||
| for (size_t i=0; i< data.size(); ++i){ | ||
| output.avg+=data[i]; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. пробелы |
||
| first_num += static_cast<double>(data[i])*static_cast<double>(data[i]); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лишний каст, достаточно одного каста |
||
| second_num+= data[i]; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не все пробелы |
||
| } | ||
| output.avg /= data.size(); | ||
| output.sd = first_num - 2. * second_num * output.avg + output.avg * output.avg * data.size(); | ||
| output.sd /= data.size(); | ||
| output.sd = sqrt(output.sd); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. для библиотек C++ используем |
||
| return output; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,49 @@ | ||
| #include <stdexcept> | ||
| #include <tuple> | ||
|
|
||
|
|
||
| struct Date { | ||
| struct Date | ||
| { | ||
| unsigned year; | ||
| unsigned month; | ||
| unsigned day; | ||
| }; | ||
|
|
||
| struct StudentInfo { | ||
| struct StudentInfo | ||
| { | ||
| size_t id; | ||
| char mark; | ||
| int score; | ||
| unsigned course; | ||
| Date birth_date; | ||
| }; | ||
| }; | ||
|
|
||
| bool operator<(const Date &s1, const Date &s2) | ||
| { | ||
| return std::tie(s1.year, s1.month, s1.day) < std::tie(s2.year, s2.month, s2.day); | ||
| } | ||
|
|
||
| bool operator==(const Date &s1, const Date &s2) | ||
| { | ||
| return std::tie(s1.year, s1.month, s1.day) == std::tie(s2.year, s2.month, s2.day); | ||
| } | ||
|
|
||
| bool operator==(const StudentInfo &s1, const StudentInfo &s2) | ||
| { | ||
| return std::tie(s1.mark, s1.score) == std::tie(s2.mark, s2.score); | ||
| } | ||
|
|
||
| bool operator<(const StudentInfo &s1, const StudentInfo &s2) | ||
| { | ||
| return std::tie(s2.mark, s1.score, s2.course, s1.birth_date) < | ||
| std::tie(s1.mark, s2.score, s1.course, s2.birth_date); | ||
| } | ||
|
|
||
| bool operator!=(const StudentInfo &s1, const StudentInfo &s2) { return !(s1 == s2); } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а что тут стиль кода сломался, чтобы было на новой строке? |
||
| bool operator>(const StudentInfo &s1, const StudentInfo &s2) { return s2 < s1; } | ||
| bool operator<=(const StudentInfo &s1, const StudentInfo &s2) { return !(s2 < s1); } | ||
| bool operator>=(const StudentInfo &s1, const StudentInfo &s2) { return !(s1 < s2); } | ||
|
|
||
| bool operator!=(const Date &d1, const Date &d2) { return !(d1 == d2); } | ||
| bool operator>(const Date &d1, const Date &d2) { return d2 < d1; } | ||
| bool operator<=(const Date &d1, const Date &d2) { return !(d2 < d1); } | ||
| bool operator>=(const Date &d1, const Date &d2) { return !(d1 < d2); } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лучше написать короткую ветвь, что делать делать если данные величины nullptr.
Что уберет уровень вложенности для основного решения