Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Function Develop #35

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
### Training Environment

1. Download tutorial
- [tutorial v3](https://www.dropbox.com/s/jwpkfn5c8d1z74y/Git-training-v3.pdf?dl=1&pv=1)
- [tutorial v2](https://www.dropbox.com/s/6o5sfs1iyd9cxdq/Git-training-v2.pdf?dl=1&pv=1) & [example code v2](https://www.dropbox.com/sh/3ywkargf9xzcfoi/AAC63uvN4eQBQhT_1m4GmCMLa?dl=1&pv=1)
- [tutorial v1](https://www.dropbox.com/s/0bplreunw6vf69p/Git-training.pdf?dl=1&pv=1) & [example code v1](https://www.dropbox.com/sh/9q2emkhxmyckoj6/AAA_H55BVhfRvGHOs9j7l9N2a?dl=1&pv=1)
- new [tutorial v2](https://www.dropbox.com/s/6o5sfs1iyd9cxdq/Git-training-v2.pdf?dl=1&pv=1) & [example code v2](https://www.dropbox.com/sh/3ywkargf9xzcfoi/AAC63uvN4eQBQhT_1m4GmCMLa?dl=1&pv=1)
- old [tutorial v1](https://www.dropbox.com/s/0bplreunw6vf69p/Git-training.pdf?dl=1&pv=1) & [example code v1](https://www.dropbox.com/sh/9q2emkhxmyckoj6/AAA_H55BVhfRvGHOs9j7l9N2a?dl=1&pv=1)
2. Install Git ([git](https://git-scm.com/downloads), [source tree](https://www.sourcetreeapp.com))
3. Editor ([atom](https://atom.io/), [sublime text](https://www.sublimetext.com/3))
4. Github account ([sign up](https://github.com/join))
Expand All @@ -16,12 +15,12 @@
### Stage
1. New version (v2)
- Basic : Stage 1 ~ 7
- Advanced : Stage 8 ~ 11
- Advanced : Stage 8 ~ 15

2. Old version (v1)
- Basic : Stage 1 ~ 10
- Advanced : Stage 11 ~ 14

### The results (v1)
![screenshot](img/results_20160327.png)
[git-training.docs.google](https://docs.google.com/spreadsheets/d/1uPMCOKISMgj_svsoxG1LF1RbozA9RMKfx7h9vT80Atc/edit#gid=0)
[git-training.docs.google](https://docs.google.com/spreadsheets/d/1uPMCOKISMgj_svsoxG1LF1RbozA9RMKfx7h9vT80Atc/edit#gid=0)
115 changes: 115 additions & 0 deletions pull_reqeust_test/GiHwan/packing_knapsack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Packing knapsack question
*
* Copyright (C) 2016, Taeung Song <[email protected]>
*
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#define MAX_INPUT 16

struct knapsack {
unsigned int maxprice;
} *knapsack_list;

struct jewelry {
unsigned int wgt;
unsigned int price;
};

unsigned int limited_wgt;

unsigned int get_cond_maxprice(int wgt, struct jewelry *jewelry)
{
/* Get maximum price based on a specific weight
* following a specific jewelry.
*/
int i;
int rest_wgt = wgt - jewelry->wgt;
int price = jewelry->price + knapsack_list[rest_wgt].maxprice;

return knapsack_list[wgt].maxprice < price ?
price : knapsack_list[wgt].maxprice;
}

void pack_knapsack(struct jewelry *jewelry)
{
/* Case by case pack knapsack following maximum
* price per limited weight.
*/
int wgt;

if (limited_wgt < jewelry->wgt)
return;

for (wgt = 0; wgt <= limited_wgt; wgt++) {
if (jewelry->wgt <= wgt) {
unsigned int maxprice = get_cond_maxprice(wgt, jewelry);

if (knapsack_list[wgt].maxprice < maxprice)
knapsack_list[wgt].maxprice = maxprice;
}
}
}

bool get_values_from(char *input, unsigned int *val1, unsigned int *val2)
{
char *arg;
char *ptr = strdup(input);

if (!ptr) {
printf("%s: strdup failed\n", __func__);
exit(1);
}

arg = strsep(&ptr, " ");
*val1 = atoi(arg);
if (ptr == NULL) {
printf("Error: Need a whitespace\n");
return false;
}

*val2 = atoi(ptr);

return true;
}

int main(int argc, const char **argv)
{
int i;
struct jewelry *jewels;
char input[MAX_INPUT];
unsigned int nr_jewels;

fgets(input, sizeof(input), stdin);
if (get_values_from(input, &nr_jewels, &limited_wgt) == false)
return -1;

jewels = malloc(sizeof(struct jewelry) * nr_jewels);
for (i = 0; i < nr_jewels; i++) {
bool ret;

fgets(input, sizeof(input), stdin);
ret = get_values_from(input, &jewels[i].wgt,
&jewels[i].price);
if (ret == false)
return -1;
}

/* from 0 to last limited weight */
knapsack_list = malloc(sizeof(struct knapsack) * (limited_wgt + 1));

for (i = 0; i <= limited_wgt; i++)
knapsack_list[i].maxprice = 0;

for (i = 0; i < nr_jewels; i++)
pack_knapsack(&jewels[i]);

printf("%d\n", knapsack_list[limited_wgt].maxprice);
free(jewels);
free(knapsack_list);
return 0;
}
Binary file added quick_sort/a.out
Binary file not shown.
45 changes: 45 additions & 0 deletions quick_sort/generator_test_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/python
import random
import os
import platform

if platform.system() != 'Linux':
print "Can't generate test script on OS other than Linux"
exit()

def get_gcc_version():
return os.popen('gcc --version').readline().rstrip()

def get_gcc_options():
dirname = os.path.split(os.getcwd())[1]
filename = dirname + ".c"
return "gcc -ggdb " + filename + " -ansi -fno-asm -O2 -Wall -lm --static"

def get_os_info():
return platform.platform()

def test_script__new():
test_script = open("test.sh","w")
test_script.write("#!/bin/bash\n")
test_script.write("#\n# TEST ENVIRONMENT\n")
test_script.write("# gcc version: " + get_gcc_version() + "\n")
test_script.write("# gcc options: " + get_gcc_options() + "\n")
test_script.write("# os: " + get_os_info() + "\n\n")
return test_script

test_script = test_script__new()
test_script.write("echo -e \"")

nr = random.randrange(1, 50001)
order = random.randrange(0, 2)

test_script.write(str(nr)+"\\n\\\n")
test_script.write(str(order)+"\\n\\\n")

billion = 10 ** 9
for i in range(1,nr):
num = random.randrange(0, billion)
test_script.write(str(num)+"\\n\\\n")

num = random.randrange(1, billion)
test_script.write(str(num)+"\\n\" | ./$1")
118 changes: 118 additions & 0 deletions quick_sort/quick_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Quick sort question
*
* Copyright (C) 2016, Taeung Song <[email protected]>
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#define ASCEND true
#define DESCEND false

bool sort_order;

int *merge_arr(int *left, int med, int *right,
int lft_len, int rgt_len)
{
int *merged_arr = malloc(sizeof(int) * (lft_len + rgt_len + 1));

memcpy(merged_arr, left, sizeof(int) * lft_len);
merged_arr[lft_len] = med;
memcpy(&merged_arr[lft_len+1], right, sizeof(int) * rgt_len);

free(left);
free(right);
return merged_arr;
}

int split_arr(int *num_arr, int **left, int med,
int **right, int len)
{
int i, lft_idx, rgt_rev_idx;
int lft_len, rgt_len;
int *temp;

temp = malloc(sizeof(int) * len);
lft_idx = 0;
rgt_rev_idx = len - 1;

for (i = 1; i <= len; i++) {
int index;

if (med < num_arr[i])
index = sort_order == ASCEND ?
rgt_rev_idx-- : lft_idx++;
else
index = sort_order == ASCEND ?
lft_idx++ : rgt_rev_idx--;

temp[index] = num_arr[i];
}

lft_len = lft_idx;
if (lft_len != 0) {
*left = malloc(sizeof(int) * lft_len);
memcpy(*left, temp, sizeof(int) * lft_len);
} else
*left = NULL;

rgt_len = len - lft_len;
if (rgt_len != 0) {
*right = malloc(sizeof(int) * rgt_len);
memcpy(*right, &temp[lft_len], sizeof(int) * rgt_len);
} else
*right = NULL;

free(temp);
return lft_len;
}

int *quick_sort(int *num_arr, int len)
{
int med, lft_len, rgt_len;
int *left, *right;
if (len <= 1)
return num_arr;
else if (len == 2) {
if ((num_arr[0] < num_arr[1]) != sort_order) {
int temp = num_arr[0];

num_arr[0] = num_arr[1];
num_arr[1] = temp;
}
return num_arr;
}

med = num_arr[0];
lft_len = split_arr(num_arr, &left, med, &right, --len);
rgt_len = len - lft_len;
free(num_arr);

left = quick_sort(left, lft_len);
right = quick_sort(right, rgt_len);

return merge_arr(left, med, right, lft_len, rgt_len);
}

int main(int argc, const char **argv)
{
int *num_arr;
unsigned int len, order, i;

scanf("%d", &len);
num_arr = malloc(sizeof(int) * len);
scanf("%d", &order);
sort_order = order == 0 ? ASCEND : DESCEND;

for (i = 0; i < len; i++)
scanf("%d", &num_arr[i]);

num_arr = quick_sort(num_arr, len);
for (i = 0; i < len; i++)
printf("%d\n", num_arr[i]);

return 0;
}
Binary file added quick_sort/quick_sort_question.pdf
Binary file not shown.
14 changes: 14 additions & 0 deletions quick_sort/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
#
# TEST ENVIRONMENT
# gcc version: gcc (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010
# gcc options: gcc -ggdb cheese.c -ansi -fno-asm -O2 -Wall -lm --static
# os: Linux-4.5.0-rc4+-x86_64-with-Ubuntu-15.10-wily

echo -e "5\n\
0\n\
9\n\
2\n\
5\n\
1\n\
100\n" | ./$1