Skip to content

Commit 07674ff

Browse files
committed
git-training
0 parents  commit 07674ff

File tree

9 files changed

+297
-0
lines changed

9 files changed

+297
-0
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Git GitHub Training
2+
3+
4+
### Training Environment
5+
6+
1. Install Git ([git](https://git-scm.com/downloads), [source tree](https://www.sourcetreeapp.com))
7+
2. Editor ([atom](https://atom.io/), [sublime text](https://www.sublimetext.com/3))
8+
3. Github account ([sign up](https://github.com/join))
9+
4. Download [the courage](https://www.dropbox.com/s/36ifeasvhhshqj8/you_can_do_git?dl=1&pv=1)
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/python
2+
import random
3+
4+
test_script = open("test.sh","w")
5+
6+
test_script.write("#!/bin/bash\n")
7+
test_script.write("echo -e \"")
8+
9+
nr = random.randrange(1,1000)
10+
w = random.randrange(1,10000)
11+
12+
test_script.write(str(nr)+" "+str(w)+"\\n\\\n")
13+
14+
15+
for i in range(1,nr-1):
16+
j_p = random.randrange(1,w)
17+
j_w = random.randrange(1,w)
18+
test_script.write(str(j_p)+" "+str(j_w)+"\\n\\\n")
19+
20+
j_p = random.randrange(1,w)
21+
j_w = random.randrange(1,w)
22+
test_script.write(str(j_p)+" "+str(j_w)+"\\n\" | ./a.out")

packing_knapsack/knapsack_problem.pdf

487 KB
Binary file not shown.

packing_knapsack/packing_knapsack.c

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Packing knapsack question
3+
*
4+
* Copyright (C) 2016, Taeung Song <[email protected]>
5+
*
6+
*/
7+
#include <stdio.h>
8+
#include <stdbool.h>
9+
#include <stdlib.h>
10+
#include <string.h>
11+
12+
#define MAX_INPUT 16
13+
14+
struct knapsack {
15+
unsigned int maxprice;
16+
} *knapsack_list;
17+
18+
struct jewelry {
19+
unsigned int wgt;
20+
unsigned int price;
21+
};
22+
23+
unsigned int limited_wgt;
24+
25+
unsigned int get_cond_maxprice(int wgt, struct jewelry *jewelry)
26+
{
27+
/* Get maximum price based on a specific weight
28+
* following a specific jewelry.
29+
*/
30+
int i;
31+
int rest_wgt = wgt - jewelry->wgt;
32+
int price = jewelry->price + knapsack_list[rest_wgt].maxprice;
33+
34+
return knapsack_list[wgt].maxprice < price ?
35+
price : knapsack_list[wgt].maxprice;
36+
}
37+
38+
void pack_knapsack(struct jewelry *jewelry)
39+
{
40+
/* Case by case pack knapsack following maximum
41+
* price per limited weight.
42+
*/
43+
int wgt;
44+
45+
if (limited_wgt < jewelry->wgt)
46+
return;
47+
48+
for (wgt = 0; wgt <= limited_wgt; wgt++) {
49+
if (jewelry->wgt <= wgt) {
50+
unsigned int maxprice = get_cond_maxprice(wgt, jewelry);
51+
52+
if (knapsack_list[wgt].maxprice < maxprice)
53+
knapsack_list[wgt].maxprice = maxprice;
54+
}
55+
}
56+
}
57+
58+
bool get_values_from(char *input, unsigned int *val1, unsigned int *val2)
59+
{
60+
char *arg;
61+
char *ptr = strdup(input);
62+
63+
if (!ptr) {
64+
printf("%s: strdup failed\n", __func__);
65+
exit(1);
66+
}
67+
68+
arg = strsep(&ptr, " ");
69+
*val1 = atoi(arg);
70+
if (ptr == NULL) {
71+
printf("Error: Need a whitespace\n");
72+
return false;
73+
}
74+
75+
*val2 = atoi(ptr);
76+
77+
return true;
78+
}
79+
80+
int main(int argc, const char **argv)
81+
{
82+
int i;
83+
struct jewelry *jewels;
84+
char input[MAX_INPUT];
85+
unsigned int nr_jewels;
86+
87+
fgets(input, sizeof(input), stdin);
88+
if (get_values_from(input, &nr_jewels, &limited_wgt) == false)
89+
return -1;
90+
91+
jewels = malloc(sizeof(struct jewelry) * nr_jewels);
92+
for (i = 0; i < nr_jewels; i++) {
93+
bool ret;
94+
95+
fgets(input, sizeof(input), stdin);
96+
ret = get_values_from(input, &jewels[i].wgt,
97+
&jewels[i].price);
98+
if (ret == false)
99+
return -1;
100+
}
101+
102+
/* from 0 to last limited weight */
103+
knapsack_list = malloc(sizeof(struct knapsack) * (limited_wgt + 1));
104+
105+
for (i = 0; i <= limited_wgt; i++)
106+
knapsack_list[i].maxprice = 0;
107+
108+
for (i = 0; i < nr_jewels; i++)
109+
pack_knapsack(&jewels[i]);
110+
111+
printf("%d\n", knapsack_list[limited_wgt].maxprice);
112+
free(jewels);
113+
free(knapsack_list);
114+
return 0;
115+
}

packing_knapsack/test.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
echo -e "4 14\n\
4+
2 40\n\
5+
5 110\n\
6+
10 200\n\
7+
3 50\n" | ./$1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/python
2+
import random
3+
4+
test_script = open("test.sh","w")
5+
6+
test_script.write("#!/bin/bash\n")
7+
test_script.write("echo -e \"")
8+
9+
nr = random.randrange(1,1000)
10+
w = random.randrange(1,10000)
11+
12+
test_script.write(str(nr)+" "+str(w)+"\\n\\\n")
13+
14+
15+
for i in range(1,nr-1):
16+
j_p = random.randrange(1,w)
17+
j_w = random.randrange(1,w)
18+
test_script.write(str(j_p)+" "+str(j_w)+"\\n\\\n")
19+
20+
j_p = random.randrange(1,w)
21+
j_w = random.randrange(1,w)
22+
test_script.write(str(j_p)+" "+str(j_w)+"\\n\" | ./a.out")
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Packing knapsack question
3+
*
4+
* Copyright (C) 2016, Taeung Song <[email protected]>
5+
*
6+
*/
7+
#include <stdio.h>
8+
#include <stdbool.h>
9+
#include <stdlib.h>
10+
#include <string.h>
11+
12+
#define MAX_INPUT 16
13+
14+
struct knapsack {
15+
unsigned int maxprice;
16+
} *knapsack_list;
17+
18+
struct jewelry {
19+
unsigned int wgt;
20+
unsigned int price;
21+
};
22+
23+
unsigned int limited_wgt;
24+
25+
unsigned int get_cond_maxprice(int wgt, struct jewelry *jewelry)
26+
{
27+
/* Get maximum price based on a specific weight
28+
* following a specific jewelry.
29+
*/
30+
int i;
31+
int rest_wgt = wgt - jewelry->wgt;
32+
int price = jewelry->price + knapsack_list[rest_wgt].maxprice;
33+
34+
return knapsack_list[wgt].maxprice < price ?
35+
price : knapsack_list[wgt].maxprice;
36+
}
37+
38+
void pack_knapsack(struct jewelry *jewelry)
39+
{
40+
/* Case by case pack knapsack following maximum
41+
* price per limited weight.
42+
*/
43+
int wgt;
44+
45+
if (limited_wgt < jewelry->wgt)
46+
return;
47+
48+
for (wgt = 0; wgt <= limited_wgt; wgt++) {
49+
if (jewelry->wgt <= wgt) {
50+
unsigned int maxprice = get_cond_maxprice(wgt, jewelry);
51+
52+
if (knapsack_list[wgt].maxprice < maxprice)
53+
knapsack_list[wgt].maxprice = maxprice;
54+
}
55+
}
56+
}
57+
58+
bool get_values_from(char *input, unsigned int *val1, unsigned int *val2)
59+
{
60+
char *arg;
61+
char *ptr = strdup(input);
62+
63+
if (!ptr) {
64+
printf("%s: strdup failed\n", __func__);
65+
exit(1);
66+
}
67+
68+
arg = strsep(&ptr, " ");
69+
*val1 = atoi(arg);
70+
if (ptr == NULL) {
71+
printf("Error: Need a whitespace\n");
72+
return false;
73+
}
74+
75+
*val2 = atoi(ptr);
76+
77+
return true;
78+
}
79+
80+
int main(int argc, const char **argv)
81+
{
82+
int i;
83+
struct jewelry *jewels;
84+
char input[MAX_INPUT];
85+
unsigned int nr_jewels;
86+
87+
fgets(input, sizeof(input), stdin);
88+
if (get_values_from(input, &nr_jewels, &limited_wgt) == false)
89+
return -1;
90+
91+
jewels = malloc(sizeof(struct jewelry) * nr_jewels);
92+
for (i = 0; i < nr_jewels; i++) {
93+
bool ret;
94+
95+
fgets(input, sizeof(input), stdin);
96+
ret = get_values_from(input, &jewels[i].wgt,
97+
&jewels[i].price);
98+
if (ret == false)
99+
return -1;
100+
}
101+
102+
/* from 0 to last limited weight */
103+
knapsack_list = malloc(sizeof(struct knapsack) * (limited_wgt + 1));
104+
105+
for (i = 0; i <= limited_wgt; i++)
106+
knapsack_list[i].maxprice = 0;
107+
108+
for (i = 0; i < nr_jewels; i++)
109+
pack_knapsack(&jewels[i]);
110+
111+
printf("%d\n", knapsack_list[limited_wgt].maxprice);
112+
free(jewels);
113+
free(knapsack_list);
114+
return 0;
115+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
echo -e "4 14\n\
4+
2 40\n\
5+
5 110\n\
6+
10 200\n\
7+
3 50\n" | ./$1

0 commit comments

Comments
 (0)