๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90

๐Ÿ Algorithm32

[Python] Lambda ํ™œ์šฉ - map, filter, reduce ํ•จ์ˆ˜ ๋žŒ๋‹ค(Lambda)์˜ ํ™œ์šฉ lambda ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ด๋ฆ„ ์—†๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. lambda ๋งค๊ฐœ๋ณ€์ˆ˜ : ํ‘œํ˜„์‹ ํ•จ์ˆ˜๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” ํ•จ์ˆ˜(map, filter, reduce)์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ•œ๋‹ค. ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ์— ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค. >>> (lambda x, y: x + y)(10, 20) 30 map ํ•จ์ˆ˜ map(function, iterable) ๋งค๊ฐœ๋ณ€์ˆ˜ function map์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ํ•จ์ˆ˜์ž๋ฆฌ์—๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ 1๊ฐœ์ธ ํ•จ์ˆ˜๋งŒ ์ด์šฉ๊ฐ€๋Šฅํ•˜๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ 1๊ฐœ๊ฐ€ ์•„๋‹Œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ TypeError๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. * ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ ์ •์˜ํ•œ ํ•จ์ˆ˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ณ€์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋„˜๊ธฐ๊ฑฐ๋‚˜, lambda๋ฅผ ์ด์šฉํ•ด ์ž‘์„ฑํ•œ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜ iterble ๋ฐ˜๋ณต๊ฐ€๋Šฅํ•œ ob.. 2020. 12. 7.
[Python] ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋ฌธ๋ฒ• ์ •๋ฆฌ Python ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋ฌธ๋ฒ• Python์—์„œ ๋ณ€์ˆ˜ _๋Š” ์ตœ๊ทผ ์ถœ๋ ฅ๋œ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ์žˆ์Œ string์—์„œ \์„ ๋ฌธ์ž๋กœ ํ‘œํ˜„์‹œ \\๊ณผ ๊ฐ™์ด \๋ฅผ ๋‘๋ฒˆ ์‚ฌ์šฉํ•˜์—ฌ escape์ฒ˜๋ฆฌ ์—ฌ๋Ÿฌ์ค„์˜ string์„ ์ž‘์„ฑํ•˜๋ ค๋ฉด """ ~ """ ํ˜น์€ ''' ~ ''' ์œผ๋กœ ์ž‘์„ฑ pass : ์•„๋ฌด ๋™์ž‘๋„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์Œ unicode string ์‚ฌ์šฉ์‹œ u'~~' ๋ฌธ์ž์—ด ๋‚ด๋ถ€์— ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž ์‚ฌ์šฉ์‹œ \nXXXX utf-8๋กœ ์ธ์ฝ”๋”ฉ์‹œ u'~~'.encode('utf-8') ์‹ค์ œ ์ธ์ฝ”๋”ฉ์‹œ ๋ช…ํ™•ํ•˜๊ฒŒ ํ‘œ์‹œํ•˜๊ธฐ ๋ฌธ๋ฒ• ์—๋Ÿฌ์™€ ์˜ˆ์™ธ Python์—์„œ ์—๋Ÿฌ๋Š” ํฌ๊ฒŒ SyntaxErrors(๋ฌธ๋ฒ• ์—๋Ÿฌ)์™€ Exceptions(์˜ˆ์™ธ)๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค. Exception์˜ ๊ฒฝ์šฐ์—๋Š” ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ๋“ค์„ except [.. 2020. 12. 7.
[์•Œ๊ณ ๋ฆฌ์ฆ˜] Day3 - 1967๋ฒˆ ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„ 00 ๋ฌธ์ œ 01 ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„?ํŠธ๋ฆฌ๋Š” ์‚ฌ์ดํด์ด ์—†๋Š” ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์ด๋ฉฐ, ์–ด๋–ค ๋‘ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•ด๋„ ๋‘˜ ์‚ฌ์ด์— ๊ฒฝ๋กœ๊ฐ€ ํ•ญ์ƒ ํ•˜๋‚˜๋งŒ ์กด์žฌํ•œ๋‹ค.ํŠธ๋ฆฌ์—์„œ ์–ด๋–ค ๋‘ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•ด์„œ ์–‘์ชฝ์œผ๋กœ ๋‹น๊ธฐ๋ฉด, ๊ฐ€์žฅ ๊ธธ๊ฒŒ ๋Š˜์–ด๋‚˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.์ด๋Ÿด ๋•Œ ํŠธ๋ฆฌ์˜ ๋ชจ๋“  ๋…ธ๋“œ๋“ค์€ ์ด ๋‘ ๋…ธ๋“œ๋ฅผ ์ง€๋ฆ„์˜ ๋์ ์œผ๋กœ ํ•˜๋Š” ์› ์•ˆ์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.์ด๋Ÿฐ ๋‘ ๋…ธ๋“œ ์‚ฌ์ด์˜ ๊ฒฝ๋กœ์˜ ๊ธธ์ด๋ฅผ ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„์ด๋ผ๊ณ  ํ•œ๋‹ค. ์ •ํ™•ํžˆ๋Š” ํŠธ๋ฆฌ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ๋กœ๋“ค ์ค‘ ๊ฐ€์žฅ ๊ธด ๊ฒฝ๋กœ์˜ ๊ธธ์ด๋ฅผ ๋งํ•œ๋‹ค.๊ฐ€์žฅ ๋จผ ๋‘ ์ •์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ or ๊ฐ€์žฅ ๋จผ ๋‘ ์ •์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒฝ๋กœ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. 02 ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„์„ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•ํŠธ๋ฆฌ์—์„œ ์ž„์˜์˜ ์ •์  x๋ฅผ ์žก๋Š”๋‹ค.์ •์  x์—์„œ ๊ฐ€์žฅ ๊นŠ์€ ๋…ธ๋“œ ์ค‘ ๊ฐ€์ค‘์น˜๊ฐ€ ๊ฐ€์žฅ ํฐ ์ •์  y๋ฅผ ์ฐพ๋Š”๋‹ค. -> DFS ์ด์šฉ์ •์  y์—์„œ ๊ฐ€์žฅ ๊นŠ์€ ๋…ธ๋“œ ์ค‘ ๊ฐ€์ค‘์น˜๊ฐ€.. 2018. 8. 8.
[์•Œ๊ณ ๋ฆฌ์ฆ˜] Day3 - 2309๋ฒˆ ์ผ๊ณฑ ๋‚œ์Ÿ์ด 00 ๋ฌธ์ œ 01 ์•„์ด๋””์–ด9๊ฐœ ์ค‘์— ์ˆœ์„œ๋Œ€๋กœ 7๊ฐœ๋ฅผ ๋ฝ‘์•„์„œ 100์ด ๋˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ  result ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.insert sort๋ฅผ ์ด์šฉํ•ด ์ •๋ ฌํ•ด ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.1234567 1234568 1234569 1234578 1234579 ์š”๋Ÿฐ ์ˆœ์„œ 02 ์ฝ”๋“œ #define scanf scanf_s#include "stdafx.h"#include #include #define N 9void find_seven_dewarf(int *height, int *result);void insertion_sort(int N_, int *num);int main(){ int i; int height[9] = { 0, }; int result[7] = { 0, }; for (i = 0; i < 9; i++) scan.. 2018. 8. 8.
[์•Œ๊ณ ๋ฆฌ์ฆ˜] Day3 - 1260๋ฒˆ DFS์™€ BFS 00 ๋ฌธ์ œ 01 DFS ์•Œ๊ณ ๋ฆฌ์ฆ˜?Depth First Search, ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰์ด๋ผ๋Š” ๋œป.ํ•˜๋‚˜์˜ ๋…ธ๋“œ์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ๋งˆ์ง€๋ง‰ ์ง€์ ๊นŒ์ง€ ๊ณ„์† ํƒ์ƒ‰ํ•˜๋Š” ํ–‰๋™์„ ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ฆ‰, ์‹œ์ž‘์ ์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ€์žฅ ์•„๋ž˜๊นŒ์ง€ ์ด๋™ํ•˜๋ฉด์„œ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ์‹ -> ์Šคํƒ์„ ์ด์šฉํ•˜์—ฌ ํ‘ธ๋Š” ๋ฐฉ์‹์Šคํƒ์— ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ณ , ๊ทธ ๊ฐ’์„ ํ†ตํ•ด ํ™•์ธํ•˜๋ฉฐ ์ข…๋ฃŒ ์กฐ๊ฑด์€ ์Šคํƒ์ด ๋น„์–ด์žˆ์„ ๋•Œ์ด๋‹ค.DFS์˜ ์žฅ์ ์€ ๊นŠ์€ ๊ณณ์— ์žˆ์„์ˆ˜๋ก ๋นจ๋ฆฌ ์ฐพ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ๋‹จ์ ์€ ์ž์นซํ•˜๋ฉด overflow๊ฐ€ ๋‚  ์ˆ˜๋„ ์žˆ๊ณ , ์–ป์–ด์ง„ ํ•ด๊ฐ€ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ผ๋Š” ๋ณด์žฅ์ด ์—†๋‹ค. 02 DFS ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„ ๋ฐฉ์‹์ธ์ ‘ํ•œ ๋…ธ๋“œ๋“ค์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด NxN ํฌ๊ธฐ์˜ adj_mat ๋ฐฐ์—ด๊ณผ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด Nx1 ํฌ๊ธฐ์˜ visited ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•œ๋‹ค. add_matrix ํ•จ์ˆ˜์—์„œ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ ์ž….. 2018. 8. 8.
[์•Œ๊ณ ๋ฆฌ์ฆ˜] Day2 - 2750๋ฒˆ ์ˆ˜ ์ •๋ ฌํ•˜๊ธฐ 01 ๋ฌธ์ œ 02 ์•Œ๊ณ ๋ฆฌ์ฆ˜์‚ฝ์ž…์ •๋ ฌ๊ณผ ๋ฒ„๋ธ”์ •๋ ฌ์— ๋Œ€ํ•˜์—ฌ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ํ–ˆ๋‹ค. 03 ์ฝ”๋“œ 1_ sort.h void insertion_sort(int N, int *num);void bubble_sort(int N, int *num);void print_num(int N, int *num); void insertion_sort(int N, int *num){ int i, j; for (i = 1; i = 0; j--) { if (num[key] 2018. 8. 4.
[์•Œ๊ณ ๋ฆฌ์ฆ˜] Day2 - 2775๋ฒˆ ๋ถ€๋…€ํšŒ์žฅ์ด ๋ ํ…Œ์•ผ 01 ๋ฌธ์ œ 02 ์•Œ๊ณ ๋ฆฌ์ฆ˜0์ธต, 1์ธต, 2์ธต … ์— ๋Œ€ํ•ด ๊ทœ์น™์„ ์ฐพ์•˜๋‹ค. 0์ธต์€ iํ˜ธ ์ผ ๋•Œ, i๋ช…์ด ์‚ด๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์—ˆ๊ณ  1์ธต ์ดํ›„๋ถ€ํ„ฐ๋Š” ํ˜„์žฌ ์ธต - 1์ธต์˜ ์ธ์›์„ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๊ณ„์‚ฐํ•ด์„œ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค. 03 ์ฝ”๋“œ #include "stdafx.h" int main(void){ int i, j, jj; int T, k, n; int residents[15][15] = { 0, }; // Test case T scanf_s("%d", &T); while (T>0) { // 1 2 3 // 1 3 6 // 1 4 10 printf("# Test case %d\n", T); scanf_s("%d", &k); scanf_s("%d", &n); // init zero floor printf("0 ์ธต :.. 2018. 8. 4.
[์•Œ๊ณ ๋ฆฌ์ฆ˜] Day1 - ๊ด„ํ˜ธ 01 ๋ฌธ์ œ 02 ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์„ ์Šคํƒ์— push, pop ํ•˜๋ฉด์„œ ๊ด„ํ˜ธ์˜ ์ง์ด ๋งž๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ๋ฌธ์ž์—ด์—์„œ ‘(‘ ์ผ ๋•Œ pushํ•œ ํšŸ์ˆ˜์™€ ‘)’์ผ ๋•Œ popํ•œ ํšŸ์ˆ˜๊ฐ€ ๊ฐ™์•„์•ผ ํ•œ๋‹ค. is_empty ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด pushํ•œ ๊ฒŒ ๋‹ค pop ๋˜์—ˆ๋Š”์ง€ ๋น„๊ตํ•˜๋Š”๋ฐ, pop์„ ๋” ๋งŽ์ด ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๊ณ ๋ คํ•ด์ฃผ์–ด์•ผ ํ•˜๋ฏ€๋กœ stack์—์„œ popํ•  ์š”์†Œ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ์— vps = -1๋กœ ํ•ด์ฃผ๊ณ , ์ด ๊ฐ’์ด ์ดˆ๊ธฐ๊ฐ’๊ณผ ๊ฐ™์€์ง€ ๋น„๊ตํ•จ. 03 ์ฝ”๋“œ #include #include #include #define LEN 100 typedef struct array_stack{ int arr[LEN]; int top;}array_stack; void stack_init(array_stack *pstack);void push(arra.. 2018. 8. 4.
[์•Œ๊ณ ๋ฆฌ์ฆ˜] Day1 - ์Šคํƒ 01 ๋ฌธ์ œ 02 ์•Œ๊ณ ๋ฆฌ์ฆ˜ - stack์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ๊ตฌ์กฐ์ฒด๋กœ ์ •์˜ํ•˜์˜€๋‹ค. stack์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” stack_init ํ•จ์ˆ˜์™€ ๋ช…๋ น์„ ์œ„ํ•œ push, pop, size, is_empty, top ํ•จ์ˆ˜ ์ƒ์„ฑ- strcmp ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ž์—ด์„ ๋น„๊ต, ๋ช…๋ น์„ ๊ตฌ๋ถ„ํ•œ๋‹ค. - push์˜ ๊ฒฝ์šฐ์—๋Š” ์ˆซ์ž์™€ ํ•จ๊ป˜ ์ž…๋ ฅ๋˜๋ฏ€๋กœ ๋„์–ด์“ฐ๊ธฐ๋ฅผ ํ† ํฐ์œผ๋กœ ์ธ์‹ํ•˜์—ฌ push ์™€ n์„ ๊ตฌ๋ถ„ํ•œ ํ›„ n์€ atoi(str -> int) ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ •์ˆ˜ ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•œ๋‹ค. 03 ์ฝ”๋“œ #include #include #include #define LEN 10000 typedef struct array_stack{ int arr[LEN]; int top;}array_stack; void stack_init(array_stack *pstack.. 2018. 8. 4.
728x90