edit-article
Home
Up
Delete
Article Name:
Article Description:
code-quiz-question - defang an ip address - defanging is this case is the process of taking an ip address value, like 192.168.1.1 and modifying a given element within that value (the dot aka period separator) in this case, to wrap it ..
Chapter ID/Name:
Status:
Write
Writing
Written
Add Photo:
Owner ID:
Content:
use HTML
Edit Content
<h1 style="text-align: center;">defang an ip address</h1> <h2>[WHAT]</h2> <ol> <li>[] defang an ip address - is the process of taking an ip address value, like 192.168.1.1 and modifying a given element within that value, the dot separator in this case, to wrap it in a pair of square brackets. </li> <li>[] Given a valid (IPv4) IP <code style="background-color: #f7f9fa; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px;">address</code>, return a defanged version of that IP address.<span style="text-align: left; color: #263238; text-transform: none; text-indent: 0px; letter-spacing: normal; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; display: inline !important; white-space: normal; orphans: 2; float: none; -webkit-text-stroke-width: 0px;"> </span>A <em style="box-sizing: border-box;">defanged IP address</em> replaces every period <code style="background-color: #f7f9fa; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px;">"."</code> with <code style="background-color: #f7f9fa; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; color: #546e7a; font-family: monospace; font-size: 13px; padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px;">"[.]"</code>.</li> <li>[] solutions are coded in javascript </li> </ol> <h2>[WHY]</h2> <ol> <li>[] practice, practice, practice </li> </ol> <h2>[WHERE]</h2> <ol> <li>[] the function will take an ip address string and will return a new string with all of the dot/periods in the string replaced with [.] sequence</li> <ol> <li>[] my initial solution was to iterate over the string and replace the target char with the new sequence of chars, </li> <li>[] afterthought - WAS a lot easier/simpler/cleaner to just use a regex , dont know what the performance differences would be</li> </ol></ol> <h2>[WHEN]</h2> <ol> <li>[x] 2019-08-22 (local) </li> </ol> <h2>[EXAMPLE]</h2> <p>// use indexOf to identify each</p> <p>elements = address.split('.');<br />elements.forEach( function(element) {element += "[.]");<br />console.log(elements);</p> <p>//</p> <p>var address = '192.168.1.1';<br />var elements = address.split('.');<br />elements = elements.forEach(function(element) {element = element + "[.]"});<br />console.log(elements);<br /><br /><strong>// my solution</strong><br /><br />var address = '192.168.1.1';<br />var elements = address.split('.');<br />console.log(elements);<br /><br />// elements = elements.forEach((element){return element + "[.]";});<br /><br />for (i=0; i<elements.length - 1; i++){<br /> elements[i] = elements[i]+ "[.]";<br />}<br />console.log(elements.join(""));<br /><br />output == "192[.]168[.]1[.]1"</p> <p><strong>// fixed forEach function </strong></p> <p>var address = "192.168.1.1";<br />address = address.split('.');<br />var address2 = [];<br />address.forEach(function(element) {<br /> address2.push(element+'[.]');<br />});<br /><br />console.log(address2.join(""));</p> <p>// still need to remove the last "[.]"</p> <p>>> </p> <p> </p> <p><strong>// better solution - regex</strong></p> <p> </p> <div> <p><code><span style="font-size: medium;">var defangIPaddr = function(address) { return address.replace(/\./g, "[.]") };</span></code></p> <p><strong>// better solution - using arrary methods</strong></p> <p><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91; font-family: &quot; sfmono-regular&quot;,consolas,&quot;liberation mono&quot;,menlo,courier,monospace; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px;">const</span><span style="display: inline !important; float: none; background-color: #f7f9fa; color: inherit; font-family: 'SFMono-Regular',Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px;"> defangIPaddr = </span><span class="hljs-function" style="box-sizing: border-box; color: #263238; font-family: &quot; sfmono-regular&quot;,consolas,&quot;liberation mono&quot;,menlo,courier,monospace; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px;">(<span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">address</span>) =></span><span style="display: inline !important; float: none; background-color: #f7f9fa; color: inherit; font-family: 'SFMono-Regular',Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px;"> {<br /> </span><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91; font-family: &quot; sfmono-regular&quot;,consolas,&quot;liberation mono&quot;,menlo,courier,monospace; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px;">return</span><span style="display: inline !important; float: none; background-color: #f7f9fa; color: inherit; font-family: 'SFMono-Regular',Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px;"> address.split(</span><span class="hljs-string" style="box-sizing: border-box; color: #c41a16; font-family: &quot; sfmono-regular&quot;,consolas,&quot;liberation mono&quot;,menlo,courier,monospace; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px;">'.'</span><span style="display: inline !important; float: none; background-color: #f7f9fa; color: inherit; font-family: 'SFMono-Regular',Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px;">).join(</span><span class="hljs-string" style="box-sizing: border-box; color: #c41a16; font-family: &quot; sfmono-regular&quot;,consolas,&quot;liberation mono&quot;,menlo,courier,monospace; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px;">'[.]'</span><span style="display: inline !important; float: none; background-color: #f7f9fa; color: inherit; font-family: 'SFMono-Regular',Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px;">)<br />}</span><strong></strong><em></em></p> </div> <p> </p> <h2>[HOW-TO]</h2> <ol> <li>[x] the code in the above example section will execute in the editor at the source site listed below, it passes all tests</li> </ol> <h2>[REFERENCE]</h2> <ol> <li><span style="display: inline !important; float: none; background-color: #ffffff; color: #000000; font-family: Verdana,Arial,Helvetica,sans-serif; font-size: 10px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;">[] <a href="https://leetcode.com/problems/defanging-an-ip-address/" target="_blank">https://leetcode.com/problems/defanging-an-ip-address/</a> - <br /></span></li> </ol>