{"id":33662,"date":"2024-11-01T09:19:07","date_gmt":"2024-11-01T09:19:07","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33662"},"modified":"2024-11-01T11:47:13","modified_gmt":"2024-11-01T11:47:13","slug":"python-coding-test-course-gift-delivery","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33662\/","title":{"rendered":"Python Coding Test Course, Gift Delivery"},"content":{"rendered":"<p><body><\/p>\n<article>\n<section>\n<h2>Problem Description<\/h2>\n<p>One day, a special event for gift delivery was held in a village. The purpose of this event is to deliver gifts among different people. There are <strong>n<\/strong> participants, and each participant must deliver exactly one gift. However, there are constraints on whom they can deliver gifts to.<\/p>\n<p>Each participant has a list or array that indicates who they can deliver their gifts to. Considering these constraints, we need to determine whether all participants can deliver gifts, meaning whether every participant can receive a gift.<\/p>\n<h2>Input Format<\/h2>\n<p>The first line contains the number of participants <strong>n<\/strong>. The next line contains a list that represents the possible recipients for each of the <strong>n<\/strong> participants. Each element of the list expresses the index of the participant that the corresponding participant can deliver their gift to.<\/p>\n<h2>Output Format<\/h2>\n<p>If all participants can receive gifts, print &#8216;YES&#8217;; otherwise, print &#8216;NO&#8217;.<\/p>\n<h2>Example Input<\/h2>\n<pre>\n5\n1 2 3 4 0\n            <\/pre>\n<h2>Example Output<\/h2>\n<pre>\nYES\n            <\/pre>\n<h2>Solution Method<\/h2>\n<p>This problem can be viewed similarly to the &#8220;cycle detection&#8221; problem in graph theory. Each participant can be represented as a node, and the relationships of whom they can give gifts to can be expressed as edges, forming a directed graph. All nodes must be visited at least once, and if the graph has only one cycle, we can return &#8216;YES&#8217;.<\/p>\n<h2>Step-by-Step Solution Process<\/h2>\n<h3>1. Constructing the Graph<\/h3>\n<p>Based on the provided information, we construct the graph in list form. Each participant&#8217;s designated recipient is represented by their index in the list.<\/p>\n<h3>2. Using DFS or BFS<\/h3>\n<p>We can traverse the graph using DFS (Depth First Search) or BFS (Breadth First Search). The goal is to determine whether all participants can receive a gift. This involves checking each node for visit status while traversing the graph.<\/p>\n<h3>3. Cycle Detection<\/h3>\n<p>We check if a cycle exists that allows every participant to give and receive gifts. This cycle must include all nodes, and if there is exactly one cycle, everyone will be able to receive a gift.<\/p>\n<h3>4. Implementation<\/h3>\n<p>Based on the above methods, we implement a Python code:<\/p>\n<\/section>\n<pre>\ndef can_give_gifts(n, connections):\n    visited = [False] * n\n    count = 0\n\n    def dfs(v):\n        nonlocal count\n        visited[v] = True\n        count += 1\n        next_giver = connections[v]\n        if not visited[next_giver]:\n            dfs(next_giver)\n\n    for i in range(n):\n        if not visited[i]:\n            count = 0\n            dfs(i)\n            if count < n:\n                return \"NO\"\n\n    return \"YES\"\n\n# Test\nn = 5\nconnections = [1, 2, 3, 4, 0]\nprint(can_give_gifts(n, connections))\n        <\/pre>\n<h2>Code Explanation<\/h2>\n<p>The code above is an algorithm to verify gift delivery. The 'can_give_gifts' function takes the number of participants <strong>n<\/strong> and the list of possible gift receivers <strong>connections<\/strong> as arguments. A <strong>visited<\/strong> list is set up to check each participant's visit status. DFS is used to visit each participant, increasing the count. If all participants are visited, it returns 'YES'; otherwise, it returns 'NO'.<\/p>\n<h2>Comprehensive Example<\/h2>\n<p>Let's examine this algorithm in action through the following example:<\/p>\n<pre>\n6\n1 2 3 4 5 0\n        <\/pre>\n<h2>Expected Output<\/h2>\n<pre>\nYES\n        <\/pre>\n<h2>Conclusion<\/h2>\n<p>This problem is a common type encountered in Python coding tests. It helps in understanding graph concepts and the method of solving problems through cycle detection. By practicing various types of problems, you can enhance your insights into algorithms.<\/p>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description One day, a special event for gift delivery was held in a village. The purpose of this event is to deliver gifts among different people. There are n participants, and each participant must deliver exactly one gift. However, there are constraints on whom they can deliver gifts to. Each participant has a list &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33662\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python Coding Test Course, Gift Delivery&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[145],"tags":[],"class_list":["post-33662","post","type-post","status-publish","format-standard","hentry","category-python-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Coding Test Course, Gift Delivery - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/33662\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Coding Test Course, Gift Delivery - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description One day, a special event for gift delivery was held in a village. The purpose of this event is to deliver gifts among different people. There are n participants, and each participant must deliver exactly one gift. However, there are constraints on whom they can deliver gifts to. Each participant has a list &hellip; \ub354 \ubcf4\uae30 &quot;Python Coding Test Course, Gift Delivery&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33662\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:19:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:47:13+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33662\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33662\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Python Coding Test Course, Gift Delivery\",\"datePublished\":\"2024-11-01T09:19:07+00:00\",\"dateModified\":\"2024-11-01T11:47:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33662\/\"},\"wordCount\":467,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33662\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33662\/\",\"name\":\"Python Coding Test Course, Gift Delivery - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:19:07+00:00\",\"dateModified\":\"2024-11-01T11:47:13+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33662\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33662\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33662\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Coding Test Course, Gift Delivery\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Coding Test Course, Gift Delivery - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/33662\/","og_locale":"ko_KR","og_type":"article","og_title":"Python Coding Test Course, Gift Delivery - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description One day, a special event for gift delivery was held in a village. The purpose of this event is to deliver gifts among different people. There are n participants, and each participant must deliver exactly one gift. However, there are constraints on whom they can deliver gifts to. Each participant has a list &hellip; \ub354 \ubcf4\uae30 \"Python Coding Test Course, Gift Delivery\"","og_url":"https:\/\/atmokpo.com\/w\/33662\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:19:07+00:00","article_modified_time":"2024-11-01T11:47:13+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33662\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33662\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Python Coding Test Course, Gift Delivery","datePublished":"2024-11-01T09:19:07+00:00","dateModified":"2024-11-01T11:47:13+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33662\/"},"wordCount":467,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33662\/","url":"https:\/\/atmokpo.com\/w\/33662\/","name":"Python Coding Test Course, Gift Delivery - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:19:07+00:00","dateModified":"2024-11-01T11:47:13+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33662\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33662\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33662\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Python Coding Test Course, Gift Delivery"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=33662"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33662\/revisions"}],"predecessor-version":[{"id":33663,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33662\/revisions\/33663"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}