Saturday, January 1, 2022

Rust function that receives two IPv4 addresses, and returns the number of addresses between them (including the first one, excluding the last one)

 

Description: A function that receives two IPv4 addresses, and returns the number of addresses between them (including the first one, excluding the last one).
NOTE: Its considered that all inputs will be valid IPv4 addresses in the form of strings. The last address will always be greater than the first one.

 
fn ips_between(start: &str, end: &str) -> u32 {
    // Convert ips to decimal
    let start_ip_split_vec = start.split('.')
                        .map(|oct| oct.parse::().unwrap())
                        .collect::>();
    let end_ip_split_vec = end.split('.')
                        .map(|oct| oct.parse::().unwrap())
                        .collect::>();
    
    let start_ip_decimal = (start_ip_split_vec[0] << 24)
                + (start_ip_split_vec[1] << 16)
                + (start_ip_split_vec[2] << 8)
                + (start_ip_split_vec[3]);
    let end_ip_decimal = (end_ip_split_vec[0] << 24)
                + (end_ip_split_vec[1] << 16)
                + (end_ip_split_vec[2] << 8)
                + (end_ip_split_vec[3]);
    
    end_ip_decimal - start_ip_decimal
}
 
Examples:
ips_between("10.0.0.0", "10.0.0.50") == 50
ips_between("10.0.0.0", "10.0.1.0") == 256
ips_between("20.0.0.10", "20.0.1.0") == 246 

Tuesday, August 4, 2015

PHP PROGRAM FOR PRINTING DIAMOND PATTERN OF ALPHABETS

This program prints diamond pattern of alphabets, validations using PHP. This program checks the input value is even number between 2 and 24 then prints the diamond pattern of alphabets.


<html>
<head>
<style type="text/css">
.but {
border-radius: 20px;
width: 250px;
height: 40px;
font-weight: bolder;
font-size: 20px;
}
</style>
<body>
<form id="formd" method="get" action="<?php $_PHP_SELF ?>">
<label>Diamond No.:</label>
<input type="text" name="dia" maxlength="2" placeholder = "0" />
<button type="submit" class="but" form="formd" value="submit">Make Diamonds</button>
</form>
</body>
</html>

<?php
$val = $_GET["dia"];
if (($val<25) && ($val>1)) {
  $t = $val%2;
if ($t==0) {
drawDiamond($val);
}
else {
echo "Only EVEN numbers between 2 and 24 are allowed";
}
}
else {
echo "Only EVEN numbers between 2 and 24 are allowed";
}

function drawDiamond($a) {
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$count= 0;
echo "<pre>";
for ($i= 1;$i<=($a/2);$i++) {
for ($s=($a/2); $s>=$i; $s--) {
print(" ");
}
for ($j= 1; $j<(2*$i); $j++) {
print("$str[$count]");
}
$count++;
print ("\n");
}
for ($p=1;$p<=$a+1;$p++) {
print("$str[$count]");
}
print("\n");
$h=1;
for ($d=1;$d<=($a/2);$d++) {
for ($e=1;$e<=$d;$e++) {
print(" ");
}
$count--;
for ($f=($a-$h);$f>=1;$f--) {
print("$str[$count]");
}
$h=$h+2;
print("\n");
}
echo "</pre>";
}
?>

JAVASCRIPT PROGRAM FOR PRINTING DIAMOND PATTERN OF ALPHABETS

HTML program for printing diamond pattern of alphabets validations using JavaScript, this program works on both ways by appending child and document.write method. For using this program with document.write method simply uncomment the commented part and comment the appendChild part and innerHTML part. This program checks the input is even number between 2 and 24 and then prints the diamond pattern of alphabets.


<html>
<head>
<style type="text/css">
.but {
border-radius: 20px;
width: 250px;
height: 40px;
font-weight: bolder;
font-size: 20px;
}
</style>
<script type="text/javascript" >
function makeDiamond() {
var n = document.getElementById("dia").value;
n = Number(n);
if ((n<25) && (n>1)) {
  var t = n%2;
if (t==0) {
drawDiamond(n);
}
else {
alert("Only EVEN numbers between 2 and 24 are allowed");
}
}
else {
alert("Only EVEN numbers between 2 and 24 are allowed");
}
}
function drawDiamond(a) {
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var count= 0;
var pree = document.createElement("pre");
var divv = document.createElement("div");
var sp= document.getElementById("ad");
pree.setAttribute("id", "pr");
sp.appendChild(divv);
sp.appendChild(pree);
// document.getElementById("pr").innerHTML += ;
// document.write("<pre>");
for (var i= 1;i<=(a/2);i++) {
for (var s=(a/2); s>=i; s--) {
// document.write(" ");
document.getElementById("pr").innerHTML += " ";
}
for (var j= 1; j<(2*i); j++) {
// document.write(str.charAt(count));
document.getElementById("pr").innerHTML += str.charAt(count);
}
count++;
// document.write("\n");
document.getElementById("pr").innerHTML += "\n";
}
for (var p=1;p<=a+1;p++) {
// document.write(str.charAt(count));
document.getElementById("pr").innerHTML += str.charAt(count);
}
// document.write("<br>");
document.getElementById("pr").innerHTML += "\n";
var h=Number(1);
for (var d=1;d<=(a/2);d++) {
for (var e=1;e<=d;e++) {
// document.write(" ");
document.getElementById("pr").innerHTML += " ";
}
count--;
for (var f=(a-h);f>=1;f--) {
// document.write(str.charAt(count));
document.getElementById("pr").innerHTML += str.charAt(count);
}
h=h+2;
// document.write("\n");
document.getElementById("pr").innerHTML += "\n";
}
// document.write("</pre>");
}

function clearScreen() {
var sp= document.getElementById("ad");
var prr = document.getElementById("pr");
sp.removeChild(sp.childNodes[0]);
}

function numb(e, t) {
            try {
                if (window.event) {
                    var charCode = window.event.keyCode;
                }
                else if (e) {
                    var charCode = e.which;
                }
                else { return true; }
                if ((charCode > 47 && charCode < 58))
                    return true;
                else
                    return false;
            }
            catch (err) {
                alert(err.Description);
            }
        }

</script>
</head>
<body>
<div>
<form id="formd" method="get" action="JavaScript:makeDiamond()">
<label for="dia">Diamond No.:</label>
<input type="text" id="dia" maxlength="2" onkeypress="return numb()" />
</form>
<button type="submit" class="but" form="formd" value="submit">Make Diamonds</button>
<button type="button" class="but" value="clear" onclick="return clearScreen()">Clear Screen</button>
</div>
<span id="ad"></span>
</body>
</html>