6 kyu

Simple Fun #319: Number And IP Address

241 of 726myjinxin2015

Description:

Task

An IP address contains four numbers(0-255) and separated by dots. It can be converted to a number by this way:

Given a string s represents a number or an IP address. Your task is to convert it to another representation(number to IP address or IP address to number).

You can assume that all inputs are valid.

Example

Example IP address: 10.0.3.193

Convert each number to a 8-bit binary string (may needs to pad leading zeros to the left side):

10  -->  00001010
0   -->  00000000
3   -->  00000011
193 -->  11000001

Combine these four strings: 00001010 00000000 00000011 11000001 and then convert them to a decimal number: 167773121

Input/Output

[input] string s

A number or IP address in string format.

[output] a string

A converted number or IP address in string format.

Example

For s = "10.0.3.193", the output should be "167773121".

For s = "167969729", the output should be "10.3.3.193".

Algorithms

Stats:

CreatedJun 6, 2017
PublishedJun 6, 2017
Warriors Trained1343
Total Skips53
Total Code Submissions3345
Total Times Completed726
JavaScript Completions241
Python Completions330
Java Completions82
Ruby Completions41
Kotlin Completions57
Total Stars32
% of votes with a positive feedback rating93% of 238
Total "Very Satisfied" Votes211
Total "Somewhat Satisfied" Votes21
Total "Not Satisfied" Votes6
Total Rank Assessments5
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • marko-bekhta Avatar
  • anter69 Avatar
  • cheeze Avatar
  • Voile Avatar
  • kdmatrosov Avatar
  • user9644768 Avatar
Ad