diff --git a/Practicum/1_Week_Solutions.cpp b/Practicum/1_Week_Solutions.cpp deleted file mode 100644 index 8b13789..0000000 --- a/Practicum/1_Week_Solutions.cpp +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Practicum/7_8_9_Week_Solutions.cpp b/Practicum/7_8_9_Week_Solutions.cpp new file mode 100644 index 0000000..6d83f5f --- /dev/null +++ b/Practicum/7_8_9_Week_Solutions.cpp @@ -0,0 +1,817 @@ +WEEK 7 +--------------------------------------------------------------------------------------------------------------------------------------------------------------- +//zad 1 +int pow(int base, int stepen) { + int num = 1; + for (int i = 0; i < stepen; i++) { + num *= base; + } + return num; +} +int getValuefromChar(char e) { + int num = 0; + if (e >= '0' && e <= '9') { + return e - '0'; + } + else if(e >= 'A' && e <= 'Z') { + return e - 'A' + 10; + } + else { + return -1; + } +} +char getValuefromInt(int e) { + if (e >= 0 && e <= 9) { + return e + '0'; + } + else if(e >= 9 && e <= 35) { + return e + 'A' - 10; + } + else { + return -1; + } +} +void from_char_to_int(char c[], int a[], int n) { + for (int i = 0; i < n; i++) { + a[i] = getValuefromChar(c[i]); + } +} +void new_array_con(int a[], int n, int k) { + int i = n - 1; + while (a[i] >= k - 1) { + a[i] = 0; + i--; + } + a[i]++; + +} +void from_int_to_char(char c[], int a[], int n) { + for (int i = 0; i < n; i++) { + c[i] = a[i] + '0'; + } +} + +void increment_char_array(char array[], int array_int[], int size, int k, char result[]) { + from_char_to_int(array, array_int, size); + new_array_con(array_int, size, k); + + from_int_to_char(result, array_int, size); +} +//zad 2 +void char_to_int(char a[], int size, int b[]) { + for(int i=0; i=0; i--) + { int sum = a[i] + b[i]; + if(edno_naum) { + sum++; + } + if(sum >= k) { + a[i] = sum - k; + edno_naum = true; + } + else { + a[i] = sum; + edno_naum = false; + } + + } + int_to_char(res, size, a); + print(size, res); + +} +//zad3-bonus-substraction +void substraction_of_chars(char ac[], char bc[], int size, int k, int a[], int b[], char res[]) { + char_to_int(ac, size, a); + char_to_int(bc, size, b); + int i = size - 1; + bool edno_naum_minus = false; + for(int i = size - 1; i>=0; i--) + { int substraction = a[i] - b[i]; + if(edno_naum_minus) { + substraction--; + } + if(substraction < 0) { + a[i] = substraction + k; + edno_naum_minus = true; + } + else { + a[i] = substraction; + edno_naum_minus = false; + } + + } + int_to_char(res, size, a); + print(size, res); + +} +//zad 4 +bool is_array_palindrome(char a[], char b[], int& len) { + + for(int i=0; i> index; + return number & 1; +} +int change_number_prim(int number, int idx, bool bit) { + if (checkbit(number, idx) != bit) { + return flip_bit(number, idx); + } + else { + return number; + } +} +//zad1 +int len_num(int a){ + if(a == 0){ + return 1; + } + int count = 0; + while(a != 0){ + a = a>>1; + count++; + + } + return count; + +} +bool checkbit(int number, int idx){ + int mask = 1 << idx; + return mask & number; +} +int slice_end(int a, int len_b){ + int res = 0 ; + for(int i=len_b-1; i>=0;i--){ + res += checkbit(a,i); + if(i == 0)break; + res = res << 1; + } + return res; +} +//zad 2 +bool checkbit(int number, int idx){ + int mask = 1 << idx; + return mask & number; +} +int ones_count(int n){ + int count_ones = 0; + int idx = 0; + int n1 = n; + while(n1){ + if(checkbit(n, idx) == 1) { + count_ones++; + } + idx++; + n1 = n1 >> 1; + } + return count_ones; +} + +//zad 3 +int last_numbers(int number, int k){ + int mask = 0; + for(int i=0;i> 1; + ar[0] = nulls_count; + ar[1] = ones_count; +}} + +bool all_bits(int num1, int num2, int n1[], int n2[]){ + count_bits(num1, n1); + count_bits(num2, n2); + + for(int i=0;i<2;i++){ + if(n1[i] > n2[i]){ + return 0;} + } + return 1; +} + + + +//BONUS ZADACHI +//1; +int getMissing(const int arr[], size_t size) +{ + int result = 0; + for(unsigned i = 0; i < size; i++) + result ^= arr[i]; + return result; +} +//2 +bool checkbit(int number, int idx){ + int mask = 1 << idx; + return mask & number; +} +int ones_count(int n){ + int count_ones = 0; + int idx = 0; + int n1 = n; + while(n1){ + if(checkbit(n, idx) == 1) { + count_ones++; + } + idx++; + n1 = n1 >> 1; + } + return count_ones; +} + +void all_digits(int n, int array[], int size){ + std::cout<<"["; + for(int idx = size - 1;idx>=0;idx--){ + + if(checkbit(n, idx))std::cout<= 'a' && (*n) <= 'z') { + *n = *n - 'a' + 'A'; + } + else if ((*n) >= 'A' && (*n) <= 'Z') { + *n = *n + 'a' - 'A'; + } + n++; + } +} +//zad4 +int char_to_int(char el) { + return el - '0'; +} +void digits_ar(int array[], char c[],int size){ + for(int i=0;i array[j]) { + min_idx = j; + } + } + if (min_idx != i) { + swap(array[min_idx], array[i]); + } + } +} + +void print_counts(int array[], int size, char ar[]){ + digits_ar(array,ar,size); + SelectionSort(array, size); + int count = 1; + for (int i = 0;i < size-1;i++) { + if (array[i] != array[i + 1]||i==size-2){ + if (i == size - 2) { + count++; + } + std::cout << count << "x" << array[i] << ' '; + count = 0; + } + count++; + + } + if (array[size - 1] != array[size - 2]) { + std::cout << 1 << "x" << array[size - 1] << " "; + } + + +} +//zad5 +char toUpper(char el) { + return el - 'a' + 'A'; +} +void CapitalLetter(char n[]){ + if (!n) { + return; + } + int i = 1; + n[0]= n[0]- 'a' + 'A'; + while (n[i] != '\0'){ + if (n[i] != ' ' && n[i - 1] == ' ') { + n[i] = toUpper(n[i]); + } + i++; + + } +} + +//zad6 +void reversed(char ar[], int size) { + //int len = str_len(ar); + for (int i = 0;i < size / 2;i++) { + + swap_one(ar[i], ar[size - 1 - i]); + } +} +//zad7 +int count_words_prim(char a[]){ + int len = str_len(a); + int count = 0; + for (int i = 0;i < len - 1;i++) { + if (!is_letter(a[i])) { + if (is_letter(a[i + 1])) { + count++; + } + } + } + return count; + +} + +//zad 8 + +void word(char text[], char w[], int start, int end) { + int j = 0; + for (int i = start; i <= end;i++) { + w[j++] = text[i]; + } + + w[j] = '\0'; + +} +bool same_prim(char text[], char word[], int i, int len) { + + for (int j = 0;j < len;j++) { + if (text[i + j] != word[j]) { + return 0; + } + } + return 1; +} +bool is_letter(char e) { + if ((e >= 'a' && e <= 'z') || (e >= 'A' && e <= 'Z')) { + return 1; + } + return 0; +} +unsigned str_len(char* n) { + int count = 0; + while ((*n) != '\0') { + n++; + count++; + } + return count; +} +void most_frequent_word(char text[]) { + int len_text = str_len(text); + int start_indexes[100], end_indexes[100]; + int words[1024]; + int len = 0; + int count_words = 0; + int count_words_prim = 0; + int start_idx = 0; + int end_idx = 0; + int counts[100]; + char w[100]; + bool is_started = false; + for (int i = 0;i < len_text;i++) { + if ((is_letter(text[i]) && i == 0) || ((!is_letter(text[i-1])) && is_letter(text[i]))) { + //start_idx = i; + start_indexes[count_words++] = i; + is_started = true; + + } + if ((is_started)&&(is_letter(text[i]) && i == len_text-1) || ((is_letter(text[i-1])) && (!is_letter(text[i])))) { + + end_indexes[count_words_prim++] = i-1; + is_started = false; + } + + + } + + int max = 0; + int max_idx = 0; + for(int i=0;i ascii_values[i] && (ascii_values[i] != 0)){ + min = ascii_values[i]; + min_idx = i; + + } + } + for(int i=0;i ascii_values[i]){ + min = ascii_values[i]; + min_idx = i; + + } + } + for(int i=0;i= 'A' && text[i] <= 'Z'){ + text[i] = text[i] - 'A' + 'a'; + } + } + +}