diff --git a/OS/Task1.sh~ b/OS/Task1.sh~ new file mode 100644 index 0000000..f61fc4e --- /dev/null +++ b/OS/Task1.sh~ @@ -0,0 +1,32 @@ +#!/bin/bash +number=$RANDOM +a="a" +b="b" +c="c" +txt=".txt" +one="1" + + +echo $number +echo $(($number % 3)) +for i in `seq 1 100`; + do + if [ "$(($number % 3))" = 0 ]; then + number=$RANDOM + the_file=$a$txt + echo $a > $the_file + a=$a$one + else + if [ "$(($number % 3))" = 1 ]; then + number=$RANDOM + the_file=$b$txt + echo $b > $the_file + b=$b$one + else + number=$RANDOM + the_file=$c$txt + echo $c > $the_file + c=$c$one + fi + fi + done diff --git a/OS/Task1/Search.txt b/OS/Task1/Search.txt new file mode 100644 index 0000000..536377b --- /dev/null +++ b/OS/Task1/Search.txt @@ -0,0 +1,5 @@ +100 Thomas Manager Sales $5,000 +200 Jason Developer Technology $5,500 +300 Raj Sysadmin Technology $7,000 +400 Nisha Manager Marketing $9,500 +500 Randy Manager Sales $6,000 diff --git a/OS/Task1/Search.txt~ b/OS/Task1/Search.txt~ new file mode 100644 index 0000000..536377b --- /dev/null +++ b/OS/Task1/Search.txt~ @@ -0,0 +1,5 @@ +100 Thomas Manager Sales $5,000 +200 Jason Developer Technology $5,500 +300 Raj Sysadmin Technology $7,000 +400 Nisha Manager Marketing $9,500 +500 Randy Manager Sales $6,000 diff --git a/OS/Task1/Task1a.sh b/OS/Task1/Task1a.sh new file mode 100644 index 0000000..f61fc4e --- /dev/null +++ b/OS/Task1/Task1a.sh @@ -0,0 +1,32 @@ +#!/bin/bash +number=$RANDOM +a="a" +b="b" +c="c" +txt=".txt" +one="1" + + +echo $number +echo $(($number % 3)) +for i in `seq 1 100`; + do + if [ "$(($number % 3))" = 0 ]; then + number=$RANDOM + the_file=$a$txt + echo $a > $the_file + a=$a$one + else + if [ "$(($number % 3))" = 1 ]; then + number=$RANDOM + the_file=$b$txt + echo $b > $the_file + b=$b$one + else + number=$RANDOM + the_file=$c$txt + echo $c > $the_file + c=$c$one + fi + fi + done diff --git a/OS/Task1/Task1b.sh b/OS/Task1/Task1b.sh new file mode 100644 index 0000000..45d22d3 --- /dev/null +++ b/OS/Task1/Task1b.sh @@ -0,0 +1 @@ +rm a* diff --git a/OS/Task1/Task1c.sh b/OS/Task1/Task1c.sh new file mode 100644 index 0000000..08f890a --- /dev/null +++ b/OS/Task1/Task1c.sh @@ -0,0 +1,5 @@ +b="a" + +for file in b* +do mv "$file" "$b$file" +done diff --git a/OS/Task1/Task1d.sh b/OS/Task1/Task1d.sh new file mode 100644 index 0000000..d2afee2 --- /dev/null +++ b/OS/Task1/Task1d.sh @@ -0,0 +1,7 @@ +c="wow" +one="1" + +for file in c* +do cp "$file" "$c$one" +c=$c$one +done diff --git a/OS/Task1/Task1e.sh b/OS/Task1/Task1e.sh new file mode 100644 index 0000000..dc057e3 --- /dev/null +++ b/OS/Task1/Task1e.sh @@ -0,0 +1,3 @@ +read pattern +echo $pattern +grep "$pattern" Search.txt > Result.txt diff --git a/OS/Task1/Task1e.sh~ b/OS/Task1/Task1e.sh~ new file mode 100644 index 0000000..9229aba --- /dev/null +++ b/OS/Task1/Task1e.sh~ @@ -0,0 +1,3 @@ +read pattern +echo $pattern +grep '$pattern' Search.txt > Result.txt diff --git a/OS/Task1b.sh~ b/OS/Task1b.sh~ new file mode 100644 index 0000000..45d22d3 --- /dev/null +++ b/OS/Task1b.sh~ @@ -0,0 +1 @@ +rm a* diff --git a/OS/Task1c.sh~ b/OS/Task1c.sh~ new file mode 100644 index 0000000..08f890a --- /dev/null +++ b/OS/Task1c.sh~ @@ -0,0 +1,5 @@ +b="a" + +for file in b* +do mv "$file" "$b$file" +done diff --git a/OS/Task1d.sh~ b/OS/Task1d.sh~ new file mode 100644 index 0000000..d2afee2 --- /dev/null +++ b/OS/Task1d.sh~ @@ -0,0 +1,7 @@ +c="wow" +one="1" + +for file in c* +do cp "$file" "$c$one" +c=$c$one +done diff --git a/OS/Task2/Task1.hs~ b/OS/Task2/Task1.hs~ new file mode 100644 index 0000000..f809422 --- /dev/null +++ b/OS/Task2/Task1.hs~ @@ -0,0 +1,17 @@ +module Main where + +nOD a b = if mod c d == 0 + then d + else nOD (mod c d) d + where c = myMax a b + d = myMin a b + +myMax :: (Ord a) => a -> a -> a +myMax a b = if a > b then a else b + +myMin :: (Ord a) => a -> a -> a +myMin a b = if a < b then a else b + +main = do a <- readLn + b <- readLn + print (nOD a b) diff --git a/OS/Task2/Task1.sh~ b/OS/Task2/Task1.sh~ new file mode 100644 index 0000000..f61fc4e --- /dev/null +++ b/OS/Task2/Task1.sh~ @@ -0,0 +1,32 @@ +#!/bin/bash +number=$RANDOM +a="a" +b="b" +c="c" +txt=".txt" +one="1" + + +echo $number +echo $(($number % 3)) +for i in `seq 1 100`; + do + if [ "$(($number % 3))" = 0 ]; then + number=$RANDOM + the_file=$a$txt + echo $a > $the_file + a=$a$one + else + if [ "$(($number % 3))" = 1 ]; then + number=$RANDOM + the_file=$b$txt + echo $b > $the_file + b=$b$one + else + number=$RANDOM + the_file=$c$txt + echo $c > $the_file + c=$c$one + fi + fi + done diff --git a/OS/Task2/Task1b.sh~ b/OS/Task2/Task1b.sh~ new file mode 100644 index 0000000..45d22d3 --- /dev/null +++ b/OS/Task2/Task1b.sh~ @@ -0,0 +1 @@ +rm a* diff --git a/OS/Task2/Task1d.sh~ b/OS/Task2/Task1d.sh~ new file mode 100644 index 0000000..d2afee2 --- /dev/null +++ b/OS/Task2/Task1d.sh~ @@ -0,0 +1,7 @@ +c="wow" +one="1" + +for file in c* +do cp "$file" "$c$one" +c=$c$one +done diff --git a/OS/Task2/Task2 b/OS/Task2/Task2 new file mode 100644 index 0000000..e7a0f38 Binary files /dev/null and b/OS/Task2/Task2 differ diff --git a/OS/Task2/Task2.c b/OS/Task2/Task2.c new file mode 100644 index 0000000..a6122fd --- /dev/null +++ b/OS/Task2/Task2.c @@ -0,0 +1,120 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define BUFFERSIZE 1024 +#define COPYMORE 0644 + + + + +int copyDir(char *src, char *dest); +int copyFiles(char *src, char *dest); +int dostat(char *filename); +int mode_isReg(struct stat info); + + + + +int main(int ac, char *av[]) +{ + + if(ac != 3) + { + fprintf(stderr, "usage: %s source destination\n", *av); + exit(1); + } + + char src[PATH_MAX+1]; + strcpy(src, av[1]); + char dest[PATH_MAX+1]; + strcpy(dest, av[2]); + + if( src[0] != '/' && dest[0] != '/' )//cp1 file1.txt file2.txt + { + copyFiles(src, dest); + } + else if( src[0] != '/' && dest[0] == '/' )//cp1 file1.txt /dir + { + strcat(dest, "/"); + strcat(dest, src); + + + copyFiles(src, dest); + } + else //cp1 /dir1 /dir2 + { + copyDir(src, dest); + } +} + + +int copyDir(char *source, char *destination) +{ + DIR *dir_ptr = NULL; + struct dirent *direntp; + char tempDest[strlen(destination)+228]; + char tempSrc[strlen(source)+228]; + strcat(destination, "/"); + strcpy(tempDest, destination); + strcat(source, "/"); + strcpy(tempSrc, source); + + struct stat fileinfo; + + + if( (dir_ptr = opendir(source)) == NULL ) + { + fprintf(stderr, "cp1: cannot open %s for copying\n", source); + return 0; + } + else + { + while( (direntp = readdir(dir_ptr))) + { + + + strcat(tempDest, direntp->d_name); + strcat(tempSrc, direntp->d_name); + copyFiles(tempSrc, tempDest); + strcpy(tempDest, destination); + strcpy(tempSrc, source); + + } + closedir(dir_ptr); + return 1; + } + + +} + +int copyFiles(char *source, char *destination) +{ + + int in_fd, out_fd, n_chars; + char buf[BUFFERSIZE]; + + in_fd=open(source, O_RDONLY); + + out_fd=creat(destination, COPYMORE); + + + while( (n_chars = read(in_fd, buf, BUFFERSIZE)) > 0 ) + { + write(out_fd, buf, n_chars); + } + + + close(in_fd); + close(out_fd); + + + return 1; +} diff --git a/OS/Task2/Task2.c~ b/OS/Task2/Task2.c~ new file mode 100644 index 0000000..0723b35 --- /dev/null +++ b/OS/Task2/Task2.c~ @@ -0,0 +1,136 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define BUFFERSIZE 1024 +#define COPYMORE 0644 + + + + +int copyDir(char *src, char *dest); +int copyFiles(char *src, char *dest); +int dostat(char *filename); +int mode_isReg(struct stat info); + + + + +int main(int ac, char *av[]) +{ + + if(ac != 3) + { + fprintf(stderr, "usage: %s source destination\n", *av); + exit(1); + } + + char src[PATH_MAX+1]; + strcpy(src, av[1]); + char dest[PATH_MAX+1]; + strcpy(dest, av[2]); + + if( src[0] != '/' && dest[0] != '/' )//cp1 file1.txt file2.txt + { + copyFiles(src, dest); + } + else if( src[0] != '/' && dest[0] == '/' )//cp1 file1.txt /dir + { + strcat(dest, "/"); + strcat(dest, src); + + + copyFiles(src, dest); + } + else //cp1 /dir1 /dir2 + { + copyDir(src, dest); + } +} + + +int copyDir(char *source, char *destination) +{ + DIR *dir_ptr = NULL; + struct dirent *direntp; + char tempDest[strlen(destination)+228]; + char tempSrc[strlen(source)+228]; + strcat(destination, "/"); + strcpy(tempDest, destination); + strcat(source, "/"); + strcpy(tempSrc, source); + + struct stat fileinfo; + + + if( (dir_ptr = opendir(source)) == NULL ) + { + fprintf(stderr, "cp1: cannot open %s for copying\n", source); + return 0; + } + else + { + while( (direntp = readdir(dir_ptr))) + { + + + strcat(tempDest, direntp->d_name); + strcat(tempSrc, direntp->d_name); + copyFiles(tempSrc, tempDest); + strcpy(tempDest, destination); + strcpy(tempSrc, source); + + } + closedir(dir_ptr); + return 1; + } + + +} + + +int dostat(char *filename) +{ + struct stat fileInfo; + + if(stat(filename, &fileInfo) >=0) + if(S_ISREG(fileInfo.st_mode)) + return 1; + else return 0; + + return; +} + + + + +int copyFiles(char *source, char *destination) +{ + + int in_fd, out_fd, n_chars; + char buf[BUFFERSIZE]; + + in_fd=open(source, O_RDONLY); + + out_fd=creat(destination, COPYMORE); + + + while( (n_chars = read(in_fd, buf, BUFFERSIZE)) > 0 ) + { + write(out_fd, buf, n_chars); + } + + + close(in_fd); + close(out_fd); + + + return 1; +} diff --git a/OS/Task2/Task2.hs~ b/OS/Task2/Task2.hs~ new file mode 100644 index 0000000..8bf9460 --- /dev/null +++ b/OS/Task2/Task2.hs~ @@ -0,0 +1,22 @@ +module Main where + +myNum :: (Integral a) => a -> a +myNum a = if d /= 0 + then 1 + (myNum d) + else 1 + where d = div a 10 + +mySum :: (Integral a) => a -> a +mySum a = if d /= 0 + then m + (mySum d) + else m + where d = div a 10 + m = if d /= 0 + then mod a (10 * d) + else mod a 10 + +main = do a <- readLn + print (myNum a) + print (mySum a) + + diff --git a/OS/Task2/Task3.hs~ b/OS/Task2/Task3.hs~ new file mode 100644 index 0000000..556bf8e --- /dev/null +++ b/OS/Task2/Task3.hs~ @@ -0,0 +1,10 @@ +module Main where + +func :: (Integral a) => a -> a +func 0 = 1 +func 1 = 2 +func 2 = 2 +func n = (func (n - 1)) + (func (n - 2)) - 2 * (func (n - 3)) + +main = do a <- readLn + print (func a) diff --git a/OS/Task2/Task4.hs~ b/OS/Task2/Task4.hs~ new file mode 100644 index 0000000..09ea725 --- /dev/null +++ b/OS/Task2/Task4.hs~ @@ -0,0 +1,4 @@ +integrate :: (Float -> Float) -> Float -> Float -> Float +integrate f a b = if a < b + then ((f a) + (f (a + 0.0001))) / 20000 + integrate f (a + 0.0001) b + else ((f a) + (f (a + 0.0001))) / 20000 diff --git a/OS/Task2/Task5.hs~ b/OS/Task2/Task5.hs~ new file mode 100644 index 0000000..f304f55 --- /dev/null +++ b/OS/Task2/Task5.hs~ @@ -0,0 +1,8 @@ +module Main where + +square :: Float -> Float +square a = func a a 0 + where func c a d = if d < 1000000 + then func c ((a + c / a) / 2) (d + 1) + else (a + c / a) / 2 + diff --git a/OS/Task2/Task6.hs~ b/OS/Task2/Task6.hs~ new file mode 100644 index 0000000..bfeb80c --- /dev/null +++ b/OS/Task2/Task6.hs~ @@ -0,0 +1,16 @@ +data Direction = North | West | South | East deriving (Eq) +data Action = Left1 | Right1 | Turnaround1 deriving (Eq) + +loc :: Direction -> Action -> Direction +loc a b | (&&) (a == North) (b == Left1) = West + | (&&) (a == North) (b == Right1) = East + | (&&) (a == North) (b == Turnaround1) = South + | (&&) (a == South) (b == Left1) = East + | (&&) (a == South) (b == Right1) = West + | (&&) (a == South) (b == Turnaround1) = North + | (&&) (a == West) (b == Left1) = South + | (&&) (a == West) (b == Right1) = North + | (&&) (a == West) (b == Turnaround1) = East + | (&&) (a == East) (b == Left1) = North + | (&&) (a == East) (b == Turnaround1) = West + | otherwise = East diff --git a/OS/Task2/Task7.hs~ b/OS/Task2/Task7.hs~ new file mode 100644 index 0000000..c89d4f8 --- /dev/null +++ b/OS/Task2/Task7.hs~ @@ -0,0 +1,13 @@ +the_division :: [a] -> Int -> [[a]] +the_division a n = b + where temp = splitAt (div (length a) n) a + b = (fst temp) : (the_division_one (snd temp) (n - 1) (div (length a) n)) + the_division_one a n c = if n > 0 + then b1 + else [a] + where temp1 = splitAt c a + b1 = (fst temp1) : (the_division_one (snd temp1) (n - 1) c) + +get1 (a, _) = a + +get2 (_, a) = a diff --git a/OS/Task2/Task8.hs~ b/OS/Task2/Task8.hs~ new file mode 100644 index 0000000..72e6fc1 --- /dev/null +++ b/OS/Task2/Task8.hs~ @@ -0,0 +1,10 @@ +func :: Int -> Bool +func a | (||) (z < 1) (z > 10) = False + | otherwise = subfunc a 100 + where z = div a 1000000000 + +subfunc :: Int -> Int -> Bool +subfunc a k | k == 10000000000 = subfunc (div a 10) 100 + | (div a 10) == 0 = True + | (mod a 10 == (div ((mod a k) - (mod a (div k 10))) 10)) = False + | otherwise = subfunc a (k * 10) diff --git a/OS/Task2/Task9.hs~ b/OS/Task2/Task9.hs~ new file mode 100644 index 0000000..958dd6d --- /dev/null +++ b/OS/Task2/Task9.hs~ @@ -0,0 +1,13 @@ +data Tree a = Nil | Node a (Tree a) (Tree a) + +width :: Tree a -> Int +width Nil = 1 +width (Node a ta tb) = (width ta) + (width tb) + +depth :: Tree a -> Int +depth Nil = 1 +depth (Node a ta tb) = 1 + myMax (depth ta) (depth tb) + +myMax :: Int -> Int -> Int +myMax a b | a > b = a + | otherwise = b diff --git a/OS/Task3/Task3 b/OS/Task3/Task3 new file mode 100644 index 0000000..1a581ee Binary files /dev/null and b/OS/Task3/Task3 differ diff --git a/OS/Task3/Task3.c b/OS/Task3/Task3.c new file mode 100644 index 0000000..572503d --- /dev/null +++ b/OS/Task3/Task3.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include + +#define true 1 +#define false 0 + +int graph[][2] = { + {0, 1}, + {1, 3}, + {0, 2}, + {0, 3} +}; + +void dfs(int* visited, int vertex){ + visited[vertex] = true; + int i = 0; + for (i; i < 4; ++i){ + if (graph[i][0] == vertex && visited[graph[i][1]] == false){ + visited[graph[i][1]] = true; + pid_t a = fork(); + if (a == -1){ + fprintf(stderr, "Yo! U don't have enough memory, man!\n"); + } else + if (a == 0){ + printf("Process %i is going to visit vertex number %i\n", getpid(), graph[i][1]); + dfs(visited, graph[i][1]); + } else { + printf("Process %i forked! Child pid: %i\n", getpid(), a); + } + } + } + } + +int main() { + printf("Parent process: %i\n", getpid()); + int memery = shmget(0, 4 * sizeof(int), IPC_CREAT | 0666); + if (memery == -1){ + fprintf(stderr, "Your computer is old, man! Or you're using python. Either way, it's bad!"); + return 1; + } + int* visited = (int*) shmat(memery, 0, 0); + memset(visited, 0, 4 * sizeof(int)); + dfs(visited, 0); + return 0; +} + diff --git a/OS/Task3/Task3.c~ b/OS/Task3/Task3.c~ new file mode 100644 index 0000000..ff4218a --- /dev/null +++ b/OS/Task3/Task3.c~ @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include + +#define true 1 +#define false 0 + +int graph[][2] = { + {0, 1}, + {1, 3}, + {0, 2}, + {0, 3} +}; + +void dfs(int* visited, int vertex){ + visited[vertex] = true; + int i = 0; + for (i; i < 4; ++i){ + if (graph[i][0] == vertex && visited[graph[i][1]] == false){ + visited[graph[i][1]] = true; + pid_t a = fork(); + if (a == -1){ + fprintf(stderr, "Yo! U don't have enough memory, man!\n"); + } else + if (a == 0){ + printf("Process %i is going to visit vertex number %i\n", getpid(), graph[i][1]); + dfs(visited, graph[i][1]); + } else { + printf("Process %i forked! Child pid: %i\n", getpid(), a); + } + } + } + } + +int main() { + printf("Parent process: %i\n", getpid()); + int memery = shmget(0, 4 * sizeof(int), IPC_CREAT | 0666); + if (memery == -1){ + fprintf(stderr, "Your computer is old, man! Or you're using python. Either way, it's bad!"); + return 1; + } + int* visited = (int*) shmat(memery, 0, 0); + memset(visited, 0, 4 * sizeof(int)); + dfs(visited, 0); + return 0; +} diff --git a/OS/Task4/Task4 b/OS/Task4/Task4 new file mode 100644 index 0000000..5dd7c7f Binary files /dev/null and b/OS/Task4/Task4 differ diff --git a/OS/Task4/Task4.c b/OS/Task4/Task4.c new file mode 100644 index 0000000..64b07e5 --- /dev/null +++ b/OS/Task4/Task4.c @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define n 228 + + +int execute(int* array){ + pid_t a = fork(); + if (a == -1){ + fprintf(stderr, "Yo! U don't have enough memory, man!\n"); + } else + if (a == 0){ + printf("Process %i is generating\n", getpid()); + int a = 0; + while(array[2 * n]){ + int i = 0; + for (i; i < n; i++){ + srand(time(NULL)); + array[i] = a; + a++; + } + } + printf("Stopped"); + return 0; + } + + pid_t b = fork(); + if (b == -1){ + fprintf(stderr, "Yo! U don't have enough memory, man!\n"); + } else + if (b == 0){ + printf("Process %i is calculating\n", getpid()); + while(array[2 * n]){ + int i = n; + for (i; i < 2 * n - 1; i++){ + array[i] = array[i - n] + array[i - n + 1]; + } + } + printf("Stopped"); + return 0; + } + + pid_t c = fork(); + if (c == -1){ + fprintf(stderr, "Yo! U don't have enough memory, man!\n"); + } else + if (c == 0){ + printf("Process %i is printing\n", getpid()); + while(array[2 * n]){ + int i = n; + for (i; i < 2 * n - 1; i++){ + printf("Result is %i\n", array[i]); + } + } + printf("Stopped"); + return 0; + } + + int stop = getchar(); + if (stop == 115){ + printf("Stopped"); + array[2 * n] = 0; + wait(a); + wait(b); + wait(c); + shmdt(array); + return 0; + } +} + +int main() { + int memery = shmget(0, 2 * n * sizeof(int) + sizeof(int), IPC_CREAT | 0666); + if (memery == -1){ + fprintf(stderr, "Your computer is old, man! Or you're using python. Either way, it's bad!"); + return 1; + } + int* array = (int*) shmat(memery, 0, 0); + memset(array, 0, 2 * n * sizeof(int)); + array[2 * n] = 1; + execute(array); + return 0; +} diff --git a/OS/Task4/Task4.c~ b/OS/Task4/Task4.c~ new file mode 100644 index 0000000..64b07e5 --- /dev/null +++ b/OS/Task4/Task4.c~ @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define n 228 + + +int execute(int* array){ + pid_t a = fork(); + if (a == -1){ + fprintf(stderr, "Yo! U don't have enough memory, man!\n"); + } else + if (a == 0){ + printf("Process %i is generating\n", getpid()); + int a = 0; + while(array[2 * n]){ + int i = 0; + for (i; i < n; i++){ + srand(time(NULL)); + array[i] = a; + a++; + } + } + printf("Stopped"); + return 0; + } + + pid_t b = fork(); + if (b == -1){ + fprintf(stderr, "Yo! U don't have enough memory, man!\n"); + } else + if (b == 0){ + printf("Process %i is calculating\n", getpid()); + while(array[2 * n]){ + int i = n; + for (i; i < 2 * n - 1; i++){ + array[i] = array[i - n] + array[i - n + 1]; + } + } + printf("Stopped"); + return 0; + } + + pid_t c = fork(); + if (c == -1){ + fprintf(stderr, "Yo! U don't have enough memory, man!\n"); + } else + if (c == 0){ + printf("Process %i is printing\n", getpid()); + while(array[2 * n]){ + int i = n; + for (i; i < 2 * n - 1; i++){ + printf("Result is %i\n", array[i]); + } + } + printf("Stopped"); + return 0; + } + + int stop = getchar(); + if (stop == 115){ + printf("Stopped"); + array[2 * n] = 0; + wait(a); + wait(b); + wait(c); + shmdt(array); + return 0; + } +} + +int main() { + int memery = shmget(0, 2 * n * sizeof(int) + sizeof(int), IPC_CREAT | 0666); + if (memery == -1){ + fprintf(stderr, "Your computer is old, man! Or you're using python. Either way, it's bad!"); + return 1; + } + int* array = (int*) shmat(memery, 0, 0); + memset(array, 0, 2 * n * sizeof(int)); + array[2 * n] = 1; + execute(array); + return 0; +} diff --git a/OS/Task5/Task5 b/OS/Task5/Task5 new file mode 100644 index 0000000..8074cea Binary files /dev/null and b/OS/Task5/Task5 differ diff --git a/OS/Task5/Task5.c b/OS/Task5/Task5.c new file mode 100644 index 0000000..a5f553f --- /dev/null +++ b/OS/Task5/Task5.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include +#include + +#define true 1 +#define false 0 + +struct arg_struct{ + int* visited; + int vertex; + pthread_t* threads; +}; + +int graph[][2] = { + {0, 1}, + {1, 3}, + {0, 2}, + {2, 3}, + {2, 6}, + {3, 6}, + {3, 4}, + {3, 5}, + {4, 5}, + {5, 6}, + {5, 6}, +}; + +void* dfs(void *arguments){ + struct arg_struct *args = arguments; + if (!args->visited[args->vertex]){ + args->visited[args->vertex] = true; + int i = 0; + for (i = 0; i < 11; ++i){ + if (graph[i][0] == args->vertex){ + struct arg_struct new_args; + new_args.visited = args->visited; + new_args.vertex = graph[i][1]; + new_args.threads = args->threads; +printf("Going to vertex %i\n", graph[i][1]); + args->visited[11]++; + pthread_create(&args->threads[args->visited[11] - 1], NULL, dfs, (void *)&new_args); + pthread_join(args->threads[args->visited[11] - 1], NULL); + } + } + return 0; + } + else{ + return 0; + } +} + +int main() { + int memery = shmget(0, 12 * sizeof(int), IPC_CREAT | 0666); + if (memery == -1){ + fprintf(stderr, "Your computer is old, man! Or you're using python. Either way, it's bad!\n"); + return 1; + } + int* visited = (int*) shmat(memery, 0, 0); + memset(visited, 0, 12 * sizeof(int)); +int memery1 = shmget(0, 11 * sizeof(pthread_t), IPC_CREAT | 0666); + if (memery1 == -1){ + fprintf(stderr, "Your computer is old, man! Or you're using python. Either way, it's bad!\n"); + return 1; + } + pthread_t* threads = (pthread_t*) shmat(memery1, 0, 0); + memset(threads, 0, 11 * sizeof(pthread_t)); + struct arg_struct new_args; + new_args.visited = visited; + new_args.vertex = 0; + new_args.threads = threads; +printf("Going to do my business\n"); + dfs((void *)&new_args); + printf("Number of threads: %i\n", visited[11]); + return 0; +} + diff --git a/OS/Task5/Task5.c~ b/OS/Task5/Task5.c~ new file mode 100644 index 0000000..2dd5468 --- /dev/null +++ b/OS/Task5/Task5.c~ @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include +#include + +#define true 1 +#define false 0 + +struct arg_struct{ + int* visited; + int vertex; + pthread_t* threads; +}; + +int graph[][2] = { + {0, 1}, + {1, 3}, + {0, 2}, + {2, 3}, + {2, 6}, + {3, 6}, + {3, 4}, + {3, 5}, + {4, 5}, + {5, 6}, + {5, 6}, +}; + +void* dfs(void *arguments){ + struct arg_struct *args = arguments; + if (!args->visited[args->vertex]){ + args->visited[args->vertex] = true; + int i = 0; + for (i = 0; i < 11; ++i){ + if (graph[i][0] == args->vertex){ + struct arg_struct new_args; + new_args.visited = args->visited; + new_args.vertex = graph[i][1]; + new_args.threads = args->threads; +printf("Going to vertex %i\n", graph[i][1]); + args->visited[11]++; + pthread_create(&args->threads[args->visited[11] - 1], NULL, dfs, (void *)&new_args); + pthread_join(args->threads[args->visited[11] - 1], NULL); + } + } + return 0; + } + else{ + return 0; + } +} + +int main() { + int memery = shmget(0, 12 * sizeof(int), IPC_CREAT | 0666); + if (memery == -1){ + fprintf(stderr, "Your computer is old, man! Or you're using python. Either way, it's bad!\n"); + return 1; + } + int* visited = (int*) shmat(memery, 0, 0); + memset(visited, 0, 12 * sizeof(int)); +int memery1 = shmget(0, 11 * sizeof(pthread_t), IPC_CREAT | 0666); + if (memery1 == -1){ + fprintf(stderr, "Your computer is old, man! Or you're using python. Either way, it's bad!\n"); + return 1; + } + pthread_t* threads = (pthread_t*) shmat(memery1, 0, 0); + memset(threads, 0, 11 * sizeof(pthread_t)); + struct arg_struct new_args; + new_args.visited = visited; + new_args.vertex = 0; + new_args.threads = threads; +printf("Going to do my business\n"); + dfs((void *)&new_args); + printf("Number of threads:%i\n", visited[11]); + return 0; +} + diff --git a/OS/Task6/Task6 b/OS/Task6/Task6 new file mode 100644 index 0000000..3b49660 Binary files /dev/null and b/OS/Task6/Task6 differ diff --git a/OS/Task6/Task6.c b/OS/Task6/Task6.c new file mode 100644 index 0000000..def81cc --- /dev/null +++ b/OS/Task6/Task6.c @@ -0,0 +1,90 @@ +// +// Created by alexkane on 5/11/16. +// +// gcc -pthread -o Task6 Task6.c + +#include +#include +#include +#include +#include +#include +#include +#include + +#define n 228 + +struct the_array{ + int* itself; +}; + +void* generate(void* arguments){ + struct the_array *args = arguments; + int a = 0; + while(args->itself[2 * n]){ + int i = 0; + for (i; i < n; i++){ + srand(time(NULL)); + args->itself[i] = a; + a++; + } + } + return 0; +} + +void* calculate(void* arguments){ + struct the_array *args = arguments; + while(args->itself[2 * n]){ + int i = n; + for (i; i < 2 * n - 1; i++){ + args->itself[i] = args->itself[i - n] + args->itself[i - n + 1]; + } + } + return 0; +} + +void* print(void* arguments){ + struct the_array *args = arguments; + while(args->itself[2 * n]){ + int i = n; + for (i; i < 2 * n - 1; i++){ + printf("Result is %i\n", args->itself[i]); + } + } + return 0; +} + +int execute(int* array, pthread_t* threads){ + struct the_array new_array; + new_array.itself = array; + pthread_create(&threads[0], NULL, generate, (void *) &new_array); + pthread_create(&threads[1], NULL, calculate, (void *) &new_array); + pthread_create(&threads[2], NULL, print, (void *) &new_array); + int stop = getchar(); + if (stop == 115){ + array[2 * n] = 0; + shmdt(array); + return 0; + } +} + +int main() { + int memery = shmget(0, 2 * n * sizeof(int) + sizeof(int), IPC_CREAT | 0666); + if (memery == -1){ + fprintf(stderr, "Your computer is old, man! Or you're using python. Either way, it's bad!"); + return 1; + } + int* array = (int*) shmat(memery, 0, 0); + memset(array, 0, 2 * n * sizeof(int)); + int memery1 = shmget(0, 3 * sizeof(pthread_t), IPC_CREAT | 0666); + if (memery1 == -1) { + fprintf(stderr, "Your computer is old, man! Or you're using python. Either way, it's bad!\n"); + return 1; + } + pthread_t *threads = (pthread_t *) shmat(memery1, 0, 0); + memset(threads, 0, 3 * sizeof(pthread_t)); + array[2 * n] = 1; + execute(array, threads); + return 0; +} + diff --git a/OS/Task6/Task6.c~ b/OS/Task6/Task6.c~ new file mode 100644 index 0000000..27254f4 --- /dev/null +++ b/OS/Task6/Task6.c~ @@ -0,0 +1,91 @@ +// +// Created by alexkane on 5/11/16. +// +// gcc -pthread -o Task6 Task6.c + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define n 228 + +struct the_array{ + int* itself; +}; + +void* generate(void* arguments){ + struct arg_struct *args = arguments; + int a = 0; + while(args->itself[2 * n]){ + int i = 0; + for (i; i < n; i++){ + srand(time(NULL)); + args->itself[i] = a; + a++; + } + } + return 0; +} + +void* calculate(void* arguments){ + struct arg_struct *args = arguments; + while(args->array[2 * n]){ + int i = n; + for (i; i < 2 * n - 1; i++){ + args->array[i] = args->array[i - n] + args->array[i - n + 1]; + } + } + return 0; +} + +void* print(void* arguments){ + struct arg_struct *args = arguments; + while(args->itself[2 * n]){ + int i = n; + for (i; i < 2 * n - 1; i++){ + printf("Result is %i\n", args->itself[i]); + } + } + return 0; +} + +int execute(int* array, pthread_t* threads){ + struct the_array new_array; + new_array.itself = array; + pthread_create(&threads[0], NULL, generate, (void *) &new_array); + pthread_create(&threads[1], NULL, calculate, (void *) &new_array); + pthread_create(&threads[2], NULL, print, (void *) &new_array); + int stop = getchar(); + if (stop == 115){ + array[2 * n] = 0; + shmdt(array); + return 0; + } +} + +int main() { + int memery = shmget(0, 2 * n * sizeof(int) + sizeof(int), IPC_CREAT | 0666); + if (memery == -1){ + fprintf(stderr, "Your computer is old, man! Or you're using python. Either way, it's bad!"); + return 1; + } + int* array = (int*) shmat(memery, 0, 0); + memset(array, 0, 2 * n * sizeof(int)); + int memery1 = shmget(0, 3 * sizeof(pthread_t), IPC_CREAT | 0666); + if (memery1 == -1) { + fprintf(stderr, "Your computer is old, man! Or you're using python. Either way, it's bad!\n"); + return 1; + } + pthread_t *threads = (pthread_t *) shmat(memery1, 0, 0); + memset(threads, 0, 3 * sizeof(pthread_t)); + array[2 * n] = 1; + execute(array, threads); + return 0; +} + diff --git a/alive.txt b/alive.txt new file mode 100644 index 0000000..be9b384 --- /dev/null +++ b/alive.txt @@ -0,0 +1 @@ +I want to live. Please do not expel me. diff --git a/bf/bf.txt b/bf/bf.txt new file mode 100644 index 0000000..1705ae8 --- /dev/null +++ b/bf/bf.txt @@ -0,0 +1 @@ +,[[-]+>,]<[<]>>[<[->+<]>>]<[[->+>+<<]>>[-<<+>>]<-]<<[[->[->+>+<<]>>[-<<+>>]<<<]>[-]>[-<<+>>]<<<]>. diff --git a/lab10/Sadovnikov/lab10.py b/lab10/Sadovnikov/lab10.py new file mode 100644 index 0000000..37aa5c8 --- /dev/null +++ b/lab10/Sadovnikov/lab10.py @@ -0,0 +1,41 @@ +import math +from sys import stdin + +__author__ = 'alexkane' + +if __name__ == "__main__": + line = stdin.readline() + input = [int(s) for s in line.split(' ')] + + +def execute_search(array): + n = len(array) + counter = 0 + for i in range(n): + array[i] **= 2 + array.sort() + for i in range(n - 1): + j = i + k = 0 + while True: + if j == k or j < k: + break + if array[k] + array[j] == array[i]: + counter += 1 + j -= 1 + while array[j] == array[j + 1]: + j -= 1 + k += 1 + while array[k - 1] == array[k]: + k += 1 + elif array[k] + array[j] > array[i]: + j -= 1 + else: + k += 1 + return counter + + +if __name__ == "__main__": + print(execute_search(input)) + + # 18 100 99 24 40 10 10 24 4 30 3 5 1 88 24 32 32 26 diff --git a/lab10/Sadovnikov/tests4lab10.py b/lab10/Sadovnikov/tests4lab10.py new file mode 100644 index 0000000..425d607 --- /dev/null +++ b/lab10/Sadovnikov/tests4lab10.py @@ -0,0 +1,18 @@ +import unittest +import lab10 + +__author__ = 'alexkane' + +class TestSorting(unittest.TestCase): + + def test_from_boss(self): + input = [23, 247, 19, 96, 264, 265, 132, 265, 181] + expected = 2 + res = lab10.execute_search(input) + self.assertEqual(expected, res) + + def hardcore_test(self): + input = [18, 100, 99, 24, 40, 10, 10, 24, 4, 30, 3, 5, 1, 88, 24, 32, 32, 26] + expected = 4 + res = lab10.execute_search(input) + self.assertEquals(expected, res) \ No newline at end of file diff --git a/lab11/Sadovnikov/lab11.py b/lab11/Sadovnikov/lab11.py new file mode 100644 index 0000000..4974ec2 --- /dev/null +++ b/lab11/Sadovnikov/lab11.py @@ -0,0 +1,50 @@ +__author__ = 'alexkane' + +from sys import stdin + +if __name__ == "__main__": + line = stdin.readline() + input = [int(s) for s in line.split(" ")] + +def execute(array): + max = -float('inf') + local_max = -float('inf') + max_square = -float('inf') + current_square = 0 + delta_x = 0 + delta_y = 0 + for i in range(len(array)): + if array[i] >= max: + max = array[i] + if current_square > max_square: + max_square = current_square + current_square = 0 + delta_x = 0 + delta_y = 0 + local_max = -float('inf') + elif i == len(array) - 1: + if array[i] >= max: + max = array[i] + if current_square > max_square: + max_square = current_square + else: + if array[i] > local_max: + local_max = array[i] + current_square += max - array[i] + delta_x += 1 + delta_y = max - local_max + current_square -= delta_x * delta_y + if current_square > max_square: + max_square = current_square + else: + if array[i] > local_max: + local_max = array[i] + current_square += max - array[i] + delta_x += 1 + delta_y = max - local_max + return max_square + +if __name__ == "__main__": + print execute(input) + + diff --git a/lab11/Sadovnikov/tests4lab11.py b/lab11/Sadovnikov/tests4lab11.py new file mode 100644 index 0000000..f06f7d8 --- /dev/null +++ b/lab11/Sadovnikov/tests4lab11.py @@ -0,0 +1,24 @@ +__author__ = 'alexkane' + +import unittest +import lab11 + +class TestHistogram(unittest.TestCase): + + def test_from_boss(self): + input = [2, 5, 1, 2, 3, 4, 7, 7, 6] + expected = 10 + res = lab11.execute(input) + self.assertEqual(expected, res) + + def test_from_me(self): + input = [10, 3, 7, 1, 11, 1, 1, 1] + expected = 19 + res = lab11.execute(input) + self.assertEqual(expected, res) + + def test_from_me_again(self): + input = [10, 3, 7, 1, 11, 0, 0, 10] + expected = 20 + res = lab11.execute(input) + self.assertEqual(expected, res) \ No newline at end of file diff --git a/lab12/Sadovnikov/lab12.py b/lab12/Sadovnikov/lab12.py new file mode 100644 index 0000000..7968485 --- /dev/null +++ b/lab12/Sadovnikov/lab12.py @@ -0,0 +1,92 @@ +__author__ = 'alexkane' + +class Set: + def add(self, value): + pass + + def iterate(self): + pass + +class Node(): + def __init__(self, value): + self.left = None + self.right = None + self.parent = None + self.value = value + +class UnbalancedBinarySearchTree(Set): + def __init__(self): + self.root = None + self.max = -float('inf') + + def add(self, value): + if value > self.max: + self.max = value + z = Node(value) + y = None + x = self.root + while x is not None: + y = x + if z.value < x.value: + x = x.left + else: + x = x.right + z.parent = y + if y is None: + self.root = z + elif z.value < y.value: + y.left = z + else: + y.right = z + + def contains(self, x, value): + if x == None: + return False + elif x.value == value: + return True + if value < x.value: + return self.contains(x.left, value) + else: + return self.contains(x.right, value) + + def __iter__(self): + # return TreeIterator(self) + return self.iterate() + + def iterate(self): + v = self.root + while v.right is not None: + v = v.right + end = v.value + v = self.root + k = -float('inf') + while v.left is not None: + v = v.left + while True: + while v.value <= k: + v = v.parent + if v.value == end: + if v.left is None or k >= v.left.value: + yield v.value + break + if v.value > k and v.left is None: + k = v.value + if v.right is None: + v = v.parent + else: + v = v.right + yield k + elif v.value > k and v.left is not None: + if v.left.value > k: + while v.left is not None: + v = v.left + k = v.value + if v.right is None: + v = v.parent + else: + v = v.right + yield k + elif v.right is not None and v.right.value <= k: + k = v.value + v = v.parent + yield k diff --git a/lab12/Sadovnikov/tests4lab12.py b/lab12/Sadovnikov/tests4lab12.py new file mode 100644 index 0000000..0c69ffa --- /dev/null +++ b/lab12/Sadovnikov/tests4lab12.py @@ -0,0 +1,54 @@ +__author__ = 'alexkane' + +import unittest +import lab12 + +class TestBSC(unittest.TestCase): + + def trivial_test(self): + set = lab12.UnbalancedBinarySearchTree() + set.add(6) + set.add(9) + set.add(8) + set.add(3) + set.add(4) + set.add(2) + set.add(88) + set.add(7) + set.add(1) + set.add(99) + set.add(10) + set.add(21) + set.add(5) + set.add(77) + set.add(88888) + set.add(11) + list = [] + for v in set: + list.append(v.value) + expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 21, 77, 88, 99, 88888] + self.assertEquals(expected, list) + + def not_so_trivial_test(self): + set = lab12.UnbalancedBinarySearchTree() + set.add(1) + set.add(2) + set.add(3) + set.add(4) + set.add(5) + set.add(6) + set.add(100) + set.add(21) + set.add(19) + set.add(18) + set.add(17) + set.add(9) + set.add(10) + set.add(11) + set.add(12) + set.add(13) + list = [] + for v in set: + list.append(v.value) + expected = [1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 17, 18, 19, 21, 100] + self.assertEquals(expected, list) \ No newline at end of file diff --git a/lab13/Sadovnikov/lab13.py b/lab13/Sadovnikov/lab13.py new file mode 100644 index 0000000..ccbd159 --- /dev/null +++ b/lab13/Sadovnikov/lab13.py @@ -0,0 +1,226 @@ +__author__ = 'alexkane' + + +class Node: + """Simple class that represents nodes in tree.""" + + def __init__(self, key, left, right, parent): + """Four fields need to be initialized. + + key -- represents value stored in node + left -- link to left child of a node + right -- link to the right child of a node + parent -- link to the parent of a node + iterated_list -- list of iterated elements of the tree + """ + + self.left = left + self.right = right + self.parent = parent + self.key = key + self.iterated_list = [] + + +class SplayTree(): + """Splay Tree itself. + + Splay tree is self-adjusting binary search tree. Pros of using this type of trees are that tree doesn't need to keep + any extra information about itself, what makes it efficient in terms of memory usage, also splay tree works much + faster than other types of binary trees when working with real input data. + In situations, when we work with already built binary search tree and need to respond to queries such as "whether + this key is present in the tree or not", and the user is mostly interested in one key and queries others only from + time to time, we might face a problem of processing m number of queries in O(m log(n)) time. But splay tree will + process such number of queries in O(m) due to its self-adjusting structure. + Obvious disadvantage of splay tree is that its height can be linear, Although all its methods still work in + amortized O(log(n)) time. + + Algorithm: + Accessing any of the nodes executes splay operation that moves accessed node to the root ot the tree. Splaying + consists of two steps: zig ,zig-zig and zig-zag. Let's assume that current node is x, parent is parent of x and + grandparent is parent of parent. Zig step is done when parent of x is the root. The tree is rotated from x to p. + Zig-zig is done when parent is not the root and x and parent are either both right children or are both left + children. The tree is rotated from parent to grandparent, then rotated from x to parent. Zig-zag is done when parent + is not the root and x is a right child and parent is a left child. The tree is rotated from x to parent, and then + rotated from x to grandparent. + """ + + def __init__(self): + """Only one field needs to be initialized by default. + + root -- root of this tree + """ + + self.root = None + + def __iter__(self): + """Method responsible for iteration.""" + self.iterated_list = [] + self.iterate(self.root) + for i in range(len(self.iterated_list)): + yield self.iterated_list[i] + + def set_parent(self, child, parent): + """Helper method to link chosen node with another chosen node. + + child -- node which parent is set + parent -- node being linked as parent to another node + """ + + if child is not None: + child.parent = parent + + def keep_parent(self, v): + """Helper method to set links between this node and its children. + + v -- chosen node + """ + + self.set_parent(v.left, v) + self.set_parent(v.right, v) + + def rotate(self, parent, child): + """Method that performs standard rotation of nods in chosen direction. + + parent -- node in which direction rotation is being done + child -- node from which rotation is being done + """ + + gparent = parent.parent + if gparent is not None: + if gparent.left == parent: + gparent.left = child + else: + gparent.right = child + + if parent.left == child: + parent.left, child.right = child.right, parent + else: + parent.right, child.left = child.left, parent + + self.keep_parent(child) + self.keep_parent(parent) + child.parent = gparent + + def splay(self, v): + """Main method in whole tree which performs transferring of a chosen node to the root of the tree keeping its features. + + v -- node that is being transferred + """ + + if v.parent is None: + self.root = v + return v + parent = v.parent + gparent = parent.parent + if gparent is None: + self.rotate(parent, v) + self.root = v + return v + else: + zigzig = (gparent.left == parent) == (parent.left == v) + if zigzig: + self.rotate(gparent, parent) + self.rotate(parent, v) + else: + self.rotate(parent, v) + self.rotate(gparent, v) + self.root = self.splay(v) + return self.root + + def split(self, root, key): + """Helper method that performs splitting of a tree into two different ones, in one of which are stored nodes with + value less than the key and in the other nodes with value greater than the key. + + root -- root of the tree + key -- key by which we divide the tree + """ + + if root is None: + return None, None + root = self.find(root, key) + if root == key: + self.set_parent(root.left, None) + self.set_parent(root.right, None) + return root.left, root.right + if root.key < key: + right, root.right = root.right, None + self.set_parent(right, None) + return root, right + else: + left, root.left = root.left, None + self.set_parent(left, None) + return left, root + + def merge(self, left, right): + """Helper method that merges two trees into one. + + left -- left tree + right -- right tree + """ + + if right is None: + return left + if left is None: + return right + right = self.find(right, left.key) + right.left, left.parent = left, right + return right + + def find(self, v, key): + """Method that searches the tree for node with set key and moves it to the root of the tree. + + v -- current node + key -- value that needs to be found + """ + + if v is None: + return None + if key == v.key: + return self.splay(v) + if key < v.key and v.left is not None: + return self.find(v.left, key) + if key > v.key and v.right is not None: + return self.find(v.right, key) + return self.splay(v) + + def add(self, key): + """Method that adds value to the tree. + + key -- value to add + """ + + left, right = self.split(self.root, key) + self.root = Node(key, left, right, None) + self.keep_parent(self.root) + return self.root + + def remove(self, key): + """Method that removes set value from the tree. + + key -- value to remove + """ + + root = self.find(self.root, key) + self.set_parent(root.left, None) + self.set_parent(root.right, None) + return self.merge(root.left, root.right) + + def contains(self, key): + """Method that checks whether tree contains given value. + + key -- value + """ + + return self.find(self.root, key) == key + + def iterate(self, v): + """Method used by __iter__ method to iterate the tree.""" + if v is not None: + self.iterate(v.left) + self.iterated_list.append(v.key) + self.iterate(v.right) + + + + + diff --git a/lab13/Sadovnikov/tests4lab13.py b/lab13/Sadovnikov/tests4lab13.py new file mode 100644 index 0000000..a8af2ec --- /dev/null +++ b/lab13/Sadovnikov/tests4lab13.py @@ -0,0 +1,54 @@ +__author__ = 'alexkane' + +import unittest +import lab13 + +class TestSplayTree(unittest.TestCase): + + def trivial_test(self): + set = lab13.SplayTree() + set.add(6) + set.add(9) + set.add(8) + set.add(3) + set.add(4) + set.add(2) + set.add(88) + set.add(7) + set.add(1) + set.add(99) + set.add(10) + set.add(21) + set.add(5) + set.add(77) + set.add(88888) + set.add(11) + list = [] + for v in set: + list.append(v.key) + expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 21, 77, 88, 99, 88888] + self.assertEquals(expected, list) + + def not_so_trivial_test(self): + set = lab13.SplayTree() + set.add(1) + set.add(2) + set.add(3) + set.add(4) + set.add(5) + set.add(6) + set.add(100) + set.add(21) + set.add(19) + set.add(18) + set.add(17) + set.add(9) + set.add(10) + set.add(11) + set.add(12) + set.add(13) + list = [] + for v in set: + list.append(v.key) + expected = [1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 17, 18, 19, 21, 100] + self.assertEquals(expected, list) \ No newline at end of file diff --git a/lab14/Sadovnikov/lab14.py b/lab14/Sadovnikov/lab14.py new file mode 100644 index 0000000..d1a1021 --- /dev/null +++ b/lab14/Sadovnikov/lab14.py @@ -0,0 +1,38 @@ +__author__ = 'alexkane' + + +class Graph: + def __init__(self): + self.graph = {} + self.bool = True + + def add_vertex(self, v): + self.graph[v] = ['white'] + + def add_directed_link(self, v1, v2): + self.graph[v1].append(v2) + + def topological_sort(self): + return self.special_DFS(self.graph) + + def special_DFS(self, graph): + list = [] + self.bool = True + for key in graph: + if graph[key][0] == 'white': + self.special_DFS_Visit(graph, graph[key], key, list) + if not self.bool: + return None + else: + return list[::-1] + + def special_DFS_Visit(self, graph, vertex, key, list): + vertex[0] = 'green' + for i in range(1, len(vertex)): + if graph[vertex[i]][0] == 'white': + self.special_DFS_Visit(graph, graph[vertex[i]], vertex[i], list) + elif graph[vertex[i]][0] == 'green': + self.bool = False + break + vertex[0] = 'blue' + list.append(key) diff --git a/lab14/Sadovnikov/tests4lab14.py b/lab14/Sadovnikov/tests4lab14.py new file mode 100644 index 0000000..9c6d896 --- /dev/null +++ b/lab14/Sadovnikov/tests4lab14.py @@ -0,0 +1,35 @@ +__author__ = 'alexkane' + +import lab14 +import unittest + +class TestTopologicalSort(unittest.TestCase): + + def trivial_test(self): + graph = lab14.Graph() + graph.add_vertex(1) + graph.add_vertex(2) + graph.add_vertex(3) + graph.add_vertex(4) + graph.add_directed_link(1, 2) + graph.add_directed_link(1, 3) + graph.add_directed_link(3, 4) + graph.add_directed_link(4, 2) + res = graph.topological_sort() + expected = [1, 3, 4, 2] + self.assertEquals(expected, res) + + def trivial_cycle_test(self): + graph = lab14.Graph() + graph.add_vertex(1) + graph.add_vertex(2) + graph.add_vertex(3) + graph.add_vertex(4) + graph.add_directed_link(1, 2) + graph.add_directed_link(1, 3) + graph.add_directed_link(3, 4) + graph.add_directed_link(4, 2) + graph.add_directed_link(2, 3) + res = graph.topological_sort() + expected = None + self.assertEquals(expected, res) \ No newline at end of file diff --git a/lab15/Sadovnikov/lab15.py b/lab15/Sadovnikov/lab15.py new file mode 100644 index 0000000..6845660 --- /dev/null +++ b/lab15/Sadovnikov/lab15.py @@ -0,0 +1,101 @@ +from sys import stdin + +__author__ = 'alexkane' + +class Node(): + def __init__(self, name): + self.name = name + self.links = [] + self.colour = "white" + self.height = 0 + + def __lt__(self, other): + return self.height < other.height + +class Graph: + def __init__(self, n, king_vertex, links): + self.graph = [] + self.heap = [] + self.tree = {} + self.n = n + self.king_vertex = king_vertex + for x in range(n): + self.graph.append(Node(x)) + self.add_links(links) + self.enter_array = [] + + def add_links(self, array): + for x in array: + self.graph[x[0]].links.append(x[1]) + self.graph[x[1]].links.append(x[0]) + + def dfs(self): + self.enter_array = [] + for x in self.graph: + x.colour = "white" + height = 0 + self.dfs_visit(self.graph[self.king_vertex], height) + + def dfs_visit(self, vertex, height): + bool = True + height += 1 + vertex.colour = "green" + vertex.height = height + for x in vertex.links: + if self.graph[x].colour == "white": + self.dfs_visit(self.graph[x], height) + if bool: + self.enter_array.append(vertex.name) + bool = False + if bool: + self.enter_array.append(vertex.name) + vertex.colour = "blue" + + def build_wicked_tree(self, tree, array, n): + for i in range(0, n): + tree[n - 1 + i] = self.graph[array[i]] + for i in range(n - 2, -1, -1): + tree[i] = min(tree[2 * i + 1], tree[2 * i + 2]) + + def search_wicked_tree(self, left, right): + leftRes = Node('theLeft') + leftRes.height = float('inf') + rightRes = Node('theRight') + rightRes.height = float('inf') + while left < right: + if left % 2 == 0: + leftRes = min(leftRes, self.tree[left]) + left = left // 2 + if right % 2 == 1: + rightRes = min(rightRes, self.tree[right]) + right = (right - 2) // 2 + if left == right: + leftRes = min(leftRes, self.tree[left]) + return min(leftRes, rightRes) + + def pre_process(self): + self.dfs() + self.tree = {} + self.enter_array = self.enter_array + self.build_wicked_tree(self.tree, self.enter_array, len(self.enter_array)) + + def execute(self, v1, v2): + r = len(self.enter_array) - self.enter_array[::-1].index(v2) - 1 + l = self.enter_array.index(v1) + left = min(l, r) + right = max(l, r) + return self.search_wicked_tree(left + len(self.tree) // 2, right + len(self.tree) // 2).name + + +if "__name__" == "__main__": + n = stdin.readline() + king = stdin.readline() + array = [] + for i in range(0, n - 1): + line = [x for x in stdin.readline().split(' ')] + array.append((line[0], line[1])) + graph = Graph(n, king, array) + graph.pre_process() + for line in stdin.readlines(): + line = [x for x in stdin.readline().split(' ')] + print(graph.execute(line[0], line[1])), diff --git a/lab15/Sadovnikov/tests4lab15.py b/lab15/Sadovnikov/tests4lab15.py new file mode 100644 index 0000000..ffd1e82 --- /dev/null +++ b/lab15/Sadovnikov/tests4lab15.py @@ -0,0 +1,33 @@ +__author__ = 'alexkane' + +import unittest +import lab15 + +class TestSuffocation(unittest.TestCase): + def test_Vovana(self): + n = 8 + root = 2 + adjacent = [(0, 6), (3, 6), (6, 2), (4, 2), (1, 4), (5, 1), (7, 1)] + graph = lab15.Graph(n, root, adjacent) + graph.pre_process() + self.assertEqual(graph.execute(0, 7), 2) + self.assertEqual(graph.execute(5, 1), 1) + self.assertEqual(graph.execute(1, 5), 1) + self.assertEqual(graph.execute(0, 3), 6) + + def test_Vovana_again(self): + n = 20 + root = 7 + adjacent = [(7, 5), (7, 13), (7, 11), (11, 4), (11, 19), (4, 6), (4, 0), (4, 1), (4, 3), (13, 15), (13, 14), + (15, 16), (16, 17), (16, 18), (5, 2), (2, 8), (8, 9), (8, 12), (8, 10)] + graph = lab15.Graph(n, root, adjacent) + graph.pre_process() + self.assertEqual(graph.execute(5, 13), 7) + self.assertEqual(graph.execute(6, 19), 11) + self.assertEqual(graph.execute(6, 12), 7) + self.assertEqual(graph.execute(15, 10), 7) + self.assertEqual(graph.execute(18, 14), 13) + self.assertEqual(graph.execute(14, 2), 7) + self.assertEqual(graph.execute(3, 19), 11) + self.assertEqual(graph.execute(18, 15), 15) + self.assertEqual(graph.execute(10, 2), 2) \ No newline at end of file diff --git a/lab16/Sadovnikov/lab16.py b/lab16/Sadovnikov/lab16.py new file mode 100644 index 0000000..0aa1444 --- /dev/null +++ b/lab16/Sadovnikov/lab16.py @@ -0,0 +1,49 @@ +__author__ = 'vmath' + +import heapq + + +class Node(): + def __init__(self, name): + self.name = name + self.outcoming_vertexes = {} + self.path = float('inf') + self.link = None + + def __lt__(self, other): + return self.path < other.path + + +class WeightedGraph(): + def __init__(self): + self.graph = [] + + def add_vertex(self, v): + self.graph.append(Node(v)) + + def add_directed_link(self, v1, v2, weight): + self.graph[v1].outcoming_vertexes[v2] = weight + + def relax(self, u, v, weight): + if self.graph[v].path > self.graph[u].path + weight: + self.graph[v].path = self.graph[u].path + weight + self.graph[v].link = u + + def paths(self, w): + q = [] + for vertex in self.graph: + if vertex.name == w: + vertex.path = 0 + vertex.link = w + else: + vertex.path = float('inf') + vertex.link = None + heapq.heappush(q, vertex) + s = [] + while len(q) != 0: + u = heapq.heappop(q) + s.append(u) + for key in u.outcoming_vertexes: + self.relax(u.name, key, u.outcoming_vertexes[key]) + return [i.path for i in self.graph] + diff --git a/lab16/Sadovnikov/tests4lab16.py b/lab16/Sadovnikov/tests4lab16.py new file mode 100644 index 0000000..cc5e332 --- /dev/null +++ b/lab16/Sadovnikov/tests4lab16.py @@ -0,0 +1,74 @@ +__author__ = 'vmath' + +import lab16 +import unittest + + +class TestDijkstra(unittest.TestCase): + def test_trivial(self): + ex = lab16.WeightedGraph() + ex.add_vertex(0) + ex.add_vertex(1) + ex.add_vertex(2) + ex.add_vertex(3) + ex.add_directed_link(0, 1, 1) + ex.add_directed_link(0, 2, 3) + ex.add_directed_link(2, 1, 5) + ex.add_directed_link(1, 2, 1) + res = ex.paths(0) + expected = [0, 1, 2, float('inf')] + self.assertEquals(expected, res) + + def test_trivial_1(self): + ex = lab16.WeightedGraph() + ex.add_vertex(0) + ex.add_vertex(1) + ex.add_vertex(2) + ex.add_vertex(3) + ex.add_directed_link(0, 1, 1) + ex.add_directed_link(0, 2, 3) + ex.add_directed_link(2, 1, 5) + ex.add_directed_link(1, 2, 1) + res = ex.paths(1) + expected = [float('inf'), 0, 1, float('inf')] + self.assertEquals(expected, res) + + def test_hardcore(self): + ex = lab16.WeightedGraph() + ex.add_vertex(0) + ex.add_vertex(1) + ex.add_vertex(2) + ex.add_vertex(3) + ex.add_vertex(4) + ex.add_directed_link(0, 1, 10) + ex.add_directed_link(0, 2, 5) + ex.add_directed_link(1, 3, 1) + ex.add_directed_link(1, 2, 2) + ex.add_directed_link(2, 1, 3) + ex.add_directed_link(2, 3, 9) + ex.add_directed_link(2, 4, 2) + ex.add_directed_link(3, 4, 4) + ex.add_directed_link(4, 3, 6) + ex.add_directed_link(4, 0, 7) + res = ex.paths(0) + expected = [0, 8, 5, 9, 7] + self.assertEquals(expected, res) + + def test_unreal_case(self): + ex = lab16.WeightedGraph() + ex.add_vertex(0) + ex.add_vertex(1) + ex.add_vertex(2) + ex.add_vertex(3) + ex.add_vertex(4) + ex.add_directed_link(1, 3, 1) + ex.add_directed_link(1, 2, 2) + ex.add_directed_link(2, 1, 3) + ex.add_directed_link(2, 3, 9) + ex.add_directed_link(2, 4, 2) + ex.add_directed_link(3, 4, 4) + ex.add_directed_link(4, 3, 6) + ex.add_directed_link(4, 0, 7) + res = ex.paths(0) + expected = [0, float('inf'), float('inf'), float('inf'), float('inf')] + self.assertEquals(expected, res) diff --git a/lab17/Sadovnikov/lab17.py b/lab17/Sadovnikov/lab17.py new file mode 100644 index 0000000..0e3cb58 --- /dev/null +++ b/lab17/Sadovnikov/lab17.py @@ -0,0 +1,51 @@ +__author__ = 'alexkane' + +import union_find + + +class Node(): + def __init__(self, name): + self.name = name + self.ribs = [] + + +class Rib(): + def __init__(self, v1, v2, weight): + self.source = v1 + self.destination = v2 + self.weight = weight + + def __lt__(self, other): + return self.weight < other.weight + + +class WeightedGraph(): + def __init__(self): + self.graph = [] + + def __cmp__(self, other): + return self.graph == other + + def add_vertex(self, v): + self.graph.append(Node(v)) + + def add_directed_link(self, v1, v2, weight): + self.graph[v1].ribs.append(Rib(v1, v2, weight)) + self.graph[v2].ribs.append(Rib(v2, v1, weight)) + + def min_tree(self): + a = WeightedGraph() + for i in range(len(self.graph)): + a.add_vertex(i) + dsu = union_find.DSU(len(self.graph)) + list_of_ribs = [] + for vertex in self.graph: + dsu.MakeSet(vertex.name) + for rib in vertex.ribs: + list_of_ribs.append(rib) + list_of_ribs.sort() + for rib in list_of_ribs: + if dsu.Find(rib.source) != dsu.Find(rib.destination): + a.add_directed_link(rib.source, rib.destination, rib.weight) + dsu.Unite(rib.source, rib.destination) + return a diff --git a/lab17/Sadovnikov/tests4lab17.py b/lab17/Sadovnikov/tests4lab17.py new file mode 100644 index 0000000..8e8ae91 --- /dev/null +++ b/lab17/Sadovnikov/tests4lab17.py @@ -0,0 +1,47 @@ +__author__ = 'alexkane' + +import lab17 +import unittest + + +class TestKruskal(unittest.TestCase): + def test_trivial(self): + graph = lab17.WeightedGraph() + graph.add_vertex(0) + graph.add_vertex(1) + graph.add_vertex(2) + graph.add_vertex(3) + graph.add_vertex(4) + graph.add_directed_link(0, 1, 3) + graph.add_directed_link(0, 2, 1) + graph.add_directed_link(2, 1, 1) + graph.add_directed_link(0, 3, 6) + graph.add_directed_link(2, 4, 2) + graph.add_directed_link(4, 3, 1) + graph.add_directed_link(3, 2, 1) + graph.add_directed_link(4, 1, 3) + res = graph.min_tree() + self.assertEqual(res.graph[0].ribs[0].source, 0) + self.assertEqual(res.graph[0].ribs[0].destination, 2) + self.assertEqual(res.graph[0].ribs[0].weight, 1) + self.assertEqual(res.graph[1].ribs[0].source, 1) + self.assertEqual(res.graph[1].ribs[0].destination, 2) + self.assertEqual(res.graph[1].ribs[0].weight, 1) + self.assertEqual(res.graph[2].ribs[0].source, 2) + self.assertEqual(res.graph[2].ribs[0].destination, 0) + self.assertEqual(res.graph[2].ribs[0].weight, 1) + self.assertEqual(res.graph[2].ribs[1].source, 2) + self.assertEqual(res.graph[2].ribs[1].destination, 1) + self.assertEqual(res.graph[2].ribs[1].weight, 1) + self.assertEqual(res.graph[2].ribs[2].source, 2) + self.assertEqual(res.graph[2].ribs[2].destination, 3) + self.assertEqual(res.graph[2].ribs[2].weight, 1) + self.assertEqual(res.graph[3].ribs[0].source, 3) + self.assertEqual(res.graph[3].ribs[0].destination, 2) + self.assertEqual(res.graph[3].ribs[0].weight, 1) + self.assertEqual(res.graph[3].ribs[1].source, 3) + self.assertEqual(res.graph[3].ribs[1].destination, 4) + self.assertEqual(res.graph[3].ribs[1].weight, 1) + self.assertEqual(res.graph[4].ribs[0].source, 4) + self.assertEqual(res.graph[4].ribs[0].destination, 3) + self.assertEqual(res.graph[4].ribs[0].weight, 1) diff --git a/lab17/Sadovnikov/union_find.py b/lab17/Sadovnikov/union_find.py new file mode 100644 index 0000000..8c8d871 --- /dev/null +++ b/lab17/Sadovnikov/union_find.py @@ -0,0 +1,28 @@ +from random import randint, random + +__author__ = 'alexkane' + +class DSU(): + def __init__(self, n): + self.parent = n * [None] + self.rank = n * [None] + + def MakeSet(self, x): + self.parent[x] = x + self.rank[x] = 0 + + def Find(self, x): + if self.parent[x] == x: + return x + self.parent[x] = self.Find(self.parent[x]) + return self.parent[x] + + def Unite(self, x, y): + x = self.Find(x) + y = self.Find(y) + if self.rank[x] < self.rank[y]: + self.parent[x] = y + else: + self.parent[y] = x + if self.rank[x] == self.rank[y]: + self.rank[x] = self.rank[x] + 1 diff --git a/lab18/Sadovnikov/lab18.py b/lab18/Sadovnikov/lab18.py new file mode 100644 index 0000000..bc8b5b6 --- /dev/null +++ b/lab18/Sadovnikov/lab18.py @@ -0,0 +1,39 @@ +__author__ = 'alexkane' + +from sys import stdin + +if __name__ == "__main__": + first_line = stdin.readline().rstrip('\n') + second_line = stdin.readline().rstrip('\n') + +def execute(line_a, line_b): + line_one = [float('inf') for i in range(len(line_a))] + line_two = [float('inf') for i in range(len(line_a))] + + for i in range(len(line_b)): + for j in range(len(line_a)): + if i == j == 0: + if line_a[0] == line_b[0]: + line_one[0] = 0 + else: + line_one[0] = 1 + else: + if i % 2 == 0: + current_line = line_one + previous_line = line_two + else: + current_line = line_two + previous_line = line_one + val = min(previous_line[j - 1] + int(not(line_b[i] == line_a[j])), previous_line[j] + 1, current_line[j - 1] + 1, previous_line[j - 1] + int(not((line_b[i - 1] == line_a[j]) and (line_b[i] == line_a[j - 1])))) + if i % 2 == 0: + line_one[j] = val + else: + line_two[j] = val + if len(line_a) == len(line_b) == 1: + return 1 + else: + return line_two[len(line_a) - 1] + +if __name__ == "__main__": + print(execute(first_line, second_line)) + diff --git a/lab18/Sadovnikov/tests4lab18.py b/lab18/Sadovnikov/tests4lab18.py new file mode 100644 index 0000000..514d8d9 --- /dev/null +++ b/lab18/Sadovnikov/tests4lab18.py @@ -0,0 +1,21 @@ +__author__ = 'alexkane' + +import lab18 +import unittest + +class TestLevi(unittest.TestCase): + def test_from_boss(self): + res = lab18.execute("Levenshtien", "Frankenstein") + expected = 7 + self.assertEqual(expected, res) + + def test_from_me(self): + res = lab18.execute("one", "two") + expected = 3 + self.assertEqual(expected, res) + + def test_trivial(self): + res = lab18.execute("1", "2") + expected = 1 + self.assertEqual(expected, res) + diff --git a/lab19/Sadovnikov/lab19.py b/lab19/Sadovnikov/lab19.py new file mode 100644 index 0000000..d1b9662 --- /dev/null +++ b/lab19/Sadovnikov/lab19.py @@ -0,0 +1,45 @@ +__author__ = 'vmath' + +from sys import stdin + +if __name__ == "__main__": + cossack = stdin.readline() + cossack = cossack[:len(cossack) - 1] + +def execute(input): + matrix = [[0 for x in range(len(input))] for x in range(len(input))] + new_matrix = [[0 for x in range(len(input))] for x in range(len(input))] + for j in range(len(input)): + for i in range(len(input) - 1, -1, -1): + if j == i: + matrix[i][j] = 1 + new_matrix[i][j] = (input[i]) + elif j < i: + matrix[i][j] = 0 + new_matrix[i][j] = ('') + elif j - i == 1: + matrix[i][j] = int(input[j] == input[i]) + 1 + if input[j] == input[i]: + new_matrix[i][j] = (2 * input[i]) + else: + new_matrix[i][j] = (input[i]) + else: + matrix[i][j] = max(2 * int(input[j] == input[i]) + matrix[i + 1][j - 1], matrix[i][j - 1], matrix[i + 1][j]) + if (matrix[i][j] == 2 * int(input[j] == input[i]) + matrix[i + 1][j - 1]) and (int(input[j] == input[i])): + new_matrix[i][j] = (input[i], new_matrix[i + 1][j - 1]) + elif matrix[i][j] == matrix[i][j - 1]: + new_matrix[i][j] = ('', new_matrix[i][j - 1]) + else: + new_matrix[i][j] = ('', new_matrix[i + 1][j]) + output = '' + return get(output, new_matrix[0][len(input) - 1]) + +def get(output, tuple): + if len(tuple) == 2: + output = tuple[0] + get(output, tuple[1]) + tuple[0] + return output + else: + return tuple[0] + +if __name__ == "__main__": + print(execute(cossack)) diff --git a/lab19/Sadovnikov/tests4lab19.py b/lab19/Sadovnikov/tests4lab19.py new file mode 100644 index 0000000..cdbe3ef --- /dev/null +++ b/lab19/Sadovnikov/tests4lab19.py @@ -0,0 +1,21 @@ +__author__ = 'alexkane' + +import unittest +import lab19 + +class TestPoly(unittest.TestCase): + + def test1(self): + res = lab19.execute('yabacrty') + expected = 'yabay' + self.assertEqual(expected, res) + + def test2(self): + res = lab19.execute('babcad') + expected = 'bab' + self.assertEqual(expected, res) + + def test3(self): + res = lab19.execute('aaaaa') + expected = 'aaaaa' + self.assertEqual(expected, res) \ No newline at end of file diff --git a/lab192/Sadovnikov/lab19.py b/lab192/Sadovnikov/lab19.py new file mode 100644 index 0000000..d1b9662 --- /dev/null +++ b/lab192/Sadovnikov/lab19.py @@ -0,0 +1,45 @@ +__author__ = 'vmath' + +from sys import stdin + +if __name__ == "__main__": + cossack = stdin.readline() + cossack = cossack[:len(cossack) - 1] + +def execute(input): + matrix = [[0 for x in range(len(input))] for x in range(len(input))] + new_matrix = [[0 for x in range(len(input))] for x in range(len(input))] + for j in range(len(input)): + for i in range(len(input) - 1, -1, -1): + if j == i: + matrix[i][j] = 1 + new_matrix[i][j] = (input[i]) + elif j < i: + matrix[i][j] = 0 + new_matrix[i][j] = ('') + elif j - i == 1: + matrix[i][j] = int(input[j] == input[i]) + 1 + if input[j] == input[i]: + new_matrix[i][j] = (2 * input[i]) + else: + new_matrix[i][j] = (input[i]) + else: + matrix[i][j] = max(2 * int(input[j] == input[i]) + matrix[i + 1][j - 1], matrix[i][j - 1], matrix[i + 1][j]) + if (matrix[i][j] == 2 * int(input[j] == input[i]) + matrix[i + 1][j - 1]) and (int(input[j] == input[i])): + new_matrix[i][j] = (input[i], new_matrix[i + 1][j - 1]) + elif matrix[i][j] == matrix[i][j - 1]: + new_matrix[i][j] = ('', new_matrix[i][j - 1]) + else: + new_matrix[i][j] = ('', new_matrix[i + 1][j]) + output = '' + return get(output, new_matrix[0][len(input) - 1]) + +def get(output, tuple): + if len(tuple) == 2: + output = tuple[0] + get(output, tuple[1]) + tuple[0] + return output + else: + return tuple[0] + +if __name__ == "__main__": + print(execute(cossack)) diff --git a/lab192/Sadovnikov/tests4lab19.py b/lab192/Sadovnikov/tests4lab19.py new file mode 100644 index 0000000..cdbe3ef --- /dev/null +++ b/lab192/Sadovnikov/tests4lab19.py @@ -0,0 +1,21 @@ +__author__ = 'alexkane' + +import unittest +import lab19 + +class TestPoly(unittest.TestCase): + + def test1(self): + res = lab19.execute('yabacrty') + expected = 'yabay' + self.assertEqual(expected, res) + + def test2(self): + res = lab19.execute('babcad') + expected = 'bab' + self.assertEqual(expected, res) + + def test3(self): + res = lab19.execute('aaaaa') + expected = 'aaaaa' + self.assertEqual(expected, res) \ No newline at end of file diff --git a/lab20/Sadovnikov/lab20.py b/lab20/Sadovnikov/lab20.py new file mode 100644 index 0000000..ed63956 --- /dev/null +++ b/lab20/Sadovnikov/lab20.py @@ -0,0 +1,33 @@ +__author__ = 'alexkane' + +from sys import stdin + +if __name__ == "__main__": + number_of_sticks = int(stdin.readline().rstrip('\n')) + second_line = stdin.readline().rstrip('\n') + sticks = [int(x) for x in second_line.split(' ')] + +def execute(number_of_sticks, sticks): + max = 0 + if number_of_sticks < 4: + return max + else: + sticks.sort() + sticks.reverse() + current_max = 0 + i = 1 + while i < len(sticks): + if sticks[i - 1] - sticks[i] <= 1: + if current_max == 0: + current_max = sticks[i] + i += 1 + else: + max += current_max * sticks[i] + current_max = 0 + i += 1 + i += 1 + return max + + +if __name__ == "__main__": + print(execute(number_of_sticks, sticks)) \ No newline at end of file diff --git a/lab20/Sadovnikov/tests4lab20.py b/lab20/Sadovnikov/tests4lab20.py new file mode 100644 index 0000000..90cb110 --- /dev/null +++ b/lab20/Sadovnikov/tests4lab20.py @@ -0,0 +1,20 @@ +__author__ = 'alexkane' + +import unittest +import lab20 + +class TestIlya(unittest.TestCase): + def test_trivial0(self): + expected = 0 + res = lab20.execute(4, [2, 3, 5, 2]) + self.assertEqual(expected, res) + + def test_trivial1(self): + expected = 8 + res = lab20.execute(4, [2, 2, 4, 4]) + self.assertEqual(expected, res) + + def test_trivial2(self): + expected = 2222222222 + res = lab20.execute(9, [1111111111, 1, 3, 200, 100, 7, 1111111111, 2, 2]) + self.assertEqual(expected, res) \ No newline at end of file diff --git a/lab4/Sadovnikov/Task4.py b/lab4/Sadovnikov/lab4.py similarity index 87% rename from lab4/Sadovnikov/Task4.py rename to lab4/Sadovnikov/lab4.py index 1cc5e04..3d34205 100644 --- a/lab4/Sadovnikov/Task4.py +++ b/lab4/Sadovnikov/lab4.py @@ -1,10 +1,9 @@ import sys - -__author__ = 'vmath' from sys import stdin -line = stdin.readline() -input = [int(s) for s in line.split(' ')] +if __name__ == "__main__": + line = stdin.readline() + input = [int(s) for s in line.split(' ')] def sort(a): @@ -62,8 +61,7 @@ def merge(a, p, q, r): j += 1 -sort(input) - -for x in input: - print x, - +if __name__ == "__main__": + sort(input) + for x in input: + print x, diff --git a/lab5/Sadovnikov/lab5.py b/lab5/Sadovnikov/lab5.py index c3aa4fc..bea0273 100644 --- a/lab5/Sadovnikov/lab5.py +++ b/lab5/Sadovnikov/lab5.py @@ -1,8 +1,10 @@ from sys import stdin from random import randint -line = stdin.readline() -input = [int(s) for s in line.split(' ')] +if __name__ == "__main__": + line = stdin.readline() + input = [int(s) for s in line.split(' ')] + def quick_sort(array, beginning_index, last_index): if beginning_index < last_index: @@ -36,7 +38,7 @@ def randomized_partition(array, beginning_index, last_index): return partition(array, beginning_index, last_index) -quick_sort(input, 0, len(input) - 1) - -for x in input: - print x, +if __name__ == "__main__": + quick_sort(input, 0, len(input) - 1) + for x in input: + print x, diff --git a/lab6/Sadovnikov/lab6.py b/lab6/Sadovnikov/lab6.py new file mode 100644 index 0000000..73cbb25 --- /dev/null +++ b/lab6/Sadovnikov/lab6.py @@ -0,0 +1,55 @@ +from sys import stdin + +if __name__ == "main": + line = stdin.readline() + input = [int(s) for s in line.split(' ')] + + +def stable_sort(input, index, k): + if index > 0: + array = [(input[i] / (10 ** index)) % 10 for i in range(len(input))] + else: + array = [input[i] % 10 for i in range(len(input))] + c = (k + 1) * [0] + output = len(input) * [0] + for i in range(len(input)): + c[array[i]] += 1 + for i in range(1, k + 1): + c[i] += c[i - 1] + for j in range(len(input) - 1, -1, -1): + c[array[j]] -= 1 + output[c[array[j]]] = input[j] + for i in range(len(input)): + input[i] = output[i] + + +def radix_sort(input): + d = 0 + positive = [] + negative = [] + for i in range(len(input)): + if input[i] >= 0: + positive.append(input[i]) + else: + negative.append(input[i]) + for x in range(len(positive)): + while positive[x] / (10 ** d) != 0: + d += 1 + for i in range(0, d): + stable_sort(positive, i, 9) + d = 0 + for x in range(len(negative)): + while negative[x] / -(10 ** d) != 0: + d += 1 + for i in range(0, d): + stable_sort(negative, i, 9) + input = negative + positive + if __name__ == "__main__": + for x in range(len(input)): + print input[x], + else: + return input + + +if __name__ == "__main__": + radix_sort(input) diff --git a/lab6/Sadovnikov/tests4lab6.py b/lab6/Sadovnikov/tests4lab6.py new file mode 100644 index 0000000..32ae373 --- /dev/null +++ b/lab6/Sadovnikov/tests4lab6.py @@ -0,0 +1,61 @@ +from random import randint +import unittest +import lab6 # import radix_sort.py from current directory + +class TestSorting(unittest.TestCase): + + def test_trivial(self): + arr = [1, 333, 22] + res = lab6.radix_sort(arr) + expected = [1, 22, 333] + + self.assertEqual(expected, res) + + def test_empty(self): + arr = [] + res = lab6.radix_sort(arr) + expected = [] + self.assertEqual(expected, res) + + def test_consecutive_integers(self): + arr = [i for i in range(100000)] + res = lab6.radix_sort(arr) + subject = 0 + for x in range (100000): + if res[x] >= subject: + subject = res[x] + else: + subject = -1 + break + self.assertTrue(subject != -1) + + def test_not_trivial(self): + arr = [randint(0, 100000) for i in range(100000)] + res = lab6.radix_sort(arr) + subject = 0 + for x in range (100000): + if res[x] >= subject: + subject = res[x] + else: + subject = -1 + break + self.assertTrue(subject != -1) + + def test_same_element(self): + arr = [1000000 for i in range(100000)] + res = lab6.radix_sort(arr) + subject = 0 + for x in range (100000): + if res[x] >= subject: + subject = res[x] + else: + subject = -1 + break + self.assertTrue(subject != -1) + + def test_negative_element(self): + arr = [-1, -333, -22] + res = lab6.radix_sort(arr) + expected = [-333, -22, -1] + + self.assertEqual(expected, res) \ No newline at end of file diff --git a/lab7/Sadovnikov/lab7.py b/lab7/Sadovnikov/lab7.py new file mode 100644 index 0000000..a97a892 --- /dev/null +++ b/lab7/Sadovnikov/lab7.py @@ -0,0 +1,91 @@ +# coding=utf-8 +from random import randint +import time +import sys + +__author__ = 'alexkane' +sys.path.insert(0, '/home/alexkane/PyRepository/lab4/Sadovnikov') +sys.path.insert(0, '/home/alexkane/PyRepository/lab5/Sadovnikov') +sys.path.insert(0, '/home/alexkane/PyRepository/lab6/Sadovnikov') +import matplotlib.pyplot as plt +import lab4 +import lab5 +import lab6 + +funcs = 4 * [None] +funcs[0] = lambda x: x.sort() +funcs[1] = lambda x: lab4.sort(x) +funcs[2] = lambda x: lab5.quick_sort(x, 0, len(x) - 1) +funcs[3] = lambda x: lab6.radix_sort(x) + +array_of_names = 4 * [None] +array_of_names[0] = "Basic" +array_of_names[1] = "Merge/insert" +array_of_names[2] = "Quick" +array_of_names[3] = "Radix" + + +def universal_plot(fig, ax, term): + xrng = xrange(100, 1000000, 100000) + t = 0 + for func in funcs: + array_of_medians = 10 * [None] + m = 0 + for k in xrng: + if term == 0: + array = [randint(-1000000, 1000000) for x in range(k)] + elif term == 1: + array = [randint(0, 10000) for x in range(k)] + elif term == 2: + array = [] + counter = k + while counter > 0: + len = randint(0, counter) + addition = randint(0, 9900) + sub_array = [x + addition for x in range(len)] + array += sub_array + counter -= len + len = randint(0, counter) + addition = randint(0, 9900) + sub_array = [len - 1 - x + addition for x in range(len)] + array += sub_array + counter -= len + elif term == 3: + array = [x for x in range(k)] + array.sort() + elif term == 4: + array = [x for x in range(k)] + array.reverse() + else: + const = randint(0, 10000) + array = [const for x in range(k)] + array_of_time = 5 * [None] + print "getting median" + for i in range(5): + beginning = time.time() + func(array) + array_of_time[i] = time.time() - beginning + array_of_time.sort() + array_of_medians[m] = array_of_time[2] + m += 1 + print array_of_medians + ax.plot(xrng, array_of_medians, label=array_of_names[t]) + t += 1 + ax.set_xlabel('size', fontsize=8) + ax.set_ylabel('ms', fontsize=8) + ax.set_title('plot name', fontsize=8) + + +fig = plt.figure() +term = 0 +for i in range(2): + for j in xrange(3): + print "Task started" + ax = plt.subplot2grid((2, 3), (i, j)) + universal_plot(fig, ax, term) + print "Task completed" + term += 1 + +plt.legend(loc='upper left', title="funcs") +plt.tight_layout() +plt.show() diff --git a/lab8/Sadovnikov/lab8.py b/lab8/Sadovnikov/lab8.py new file mode 100644 index 0000000..7715493 --- /dev/null +++ b/lab8/Sadovnikov/lab8.py @@ -0,0 +1,99 @@ +__author__ = 'alexkane' + + +class Queue: + def pop(self): + pass + + def push(self, n): + pass + + def size(self): + pass + + +class StacksQueue(Queue): + def __init__(self): + self._stack1 = [] + self._stack2 = [] + self._direction = True + + def pop(self): + if self._direction: + for i in range(len(self._stack1) - 1): + self._stack2.append(self._stack1.pop()) + value = self._stack1.pop() + self._direction = False + self._private_write(value) + else: + if len(self._stack2) != 0: + value = self._stack2.pop() + self._private_write(value) + elif len(self._stack1) != 0: + self._direction = True + value = self.pop() + else: + value = "empty" + print "empty" + return value + + def _private_calc(self, n): + print("push! " + str(n)) + + def _private_write(self, n): + print("pop! " + str(n)) + + def _private_write_size(self, n): + print("size! " + str(n)) + + def push(self, n): + self._stack1.append(n) + self._private_calc(n) + + def size(self): + self._private_write_size(len(self._stack1) + len(self._stack2)) + return len(self._stack1) + len(self._stack2) + + +class MaxElementQueue(Queue): + def __init__(self): + self._max = None + self._queue = StacksQueue() + self._tricky_list = [] + self._former_length = 0 + self._max_index = 0 + + def pop(self): + self._max_index += 1 + if self._max_index < len(self._tricky_list): + self._max = self._tricky_list[self._max_index] + return self._queue.pop() + + def push(self, n): + if self._max == None: + self._max = n + self._former_length += 1 + if n > self._max: + self._max = n + self._tricky_list = (len(self._tricky_list) + 1) * [self._max] + self._former_length = len(self._tricky_list) + 1 + else: + self._tricky_list.append(n) + k = 1 + for i in range(len(self._tricky_list) - 2, self._former_length - 1, -1): + if self._tricky_list[i] >= n: + break + else: + k += 1 + if k != 1: + self._tricky_list = self._tricky_list[:self._former_length + self._max_index] + k * [n] + return self._queue.push(n) + + def size(self): + return self._queue.size() + + def max(self): + if self._max_index < len(self._tricky_list): + return self._tricky_list[self._max_index] + else: + return "empty" diff --git a/lab8/Sadovnikov/tests4lab8.py b/lab8/Sadovnikov/tests4lab8.py new file mode 100644 index 0000000..5ae523d --- /dev/null +++ b/lab8/Sadovnikov/tests4lab8.py @@ -0,0 +1,68 @@ +from random import randint +import unittest +import lab8 + +class TestSorting(unittest.TestCase): + + def test_trivial(self): + queue = lab8.MaxElementQueue() + queue.push(8) + queue.push(1) + queue.push(3) + queue.push(4) + queue.push(2) + queue.push(0) + queue.push(0) + queue.push(0) + res = queue.max() + expected = 8 + self.assertEqual(expected, res) + + def test_trivial_2(self): + queue = lab8.MaxElementQueue() + queue.push(8) + queue.push(1) + queue.push(3) + queue.push(4) + queue.push(2) + queue.push(0) + queue.push(0) + queue.push(0) + queue.pop() + queue.pop() + queue.pop() + queue.pop() + queue.pop() + queue.pop() + res = queue.max() + expected = 0 + self.assertEqual(expected, res) + + + def test_trivial_3(self): + queue = lab8.MaxElementQueue() + queue.push(8) + queue.push(1) + queue.push(3) + queue.push(4) + queue.push(2) + queue.push(0) + queue.push(0) + queue.push(0) + queue.pop() + queue.pop() + queue.pop() + queue.push(3) + res = queue.max() + expected = 4 + self.assertEqual(expected, res) + + + def test_erofeev(self): + queue = lab8.MaxElementQueue() + queue.push(3) + queue.push(42) + self.assertEqual(42, queue.max()) + self.assertEqual(3, queue.pop()) + self.assertEqual(42, queue.max()) + self.assertEqual(42, queue.pop()) \ No newline at end of file diff --git a/lab9/Sadovnikov/lab9part1.py b/lab9/Sadovnikov/lab9part1.py new file mode 100644 index 0000000..9ad0406 --- /dev/null +++ b/lab9/Sadovnikov/lab9part1.py @@ -0,0 +1,58 @@ +from sys import stdin + +if __name__ == "__main__": + the_k = int(stdin.readline()) + line = stdin.readline() + input = [int(s) for s in line.split(' ')] + + +class HeapPriorityQueue: + def __init__(self): + self._number_of_max = 0 + self._array = [] + + def insert(self, key): + if len(self._array) < self._number_of_max: + self._array.append((-1) * float('inf')) + self.increase_key(len(self._array) - 1, key) + else: + self.increase_key(0, key) + self._min_heapify(self._array, 0) + + def increase_key(self, i, key): + if key > self._array[i]: + self._array[i] = key + while i > 0 and self._array[i / 2] > self._array[i]: + self._array[i], self._array[i / 2] = self._array[i / 2], self._array[i] + i /= 2 + + def _min_heapify(self, array, i): + l = i * 2 + r = i * 2 + 1 + if l < len(array) and array[l] < array[i]: + smallest = l + else: + smallest = i + if r < len(array) and array[r] < array[smallest]: + smallest = r + if smallest != i: + array[i], array[smallest] = array[smallest], array[i] + self._min_heapify(array, smallest) + + def set_special_k(self, k): + self._number_of_max = k + + def get_array(self): + for i in range(len(self._array)): + print self._array[i], + return self._array + + +if __name__ == "__main__": + heap = HeapPriorityQueue() + heap.set_special_k(the_k) + + for i in range(len(input)): + heap.insert(input[i]) + + heap.get_array() diff --git a/lab9/Sadovnikov/lab9part2.py b/lab9/Sadovnikov/lab9part2.py new file mode 100644 index 0000000..91c0e13 --- /dev/null +++ b/lab9/Sadovnikov/lab9part2.py @@ -0,0 +1,66 @@ +__author__ = 'alexkane' + +from sys import stdin + + + +if __name__ == "__main__": + special_k = int(stdin.readline()) + line = stdin.readline() + input = [int(s) for s in line.split(' ')] + + +def quick_search(array, beginning_index, last_index): + if beginning_index < last_index: + new_index = find_pivot(array, beginning_index, last_index) + quick_search(array, new_index + 1, last_index) + quick_search(array, beginning_index, new_index) + return array + +def find_pivot(array, beginning_index, last_index): + array_counter = 0 + if last_index - beginning_index < 5: + n = last_index - beginning_index + else: + n = 5 + local_array = [] + array_of_medians = [] + for k in range(beginning_index, last_index): + if array_counter < n - 1: + local_array.append(array[k]) + array_counter += 1 + else: + local_array.append(array[k]) + array_counter = 0 + for j in range(0, n): + key = local_array[j] + i = j - 1 + while i >= 0 and local_array[i] > key: + local_array[i + 1] = local_array[i] + i -= 1 + local_array[i + 1] = key + if last_index - k < n: + n = last_index - k - 1 + array_of_medians.append(local_array[len(local_array) / 2]) + local_array = [] + array_of_medians.sort() + the_median = array_of_medians[len(array_of_medians) / 2] + array[array.index(the_median)], array[last_index - 1] = array[last_index - 1], array[array.index(the_median)] + return partition(array, beginning_index, last_index - 1) + + +def partition(array, beginning_index, last_index): + default_variable = array[last_index] + i = beginning_index - 1 + for j in range(beginning_index, last_index): + if array[j] <= default_variable: + i += 1 + array[i], array[j] = array[j], array[i] + array[i + 1], array[last_index] = array[last_index], array[i + 1] + return i + 1 + + +if __name__ == "__main__": + quick_search(input, 0, len(input)) + for x in range(len(input) - special_k, len(input)): + print input[x], diff --git a/lab9/Sadovnikov/tests4lab9.py b/lab9/Sadovnikov/tests4lab9.py new file mode 100644 index 0000000..7d410a8 --- /dev/null +++ b/lab9/Sadovnikov/tests4lab9.py @@ -0,0 +1,36 @@ +from random import randint, random + +__author__ = 'alexkane' + +import unittest +import lab9 +import lab9part2 + +class TestSorting(unittest.TestCase): + + def part1_test_trivial(self): + queue = lab9.HeapPriorityQueue() + queue.set_special_k(1) + queue.insert(2) + queue.insert(1) + expected = 2 + res = queue.get_array()[0] + self.assertEqual(expected, res) + + def part1_test_hardcore(self): + queue = lab9.HeapPriorityQueue() + queue.set_special_k(10) + for i in range(101): + if i % 10 != 0: + queue.insert(randint(-1000, 1000)) + else: + queue.insert(1111) + expected = [1111, 1111, 1111, 1111, 1111, 1111, 1111, 1111, 1111, 1111] + res = queue.get_array() + self.assertEqual(expected, res) + + def part2_test_trivial(self): + array = [1, 0, 8, 2, 1] + res = lab9part2.quick_search(array, 0, 5) + expected = [0, 1, 1, 2, 8] + self.assertEqual(expected, res) \ No newline at end of file diff --git a/task6.txt b/task6.txt new file mode 100644 index 0000000..0f2416e --- /dev/null +++ b/task6.txt @@ -0,0 +1 @@ +Something