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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/python
import random

test_script = open("test.sh","w")

test_script.write("#!/bin/bash\n")
test_script.write("echo -e \"")

nr = random.randrange(1,1000)
w = random.randrange(1,10000)

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


for i in range(1,nr-1):
j_p = random.randrange(1,w)
j_w = random.randrange(1,w)
test_script.write(str(j_p)+" "+str(j_w)+"\\n\\\n")

j_p = random.randrange(1,w)
j_w = random.randrange(1,w)
test_script.write(str(j_p)+" "+str(j_w)+"\\n\" | ./a.out")
Binary file not shown.
96 changes: 96 additions & 0 deletions pull_reqeust_test/Dev-TenC/packing_knapsack/packing_knapsack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Packing knapsack question
*
* Copyright (C) 2016, Taeung Song <treeze.taeung@gmail.com>
*
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#define MAX_INPUT 16

struct knapsack {
unsigned int limited_wgt;
unsigned int max_price;
};

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

void pack_knapsack(struct knapsack *knapsack_list,
struct jewelry **usable_jewels)
{
/* Case by case pack knapsack following maximum
* price per limited weight.
*/
}

int get_maxprice(struct knapsack *knapsack_list, struct jewelry *jewelry)
{
/* Get maximum price based on limited weight */
}

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

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

*val2 = atoi(ptr);

if (*val1 == 0 || *val2 == 0) {
printf("Error: Input have to contain only numbers\n");
return false;
}

return true;
}

int main(int argc, const char **argv)
{
char input[MAX_INPUT];
char *ptr = input;
int nr_jewels, limited_wgt;
struct jewelry *jewels;
struct knapsack *knapsack_list;

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 (int 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;
}

knapsack_list = malloc(sizeof(struct knapsack) * limited_wgt);

for (int i = 0; i < nr_jewels; i++) {
struct jewelry **usable_jewels = malloc(sizeof(struct jewelry *)
* (i + 1));

for (int j = 0; j <= i; j++)
usable_jewels[i] = &jewels[j];
pack_knapsack(knapsack_list, usable_jewels);
free(usable_jewels);
}

printf("%d\n", knapsack_list[limited_wgt-1].max_price);
free(jewels);
free(knapsack_list);
return 0;
}
7 changes: 7 additions & 0 deletions pull_reqeust_test/Dev-TenC/packing_knapsack/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

echo -e "4 14\n\
2 40\n\
5 110\n\
10 200\n\
3 50\n" | ./$1
83 changes: 32 additions & 51 deletions pull_reqeust_test/taeung/packing_knapsack/packing_knapsack.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,84 +12,62 @@
#define MAX_INPUT 16

struct knapsack {
unsigned int maxprice;
} *knapsack_list;
unsigned int limited_wgt;
unsigned int max_price;
};

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)
void pack_knapsack(struct knapsack *knapsack_list,
struct jewelry **usable_jewels)
{
/* 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)
int get_maxprice(struct knapsack *knapsack_list, struct jewelry *jewelry)
{
char *arg;
char *ptr = strdup(input);
/* Get maximum price based on limited weight */
}

if (!ptr) {
printf("%s: strdup failed\n", __func__);
exit(1);
}
bool get_values_from(char *input, int *val1, int *val2)
{
char *ptr = input;

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

*val2 = atoi(ptr);

if (*val1 == 0 || *val2 == 0) {
printf("Error: Input have to contain only numbers\n");
return false;
}

return true;
}

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

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++) {
for (int i = 0; i < nr_jewels; i++) {
bool ret;

fgets(input, sizeof(input), stdin);
Expand All @@ -99,16 +77,19 @@ int main(int argc, const char **argv)
return -1;
}

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

for (i = 0; i <= limited_wgt; i++)
knapsack_list[i].maxprice = 0;
for (int i = 0; i < nr_jewels; i++) {
struct jewelry **usable_jewels = malloc(sizeof(struct jewelry *)
* (i + 1));

for (i = 0; i < nr_jewels; i++)
pack_knapsack(&jewels[i]);
for (int j = 0; j <= i; j++)
usable_jewels[i] = &jewels[j];
pack_knapsack(knapsack_list, usable_jewels);
free(usable_jewels);
}

printf("%d\n", knapsack_list[limited_wgt].maxprice);
printf("%d\n", knapsack_list[limited_wgt-1].max_price);
free(jewels);
free(knapsack_list);
return 0;
Expand Down