{"id":226545,"date":"2025-06-05T10:52:44","date_gmt":"2025-06-05T10:52:44","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=226545"},"modified":"2025-06-05T10:52:46","modified_gmt":"2025-06-05T10:52:46","slug":"code-practice","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/06\/05\/code-practice\/","title":{"rendered":"Code Practice"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Code Practice: Question 3 Instructions For this Code Practice, use the following initializer list, terms: terms = [&#8220;Bandwidth&#8221;, &#8220;Hierarchy&#8221;, &#8220;IPv6&#8221;, &#8220;Software&#8221;, &#8220;Firewall&#8221;, &#8220;Cybersecurity&#8221;, &#8220;Lists&#8221;, &#8220;Program&#8221;, &#8220;Logic&#8221;, &#8220;Reliability&#8221;] Write a sort program to alphabetize the list of computer terms, much like the preceding question. However, this time, define and use a function named swap as part of your solution. The swap function should not be a sort function, but should instead implement the swap functionality used in sorting. This function should swap the elements in the array at each of the indexes by comparing two elements to one another, and swapping them if they need to be in a different alphabetical order. Your function should take three parameters: the first is the array name, and the second and third are the indexes to swap. Print the terms array before and after it is sorted. Expected Output [&#8216;Bandwidth&#8217;, &#8216;Hierarchy&#8217;, &#8216;IPv6&#8217;, &#8216;Software&#8217;, &#8216;Firewall&#8217;, &#8216;Cybersecurity&#8217;, &#8216;Lists&#8217;, &#8216;Program&#8217;, &#8216;Logic&#8217;, &#8216;Reliability&#8217;] [&#8216;Bandwidth&#8217;, &#8216;Cybersecurity&#8217;, &#8216;Firewall&#8217;, &#8216;Hierarchy&#8217;, &#8216;IPv6&#8217;, &#8216;Lists&#8217;, &#8216;Logic&#8217;, &#8216;Program&#8217;, &#8216;Reliability&#8217;, &#8216;Software&#8217;]<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/06\/image-148.png\" alt=\"\" class=\"wp-image-226546\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-1-color\">The Correct Answer and Explanation is:<\/mark><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s the <strong>Python code<\/strong> that fulfills the assignment exactly as requested:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Initial list of terms\nterms = &#91;\"Bandwidth\", \"Hierarchy\", \"IPv6\", \"Software\", \"Firewall\", \"Cybersecurity\", \n         \"Lists\", \"Program\", \"Logic\", \"Reliability\"]\n\n# Swap function\ndef swap(arr, i, j):\n    # Swap elements if they are in the wrong order\n    if arr&#91;i] &gt; arr&#91;j]:\n        arr&#91;i], arr&#91;j] = arr&#91;j], arr&#91;i]\n\n# Print the original list\nprint(\"Before sorting:\")\nprint(terms)\n\n# Simple bubble sort using swap function\nn = len(terms)\nfor i in range(n):\n    for j in range(0, n - i - 1):\n        swap(terms, j, j + 1)\n\n# Print the sorted list\nprint(\"\\nAfter sorting:\")\nprint(terms)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd0d Explanation <\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This Python program implements a basic <strong>alphabetical sorting algorithm<\/strong> using a helper function called <code>swap<\/code>. The purpose is to sort a list of computer-related terms using this custom function rather than using Python\u2019s built-in sort capabilities directly.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1. Initial List<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The list named <code>terms<\/code> contains strings like <code>\"Bandwidth\"<\/code>, <code>\"IPv6\"<\/code>, <code>\"Software\"<\/code>, and so on. This list is printed before sorting to show its original state.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2. Swap Function<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>swap<\/code> function is essential. It accepts three parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>arr<\/code>: the list to work on.<\/li>\n\n\n\n<li><code>i<\/code> and <code>j<\/code>: the indices of the elements to compare and possibly swap.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">It checks whether <code>arr[i] &gt; arr[j]<\/code> using Python\u2019s built-in string comparison (which is lexicographical, or dictionary-order). If so, the two elements are swapped.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3. Sorting Algorithm<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>bubble sort<\/strong> is used here for simplicity and clarity. It repeatedly compares adjacent pairs of items and uses <code>swap<\/code> to move them into the correct order. Each pass &#8220;bubbles&#8221; the largest item left to the end, and the process continues until the list is sorted.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">While bubble sort is not efficient for large datasets, it is ideal for illustrating how swapping works and for small, controlled examples like this one.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>4. Output<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The first output shows the original unsorted list.<\/li>\n\n\n\n<li>After the loop finishes, the sorted list is printed. The result matches the required output:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'Bandwidth', 'Cybersecurity', 'Firewall', 'Hierarchy', 'IPv6', \n 'Lists', 'Logic', 'Program', 'Reliability', 'Software']\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This confirms that the code meets the problem requirements exactly.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/06\/learnexams-banner4-451.jpeg\" alt=\"\" class=\"wp-image-226547\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Code Practice: Question 3 Instructions For this Code Practice, use the following initializer list, terms: terms = [&#8220;Bandwidth&#8221;, &#8220;Hierarchy&#8221;, &#8220;IPv6&#8221;, &#8220;Software&#8221;, &#8220;Firewall&#8221;, &#8220;Cybersecurity&#8221;, &#8220;Lists&#8221;, &#8220;Program&#8221;, &#8220;Logic&#8221;, &#8220;Reliability&#8221;] Write a sort program to alphabetize the list of computer terms, much like the preceding question. However, this time, define and use a function named swap as part [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-226545","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/226545","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=226545"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/226545\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=226545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=226545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=226545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}