Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Tuesday, September 8, 2015

array stack and queue function in a class to demonstrate push, pop, first, last, pop from last, get element

<?php
class Stack {
    private $stack_array = array();
    private $index = -1;

    public function print_array()
    {
        print_r($this->stack_array);
    }
    public function isEmpty()
    {
        if($this->index == -1){
            return true;
        }else{
            return false;
        }
    }

    public function push($element)
    {
        $this->index++;
        $this->stack_array[$this->index] = $element;
    }

    public function pop()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            $element = $this->stack_array[$this->index];
            unset($this->stack_array[$this->index]);
            $this->index--;
        }
    }

    public function pop_from_last()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            $element = $this->stack_array[0];
            unset($this->stack_array[0]);
            $this->index--;
            return $element;
        }
    }
    public function first()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            return $this->stack_array[$this->index];
        }
    }

    public function last()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            return $this->stack_array[0];
        }
    }

    public function get_element($position)
    {
        if($position >= $this->index + 1){
           echo 'position is exceeding the array index';
        }else{
            $i = $this->index - ($position - 1);
            return $this->stack_array[$i];

        }
    }
}

$stack = new Stack();
$stack->push(8);
$stack->push(5);
$stack->push(4);
$stack->push(9);
$stack->push(2);
$stack->print_array();
echo '<br>';
echo $stack->get_element(1);
echo '<br>';
echo $stack->get_element(2);
echo '<br>';
echo $stack->get_element(3);
echo '<br>';
echo $stack->get_element(30);

Monday, September 7, 2015

Basic object oriented programming in php

<?php

function is_phone_number($number){
$length = strlen($number);
if($length == 11){

for ($i=0; $i < $length; $i++) { 
if(ord( $number[$i]) < ord('0') ||  ord( $number[$i]) > ord('9')){
return false;
}
}
return true;
}
return false;


class Person {
public $name;
public $profession;
public $phone_number;
public function check_phone_number($phone_number){
if(is_phone_number($phone_number)){
$this->phone_number = $phone_number;
}
}
}

$polo = new Person();
$polo->name = 'Polo dev';
$polo->profession = 'Student';
$polo->check_phone_number('01670978989');

echo $polo->name . '<br>'; 
echo $polo->profession . '<br>'; 
echo $polo->phone_number . '<br>'; 

Saturday, September 5, 2015

manual dictionary function in php

<?php
function lowerCase($string){
    $new_string = "";
    $length = strlen($string);
    for($i = 0; $i < $length; $i++){
        if(ord($string[$i]) >= 65 && ord($string[$i]) <= 91){
            $new_string .= chr(ord($string[$i]) + 32);
        }else{
            $new_string .= $string[$i];
        }
    }
    return $new_string;
}

function which_is_first($string1, $string2){
    $string1 = lowerCase($string1);
    $string2 = lowerCase($string2);
    $len1 = strlen($string1);
    $len2 = strlen($string2);
    if($len1 < $len2){
        $min_len = $len1;
    }else{
        $min_len = $len2;
    }
    for($i = 0; $i < $min_len; $i++){
        if($string1[$i] > $string2[$i]){
            return 2;
        }elseif($string1[$i] < $string2[$i]){
            return 1;
        }
    }
    if($len1 < $len2){
        return 1;
    }else{
        return 2;
    }

}
function dictionary($words){
    $arr_len = count($words);
    for($i = 0; $i < $arr_len; $i++){
        for($k = $i; $k < $arr_len; $k++){
            if(which_is_first($words[$i], $words[$k]) == 2 ){
                $temp = $words[$i];
                $words[$i] = $words[$k];
                $words[$k] = $temp;
            }
        }
    }
    return $words;
}
$dictionary = dictionary(['cat', 'bat', 'ball', 'home', 'he', 'she']);
print_r($dictionary);

Thursday, September 3, 2015

manual anagram in php

<?php
function lowerCase($string){
    $new_string = "";
    $length = strlen($string);
    for($i = 0; $i < $length; $i++){
        if(ord($string[$i]) >= 65 && ord($string[$i]) <= 91){
            $new_string .= chr(ord($string[$i]) + 32);
        }else{
            $new_string .= $string[$i];
        }
    }
    return $new_string;
}
function is_anagram($string_one , $string_two){
    $string_one = lowerCase($string_one);
    $string_two = lowerCase($string_two);
    $length_string_one = strlen($string_one);
    $length_string_two = strlen($string_two);
    $length = 0;
    if ($length_string_one == $length_string_two){
        for($i = 0; $i < $length_string_one; $i++){
            for($k = 0; $k < $length_string_one; $k++){
                if($string_one[$i] == $string_two[$k]){
                    $length += 1;
                    break;
                }
            }
        }
        if($length == $length_string_one){
            return true;
        }else{
            return false;
        }
    }
}

echo is_anagram('bangladesh', 'eshbanglad');

Sunday, August 30, 2015

findings frequency number from a file by using own explode code and frequency code - modularity approach

in functions.php
===========================

<?php
//for exploding a string to array
function myExplode($string)
{
    $length = strlen($string);
    $word = "";
    $words = array();
    for ($i = 0; $i < $length; $i++) {
        if (ord($string[$i]) == 13) {
            continue;
        }
        if ($string[$i] == " " || $i == $length - 1 || $string[$i] == "\n") {

            if ($i == $length - 1) {
                $word .= $string[$i];
            }
            if(!empty($word)){
                $words[] = $word;
            }
            $word = "";
        } else {
            $word .= $string[$i];
        }
    }
    return $words;
}




//for determine frequency
function frequency($words){
    $frequency = array();

    foreach ($words as $word) {
        if(array_key_exists($word, $frequency)){
            $frequency[$word] ++;
        } else {
            $frequency[$word] = 1;
        }
    }

    return $frequency;
}


in file.php
================

<?php
require 'functions.php';
$pointer = fopen('php.txt', 'r');
$string = fread($pointer, filesize('php.txt'));
$words = myExplode($string);
$frequency = frequency($words);
print_r($frequency);

findings frequency number from a file by using own explode code and frequency code

<?php
$pointer = fopen('php.txt', 'r');
$string = fread($pointer, filesize('php.txt'));
$length = strlen($string);
$word = "";
$words = array();

for($i = 0; $i < $length; $i++){
    if(ord($string[$i]) == 13){
            continue;
        }
    if($string[$i] == " " || $i == $length - 1 || $string[$i] == "\n"){

        if($i == $length - 1){
            $word .= $string[$i];
        }
        if(!empty($word)){
            $words[] = $word;
        }
        $word = "";
    }else{
        $word .= $string[$i];
    }
}
//for determine frequency
$frequency = array();

foreach ($words as $word) {
    if(array_key_exists($word, $frequency)){
        $frequency[$word] ++;
    } else {
        $frequency[$word] = 1;
    }
}

foreach($frequency as $key => $value){
    echo "$key => $value <br>";
}

Friday, August 28, 2015

findings frequency number from array using foreach loop

<?php
/**
 * Created by PhpStorm.
 * User: Polo-Dev
 * Date: 29-Aug-15
 * Time: 9:20 AM
 */
$string = "hi how are you hi you";
$words = explode(" ", $string);
$new_array = array();

foreach ($words as $word) {
   if(array_key_exists($word, $new_array)){
       $new_array[$word] ++;
   } else {
       $new_array[$word] = 1;
   }
}

foreach($new_array as $key => $value){
    echo "$key => $value <br>";
}

array in descending order in php

<?php
$array = [2, 5, 7, 99, 90, 42, 88, 8, 9, 0, 56];
$count = count($array);
for($i = 0; $i < $count; $i++){
    for($j = 0; $j < $count-1; $j++){
        if($array[$j] < $array[$j+1]){
           $temp = $array[$j+1];
            $array[$j+1]= $array[$j];
            $array[$j] = $temp;
        }
    }
}

for($k = 0; $k < $count; $k++){
    echo $array[$k] . '<br>';
}

sorting ascending order manually in php

<?php
$array = [2, 5, 7, 99, 90, 42, 88, 8, 9, 0, 56];
$count = count($array);
for($i = 0; $i < $count; $i++){
    for($j = 0; $j < $count-1; $j++){
        if($array[$j] > $array[$j+1]){
           $temp = $array[$j+1];
            $array[$j+1]= $array[$j];
            $array[$j] = $temp;
        }
    }
}

for($k = 0; $k < $count; $k++){
    echo $array[$k] . '<br>';
}

programming glossary

lazy evaluation,
DRY - don't repeat yourself

How to reverse a string

//how to reverse a string
$string = "FTFL!!";
$count = strlen($string) - 1;;
for($i = $count; $i >= 0; $i--){
  echo $string[$i];
}

Variable swap

/*
 * variable swap
 * */
$a = 7;
$b = 8;
$i = $a;
$a = $b;
$b = $i;
echo $a , $b;

/*
 * another way to variable swap
 * */
$a = 25;
$b = 30;
$a = $a + $b;
$b = $a - $b;
$a = $a - $b;
echo $a, $b;

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }