Swift Coding Test Course, DNA Password

This article deals with algorithm problems that are frequently presented in coding tests, and explains in depth the process of solving those problems. Today’s topic is ‘DNA Password’, where we will solve a password problem based on DNA strings.

Problem Description

You need to design a system that generates passwords using DNA strings. The DNA string consists of uppercase letters A, C, G, and T, and each character becomes a component of the password in various combinations.

You are to find specific patterns in the given DNA string and generate a password based on these patterns. The following is a detailed problem definition:

Problem Definition

Given a string s and a natural number k, you should consider all substrings of length k from s and generate a password using the frequency of A, C, G, and T in these substrings.

The password is defined as the count of substrings where the frequency of A, C, G, and T is at least min_count.

Input Example

s = "ACGTACGTGACG", k = 4, min_count = 1

Output Example

3

Here, “ACGT”, “CGTA”, and “GTAC” are substrings with frequencies of at least 1.

Problem Solving Strategy

The strategy to solve this problem is as follows:

  1. Generate substrings of length k from the entire string s.
  2. Calculate the frequencies of A, C, G, and T for each substring.
  3. If the frequencies of A, C, G, and T are at least min_count, increase the count.
  4. Finally, output the count.

Implementation Steps

Now let’s look at how to implement the problem using Swift. Here is the step-by-step code to solve the problem.

1. Input

First, we take the string and variables as input. In Swift, values can be received through command-line arguments or direct input.


let inputString = "ACGTACGTGACG"
let k = 4
let minCount = 1

2. Generate Substrings

Generate all substrings of length k from the string.


var count = 0
let length = inputString.count

for i in 0...(length - k) {
    let startIndex = inputString.index(inputString.startIndex, offsetBy: i)
    let endIndex = inputString.index(startIndex, offsetBy: k)
    let substring = String(inputString[startIndex..<endindex]) <="" code="">
</endindex])>
<h3>3. Frequency Calculation</h3> <p>Calculate the frequencies of each character (A, C, G, T) for the generated substrings.</p> <pre><code> var frequencies = [Character: Int]() for char in substring { frequencies[char, default: 0] += 1 } // Condition Check if frequencies["A"]! &gt;= minCount &amp;&amp; frequencies["C"]! &gt;= minCount &amp;&amp; frequencies["G"]! &gt;= minCount &amp;&amp; frequencies["T"]! &gt;= minCount { count += 1 } </code> </pre> <h3>4. Output Result</h3> <p>Print the final count.</p> <pre><code> print("Total valid passwords: \(count)") </code> </pre>
<section> <h2>Complete Code</h2> <p>Now let's take a look at the complete code that integrates all the steps.</p> <pre><code> let inputString = "ACGTACGTGACG" let k = 4 let minCount = 1 var count = 0 let length = inputString.count for i in 0...(length - k) { let startIndex = inputString.index(inputString.startIndex, offsetBy: i) let endIndex = inputString.index(startIndex, offsetBy: k) let substring = String(inputString[startIndex..<endindex]) var="" frequencies="[Character:" int]()="" for="" char="" in="" substring="" {="" frequencies[char,="" default:="" 0]="" +="1" }="" condition="" check="" if="" frequencies["a"]!="">= minCount &amp;&amp; frequencies["C"]! &gt;= minCount &amp;&amp; frequencies["G"]! &gt;= minCount &amp;&amp; frequencies["T"]! &gt;= minCount { count += 1 } } print("Total valid passwords: \(count)") </endindex])></code> </pre> </section> <section> <h2>Result</h2> <p>Running this code with the given input will output the number of valid passwords. Understanding the basic algorithm patterns of handling strings and checking conditions is very useful in coding tests.</p> <p>Furthermore, you can enhance your problem-solving skills by testing the code against various input cases and adding exception handling.</p> </section> <footer> <p>I hope this article helps you prepare for coding tests using Swift. Keep practicing and solving problems to improve your skills!</p> </footer>
<p></p> <div id="jp-relatedposts" class="jp-relatedposts"> <h3 class="jp-relatedposts-headline"><em>관련</em></h3> </div>
<footer class="entry-footer"> <span class="byline"><span class="author vcard"><img alt="" src="https://secure.gravatar.com/avatar/5b7f47db621d1eab02540d35048be506?s=49&amp;d=mm&amp;r=g" srcset="https://secure.gravatar.com/avatar/5b7f47db621d1eab02540d35048be506?s=98&amp;d=mm&amp;r=g 2x" class="avatar avatar-49 photo" height="49" width="49" decoding="async"><span class="screen-reader-text">Author </span> <a class="url fn n" href="https://atmokpo.com/w/en/author/root/">root</a></span></span><span class="posted-on"><span class="screen-reader-text">Posted on </span><a href="https://atmokpo.com/w/34660/" rel="bookmark"><time class="entry-date published" datetime="2024-11-01T09:30:35+00:00">2024/11/01</time><time class="updated" datetime="2024-11-01T11:27:06+00:00">2024/11/01</time></a></span><span class="cat-links"><span class="screen-reader-text">Categories </span><a href="https://atmokpo.com/w/category/swift-coding-test/" rel="category tag">Swift Coding Test</a></span> </footer><!-- .entry-footer -->
<nav class="navigation post-navigation" aria-label="Posts"> <h2 class="screen-reader-text">Post navigation</h2> <div class="nav-links"><div class="nav-previous"><a href="https://atmokpo.com/w/34658/" rel="prev"><span class="meta-nav" aria-hidden="true">Previous</span> <span class="screen-reader-text">Previous post:</span> <span class="post-title">Swift Coding Test Course, DFS and BFS Programs</span></a></div><div class="nav-next"><a href="https://atmokpo.com/w/34662/" rel="next"><span class="meta-nav" aria-hidden="true">Next</span> <span class="screen-reader-text">Next post:</span> <span class="post-title">Swift Coding Test Course, Finding the K-th Number</span></a></div></div> </nav>
<aside id="secondary" class="sidebar widget-area"> <section id="block-2" class="widget widget_block widget_search"><form role="search" method="get" action="https://atmokpo.com/w/en/" class="wp-block-search__button-outside wp-block-search__text-button wp-block-search"><label class="wp-block-search__label" for="wp-block-search__input-1">Search</label><div class="wp-block-search__inside-wrapper "><input class="wp-block-search__input" id="wp-block-search__input-1" placeholder="" value="" type="search" name="s" required=""><button aria-label="Search" class="wp-block-search__button wp-element-button" type="submit">Search</button></div></form></section><section id="block-10" class="widget widget_block"><ul class="wp-block-page-list"><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/c-coding-test-tutorials/">C++ Coding Test Tutorials</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/collection-of-c-coding-test-tutorials/">Collection of C# Coding Test Tutorials</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/deep-learning-automated-trading/">Deep learning Automated trading</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/deep-learning-natural-language-processing/">Deep learning natural language processing</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/english-sentence-study/">English sentence study</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/flutter-course/">Flutter course</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/gan-deep-learning-course/">GAN deep learning course</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/java-android-app-development/">Java Android app development</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/java-coding-test/">Java Coding Test</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/javascript-coding-test/">Javascript Coding Test</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/kotlin-android-app-development/">Kotlin Android app development</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/kotlin-coding-test/">Kotlin coding test</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/python-auto-trading/">Python Auto Trading</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/python-coding-test/">Python Coding Test</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/python-study/">Python Study</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/pytorch-study/">PyTorch Study</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/react-basics-course/">React basics course</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/spring-boot-backend-development/">Spring Boot backend development</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/swift-coding-test/">Swift Coding Test</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/swift-iphone-app-development-swiftui/">Swift iPhone app development (SwiftUI)</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/swift-iphone-app-development-uikit/">Swift iPhone app development (UIKit)</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/unity-basic/">Unity Basic</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/using-hugging-face/">Using Hugging Face</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/uwp-programming/">UWP Programming</a></li><li class="wp-block-pages-list__item"><a class="wp-block-pages-list__item__link" href="https://atmokpo.com/w/wpf-programming/">WPF Programming</a></li></ul></section><section id="listcategorypostswidget-3" class="widget widget_listcategorypostswidget"><h2 class="widget-title">Category Post List</h2><ul class="lcp_catlist" id="lcp_instance_listcategorypostswidget-3"><li><a href="https://atmokpo.com/w/34916/">Swift Coding Test Course, Hacking Efficiently</a></li><li><a href="https://atmokpo.com/w/34914/">Swift Coding Test Course, Assigning Meeting Rooms</a></li><li><a href="https://atmokpo.com/w/34912/">Swift Coding Test Course, Extended Euclidean Algorithm</a></li><li><a href="https://atmokpo.com/w/34910/">Swift Coding Test Course, Finding the Minimum Number of Matrix Multiplication Operations</a></li><li><a href="https://atmokpo.com/w/34908/">Swift Coding Test Course, Floyd-Warshall</a></li><li><a href="https://atmokpo.com/w/34906/">Swift Coding Test Course, Calculating Average</a></li><li><a href="https://atmokpo.com/w/34904/">Swift Coding Test Course, Find Cities at a Specific Distance</a></li><li><a href="https://atmokpo.com/w/34902/">Swift Coding Test Course, Finding the Diameter of a Tree</a></li><li><a href="https://atmokpo.com/w/34900/">Swift Coding Test Course, Finding the Parent of a Tree</a></li><li><a href="https://atmokpo.com/w/34898/">Swift Coding Test Course, Understanding Trees</a></li></ul><ul class="lcp_paginator"><li class="lcp_currentpage">1</li><li><a href="https://atmokpo.com/w/34660/?lcp_pagelistcategorypostswidget-3=2#lcp_instance_listcategorypostswidget-3" title="2">2</a></li><li><a href="https://atmokpo.com/w/34660/?lcp_pagelistcategorypostswidget-3=3#lcp_instance_listcategorypostswidget-3" title="3">3</a></li><li><a href="https://atmokpo.com/w/34660/?lcp_pagelistcategorypostswidget-3=4#lcp_instance_listcategorypostswidget-3" title="4">4</a></li><li><a href="https://atmokpo.com/w/34660/?lcp_pagelistcategorypostswidget-3=5#lcp_instance_listcategorypostswidget-3" title="5">5</a></li><li><a href="https://atmokpo.com/w/34660/?lcp_pagelistcategorypostswidget-3=6#lcp_instance_listcategorypostswidget-3" title="6">6</a></li><span class="lcp_elipsis">...</span><li><a href="https://atmokpo.com/w/34660/?lcp_pagelistcategorypostswidget-3=14#lcp_instance_listcategorypostswidget-3" title="14">14</a></li><li><a href="https://atmokpo.com/w/34660/?lcp_pagelistcategorypostswidget-3=2#lcp_instance_listcategorypostswidget-3" title="2" class="lcp_nextlink">&gt;&gt;</a></li></ul></section><section id="block-3" class="widget widget_block"> <div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow"> <h3 class="wp-block-heading">최신 글</h3> <ul class="wp-block-latest-posts__list wp-block-latest-posts"><li><a class="wp-block-latest-posts__post-title" href="https://atmokpo.com/w/37979/">Unity 2D Game Development, Create a Platform Game Including Jumps, Obstacles, and Enemies.</a></li> <li><a class="wp-block-latest-posts__post-title" href="https://atmokpo.com/w/37977/">Unity 2D Game Development, Adding Effects Using Particle System Implementing visual effects such as explosions and flames using the particle system.</a></li> <li><a class="wp-block-latest-posts__post-title" href="https://atmokpo.com/w/37973/">Unity 2D Game Development, Touch Input and Mobile Game Development Creation of 2D games utilizing touch input on mobile devices.</a></li> <li><a class="wp-block-latest-posts__post-title" href="https://atmokpo.com/w/37975/">Unity 2D Game Development, Power-Up and Buff System Creating a power-up system that temporarily enhances the player’s abilities.</a></li> <li><a class="wp-block-latest-posts__post-title" href="https://atmokpo.com/w/37971/">Unity 2D Game Development, Quest and Mission System Creating a quest system where rewards are given for achieving specific goals.</a></li> </ul></div></div> </section> </aside><!-- .sidebar .widget-area -->
<footer id="colophon" class="site-footer"> <div class="site-info"> <span class="site-title"><a href="https://atmokpo.com/w/en/" rel="home">라이브스마트</a></span> <a href="https://wordpress.org/" class="imprint"> Proudly powered by WordPress </a> </div><!-- .site-info --> </footer><!-- .site-footer -->
<link rel="stylesheet" id="lcp_paginator-css" href="https://atmokpo.com/w/wp-content/plugins/list-category-posts//lcp_paginator.css?ver=6.7.2" media="all"> <script src="https://atmokpo.com/w/wp-content/plugins/collapse-magic/js/collapse-magic.js?x=60&amp;ver=1.0" id="claps-main-js"></script> <script defer="" src="https://atmokpo.com/w/wp-content/plugins/koko-analytics/assets/dist/js/script.js?ver=1.6.4" id="koko-analytics-js"></script> <script src="https://atmokpo.com/w/wp-content/plugins/responsive-accordion-and-collapse/js/accordion-custom.js?ver=6.7.2" id="call_ac-custom-js-front-js"></script> <script src="https://atmokpo.com/w/wp-content/plugins/responsive-accordion-and-collapse/js/accordion.js?ver=6.7.2" id="call_ac-js-front-js"></script> <script src="https://stats.wp.com/e-202514.js" id="jetpack-stats-js" data-wp-strategy="defer"></script> <script id="jetpack-stats-js-after"> _stq = window._stq || []; _stq.push([ "view", JSON.parse("{\"v\":\"ext\",\"blog\":\"238449126\",\"post\":\"34660\",\"tz\":\"0\",\"srv\":\"atmokpo.com\",\"j\":\"1:14.2.1\"}") ]); _stq.push([ "clickTrackerInit", "238449126", "34660" ]); </script> <script> document.querySelectorAll("code").forEach(function(codeBlock) { // 내용에 '<'나 '>'가 포함된 경우에만 변환 if (codeBlock.innerHTML.includes("<") && codeBlock.innerHTML.includes(">")) { codeBlock.textContent = codeBlock.innerHTML; } }); </script>