Showing posts with label General Computing. Show all posts
Showing posts with label General Computing. Show all posts

Friday, February 23, 2018

Imperative vs Object-Oriented vs Functional Programming Style

Same program written in three programming styles.

IMPERATIVE PROGRAMMING STYLE




OBJECT-ORIENTED PROGRAMMING STYLE




FUNCTIONAL PROGRAMMING STYLE





Function Closure


A function closure contains a function and the environment the function is referenced in. The referencing environment may contain variables that are not local to the function but are nonetheless accessible to the function code. These non-local variables are called free variables or upvalues.

// example of function closure for Inner()
function Outer(){
    var upvalue = 1

    function Inner(){
        // upvalue is available here & part of function closure
        // for Inner()
        var funcVar = 2
    }
}

Friday, June 16, 2017

Iteration vs. Recursion

Code

def factorial_iter(n):
    num = 1
    while n >= 1:
        num = num * n
        n = n - 1
    return num
   
def factorial_recur(n):
    if n == 1:
        # base case that stops recursion
        print ('  return 1 (this is the top of the stack)')
        return 1
    else:
        print ('  return ' + str(n) + '*' + 'factorial_recur(' + str(n-1) + ')')
        # the function is calling ITSELF
        return n * factorial_recur(n-1)
       
number = 5

print ('-' * 20)
print ('Using Iteration. Factorial of ' + str(number))
print ('-' * 20)
print (str(factorial_iter(number)))

print (' ' * 20)

print ('-' * 20)
print ('Using Recursion. Factorial of ' + str(number))
print ('-' * 20)
print ("factorial_recur: stack trace")
print ("Note how the function calls itself. This is no different than calling another function")
print ("It is called RECURSION because the function is calling ITSELF")
print ("When it reaches the BASE CASE (n==1), it stops calling itself")
print ("The function MUST stop calling itself at some point, otherwise it will never end (likely run of out memory)")
print ("The innermost function call is evaluated first, where n=1 and then return 2 * 1, then return 3 * 2, and so on")
print ("This is called unwinding of the stack. Start from the BOTTOM and evaluate upwards. Think of a function evaluation as follows.")
print ("(5 * (4 * (3 * 2) * (1)))")
print ("Stack trace")
print (str(factorial_recur(number)))

Output

--------------------
Using Iteration. Factorial of 5
--------------------
120
                   
--------------------
Using Recursion. Factorial of 5
--------------------
factorial_recur: stack trace
Note how the function calls itself. This is no different than calling another function
It is called RECURSION because the function is calling ITSELF
When it reaches the BASE CASE (n==1), it stops calling itself
The function MUST stop calling itself at some point, otherwise it will never end (likely run of out memory)
The innermost function call is evaluated first, where n=1 and then return 2 * 1, then return 3 * 2, and so on
This is called unwinding of the stack. Start from the BOTTOM and evaluate upwards. Think of a function evaluation as follows.
(5 * (4 * (3 * 2) * (1)))
Stack trace
  return 5*factorial_recur(4)
  return 4*factorial_recur(3)
  return 3*factorial_recur(2)
  return 2*factorial_recur(1)
  return 1 (this is the top of the stack)
120

Wednesday, December 7, 2016

Takeaways from re:Invent 2016 Amazon Web Services Conference

I attended the Amazon Web Services conference last week. Here are my main takeaways.

AWS is introducing a large number of higher level services. Amazon Web Services now spans the entire spectrum from IaaS (Infrastructure as Service,  e.g. EC2 VMs and VPCs) to SaaS (Software as a Service, e.g. hosted machine learning). The main themes of this year's conference were serverless computing and machine learning (Artificial Intelligence).

AWS has settled on three computing models: EC2 instances (VMs you provision and manage), Containers (Fully-managed Docker containers you run your micro services in an isolated manner) and Lambda (code you just upload to AWS and run; AWS does all the provisioning and scaling). Lambda now supports C#, in addition to node.js, Java, and Python. We have been successfully using Lambda (Java implementation) since the beginning of this year. Having C# as an option, makes Lambda more useful for us since we can now refactor and reuse our current C# code.

With respect to machine learning, AWS introduced Rekognition (hosted image recognition service), Polly (speech to text engine), and Lex (text to speech engine). This provides the entire stack for creating speech-based systems. With the success of Amazon Echo (Alexa), I think speech-based systems are going to be big in 2017.

It appears relational database systems are still the storage solution of choice for most companies. AWS Aurora (a MySql-like hosted database service) was extensively covered. It is apparently Amazon's fastest growing service. There wasn't as much talk of DynamoDb (NoSql) database this year.

Wednesday, November 16, 2016

Setup and use a new git repository on github.com

1. Install git from http://git-scm.com

2. Signup for an account at http://github.com

3. Login github.com and create a repository
+ sign at the top right
public
Add .gitignore: Node so that Node module folder does not get source controlled (large in size)

4. Get the reposiory url. Clone or Download button.

5. Run the git clone command in the directory where you want the repository cloned.

git clone <git-repository-url>

6. Three common commands:

Stage your work (. means stage everything).
git add .

Commit your changes locally
git commit -m "comment"

Push your work to Github
git push


Monday, October 20, 2014

Ten Safe Computing Tips

1) Do not install anything you don't absolutely need

  • The more useless junk you install, the slower your computer gets!
  • If you must install something, install from a reputable source. For example, if you want to install Adobe Reader, install it from adobe.com, not from any other site.
  • Be very suspicious of "free" software. Such software may be ad driven (i.e. pops up ads on your computer) or may track activity on your computer. At best, the site may gather and sell your information as marketing intelligence. At worst, a malicious hacker might use this information to learn more about you.
  • Pay close attention to all installation options as you install any software, even from a reputable company. Read carefully what is about to be installed on your computer. Instead of simply choosing "Default (or Express/Common)" installation option, choose "Custom".  As you install, look for "options" that are pre-checked such as "Make search engine X the default" or make "Site Y my home page" or "Send diagnostic data to Z". Be sure to un-check all options you don't need or understand.
  • Web-based applications are a good alternative to installing software on your local drive but be sure to use applications from reputable sites (such as Google docs and sheets or Microsoft 360).

2) Secure your computer

  • Set a login password for your computer. This is the least you can do. This is your first line of defense, if you lose or misplace your computer.
  • Keep your computer updated with security updates. For example, Microsoft updates Windows Defender frequently to guard against the latest malware. Make sure you get these updates.
  • Do use the built-in security of your operating system. For example, in Windows make sure you have the Windows Defender and Windows Firewall turned on. You may want to install a dedicated virus protection software but bear in mind that such software may slow down your computer.
  • Secure your computer with a physical lock (such as a cable lock for your laptop).

3) Use a strong password, do not use the same password for multiple websites

  • Longer the password, the better. Use a phrase you can remember as a password.
  • Don't use the same password for multiple sites. If one password is leaked, a hacker can use that password to access your confidential information on other sites.

4) Do not store unencrypted passwords on your computer

  • Do not store passwords in clear text or in a Word document on your computer. Use a program such as KeePass to save passwords in encrypted form.
  • In addition, you may want to store your encrypted KeePass file containing passwords in Dropbox, SkyDrive, or Google Drive. That way your passwords are backed up (in encrypted form) and you can access them from multiple devices. (KeePass is available on multiple devices.)

5) Do not communicate passwords or social security number through email

  • Email is not secure; information present in an email can be stolen in transit. 
  • In addition, copies of email are stored on email servers for a long time. If an email account is comprised, you don't want your passwords or social security number to be compromised as well.

6) Do not store unencrypted personal information in the Cloud

  • You do not know where your data will end up once you upload it to the Cloud (e.g. Dropbox, Google Drive, iCloud, or Sky Drive). Do not store unencrypted confidential information in the Cloud.
  • You can zip and encrypt sensitive files with a program such as 7-Zip before storing them in the Cloud.
  • Store personal information (such as Social Security number, Driver License Number, etc.) in an encrypted form using a program such as KeePass. The encrypted file containing personal information can then be stored in the Cloud (See the advice in item 4).
  • MS Word and Excel documents that contain confidential information (such as list of financial assets or account numbers) should be secured with a password (MS Office allows you to set a password for a document or spreadsheet). You should store document or spreadsheet passwords in KeePass so you don't forget them one year from now.

7) Browse the Web cautiously

  • When you visit a site, ensure you are visiting a legitimate site. Look in the Url box. If you are intending to visit your Wells Fargo banking site, the Url box must say wellsfargo.com. Look closely at the spelling of the Url. In addition, make sure there is a green padlock icon in the Url box. This padlock indicates that the communication between your computer and the website is encrypted.
  • When you visit a website, that website can leave behind bits of tracking information called cookies. Cookies have legitimate uses but some of the sites use these cookies to track your activity and sell the marketing intelligence they glean from your activity. Don't visit or linger on a website you don't trust.
  • Some sites have big, flashy buttons that invite you to install "free" games or productivity software or offer free virus scan of your computer. Others offer to make your computer faster by running diagnostics. Don't do it. Trust reputable websites only. If uncertain, go to google.com and type in a search term to do your own investigation first, for example, type: Is abc free game malware

8) Control your digital footprint

  • Do you run around the mall blurting out personal information, what you like or dislike, where you have been or where you are planning to visit? Incredibly, people have no qualms about putting such information on the Internet where 8 billion people can access it.
  • Do you really need to sign up for that many social networking sites? The more information you divulge about yourself online, the more information about you is available to bad guys.
  • Whatever you put out on the Internet lives on forever and may come back to haunt you when you apply for your dream job or college admission.
  • There are, of course, good reasons to have presence on the Web such as a professional blog to promote your expertise in a certain area.

9) Be wary of clicking any link in an email

  • As a general rule, do not click links sent to you in an email. This is perhaps the most common way in which bad guys try to steal your personal information by misdirecting you to a malicious website.
  • Instead, start the Web browser and go to the website directly. For example, let's say you receive an email that says you have received money in your PayPal account: Click here to see your new balance. Do not click the link. Instead, start the Web browser, type in paypal.com in the Url box and log into PayPal to check your balance.

10) Be alert when using your computer in public places

  • When using your computer in public, look who is behind you, next to you. Is someone watching as you type in your password?
  • If possible, do not visit your banking or financial sites in public places. Even from a distance, a bad guy can see which bank site you are visiting (and thus know where you bank) and can use that information to do harm. 
  • Do not use public computers (libraries, hotels, airports) to access your personal information (banking, email, etc.). If you must, make sure to uncheck the box on the login screen that says "Remember Me" or "Sign me in automatically". When done, explicitly log out of the account. To be sure not to leave any personal information behind, you may want to clear the browser cache and cookies after you are done using the computer (this option is in browser Settings in Chrome and File | Tools | Options in IE). Then, close all browser windows.