Monday, March 25, 2019

Statistics 101

What is Statistics
A branch of mathematics dealing with data collection, organization, analysis, interpretation and presentation.

Probability is a mathematical language to discuss uncertainties and it plays a key role in statistics.
In layman's term, statistics is a toolbox with methods to get answers from data.
Terms
Binomial random variable
  • A distribution of a sum of the squares of k independent standard normal random variables.
  • It is a special case of the gamma distribution
  • Is one of the most commonly used probability distributions in inferential statistics.
Chi-squared distribution with k-degrees of freedom
  • The random variable from the experiment that has only two possible values or outcomes.
Chi-square analysis
  • χ2 test is a hypothesis test where the sampling distribution of the test statistic is a chi-squared distribution when the null hypothesis is true.
  • In simple term, it often means 'Pearson's chi-squared test.
  • Used to determine whether there is a significant difference between the expected frequencies and the observed frequencies in one or more categories.
  • It is often constructed from a sum of squared errors or the sample variance.
  • It assumes the population has independent normally distributed data, which is valid due to central limit theorem.
Confidence interval
  • Estimate parameters of a population using a sample.
  • Use the mean x from sample to find a range of values that we can be confident to contain the mean of the population sampled
  • Lower bound = estimate - margin of error
  • Upper bound = estimate + margin of error
  • T-intervals - use it when population standard deviation is unknown and original population normal or sample size >= 30. This formulus use sample standard deviation instead of population standard deviation.
  • Z-intervals - use it when sample size >= 30 and population standard deviation known, or original population normal with the population standard deviation known.
Gamma distribution
  • Two parameter family of continuous probability distributions.
  • Exponential distribution, Erlang distribution, and chi-squared distribution are special cases of the gamma distribution.
  • Three different parametrizations in common use:
  • 1. With a shape parameter 𝞳 and a scale parameter 𝞡.
    2. With a shape parameter 𝞪 = 𝞳 and an inverse scale parameter 𝞫 = 1/𝞡.
    3. With a shape parameter 𝞳 and a mean parameter 𝞵 = 𝞳𝞡 = 𝞪/𝞫.
    
  • Gamma distribution is the maximum entropy probability distribution for a radom variable X for which E[X] = kθ = α/β is fixed and greater than 0.
  • E[ln(X)] = ψ(k) + ln(θ) = ψ(α) − ln(β) is fixed (ψ is the digamma function)
Hypothesis testing
  • Two types of errors in hypothesis testing: I and II.
  • Test and draw conclusions about the value of a parameter
  • Power analysis
  • Tests of proportion
  • P-value approach
Normal distribution
  • symmetrical about its mean.
  • Bell-shaped with a single peak at the center of the distribution.
  • Arithmetic mean is at the peak and at the center, with half the area above the mean and half under the mean.
  • It is asymptotic and the curve gets closer to the X-axis but never really touches it.
  • Mean, median and mode are equal
  • Curve extends to infinity theoretically
  • Standard Normal distribution has a mean of 0 and a standard deviation of 1
  • Z-score or Z-value is the distance between a selected value x, and the population mean mu, divided by the population standard deviation sigma.
  • z = (x-𝞵) / 𝞼
    
  • 68.26 % of the area under the normal curve is within one standard deviation of the mean. 𝞵 ± 𝞼
  • 95.44 % of the area under the normal curve is within two standard deviation of the mean. 𝞵 ± 2𝞼
  • 99.74 % of the area under the normal curve is within three standard deviation of the mean. 𝞵 ± 3𝞼
Probability distribution
  • all possible outcomes of an experiment and the corresponding probability
  • the sum of the probabilities of the various outcomes is 1.
  • The probability of a particular outcome is between 0 and 1
  • The standard deviation of particular probability is in inverse proportion to the sample size
Other basic terms
  • ANOVA - analysis of variance
  • degrees of freedom
  • mean E[x]
  • median
  • mode - most prevalent data points in the data set
  • normalize
  • outlier
  • p-value
  • parameter - any summary number that describes the population, ie average or percentage
  • population - any large collection of objects of interest.
  • r-squared - how much the regression function explains the variation in outcomes
  • random variable - numerical value determined by the outcome of an experiment
  • range
  • random sample
  • sample - a representative group chosen from the entire population
  • standard deviation - how far away is the data from the mean.
  • standard error
  • statistic - a summary number that describe the sample, ie average or percentage
  • Variance of a probability distribution - sigma squared Var[x]
  • z-score
  • References

    Thursday, March 14, 2019

    Python Projects

    GAPMINDER visualization
  • Gapminder.org data
  • ## www.gapminder.org/data
    ## www.gapminder.org/tools
    ## Factfulness (2018) Hans Rosling
    ## Search YouTube: Hans Rosling's 200 Countries, 200 Years, 4 minutes
    
    Example
    
    import re, mailbox, csv
    import numpy as np
    import pandas as pd
    import scipy.stats
    from sklearn import datasets
    import matplotlib
    import matplotlib.pyplot as plt
    from IPython import display
    from ipywidgets import interact, widgets
    # %matplotlib inline
    gapminder = pd.read_csv('gapminder.csv')
    
    
    Wine quality
    Email Analysis

    Sunday, March 10, 2019

    Python Scikit-Learn Library

    Meet sklearn
  • scikit-learn.org
  • scikit-learn tutorial
  • Scikit-learn user guide
  • import sklearn as sk
    sklearn.__version__
    import nose
    nosetest sklearn -exe
    
    SkLearn Basics
  • Compliments and extend scipy
  • Classification
  • Regression
  • clustering
  • Dimensionality reduction
  • Example
    from sklearn import datasets
    iris = datasets.load_iris()
    digits = datasets.load_digits()
    print(digits.data)
    from sklearn import svm
    clf = svm.SVC(gamma=0.001, C=100)
    clf.fit(digits.data[:-1], digits.target[:-1])
    clf.predict(digits.data[-1:])
    
    Example
    # Install a pip package in the current Jupyter kernel
    import sys
    !{sys.executable} -m pip install mglearn
    
    Example
    from sklearn.linear_model import LinearRegression
    # Training data
    X = [[7], [8], [10], [14], [18]]
    y = [[8], [9], [13], [17.5], [20]]
    # Create and fit the model
    model = LinearRegression()
    model.fit(X, y)
    print 'Predict value: $%.2f' % model.predict([12])[0]
    

    Tuesday, March 5, 2019

    Python Decorators

    Python Decorators
  • Python Decorators
  • @accepts(int,int)
    @classmethod
    @decorator
    @property
    @returns(float)
    @staticmethod
    @funcattrs(grammar="'@' dotted_name [ '(' [arglist] ')' ]",
                   status="experimental", author="BDFL")
    
    @property
    class TempC2F:
        def __init__(self, temp = 0):
            self._t = temp
    
        def to_fahrenheit(self):
            return (self.temperature * 1.8) + 32
    
        @property
        def temperature(self):
            return self._t
    
        @temperature.setter
        def temperature(self, v):
            if v < -273:
                raise ValueError("Temperature below -273 is not possible")
            self._t = v
    

    Friday, March 1, 2019

    Java Exercises #1

    Exercise 1-1
    1. Write a program that take inputs from command line and create a todo list.
    2. Take names from command line and create a list. Print out the sorted list.
    3. Take a list of numbers from input and search if a certain number is there.
       Try different ways and analyze run time.
    4. Take two n digit numbers and use an algorithm better than brute-force to multiply.
    5. Implement Karatsuba algorithm for integer multiplication
    Exercise 1-2
    1. Build a binary tree
    2. Take "car" and return how many possible way to convert it to "let", one letter at a time.
    Exercise 1-3
    Take a list of students name and their score. Print the top 5 scores and their names.
    Exercise 1-4
    Take a list of pairs of numbers, which indicate pairs of nodes that is connected. Provide two functions:
    1. Output the connected clusters.
    2. Return true or false if two nodes are connected.

    Python Exercises #1

    Exercise 1-1
    1. Write a program that take inputs from command line and create a todo list.
    2. Take names from command line and create a list. Print out the sorted list.
    3. Take a list of numbers from input and search if a certain number is there.
             Try different ways and analyze run time.
    4. Take two n digit numbers and use an algorithm better than brute-force to multiply.
    5. Implement Karatsuba algorithm for integer multiplication
    Exercise 1-2
    1. Build a binary tree
    2. Take "car" and return how many possible way to convert it to "let", one letter at a time.
    Exercise 1-3
    Take a list of students name and their score. Print the top 5 scores and their names.
    Exercise 1-4
    Take a list of pairs of numbers, which indicate pairs of nodes that is connected. Provide two functions:
    1. Output the connected clusters.
    2. Return true or false if two nodes are connected.

    Wednesday, February 27, 2019

    Python Seaborn Library

    Meet Seaborn
    Why Seaborn
  • Compliments and extend matplotlib
  • Work with NumPy, Pandas data frames
  • Makes default parameters in matplotlib easy
  • Built-in themes for data analysis and machine learning algorithms
  • Matplotlib vs Seaborn
  • Matplotlib
  • import matplotlib.pyplot as plt
    import pandas as pd
    
    fg, ax = plt.subplots()
    mydt = pd.read_csv("https://path/to/data.csv")
    
    ax.violinplot(mydt["axi_label"], vert=False)
    plt.show()
    
  • Seaborn
  • # Import the necessary libraries
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    mydt = sns.load_dataset("data")
    
    sns.violinplot(x = "axi_label", data=mydt)
    plt.show()
    

    Thursday, February 21, 2019

    Jupyter 101

    Jupyter notebook : IPython
  • jupyter.org
  • Install Jupyter notebook
  • Install Anaconda
  • Anaconda.org Cloud
  • Anaconda packages using pip
  • Anaconda packages development in GitHub
  • Anaconda documentation
  • # anaconda packages installation
    # conda install pip
    % conda install -c anaconda pip
    % pip install package-name 
    # to launch
    % jupyter notebook
    # Or, launch from Anaconda Navigator
    # to install new packages
    # import sys
    # !{sys.executable} -m pip install newpackage
    
    Basics
  • Dashboard
  •    File
       Run
       New
    
  • Menu Bar
  • Tool Bar
  • Create new notebook
  • Rename notebook
  • Save notebook
  • Load notebook and "run all"
  • Cell : command mode, edit mode, raw NB convert mode
  • shortcuts : command palette
  • Shift-Enter
  • Example : code cell
    import numpy as np
    print (np.__version__)
    
    import sqlite3
    # from IPython.utils.path import get_ipython_dir # moved to IPython.paths
    import pprint
    from IPython.paths import get_ipython_dir
    hist_file = '%s/profile_default/history.sqlite' % get_ipython_dir()
    print (hist_file)
    
    Example : markdown cell (use print preview or shift-enter, double click to edit)
    [esc]+m
    %matplotlib qt
    %matplotlib auto
    %matplotlib inline
    %matplotlib -l
    %matplotlib --list
    [esc]+o           ## toggle output
    %load_ext?
    %time?
    !ls
    !dir
    !pwd
    
    ### Heading1
    ## Heading2
    # Heading1
     [Link to this page](http://www.google.com)
    
    1. first
        1. first of first
    2. second
    
    - bullet
        - bullet
    *italic*
    _italic_
    **bold**
    ***italic and bold***
    ~~strike through~~
    
    Example : Markdown Table
    # use http://www.tablesgenerator.com/markdown_tables
    #     to generate the markdown code for tables
    # copy the table form Excel or Google Sheet
    #     paste to the table on This Page
    #     and click [Generate]
    # copy the Result code to your Jupyter Notebook Markdown Cell
    
    Example : Fun with matplotlib
    import matplotlib.pyplot as plt
    t = np.ones(30)
    # red dashes, blue squares and green triangles
    plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
    plt.show()
    
    Example : Visualize Big O Notation
    import numpy as np
    import matplotlib.pyplot as plt
    n = np.ones(40)
    t = np.arange(1.,20.,0.5)
    lineflt = plt.plot(t, t*0, 'g--' )
    linelog = plt.plot(t, np.log(t), color='#990000', label="O(Log n)")
    #plt.plot(t, np.log(t), 'b', t, t, 'r', t, t*t,)
    linelin = plt.plot(t,t, label="O(n) linear", color='orange')
    linenlg = plt.plot(t,t*np.log(t), label="O(n Log n)")
    linen2n = plt.plot(t,t*t, 'c', label="O(n^2)")
    linen3n = plt.plot(t,t**3, color='#ffa500', label="O(n^3)")
    line2tn = plt.plot(t,2**t, 'm', label="O(2^n)")
    plt.ylim(0,100)
    plt.legend()
    plt.grid(True)
    # plt.ylabel('some numbers')
    plt.show()
    

    Wednesday, February 20, 2019

    Python Pandas Library

    What is Pandas
  • Pandas : Panel Data
  • Structure data as a virtual spreadsheet.
  • Visualize data to find issues, such as sparse dataset, missing dataset.
  • Pandas.pydata.org
  • Pandas Docs
  • Pandas Quick Overview
  • NumPy.org
  • SciPy.org
  • See PyPI for 3rd party libraries
  • >>> import pandas as pd
    >>> from pandas import DataFrame, Series
    >>> help (pd)
    
    Applications
  • For a recommendation system, create a rating matrix and predict missing ratings for each user
           based on existing reviews.
  • Classification
  • Value estimation
  • Basics
    Example : First Look at pandas
    import numpy as np
    import pandas as pd
    mysr = pd.Series ([2,4,6, np.non, 7,9])
    mydt = pd.date_range('20200202', periods = 6)     ## default frequency is daily
    mynp = np.array (np.arange(24)).reshape((6,4))
    mydf = pd.DataFrame (mynp, index = mydt, columns = list('PQST'))
    print ( mydf.head() )                             ## default print head 5 rows
    print ( mydf.tail(10) )                           ## default print tail 5 rows
    print ( mydf.values )
    print ( mydf.index )
    print ( mydf.columns )
    
    d.set_option ('display.percision', 2)
    # pandas option docs
    print ( mydf.describe() )                         ## a quick statistical summary, 
                                                      ## int will be viewed as float
    pd.set_option ('display.percision', 2)
    # pandas option docs
    
    # create data frame from python dictionary
    dcdf = pd.DataFrame ({ 
        'float' : 1., 
        'time' : pd.Timestamp ('20200808'),
        'series' : pd.Series (1, index = list (range(4)), dtype = 'float32' ),
        'array' : np.array ([3] * 4, dtype = 'int32' ),
        'categories' : pd.Categorical (['t1','t2','t3','t4']),
        'misc' : 'useless data'
        })
    print (dcdf.dtypes)
    print (dcdf.T)
    dcdf.sort_index( axis=1, ascending=False)          ## sort along the axis, default axis=0
    dcdf.sort_values (by='Q', ascending=False)
    
    Example : get json data and plot density charts
    from pandas import DataFrame, Series
    import pandas as pd
    import json
    from  collections import defaultdict
    
    def get_count(l=[]):
        res = defaultdict(int)
        for i in l:
            res[i] += 1
        return res
    
    def best_count(h={}, n=5):
        list = [ (v, k) for k, v in h.items() ]
        return list[-n:]
    
    # file.json contains list of dict, each has many fields
    data = [json.loads(line) for line in open('file.json')
    frame = DataFrame (data)
    print (frame)
    
    namelist = [item['name'] for item in data if 'name' in item]
    hcount = get_count(namelist)
    toplist = best_count(hcount)
    
    # using pandas
    ptoplist = frame['name'].value_counts()
    print( ptoplist[:5] )
    
    # cleaning dataset
    newlist = frame['name'].fillna('Missing name')       ## replace missing values
    newlist[newlist == ''] = 'Unknown'                   ## replace empty entries
    
    # Series
    results = Series ( [s.split()[0] for s in frame.key.dropna()] )
    print (results[:5])
    print (results.value_counts()[:8]
    newframe = frame [frame.key.notnull()]
    location = np.where( newframe['key'].str.contains('USA'), 'In USA', 'Out of USA' )
    by_location = newframe.groupby ( ['name', location] )
    groupcnt = by_location.size().unstack().fillna(0)
    print (groupcnt[:5])
    
    sortlist = groupcnt.sum(1).argsort()
    print (sortlist[:5])
    
    sublist = groupcnt.take( sortlist )[-12:]
    sublist.plot( kind='barh', stacked=True )
    normlist = sublist.div( sublist.sum(1), axis=0 )
    normlist.plot ( kind = 'barh', stacked=True )
    
    
    Example : view data from CSV file
    import pandas
    import webbrowser
    import os
    
    # "data_id" is the column header in the file
    data_table = pandas.read_csv("data_set.csv", index_col="data_id")
    html = data_table[0:100].to_html()
    with open("data.html", "w") as f:
        f.write(html)
     
    full_filename = os.path.abspath("data.html")
    webbrowser.open("file://{}".format(full_filename))
    
    Example : Using Pandas with NumPy
    import pandas as pd
    import numpy as np
    import webbrowser
    import os
    
    # "data_id" is the column header in the file
    data_table = pd.read_csv("data_set.csv")
    result = pd.pivot_table(data_table, index='user_id', columns='data_id', aggfunc=np.max)
    
    # write to a csv file
    result.to_csv("data.csv", na_rep="")
    
    # or generate a html and write out
    html = result[0:100].to_html(na_rep="")
    with open("data.html", "w") as f:
        f.write(html)
     
    full_filename = os.path.abspath("data.html")
    webbrowser.open("file://{}".format(full_filename))
    
    Example : Create a Recommendation matrix
    import numpy as np
    import pandas as pd
    import matrix_factorization_utilities
    
    raw_data = pd.read_csv('data.csv')
    ratings = pd.pivot_table(
              raw_data, index='user_id', columns='movie_id', aggfunc=np.max)
    
    # Apply matrix factorization to find the latent features
    x,y = matrix_factorization_utilities.low_rank_matrix_factorization(
                                                   ratings.as_matrix(),
                                                   num_features=15,
                                                   regularization_amount=0.1)
    
    # Calculate ratings by multiplying the x by y
    predicted_ratings = np.matmul(x, y)
    df = pd.DataFrame(index=ratings.index,
                      columns=ratings.columns,
                      data=predicted_ratings)
    df.to_csv("predicted_ratings.csv")
    

    Python NumPy Library

    What is NumPy
  • NumPy : Numerical Python
  • Support large multi-dimensional arrays and metrics
  • Support large collection of high-level mathematical functions on these arrays
  • NumPy.org
  • NumPy.org
  • NumPy reference docs
  • NumPy commands html
  • See PyPI for 3rd party libraries
  • >>> import numpy as np
    >>> help (np)
    >>> a = np.array([1,2,3])
    >>> b = a.tolist()
    >>> c = np.arange(4).reshape(2.2)
    >>> d = np.zeros((2,2))
    
    Basics
  • Attributes : data types, size, shape, memory consumption
  • Indexing
  • Slicing
  • Reshaping
  • Joining and splitting
  • Masking
  • Broadcasting
  • Structured arrays
  • Example : Attributes
    import numpy as np
    dir(np)
    print(np.__version__)
    nparray = np.arange(10)
    print (nparray)
    
    np.random.seed(0)
    x = np.random.randint(10, size=8)
    
    x.ndim
    x.shape
    x.size
    x.dtype
    x.dtype.str
    x.itemsize
    x.nbytes
    x.real              # for complex numbers, this returns the real part
    x.imag              # for complex numbers, this returns the imaginary part
    x.size
    
    Example : Indexing
    import numpy as np
    np.random.seed(0)
    x = np.random.randint(10, size=8)
    x[1]
    x[-1]
    x[-2]
    x[0] = 3.456 # will be truncated, dtype is int32
    
    y = np.random.randint(10, size=(5,5))
    y[1,2]
    y[1][2]             # same as y[1,2]         
    y[2,-1]
    y[0,1] = 20
    y
    
    Example : Slicing
    import numpy as np
    z = np.arange(10)
    z[:5]
    z[3:]
    z[2:8]
    z[::3]              # every third element
    z[1::2]             # every other, odd elements
    z[::-1]             # all elements in reversed order
    z[9::-4]            # every fourth element in reversed order
    
    Example : Multi-dimensional subarrays
    import numpy as np
    np.random.seed(0)
    x = np.random.randint(10, size=(3,4,3))
    x
    x[:2, :3]           # sub array taking 2 rows and three columns
    x[:3, ::2]          # sub array taking 3 rows and every other column
    x[::-1, ::-1]       # sub array reversing rows and columns
    x[::-1]             # reverse the whole array
    
    # accessing single row or column
    x[0, :]             # first row, same as x[0]
    x[:, 0]             # first column
    
    Example : Creating copies of subarrays
    # Slice sub arrays without copying will modify the original array
    # when you modify the subarray
    import numpy as np
    np.random.seed(0)
    x = np.random.randint(10, size=(3,4,3))
    x
    y = x[:2, :2]
    y[0,0] = 12345
    print(y)
    print(x)
    
    # create copy of x
    z = x[:4,:4].copy()
    # now try to change z and check x
    q = z.resize((1,2))
    q.ravel()             # flatten as a view, point to same object
    q.flatten()           # same as ravel, and allocate new memory 
    
    p = q.view()          # p and q point to same object
    id(p)
    id(q)
    p is q
    r = np.insert(p, 1, 505, axis=0)
    s = np.empty(p.shape) # create new array in same shape as 'p'
    np.copyto( s, q )
    np.delete( q, 1, axis=0 )
    
    Example : Reshaping
    import numpy as np
    x = np.arange(1,10).reshape(3,3)
    y = np.arange(1,17).reshape(4,4)
    # size of np.arange must match size of the reshaped array
    
    x1 = np.array([1,2,3])
    x1.reshape((1,3))
    x1[np.newaxis, :]     # row vector using newaxis
    x1[:, np.newaxis]     # column vector using newaxis
    x1.reshape((3,1))     # column vector using reshape
    
    Example : Concatenation and Stacking of arrays
    import numpy as np
    x = np.arange(5)
    xtriple = x*3
    print(xtriple)
    y = np.arange(10)
    z = np.concatenate([x,y])
    q = np.concatenate([x,y,z])
    
    x2d = np.array([[1,2,3],[4,5,6]])
    y2d = np.concatenate ([x2d,x2d] )
    z2d = np.concatenate ([x2d,x2d], axis=1)
    
    # using np.hstack or np.vstack
    # np.dstack will stack along the third axis
    x = np.array([1,2,3])
    x2d = np.array([[9,9,9],[8,7,6]])
    xx = np.vstack([x, x3d])
    y = np.array([[3],[3]])
    yy = np.hstack([x2d, y])
    column_stack((x,y))
    row_stack((x,y))
    
    a = arange(4)reshape(2,2)
    b = np.array([[5,6]])
    hstack((a,b))   # same as np.append(a,b,axis=1) # append along y, change (*, y, *) shape
    vstack((a,b))   # same as np.append(a,b,axis=2) # append along x, change (*, *, x) shape
    concatenate((a,b), axis=0) # same as vstack
    dstack((a,b)) # depth stacking
    column_stack((a,b)) # stack 1d array column-wise
    row_stack((a,b))
    
    Example : Splitting, squeezing and reshaping
    import numpy as np
    x = [1,2,3,4,5,6,7,8]
    a,b,c = np.split(x, [3,5])
    # np.hsplit and np.vsplit
    x2d = np.arange(16).reshape((4,4))
    upper_ary, lower_ary = np.vsplit(x2d,[2])
    # check upper and lower arrays
    left_ary, right_ary = np.hsplit(x2d,[2])
    # check left and right arrays
    
    X = np.arange(27).reshape(3,3,3)
    # split()
    # hsplit()
    # vsplit()
    # dsplit()
    
    hsplit(X)
    split(X, 3, axis=1) # same as hsplit(X,3)
    split(X, 3, axis=0) # same as vsplit(X,3)
    dsplit(array,3)      # deep split
    
    Example : linespace, zeros, ones
    import numpy as np
    # notice linspac euse closed interval where end point is included
    ary = np.linspace(7,26,9)
    ary = np.linspace(7,26,9, retstep=True)
    ary = np.zeros(5, dtype='int32')
    np.ones((7,8))
    # default dtype for zeros and ones is float
    # default dtype for arange and linspace is int
    
    ary = np.ones((2,2))
    ary.squeeze()
    
    Example : random
    import numpy as np
    # notice linspac euse closed interval where end point is included
    ary = np.random.random.rand()            ## uniform distribution
    ary = np.random.random.randn()           ## gaussian normal distribution
    np.random.random.seed(0)                 ## not thread safe
    
    Example : Mathematical operators
    import numpy as np
    m0 = np.ones(2)
    m1 = np.ones(2)*3
    np.inner (m0,m1)
    
    m0 = np.arange(4).reshape(2,2)
    m1 = np.arange(8).reshape(2,4)
    np.dot (m0,m1)
    
    nparray.shape(1,2,3)
    nparray.sum(axis=2)
    np.set_printoptions(precision=4)
    
    Example : logical operators
    import numpy as np
    ary = np.arange(12).reshape(3,4)
    x0  = 0 == (ary % 3)
    x1  = ary > 3
    x2  = np.logical_and (x0, x1)
    # y and z would result in same matrix
    y = ary[x2]
    z = ary[ary > 3]
    
    Example : structured array
    import numpy as np
    
    person_data_def = [('name','s10'), ('height','f8'), ('weight','f8'), ('age','i2')] 
    people_array = np.zeros((4), dtype=person_data_def)
    
    Example : Comparison
    import numpy as np
    
    x = [1,2,3,4,5]
    print (x*2)
    y = np.array([1,2,3,4,5])
    print (y*2)
    
    # to get the same result for x
    for i, v in enumerate (x):
        x[i] *= 2
    print(x)
    

    Tuesday, February 19, 2019

    Java Maps

    Java Map Interface
    Library
  • java.util.Map
  • java.util.TreeMap

  • Background
  • Maps are part of the Collections Framework but technically Maps are not collections
           because they do not implement the Collection interface.
  • Maps store key / value pairs and all keys must be unique.
  • All maps implement the Map interface and share a common functionality that allows
           adding a key/value pair or access the value given they key.
  • Map provides two collection-views to deal with the keys and values separately.
  • A set of the entries is obtained by calling entrySet(). It returns a Set collection.
           The Set that contains the entries, which are held in an object of type Map.Entry.
  • The key can be obtained by calling getKey().
  • The value can be obtained by calling getValue().
  • The Set of the keys can be obtained by calling keySet().
  • The Collection of the values can be obtained by calling values().

  • Methods
    # Map is a generic interface
    # interface Map<K, V>
    public void clear()
    public boolean containsKey(Object k)
    public boolean containsValue(Object v)
    public Set<Map.Entry<K, V>> entrySet()
    public V get(Object k)
    public boolean isEmpty()
    public Set<K> KeySet()
    public V put(K k, V v)
    public void putAll (Map<? extends K, ? extends V> m)
    public v remove(Object k)
    public int size()
    public Collection<V> values()
    
    Java Map example code
    import java.io.*;
    import java.util.*;
    
    class MyMaps
    {
        public static void main(String args[]) {
            // Create a tree map
            TreeMap<String, Integer> myNums = new TreeMap<String, Integer>();
            myNums.put ("this", 99);
            myNums.put ("one", 1);
            myNums.put ("two", 2);
    
            System.out.println ("the map contains " + myNums.size() + " items.");
    
            // Create a set of the entry
            Set<Map.Entry<String, Integer>> set = myNums.entrySet();
            for (Map.Entry<String, Integer> my : set) {
                System.out.println (my.getKey());
                System.out.println (my.getValue());
            }
    
            // A new map to merge with myNums
            TreeMap<String, Integer> myNums2 = new TreeMap<String, Integer>();
            myNums2.put ("three", 3);
            myNums2.put ("four", 4);
            myNums.putAll (myNums2);
    
            // To show all entries after merging
            set = myNums.entrySet();
            System.out.println (myNums.size());
    
            for (Map.Entry<String, Integer> my : set) {
                System.out.println (my.getKey());
                System.out.println (my.getValue());
            }
    
            // To search a key
            if (myNums.containsKey("six")
                 System.out.println (myNums.get("six"));
            // Search for a value
            if (my.Nums.containsValue (1))
                System.out.println ("yes");
    
            // To remove an entry
            if (myNums.remove("six") != null)
                System.out.println ("six is removed!");
            else
                System.out.println (my.getValue());
    
            // Show entries after removal
            Set<String> keys = myNums.keySet();
            for (String str : keys)
                System.out.println (str);
            // Display the value set after removal
            Collection<Integer> values = myNums.values();
            for (Integer i : values)
                System.out.println (i);
    
            // Clear the map
            myNums.clear()
            if (! myNums.isEmpty())
                System.out.println ("The map is not empty! Check again!");        
        }
    }
    

    Thursday, February 14, 2019

    Java Collections!

    Java Collections and Iterators
    Java collection framework includes:
    - Interface
    - Class of implementation
    - Algorithm
    
    Methods
    public boolean hasNext()
    public object next()
    public void remove()
    
    Java Multi Dimentional Arrays

    Examples
    import java.io.*;
    import java.util.*;
    import static java.lang.System.*;
    // import java.util.ArrayList;
    // import java.util.Arrays;
    // import java.util.List;
    
    
    public class ary2d{
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new FileReader("my.in"));
            PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("my.out")));
    
            int n = Integer.parseInt(br.readLine());
            StringTokenizer st = new StringTokenizer(br.readLine());
    
            ArrayList<String> list = new MyArrayList<String>("a", "b", "c");
            ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
    
            ArrayList<ArrayList<String>> biDemArrList = new ArrayList<ArrayList<String>>();
            ArrayList<String> temp = new ArrayList<String>(); // added () 
            temp.add("Hello world.");
            biDemArrList.add(temp);
    
            ArrayList<ArrayList<String>> matrix = new ArrayList<ArrayList<String>>();
    
            // List<ArrayList<Integer>> a = new ArrayList<>(); // java 1.7+
            List<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
    
            ArrayList<Integer> a1 = new ArrayList<Integer>();
            a1.add(1);
    
        }
    }
    
    class Array2D<T> extends ArrayList<ArrayList<T>> {
        public void addToInnerArray(int index, T element) {
            while (index >= this.size()) {
                this.add(new ArrayList<T>());
            }
            this.get(index).add(element);
        }
    
        public void addToInnerArray(int index, int index2, T element) {
            while (index >= this.size()) {
                this.add(new ArrayList<T>());
            }
    
            ArrayList<T> inner = this.get(index);
            while (index2 >= inner.size()) {
                inner.add(null);
            }
    
            inner.set(index2, element);
        }
    }
    
    class TwoDimArray<T> extends ArrayList<ArrayList<T>> {
        public void addToInnerArray(int index, T element) {
            while (index >= this.size()) {
                // Create enough Arrays to get to position = index
                this.add(new ArrayList<T>()); // (as if going along Vertical axis)
            }
            // this.get(index) returns the Arraylist instance at the "index" position
            this.get(index).add(element); // (as if going along Horizontal axis)
        }
    
        public void addToInnerArray(int index, int index2, T element) {
            while (index >= this.size()) {
                this.add(new ArrayList<T>());// (as if going along Vertical
            }
            //access the inner ArrayList at the "index" position.
            ArrayList<T> inner = this.get(index);
            while (index2 >= inner.size()) {
                //add enough positions containing "null" to get to the position index 2 ..
                //.. within the inner array. (if the requested position is too far)
                inner.add(null); // (as if going along Horizontal axis)
            }
            //Overwrite "null" or "old_element" with the new "element" at the "index 2" ..
            //.. position of the chosen(index) inner ArrayList
            inner.set(index2, element); // (as if going along Horizontal axis)
        }
    }
    
    Java Collections
        ArrayList astr=new ArrayList();
        astr.add("john");
        Iterator itr=astr.iterator();
        while(itr.hasNext()){
            System.out.println(itr.next());
        }
    

    C++ arrays!

    C++ arrays
    #include <stdio.h>
    #include <algorithm>
    #include <fstream>
    #include <iostream>
    #include <map>
    
    using namespace std;
    
    int main()
    {
        ifstream fin ("text.in");
        int n;
    
        fin >> n;
        int *array = new int[3];
        cout << " array " << endl;
        for ( int i = 0; i < 3; i++ ) {
            // array[i] = new int[n];
            cout << array[i] << endl;
        }
        cout << endl;
        int** ary = new int*[3];
        for ( int i = 0; i < 3; i++ ) {
            ary[i] = new int[n];
            cout << ary[i][0];
        }
        for ( int i = 0; i<3; i++ ) {
            delete [] ary[i];
        }
        typedef int ARR1[2];
        int (*arr)[2] = new int[3][2];
        ARR1* arrtype = new ARR1[3];
        int ***arr3 = new int **[2];
        int **arrr = new int*[4];
    
        // auto arrauto = 4;
        auto arrauto = new int[4][2];
    
        printf("n %d\n", n);
        cout << "n: " << n << endl;
    
        auto ary2 = new int[5001]();    // initialize all elements with 0
        fill_n (ary2, 5001, 3);         // initialize all elements with (int)3
        // c style, cstring use memset (ary2, 0, n*sizeof(int));
    
    #include <algorithm>
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {   
        ifstream fin ("game.in");
        int num_of_cup = 3;
        int n;
        fin >> n;
        auto ary = new int*[n];
        for ( int i = 0; i < n; i++ ) {
            ary[i] = new int[3];
            for ( int j = 0; j < 3; j++ ) {
                fin >> ary[i][j];
            }
        }   
                
        int maxpoint = 0; 
        for (int i = 1; i < num_of_cup + 1; i++) {
            int point = 0;
            int current = i;
            for (int j = 0; j < n; j++) {
                if (ary[j][0] == current) {current = ary[j][1];}
                else if (ary[j][1] == current) {current = ary[j][0];}
                if (ary[j][2] == current) {point++;}
            }
            maxpoint = max( maxpoint, point );
        }
        cout << maxpoint << endl;
    
        return 0;
    }
    
    #include <algorithm>
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        ifstream fin ("sleepysort.in");
        int n;
        fin >> n;
        auto animal = new int[n];
        auto animal_name = new string[n];
        auto animal_is = new string*[n];
        for (int i = 0; i < n; i++) {
            string s;
            int k;
            fin >> s;
            fin >> k;
            for (int j = 0; j < k; j++) {
                fin >> s;
                cout << s << " ";
            }
            cout << endl;
            // fin >> animal_name[i];
        }
    }
    
    Using <vector>
    #include <algorithm>
    #include <fstream>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        ifstream fin ("guess.in");
        int n;
        fin >> n;
        // auto animal = new int[n];
        auto animal_name = new string[n];
        auto animal_is = new string*[n];
        // pair<int,int> animal_has[n];
        // pair<string,int> *guess_has;
        // vector<pair<string,int>> guess_has;
        vector<string> has;
        vector<pair<int,string>> has_vec;
        // auto has_name  = new string[101];
        // auto has_count = new int[101];
        string has_name[101];
        int has_count[101]; 
        for (int i=0; i < n; i++) {
            int k;
            fin >> animal_name[i];
            fin >> k;
            animal_is[i] = new string[k];
            for (int j = 0; j < k; j++) {
                fin >> animal_is[i][j];
                // guess_has.push_back( make_pair(animal_is[i][j], 1) );
                has.push_back( animal_is[i][j] );
                // cout << animal_is[i][j] << " ";
            } 
        }
        sort( has.begin(), has.end() );
        
        string last_name = "";
        int index = 0;
        for (auto i : has) {
            if (last_name == i) {
                // cout << index << " same** " << i << " " << last_name << endl;
                has_count[index]++;
                // cout << index << " " << has_name[index] << " " << has_count[index] << endl;
            } else {
                // cout << index << " " << i << " " << last_name << endl;
                index ++;
                has_name[index] = i;
                has_count[index] = 1;
                last_name = i;
                // cout << index << " " << has_name[index] << " " << has_count[index] << endl;
            }
        }
    
        for (int i = 0; i < 101; i++) {
            if (has_count[i] > 0) { has_vec.push_back( make_pair( has_count[i], string(has_name[i]) ) ); }
            // if (has_count[i] > 0) { cout << i << " " << has_count[i] << " " << has_name[i] << endl; }
        }
        sort( has_vec.begin(), has_vec.end(), greater<>() );
        for (auto i : has_vec) {
            cout << i.first << " " << i.second << endl;
            int n = i.first;
            for
            for (int i=0; i<3; i++) {
            }
        }
    
        cout << endl;
    
        return 0;
    }
    

    Tuesday, February 12, 2019

    Java arrays!

    Java Arrays
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class array {
        public static void main(String[] args) { 
    
            int i,j,k;
            i = j = k = 0;
            System.out.println("demo hello");
            
            int[] nums = new int[20];
            double[] prices = { 5.5, 3, 4.2 };
            boolean[] ans = { true,false,true };
            char[] vowels = { 'a','e','i','o','u' };
            String[] names = new String[10];
    
            nums[1] = 1;
            nums[2] = 2;
            printarray (nums);
            ArrayList list = new ArrayList<>();
            Arrays.sort (nums);
            Arrays.sort (nums);
            int[][] rents = {{},{},{}};
            
            // ArrayList can only hold Objects
            // ArrayList is a dynamic array
            List<Integer> myiary = new ArrayList<Integer>();
            myiary.add(2);
        }
     
        public static void printarray (int[] a) {
            int j = 0;
            for (int i : a) {
                System.out.printf("s[%d] = %d, " , j,i);
                j++;
                if (j % 5 == 0) {
                    System.out.println();
                }
            }
        }
     
        public static void printarray (String[] a) {
            for (String i : a) {
                System.out.printf("%s\n" , i);
            }
        }
    }
    
    Java String and Characters
    public static void main(String[] args) throws IOException {
        BufferedReader f = new BufferedReader(new FileReader("beads.in"));
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("beads.out")));
        StringTokenizer st = new StringTokenizer(f.readLine());
         
        int n = Integer.parseInt(st.nextToken());
        String s = f.readLine();
        char[] sc = s.toCharArray();
        System.out.println(sc);
    
        char[] chars = {'t','h','i','s',' ','\u0024'};  // \u0024 is $ sign
        String s = new String(chars);
        System.out.println( s );
    }
    
    Insert and copy arrays
        public static void copyArray(int[] array, int index) {
             List<Integer> myiary = new ArrayList<Integer>();
             myiary.add(2);
    
             int[] tmp = new int[array.length-1];
             System.arraycopy( array, 0, tmp, 0, index);
    
             Integer[] iarray = new Integer[] { 2,3,4 };
             List<Integer>> iiarray = new ArrayList<Integer>() {{
                 add(1);add(2);add(3);
             }};
        
             List<Integer> list = new ArrayList<Integer>(Arrays.asList(iarray));
             List<Integer> iilist = new ArrayList<Integer>(iiarray);
    
             ArrayList<String> slist = new ArrayList<String>(Arrays.asList(
                 new String[] {"One","Two","Three","Four"}));
        }
    
    Modify and merge elements in arrays
        String a[] = { "A", "E", "I" };
        String b[] = { "O", "U" };
        List list = new ArrayList(Arrays.asList(a));
        list.addAll(Arrays.asList(b));
        Object[] c = list.toArray();
        System.out.println(Arrays.toString(c));
    
        int[] ia = {1,2,3};
        int[] ib = {4,5,6};
        int[] ic = new int[ ia.length + ib.length ];
        // use loop to concatenate ia, ib to ic
    
    Slice the array
        public static void sliceArray() {
            // intializing an array arr1
            byte[] arr1 = new byte[] {5, 62, 15};
    
            // printing the array arr1
            System.out.println("Printing 1st array:");
            for (int i = 0; i < arr1.length; i++) {
                System.out.println(arr1[i]);  
            }
    
            // copying array arr1 to arr2 with range from index 1 to 7
            byte[] arr2 = Arrays.copyOfRange(arr1, 1, 7);
    
            // printing the array arr2
            System.out.println("Printing new array:");
            for (int i = 0; i < arr2.length; i++) {
                System.out.println(arr2[i]);
            }
        }
    
    Remove elements in arrays
        public static void removeElement(Integer[] array, int index) {
            int[] tmp = new int[array.length-1];
            System.arraycopy( array, 0, tmp, 0, index);
    
            List<Integer> list = new ArrayList<Integer>(Arrays.asList(array));
            list.add(3);
            list.add(4);
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).equals(index)) {
                    list.remove(i);
                }
            }
        }
    
    Looping over the array
        for (<type> i : array_of_type) {
        }
        char[] chars = {'t','h','i','s',' ','\u0024'};
        // char to string
        String s = new String(chars);
        // string to char
        char[] chars2 = s.toCharArray();
        for (char c : chars) { System.out.print(c); }
        System.out.println();
    
    Using Java 8 Stream API
        List<String> nums = Stream.of("ONE", "TWO", "THREE", "FOUR")
            .collect(Collectors.toList());
    
    Using Java 8 Collections API
        List<String> places = Collections.unmodifiableList
            (Arrays.asList("One", "Two", "Three"));
    
    Using Java 9 API
        List<String> sary = List.of("One", "Two", "Three");
        List<Integer> iary = List.of(1,2,3);
    
    Using Class for Arrays
    import java.util.*;
    import java.util.Arrays;
    
    public class ary
    {
        public static void main (String[] args) {
            int iary1[] = { 1,2,3,4 };
            int iary2[] = { 5,6,7,8 };
            int iary3[] = Arrays.copyOf (iary2, 10);
            int iary4[] = Arrays.copyOfRange (iary3, 1, 3);
            int iary5[] = { 6,2,7,0,1,3,4 };
    
            System.out.println ("arrays:");
            System.out.println ( Arrays.toString(iary1));
            System.out.println ("compare two array:");
            System.out.println ( Arrays.equals(iary1, iary1));
            System.out.println ( Arrays.equals(iary1, iary4));
    
            System.out.println ("array sort:");
            Arrays.sort(iary5);
            System.out.println ( Arrays.toString(iary5));
    
            int tary1[][] = { { 9,8,7,6,5,4} };
            int tary2[][] = { { 9,6,4 } };
            System.out.println ( "Arrays comparison" );
            System.out.println ( Arrays.deepEquals (tary1, tary2 ));
            // Arrays.compare (iary1, iary2 );
            // Arrays.compareUnsigned (iary1, iary2 );
            // System.out.println ( Arrays.compare (iary1, iary2 ));
    
            ArrayList list = new MyArrayList("a", "b", "c");
        }
    }
    
    public class MyArrayList<T> extends ArrayList<T> {
        public MyArrayList(T... values) {
            super(Arrays.asList(values));
        }
    }
    

    Java Class

    Java Class
    - An object is an instance of a class.
    - Non-primitive variables are references to objects.
    - Objects can have multiple references.
    
    Creation of Instances 
    - A class can have multiple objects created/constructed.
    - A class has only one blueprint.
    - An object can only be created using the key word "new".
    - When "new" is called, the object is instantiated calling the "constructor method".
    - String values are instances of java.lang.String
    
          String str = new String("string");
          String str = "string";
    
    Static vs Dynamic * Instance variables vs Class variables - Instance variable is a member of an instance/objects, not a member of the class * Instance methods vs Class methods
    Java Strings are immutable, meaning?
    That means that once you instantiate and assign their values, you can't really change them.
    Java compiler will make it look like you can change them.
          String str = "Hello, world!";
          str = "new string";
    it looks like you are changing the object. But in fact, in the background, the Java compiler is de-referencing the original object. That object can now be cleared from memory in a process known as garbage collection, and a brand new object is created with the new value, which is now pointed to by "str". That is, Java reset the value of a String by creating a new object and give that to you.
    Examples
    public class Bicycle {
        // the Bicycle class has
        // three fields
        public int cadence;
        public int gear;
        public int speed;
            
        // the Bicycle class has
        // one constructor
        public Bicycle(int startCadence, int startSpeed, int startGear) {
            gear = startGear;
            cadence = startCadence;
            speed = startSpeed;
        }
            
        // the Bicycle class has
        // four methods
        public void setCadence(int newValue) {
            cadence = newValue;
        }
            
        public void setGear(int newValue) {
            gear = newValue;
        }
            
        public void applyBrake(int decrement) {
            speed -= decrement;
        }
            
        public void speedUp(int increment) {
            speed += increment;
        }
    }
    
    public class MountainBike extends Bicycle {
     // the MountainBike subclass has
        // one field
        public int seatHeight;
    
        // the MountainBike subclass has
        // one constructor
        public MountainBike(int startHeight, int startCadence,
                            int startSpeed, int startGear) {
            super(startCadence, startSpeed, startGear);
            seatHeight = startHeight;
        }   
            
        // the MountainBike subclass has
        // one method
        public void setHeight(int newValue) {
            seatHeight = newValue;
        }
    }
    
    import java.io.*;
    import java.util.*;
    
    class gift1 {
    
        public static void main(String[] args) throws IOException {
            BufferedReader f = new BufferedReader(new FileReader("gift1.in"));
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("gift1.out")));
            StringTokenizer st = new StringTokenizer(f.readLine());
    
            int n = Integer.parseInt(st.nextToken());
            Map<String, List<String>> giveto = new HashMap giftmoney = new HashMap<String, Gift>();
    
            String[] groups = new String[n];
            for (int i = 0; i < n; i++) {
                groups[i] = f.readLine();
                System.out.println(groups[i]);
            }
            for (int i = 0; i < n; i++) {
                String s = f.readLine();
                List<String> friends = new ArrayList<String>();
    
                st = new StringTokenizer(f.readLine());
                int totalgiftmoney = Integer.parseInt(st.nextToken());
                int givetoNum = Integer.parseInt(st.nextToken());
                for (int j = 0; j < givetoNum; j++ ) {
                    String friend = f.readLine();
                    friends.add( friend );
                }
                Gift g = new Gift(totalgiftmoney, givetoNum);
                giveto.put(s, friends);
                giftmoney.put(s, g);
                for ( Map.Entry<String,Gift> m : giftmoney.entrySet() ) {
                    System.out.println(m.getKey());
                    System.out.println(m.getValue().total);
                }
            }
            f.close();
            out.close();
        }
    
    }
    
    class Gift
    {
        public int total = 0;
        public int toNum = 0;
        public void Gift () {
        }
    
        public Gift (int total, int num) {
            this.total = total;
            this.toNum = num;
        }
    }
    
    Java Class Operators
    // instanceof // Class membership
    String s = "string";
    System.out.println( s instanceof java.lang.String );
    
    // Strings cannot be compared using ==
    // Strings should use the String.equals to compare
    
    if (s1.equals(s2)) { System.out.println ("equal"); }
    else               { System.out.println ("different"); }
    

    Monday, February 11, 2019

    Java Homeworks #2

    Homework 2-1: Variations from Homework 1-1
    Read one data and convert to another format.
    
    // Practice I/O
    Read from command line.
    Read from user input.
    Read from file.
    
    // Practice loops
    Read multiple input data set until quit.
    
    // Practice conditionals
    Output different phrases depending on the temperatures.
    
    // Practice handling run time errors.
    Using Exceptions.
    Using conditionals.
    
    // Practice date time printing
    // Practice using <string>
    
    Homework 2-2: Big O
    Calculate the minimum n that will make 2 to the power of n greater than 100 * n squared.
               For some n, 2n > 100 * n 2
    Calculate the minimum n that will make n squared greater than 1000 * (n log n)
    
    Homework 2-3: Calculate this.
    6/22
    111 % 8
    55 / 2.25
    33 + 4 % 5
    77 * 9 % 8
    77 % 9 * 8
    77 % 8 * 9
    x = -55 * i++ - 7 % 8;
    
    // logical
    // Take a input integer and output result of the following logical expressions
    x < 20 && x >= -3
    !x && x >= 33
    x >= 33 && !x
    x++ == 5 || x == 9
    

    C++ Data Structure

    Data Type Limits (limits.h)
    #include <climits>
    
    int main()
    {
        printf ( "char_min %d, char_max %d, int_min %d, int_max %d\n", 
            CHAR_MIN, CHAR_MAX, INT_MIN, INT_MAX );
    }
    
    C++ Arrays
    #include <cstdio>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int * ia = new int[5];
        int ib[5];
        int *ip = ib;
        ib[0] = 1;
        *ib = 1;
        *ip = 2;
        ++ip;
        *ip = 3;
        *(++ip) = 4;
        int ic [] = { 1,2,3,4,5 };
        int id [5] = { 1,2,3,4,5 };
    
        // cstring, a primitive string
        char s0[] = "string";
        char s[] = { 's', 't', 'r', 'i', 'n', 'g', 0 };
        for (int i = 0; s[i] != 0; ++i ) {
            printf ( "%c ", s[i] );
        }
        cout << endl;
        for (char * cp = s; *cp; ++cp ) {
            printf ( "%c ", *cp );
        }
        cout << endl;
        // range based loop
        for (char c : s ) {
            printf ( "%c ", c );
        }
        cout << endl;
    }
    
    C++ Containers
    Sequence containers:
    - array                              - Fixed size, Direct access to any element.
    - deque                            - Rapid insertions and deletions at front or back.
    - forward_list                 - Singly linked list, rapid insertion and deletion anywhere. New in C++11
    - list                                  - Doubly linked list, rapid insertion and deletion anywhere.
    - vector<T>                     - Rapid insertions and deletions at back. Direct access.
    
    Ordered associative containers - keys are maintained in sorted order.
    - set                                  - Rapid lookup, no duplicates allowed.
    - multiset                         - Rapid lookup, duplicates allowed.
    - map                                - one-to-one mapping, no duplicates allowed. Rapid key-based lookup.
    - multimap                      - one-to-one mapping, duplicates allowed. Rapid key-based lookup.
    
    Un-ordered associative containers
    - unordered_set              - Rapid lookup, no duplicates allowed.
    - unordered_multiset     - Rapid lookup, duplicates allowed.
    - unordered_map            - one-to-one mapping, no duplicates allowed. Rapid key-based lookup.
    - unordered_multimap   - one-to-one mapping, no duplicates allowed. Rapid key-based lookup.
    
    Container adapters
    - stack                                - Last-in first-out (LIFO)
    - queue                              - First-in first-out (FIFO)
    - priority_queue              - Highest-priority element is always the first element out
    
    Sorting
    # Example
    
    
    Searching
    # Example
    
    List
    # Example
    
    
    Stack
    # Example
    
    Queue
    # Example
    
    Map
    # Example
    
    Set
    # Example
    
    Tree
    # Example
    
    
    Graph
    # Example
    

    Java Functions

    Primitives
    public class method {
        static String[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    
        public static void main(String[] args) {
            func1();
            func1( "John" );
            func1( 3 );
        }   
            
        static void func1() {
            System.out.println("Howdy!");
        }   
            
        static void func1( String label ) { 
            System.out.printf("Hello %s!\n" , label); 
        }   
    
        static void func1( int x ) { 
            for (int i = 0; i < x; i++) {
                System.out.println("i = " + i);
            }       
        }   
    }
    

    Java Loops

    Java Loops
    public class loop {
    
        public static void main(String[] args) {
            int m = 3;
            for (int i = 0; i < m; i++) {
                System.out.println("i = " + i);
            }
    
            String[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
            for (String s : days) { 
                System.out.println(s);
            }
    
            for (int i = 0; i < days.length-1; i++) {
                System.out.println("for loop: " + days[i]); 
            }
    
            while (m > -1) { 
                System.out.println("while loop: " + days[m]); 
                m--;
            }
    
            m++;
            do {
                System.out.println("do while loop: " + days[m]); 
                m++;
            } while (m < days.length -1 );
    
        }
    
    }
    
    

    Java Conditionals

    Primitives
    import java.util.Scanner;
    
    public class Cond {
        public static void main(String[] args) {
            int m = 8;
            if (m > 0 && m < 4) {
                System.out.println("You are between 1 to 3.");
            } else if (m > 3 && m < 7) {
                System.out.println("You are between 4 to 6.");
            } else if (m > 6 && m < 10) {
                System.out.println("You are less than 10.");
            } else {
                System.out.println("You are bigger than 9 or less than 0.");
            }
    
            switch (m) {
                case 1:
                case 3:
                    System.out.println("M is odd and less than 4.");
                    break;
                case 2:
                case 4:
                    System.out.println("m is even number: " + m);
                    break;
                default:
                    System.out.println("m equals " + m);
            }
    
            Scanner sc = new Scanner(System.in);
            System.out.print("Please enter a day in the week: ");
            String s = sc.nextLine();
            // only works after Java 7
            switch (s) {
                case "0":
                    System.out.println("Sunday"); break;
                case "1":
                    System.out.println("Monday"); break;
                case "2":
                    System.out.println("Tuesday"); break;
                case "3":
                    System.out.println("Wednesday"); break;
                case "4":
                    System.out.println("Thursday"); break;
                case "5":
                    System.out.println("Friday"); break;
                case "6":
                    System.out.println("Saturday"); break;
            }
        }
    }
    

    Friday, February 8, 2019

    ASCII code vs Unicode

    ASCII Code
    ASCII defines 128 characters with 7-bits, because the center of the computer industry was in the USA at that time and 7 bits was originally sufficient for the binary representation of English language.
    ASCII Extended
    Some people extend the 7-bit ASCII code to 8-bit in order to encode more characters to support their language, such as French.
    The name for this are often referred to as "extended ASCII" or "8-bit ASCII".
    Unicode on the rise
    ASCII Extended solves the problem for Latin alphabetical based languages but what about the others that need a completely different set of alphabets, such as Greek, Russian, Arabic, Chinese or Japanese?

    Unicode is a superset of ASCII.
    Unicode defines up to 221 characters

    UTF Varieties Explained
    Unicode encoding: UTF-8 vs UTF-16 vs UTF-32
    UTF-8 and UTF-16 are variable length encodings.

    In UTF-8, a character may occupy a minimum of 8 bits.
    In UTF-16, a character length starts with 16 bits.
    UTF-32 is a fixed length encoding of 32 bits.

    UTF-8 uses the ASCII set for the first 128 characters. That's handy because it means ASCII text is also valid in UTF-8.

    Mnemonics:
    UTF-8: minimum 8 bits.
    UTF-16: minimum 16 bits.
    UTF-32: minimum and maximum 32 bits.
    Java Supports Unicode
    Java Char provides support for Unicode with 2 bytes size, ranging from 0 to 65535.
    More...
    ASCII and Unicode are two character encoding standards on how to represent characters in binary code.
    The main difference between the two is how they encode the character and the number of bits that they use for each. 
    
    ASCII originally used seven bits to encode each character. This was later increased to eight with Extended ASCII to address the apparent inadequacy of the original to encode languages other than English. 
    
    In contrast, Unicode can choose between 32, 16, and 8-bit encodings. Using more bits lets you use more characters at the expense of larger files while fewer bits give you a limited choice but you save a lot of space. Using fewer bits (i.e. UTF-8 or ASCII) would probably be best if you are encoding a large document in English.
    
    Unicode solves the main problem arose from the many non-standard extended ASCII programs. Unless you are using the prevalent page, which is used by Microsoft and most other software companies, then you are likely to encounter problems with your characters appearing as boxes. Unicode eliminates this problem as all the character code points were standardized.
    
    Another major advantage of Unicode is that at its maximum it can accommodate a huge number of characters. Because of this, Unicode currently contains most written languages and still has room for even more. This includes typical left-to-right scripts like English and even right-to-left scripts like Arabic. Chinese, Japanese, and the many other variants are also represented within Unicode.
    
    In order to maintain compatibility with the older ASCII, which was already in widespread use at the time, Unicode was designed in such a way that the first eight bits matched that of the most popular ASCII page. If you open an ASCII encoded file with Unicode, you still get the correct characters encoded in the file. This facilitated the adoption of Unicode as it lessened the impact of adopting a new encoding standard for those who were already using ASCII.
    
    Summary:
    1.ASCII uses an 8-bit encoding while Unicode uses a variable bit encoding.
    2.Unicode is standardized while ASCII isn’t.
    3.Unicode represents most written languages in the world while ASCII does not.
    4.ASCII has its equivalent within Unicode.
    
    Taken from here
    

    C++ Exercise #1

    Exercise 1-1
    Write a program that take inputs from command line and create a todo list.
    
    Exercise 1-2
    Take names from command line and create a list. Print out the sorted list.
    Exercise 1-3
    Take a list of students name and their score. Print the top 5 scores and their names.
    Exercise 1-4
    Take a list of pairs of numbers, which indicate pairs of nodes that is connected. Provide two functions:
    1. Output the connected clusters.
    2. Return true or false if two nodes are connected.
    Exercise 1-5 What does this print out?
    #include <cstdlib>                  // Prototypes of srand() and rand()
    #include <ctime>                    // Prototype of time()
    #include <iostream>
    using namespace std;
    int main()
    {
        int number, attempt;
        char wb = 'r';                  // Repeat or finish.
        long sec;
        time( &sec);                    // Get the time in seconds.
        srand((unsigned)sec);           // Seeds the random
                                        // number generator
        cout << "\n\n "
             << " ******* A NUMERICAL GAME *******" << endl;
        cout << "\n\nRules of the game:" << endl;
        while( wb == 'r')
        {
            cout << "I have a number between 1 and 15 in mind \n"
                 << "You have three chances to guess correctly!\n"
                 << endl;
            number = (rand() % 15) + 1;
            bool found = false; int count = 0;
            while( !found && count < 3 )
            {
                cin.sync();             // Clear input buffer
                cin.clear();
                cout << ++count << ". attempt: ";
                cin >> attempt;
                if(attempt < number) cout << "too small!"<< endl;
                else if(attempt > number) cout <<"too big!"<< endl;
                else found = true;
            }
            if( !found) {
                cout << "\nI won!"
                     << " The number in question was: "
                     << number << endl;
            } else {
                cout << "\nCongratulations! You won!" << endl;
            }
            cout << "Repeat —> <r> Finish —> <f>\n";
            do {
                cin.get(wb);
            }
            while( wb != 'r' && wb != 'f');
        }
        return 0;
    }
    Exercise 1-6
    1. Write a program that take inputs from command line and create a todo list.
    2. Take names from command line and create a list. Print out the sorted list.
    3. Take a list of numbers from input and search if a certain number is there.
             Try different ways and analyze run time.
    4. Take two n digit numbers and use an algorithm better than brute-force to multiply.
    5. Implement Karatsuba algorithm for integer multiplication
    6. Build a binary tree
    7. Take a list of pairs of indices and build a network from them. Then given any input number, search if it is in the network
    8. From above network, given any two numbers, return True if they are connected, otherwise return False.
    9. From above network, return the shortest path
    Exercise 1-7
    1. Build a graph
    2. Search a graph to see if two nodes are connected
    3. Take "car" and return how many possible way to convert it to "let", one letter at a time.
    4. Take a list of students name and their score. Print the top 5 scores and their names.

    C++ Operators

    Operator Precedence and Associativity
    C++ Operator Precendence
    
    Operators for Fundamental Types

    Arithmetic Operators
    +
    -
    *
    /
    %
    
    // Not all operators can be perform on binary numbers
    // Divisions performed with integral operands will produce integral results.
    // Remainder division or Modulus applies only to integral operands
    
    Unary Arithmetic Operators
    // Sign Operators
    +
    -
    // Increment / Decrement Operators
    ++
    --
    
    i++
    ++i
    j--
    --j
    
    Logical Operators
    &
    |
    !
    &&
    ||
    
    Relational Operators
    <
    <=
    >
    >=
    ==
    !=
    

    Java Homeworks #1

    Homework 1-1 Conditionals
    1. Convert Gallons to Liters
    2. Convert temperature from Fahrenheit to Celsius
    3. Convert number to names
    4. Create a help menu providing phone numbers for agents, institutions.
    5. Read from command line, or user input, or from file
    6. Process multiple input data set until user quit.
    7. Output different phrases or greetings depends on the input data.
    8. Handle user input errors using exceptions or conditionals.
    Homework 1-2 Lists, Loops, Files
    1. Write a program that will print A to F vertically on each new line.
    2. Create an array or list of numbers and calculate all.
    3. Make it interactive and take inputs from users until 'q' or 'Q' is presssed.
    4. Take inputs from file and print output to a file.
    Homework 1-3 Numbers and Type Conversions
    1. Compute the circumference area of a circle given the radius.
    2. Take two numbers and calculate their GCD.
    3. Take two numbers and output the max / min / mean / sum of both.
    4. Convert Coins to currency amount
    Fix it!
    class FirstFix
    {
        System.out.print( "If this text" );
        System.out.println "is what you see on your screen, ";
        print( "Then your program is working" );
    )
    

    Thursday, February 7, 2019

    C++ Initialization

    Primitives
        constexpr size_t byte = 8;
        const double pi = 3.14159;
    
        char c = 'a';
        float f(1.875);
        int x[5] = { 1,2,3,4,5 };
        char s[] = "this is a string";
    
        short int sh = c;
        long int  li = 0L;
        long long int lli = 0LL;
    
        double area, circuit, radius = 1.5;
    

    C++ Qualifiers and Macros

    type qualifier: const vs volatile
    There are two types of qualifiers, const or volatile, then there is mutable.
    Type qualifier is also known as CV qualifier, where CV stands for constant and volatile.
    Type qualifiers express additional information (quality) about a value through the type system.

    const
    const marks a variable as read-only or immutable. Its value cannot be changed once it's been defined.

    volatile
    volatile marks a variable that may be changed by another process. This is generally used for threaded code.
    The keyword volatile, which is rarely used, creates variables that can be modified not only by the program
    but also by other programs and external events. Events can be initiated by interrupts or by a hardware clock.

    mutable
    And mutable is used on a data member to make it writable from a const qualified member function.
    Examples
      // const
      // volatile
      // mutable
      
      // qualified type : constant integer
      const int cint = 0;
      
      // unqualified type : simply an integer
      int unqualified_int = 0;
      
      // mutable with lambda
      int accum = 9;
      auto x = [accum](int d) mutable -> int { return accum += d; };
      vector v1 = { 1,2,3,4,5 };
      vector v2 (v2.size());
      transform (v1.begin(), v1.end(), v2.begin(), x);
      
      Storage Duration
      static
      register
      extern
      
      Modifiers
      signed
      unsigned
      
      Operator
      sizeof(type) // byte size of data type
      
      Integer Constants
      printf ( "binary %u, decimal %d, %d, octal %o, hex %x\n", 
                0b10000, 0b10000, 16, 020, 0x10 );
      
      //------------------------------------------------------------------------------
      // Decimal      Octal           Hexadecimal             Type
      //------------------------------------------------------------------------------
      // 16           020             0x10                    int
      // 255          0377            0Xff                    int
      // 32768U       0100000U        0x8000U                 uint
      // 10L          012L            0xaL                    long
      // 27UL         033UL           0x1bUL                  unsigned long
      //
      // Binary: 0b110 (decimal 6)
      //------------------------------------------------------------------------------
      
      
      Floating-point Constants
      5.19            0.519E1         0.0519e2        519.OE-2
      12.             12.0            .12E+2          12e0
      0.75            .75             7.5e-1          75E-2
      0.00004         0.4e-4          .4E-4           4E-5
      
      Character Constants
      //------------------------------------------------------------------------------
      // Constant             Character               Constant Value (ASCII code)
      //------------------------------------------------------------------------------
      'A'                     A                               65
      'a'                     a                               97
      ' '                     blank                           32
      '.'                     dot                             46
      '0'                     digit 0                         48
      '\0'                    terminating null character      0
      //------------------------------------------------------------------------------
      
      // String "0" comprises two bytes, 
      // first byte contains the code for the character zero '0' (ASCII code 48)
      // second byte contains the value 0 ('\0')
      
      //------------------------------------------------------------------------------
      // Special Characters:
      \a      alert (BEL)                     7
      \b      backspace (BS)                  8
      \t      horizontal tab (HT)             9
      \n      line feed (LF)                  10
      \v      vertical tab (VT)               11
      \f      form feed (FF)                  12
      \r      carriage return (CR)            13
      \"      " (double quote)                34
      \'      ' (single quote)                39
      \?      ? (question mark)               63
      \\      \ (backslash)                   92
      \0      string terminating character    0
      //------------------------------------------------------------------------------
      \ooo (up to 3 octal digits)  numerical value of a character    ooo (octal)
      \xhh (hex digits)            numerical value of a character    hh (hex)
      //------------------------------------------------------------------------------
      
      
      MACROS
      #define CLEARSCREEN  ( cout << "\033[2J" )
      #define LOCATE(x,y)  ( cout << "\033[" << x << ';' << y << 'H' )
                           // LOCATE will position cursor in row x column y
      #define SQUARE(x)    ( (x)*(x) )
      
      #define PI           3.14159
      #define UPPERBOUND   10
      #define LOWERBOUND   (-3)
      #define PRINTHDR     ( cout << \
                             " ***** REPORT *****\n\n" )
      
      int main()
      {
          PRINTHDR;
      }
      
      #define MIN(x,y)     ( (x)<(y)? (x) : (y))
      // ... use MIN 
      #undef MIN
      // cannot use MIN any more
      
      #ifdef _NAME_
      #endif
      #ifndef _NAME_
      #endif
      
      #ifndef _FILE_NAME_H_
      #define _FILE_NAME_H_
      #endif
      

      C++ Homeworks #2

      Homework 2-1: Variations from Homework 1-1
      Read one data and convert to another format.
      
      // Practice I/O
      Read from command line.
      Read from user input.
      Read from file.
      
      // Practice loops
      Read multiple input data set until quit.
      
      // Practice conditionals
      Output different phrases depending on the temperatures.
      
      // Practice handling run time errors.
      Using Exceptions.
      Using conditionals.
      
      // Practice date time printing
      // Practice using <string>
      
      Homework 2-2: Big O
      Calculate the minimum n that will make 
           2 to the power of n become greater than 100 * n squared.
                 For some n, 2n > 100 * n 2
      Calculate the minimum n that will make n squared greater than 1000 * (n log n)
      
      Homework 2-3: Calculate this.
      6/22
      111 % 8
      55 / 2.25
      33 + 4 % 5
      77 * 9 % 8
      77 % 9 * 8
      77 % 8 * 9
      x = -55 * i++ - 7 % 8;
      
      // logical
      // Take a input integer and output result of the following logical expressions
      x < 20 && x >= -3
      !x && x >= 33
      x >= 33 && !x
      x++ == 5 || x == 9
      
      Homework 2-4: Random numbers
      #include <stdlib.h>                   // Prototypes of srand() and rand()
      #include <iostream>
      #include <iomanip>                    // using setw(n) for cout
      
      // Print "Please enter a number between 0 and n"
      // cin >> seed;                       // use the number as seed
      // srand ( seed )                     // seeds the random number generator
      // x = rand() % n + 1                 // generate the random number
      // Print out the random number