Posts

LeetCode - Pascal Triangle

Given  numRows , generate the first  numRows  of Pascal's triangle. For example, given  numRows  = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] Solution: class Solution(object): def generate(self, numRows): lists = [] for i in range(numRows): lists.append([1]*(i+1)) if i>1 : for j in range(1,i): lists[i][j]=lists[i-1][j-1]+lists[i-1][j] return list """ :type numRows: int :rtype: List[List[int]] """

LeetCode - Majority Element

Given an array of size  n , find the majority element. The majority element is the element that appears  more than ⌊ n/2 ⌋  times. You may assume that the array is non-empty and the majority element always exist in the array. #python #optiomal #interview Solution: class Solution(object): def majorityElement(self, nums): nums.sort() return nums[len(nums)/2] """ :type nums: List[int] :rtype: int """

How to install a Python library in IBM CC Lab

To install a Python library IBM workbench CC Lab is a good platform for data scientist. It has Jupyter Notebooks that allows user to create and collaborate codes on Python, R, and Scala notebooks that contain code and visualizations. There are so many libraries are already installed in it, but still some of the libraries are missing. If you want to add the libraries which are not pre-installed in Jupyter Notebook, follow below steps :  Use the Python pip package installer command to install Python libraries to your notebook. For example, run the following command in a code cell to install the pygal library: !pip install --user pygal The installed packages can be used by all notebooks that use the same Python version in the Spark service. Restart (Kernel >> Restart) Use the Python import command to import the library components. For example, run the following command in a code cell: import pygal as pg

Hackerrank - Arrays: Left Rotation

A  left rotation  operation on an array of size   shifts each of the array's elements   unit to the left. For example, if  left rotations are performed on array  , then the array would become  . Given an array of   integers and a number,  , perform   left rotations on the array. Then print the updated array as a single line of space-separated integers. Input Format The first line contains two space-separated integers denoting the respective values of   (the number of integers) and   (the number of left rotations you must perform).  The second line contains   space-separated integers describing the respective elements of the array's initial state. Constraints Output Format Print a single line of   space-separated integers denoting the final state of the array after performing   left rotations. Sample Input 5 4 1 2 3 4 5 Sample Output 5 1 2 3 4 Explanation When we perform   left rotations, the array u

Probability distribution analysis of Energy Consortium

Image
Acme Energy Consortium Acme Energy Consortium consists of 15 power stations that operate in and serve a county that consists of 20 cities. Each station has a number of generators, with each station generator being capable of producing a uniform number of megawatts (MW) per day. Also, each generator has a uniform probability p of not operating on a given day. The table below provides this information for each station. Note that all generator non-operations are assumed to occur independently. Each city has a daily demand (in megawatts) for energy, as shown in the following table. For this reason it has contracts with some of the power stations, where a contract requires a station to commit a fixed percentage of its output to that city. As a result, for each station, the distribution of its generated power to each city is fixed, and shown in the matrix A below, where aij is the percent of energy that station i has committed to city j. Note that stati

Best Programming language for machine learning.