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
98 changes: 98 additions & 0 deletions src/rewrite/task1/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <stdio.h>
#include <stdlib.h>

void merge(int* arr, int left, int middle, int right)
{
int i, j, k;
int n = middle - left + 1;
int m = right - middle;
int* leftArr = malloc(n * sizeof(int));
int* rightArr = malloc(m * sizeof(int));

for (i = 0; i < n; i++)
leftArr[i] = arr[left + i];

for (j = 0; j < m; j++)
rightArr[j] = arr[middle + 1 + j];

i = 0;
j = 0;
k = left;

while (i < n && j < m) {
if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i++;
}
else {
arr[k] = rightArr[j];
j++;
}
k++;
}

while (i < n) {
arr[k] = leftArr[i];
i++;
k++;
}

while (j < m) {
arr[k] = rightArr[j];
j++;
k++;
}

free(leftArr);
free(rightArr);
}

void mergeSort(int* arr, int left, int right)
{
if (left < right) {
int middle = left + (right - left) / 2;
mergeSort(arr, left, middle);
mergeSort(arr, middle + 1, right);
merge(arr, left, middle, right);
}
}


int* removeDuplicates(int* arr, int* arraySize)
{
if (*arraySize <= 0)
return arr;

int j = 0;
for (int i = 0; i < *arraySize - 1; i++) {
if (arr[i] != arr[i + 1])
arr[j++] = arr[i];
}
arr[j++] = arr[*arraySize - 1];

*arraySize = j;
return realloc(arr, *arraySize * sizeof(int));
}

int main(void)
{
int* array = malloc(1 * sizeof(int));
int number = 999;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Довольно странный инициализатор, но ладно

int count = 0;

while (number != 0) {
scanf("%d", &number);
count++;
array = realloc(array, count * sizeof(int));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А почистить память?

array[count - 1] = number;
}

mergeSort(array, 0, count - 1);
array = removeDuplicates(array, &count);
Comment on lines +90 to +91
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А от Вас просили другого?


for (int i = 1; i < count; i++)
printf("%d ", array[i]);

printf("\n");
return 0;
}
58 changes: 58 additions & 0 deletions src/rewrite/task2/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <stdio.h>
#include <stdbool.h>

long int minimizeNumber(long int n)
{
if (n <= 0) {
return 0;
}
Comment on lines +6 to +8
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А почему бы не использовать беззнаковый тип?


int cnt[10] = {0};

while (n > 0) {
int d = n % 10;
cnt[d]++;
n /= 10;
}

long int result = 0;

for (int d = 1; d <= 9; d++) {
if (cnt[d] > 0) {
result = d;
cnt[d]--;
break;
}
}
Comment on lines +20 to +26
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ну, нет, всё-таки нули стоило откинуть


for (int d = 0; d <= 9; d++) {
while (cnt[d] > 0) {
result = result * 10 + d;
cnt[d]--;
}
}

return result;
}

bool testZero()
{
return minimizeNumber(0) == 0;
}

bool testWithZero()
{
return minimizeNumber(1001) == 1001;
}

bool testAllDigits()
{
return minimizeNumber(8967450123L) == 1023456789L;
}

int main(void)
{
if (!testZero() || !testWithZero() || !testAllDigits())
return 1;
return 0;
}
49 changes: 49 additions & 0 deletions src/rewrite/task3/list.c
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тестов нет

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <stdlib.h>

typedef struct listItem {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

По стайлгайду

Suggested change
typedef struct listItem {
typedef struct ListItem {

struct listItem* before;
int value;
int index;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

И по-моему без окончания строки оно не скомпилируется


listItem create()
{
listItem* item = (*listItem)malloc(1 * sizeif(listItem));
item->value = 0;
item->before = NULL;
item->index = 0;
return item;
}


listItem* insert(listItem* list, newValue* list, int where)
{
if where > list->index {
listItem* correctItem;
correctItem->index = list->index + 1
correctItem->before = list
} else {
listItem cursor = *list;
while (cursor->index != where)
cursor = cursor->before;
listItem* correctItem = *cursor;

correctItem->value = *newValue;
}
}
Comment on lines +32 to +33
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь скобочек многовато


listItem* revert(listItem* list)
{
int lenght = list->index + 1;
listItem after = NULL;

while (list->index != 0) {
list->after = before;
list->index = lenght - list->index;
after = list;
list = list->before;
}

return list;
}