Monday, November 28, 2022

Algorithm Recursive/Recursion Method

 Recursion method by definitions is a way of solving a problem by having a function calling itself and make the problems become smaller and easier to find solutions.

Why We Need Recursion?

1. Recursion can break big problems into smaller ones and easy to use

2. Prominent usage of recursion in data structures like trees and graphs

3. Used in many algorithm (divide and conquer, greedy and dynamic programming)

How it works?

1. The method call it self

2. There's end conditions to interrupt loop

3. In stack Memory using FILO.

Recursive Vs Iterative Solutions?

Stack memory require?
    Recursive: Yes
    Iterative: No

Time Efficient?
    Recursive: No
    Iterative: Yes

Easy To Code?
    Recursive: Yes
    Iterative: No

When To Use Recursion:

1. When conditions breakdown to similar small sub problems meet

2. When Fine with extra overhead

3. When need quick working solutions

4. When traverse a tree

5. When use memorization in recursion

Method to do Recursion:

1. Identify the Recursive Flow

    Example:

    Factorial Problems: 5! = 5*4*3*2*1 => n * (n-1) * (n-2) * (n-3) ... *2 * 1 
    Define Formula: n! = n * f(n-1)

2. Determine Stop Conditions
    if n in [0,1]:

3. Handle Unintentional case - the constraint
    assert n >= 0 and int(n) == n 

Example:


Example with Factorial Problems (5! = 5*4*3*2*1):

import sys
sys.setrecursionlimit(100)

def factorial(n):
    # Step 3. Handle Unintentional case. The number must Positive Int Only
    assert n >= 0 and int(n) == n
    # step 2. Determine Stop Conditions
    if n in [0,1]:
        return 1
    else:
        # step 1. Identify the Recursive Flow
        return n * factorial(n-1)

print(factorial(-3))

Example with Fibonaci Problems (0, 1, 1, 2, 3, 5, 8, ...):

import sys
sys.setrecursionlimit(100)

def fibonacci(n):
    # Step 3. Handle Unintentional case. The number must Positive Int Only
    assert n >= 0 and int(n) == n
    # step 2. Determine Stop Conditions. if 0 or 1
    if n in [0,1]:
        return n
    else:
        # step 1. Identify the Recursive Flow
        # 0,1,1,2,3,5,8:
        # n2 = 0 + 1 ; n3 = 1 + 1 ; n4 = 2 + 1 ; n5 = 3 + 2 ; n6 = 5 + 3
        #  f = f(n - 1) + f (n - 2)
        return fibonacci(n-1) + fibonacci(n-2)

print(fibonacci(5))


Example with Sum Of Digits ( 108 = 10 +8 ;  56 = 5 + 6):




Share:

Sunday, February 16, 2020

How to fix ngx-gallery: Class constructor HammerGestureConfig cannot be invoked without 'new'



When I try to implement ngx-gallery in Angular 8. I found some strange error message:

Class constructor HammerGestureConfig cannot be invoked without 'new'

My first though there's something happen in Angular or ngx-bootsrap version. But, after some debug, definitely it's version problems and we can add same patch solution from github and stackoverflow. This is the full documentation:

  • Go To app.module.ts
  • add this code:

export class CustomHammerConfig extends HammerGestureConfig {
   overrides = {
      pinch: { enable: false },
      rotate: { enable: false },
   };
}
  • apply this patch in your providers app.module.ts
   providers: [
      {
         provide: HAMMER_GESTURE_CONFIGuseClass: CustomHammerConfig
      }
   ],
  • Add Import if necessary 
import { BrowserModuleHAMMER_GESTURE_CONFIGHammerGestureConfig } from '@angular/platform-browser';

Hope it works !!!
Share:

Wednesday, January 8, 2020

Install JDK on Ubuntu 32 bit and 64 bit




To install JDK on Ubuntu we need to do several step. Basically this tutorial for 32-bit and 64-bit Oracle Java 8 JDK on 32-bit and 64-bit Ubuntu operating systems. These instructions will also work on Debian and Linux Mint

1.  Go to terminal and type /sbin/init, this command to check your Ubuntu architecture weather it's 32 or 64 bit.

2. Check Java installed, type java-version on your terminal. If you have OpenJDK installed on your system it will give this results:

  • java version "1.7.0_60"
    OpenJDK Runtime Environment (IcedTea6 1.10pre) (7b15~pre1-0lucid1)
    OpenJDK 64-Bit Server VM (build 19.0-b09, mixed mode) 
Remembered that OpenJDK is not same with JDK.

3. Remove OpenJDK/JRE from your system by running this command: sudo apt-get purge openjdk-\* 

4. Download JDK from Download JDK .Please Remember to download JDK version which fits with your architecture system. For example, if your system running on 32 Bit download 32 Bit Oracle Java binaries.


5.After finished download the JDK file, copy your JDK file to '/usr/local/java'.

   sudo cp -r your-java-binaries /usr/local/java/ 

6.Go to '/usr/local/java/'. cd /usr/local/java/

7. Unpack The Java Binaries in /usr/local/java. Type sudo tar your-java-binaries

8. Your java/ folder will contain jdk and jre directory.


9. Edit system path file.

  • sudo gedit /etc/profile
  • Scroll down until end of file and type:
         JAVA_HOME=/usr/local/java/your-jdk-directory
         PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
         export JAVA_HOME
         export PATH
  • Save and exit
10. Inform your system where your Oracle Java JDK/JRE is located
  • sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/your-jdk-directory/bin/java" 1
  • sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/your-jdk-directory/bin/javac" 1
  • sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/your-jdk-directory/bin/javaws" 1 
 11. Inform your system Oracle Java JDK/JRE must default
  • sudo update-alternatives --set java /usr/local/java/your-jdk-directory/bin/java  
  • sudo update-alternatives --set javac /usr/local/java/your-jdk-directory/bin/javac 
  • sudo update-alternatives --set javaws /usr/local/java/your-jdk-directory/bin/javaws
12. Reload your system wide PATH /etc/profile by typing source /etc/profile
13. Test by run java -version  
  • If your instalation is correct, in terminal will displaying something like this:
          java version "1.8.0_05"
         Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
         Java HotSpot(TM) Server VM (build 24.51-b03, mixed mode) 

 
14. Test your javac by run javac -version
  • You should receive a message which displaying something like this: javac 1.8.0_05 
15.Reboot Your System.
Share:

Friday, December 20, 2019

Instal RVM on Linux Ubuntu 64 Bit






RVM is a command-line tool which allows you to easily install, manage, and work with multiple ruby environments from interpreters to sets of gems. To install Ubuntu in RVM

1. Check Apache and Mysql existed in your machine.
  •  Type http://localhost or http://127.0.0.1 on your browser to check apache running.
  • Check /var/www/ directory.
  • In /var/www/index.html type <?php phpinfo(); ?> , then type http://localhost or http://127.0.0.1 on your browser. If apache installation in your machine already correct, your browser will load PHP informations installed on your machine
2.Run on your terminal:
  • bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
  • source ~/.bash_profile .Note : This command is for reload opened terminal. 
  • rvm requirements. Note : This command is for searching all libs that still needed, and then install the lib.
 3. Install Ruby according to the version we need:     
  • rvm install 1.9.3
4.Install another version of ruby with this method.
5. To view all installed RVM 
  • rvm list
6.To use specifics RVM 
  • rvm use 1.9.3
 7.If there is an error when running rvm use 1.9.3 try open file ~/.bashrc and ~/.bash_profile and then add this script :

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session

8. To use Specifics Gemset

  • rvm use 1.9.3
  • rvm gemset use rails3.2
  • rvm use 1.9.3@rails3.2
9. Set Default RVM
  • rvm use 1.9.3@rails3.2 --default

Share:

Thursday, November 28, 2019

MongoDB in PHP





The MongoDB PHP driver should work on nearly any system: Windows, Mac OS X, Unix, and Linux; little- and big-endian machines; 32- and 64-bit machines; PHP 5.2, 5.3, 5.4 and 5.5.

Install MongoDB Driver PHP

  • sudo pecl install mongo
  • Add extension=mongo.so in php.ini (/etc/php5/cli/php.ini)
  • Restart Apache: sudo service apache2 restart
Check Installed MongoDB
  • Check is MongoDB extensions already loaded in your PHP php --ini
    already installed: /etc/php5/cli/conf.d/mongo.ini
  • Check is MongoDB extesions already enabled php -i | grep 'Mongo' Already Enabled: MongoDB Support => enabled
  • Type <? phpinfo(); in your PHP applications.
Share:

Monday, November 18, 2019

Install MongoDB on Ubuntu





MonggoDB provides package that supported officially. This package contains:

  • mongodb-org
  • mongodb-org-server
  • mongodb-org-mongos
  • mongodb-org-shell
  • mongodb-org-tools
Install MonggoDB Package.

  • Import Public Key used by Management System.
    Type on Terminal: sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
  • Create List File.
    echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
  • Update Package.
    sudo apt-get update
  • Install MongoDB package
    sudo apt-get install -y mongodb-org


Running MongoDB
  • sudo service mongod start
MongoDB Shell
  • type: mongo
Share:

Saturday, November 2, 2019

Install JRE on Ubuntu


Java Runtime Enviroment also known as JRE is part of Java Development Kit JDK a software development environment for writing Java applications. JRE consist Java Virtual Machine, core classes, and supporting files.
Installing JRE on ubuntu is a little bit different with installation JDK. First, Download the JRE file in here. Remember to download JRE version based on your system version either it's 32bit or 64bit.
After finished downloading your JRE version, we can begin the installation process.

  1. Create new folder in opt directory. In terminal type this command "sudo mkdir -p -v /opt/java/64".
  2. Go to folder containing your JRE download file(example: use"cd Downloads" to move to Download directory from your home directory) and unpack the file by typing "tar xvzf YOUR JRE FILE".
  3. Moved unpacked contents into system folder that you created in step1. Type "sudo mv -v YOUR JRE FILE /opt/java/64".
  4. Inform the system and make the new JRE become your system default.
    sudo update-alternatives --install "/usr/bin/java" "java" "/opt/java/64/YOUR EXTRACTED JRE FILE/bin/java" 1
    sudo update-alternatives --set java /opt/java/64/YOUR EXTRACTED JRE FILE/bin/java
  5. Install Firefox plugins. Type "mkdir -v ~/.mozilla/plugins".
  6. Removed IcedTea plugin, if it has been installed. type "sudo apt-get remove icedtea-6-plugin && sudo apt-get remove icedtea-7-plugin".
  7. Remove an older version of the Java plugin. Type "rm -v ~/.mozilla/plugins/libnpjp2.so".
  8. Install the plugin by creating symbolic link. Type "ln -s /opt/java/64/jre1.7.0_60/lib/amd64/libnpjp2.so ~/.mozilla/plugins/".
  9. Close and restart Firefox
  10. Type "about:plugins" in Firefox URL bar. If your installation is correct you will see something like this:

Share: