Tuesday, October 3, 2017

PowerShell -Part 2: Install/Update Help File, Enabling PowerShell Scripting and Remoting

By default Powershell runs under normal user access privileges. You need to run Powershell as Administrator for running certain commands.
How to run Powershell as Administrator using Powershell cmdlet?
Execute Start-Process Powershell –verb runas command in powershell cmdlet
Run Powershell as Administrator 

Install/Update the Powershell Help File:
You need to execute Update-Help command in Powershell cmdlet to install or get update on Powershell Help file.
Note: You need to run Powershell as Administrator
Using Update-Help command Install or Update Powershell Help File

Enable Powershell Script Execution:
Due to security provision, the ability to execute Powershell scripts is disabled by default. In order to execute Powershell script Execution Policy must be set.

You need to run Powershell cmdlet as Administrator and execute Set-ExecutionPolicy RemoteSigned.
Set Execution Policy
Enable Powershell Remoting:
By default, Powershell is configured to run remote commands on other Windows Computers. However the computers will not allow remote commands to be executed on them. On each computer that you want remote commands to execute on, run the following Powershell command in cmdlet (Remember: You need to run Powershell cmdlet as Administrator)
Enable –PSRemoting and follow the instructions as you are prompted.

Note: Windows Server 2012 machines will allow remote commands to be executed by default.

PowerShell -Part 1: Introduction to Powershell and how to check and change Powershell version

PowerShell -Part 1: Introduction to Powershell and how to check and change Powershell version
Powershell is a commandline tool for Windows Administrators. It hooks in to .NET Framework.
Powershell provides:
(A). cmdlets for performing common system administration tasks to manage such as:
            - The registry
            - Services
            - Processes
            - Event logs
            - Windows Management Instrumentation (WMI)
(B). A task-based scripting language and support for existing scripts and command-line tools.
(C). Common syntax, Naming Convention, Pipelining
(D). Simplified, command-based navigation of the Operating System: For example, the registry and other data stores can be accessed by using the same techniques that are used to navigate the file system.
(E). Object Manipulation: Object can be directly manipulated or sent to other tools or databases.
(F). Most importantly Powershell is an Object Based Scripting Language: Powershell provides the programming power and accessibility of an object oriented programming language in a scripting environment.

How to find your Powershell Version?
By typing $PSVersionTable in Powershell cmdlet, you can check Powershell version in your system:
Check Powershell Version

Powershell 5 is part of Windows Management Framework (WMF) 5.0. Similarly Powershell 2.0, 3.0 and 4.0 are part of WMF 2.0, 3.0 and 4.0 respectively.

Important Note:
Systems that are running the following server applications should not run Windows Management Framework 4.0:
1.      System Center 2012 Configuration Manager (Not Including SP1)
2.      System Center Virtual Machine Manager 2008R2 (Including SP1)
3.      Microsoft Exchange Server 2007
4.      Windows Small Business Server 2011 Standard

How to change Powershell Version?
You can change Powershell version by using following syntax in Powershell command-line:
powershell –version 2.0 (Here I am changing to Powershell version 2.0.)
Powershell Version Change Command in Powershell Cmdlets

Now your Powershell version changed to 4.0 and you can validate by running $PSVersionTable command
Powershell Version after changing the version

Powershell Backward Compatibility:
1.      Powershell 2.0 is fully backward compatible with 1.0. Any script written on 1.0 will run on 2.0.
2.      Powershell 3.0 and 4.0 are fully backward compatibility with 2.0.
3.      You can run 2.0 and Powershell 3.0 or 4.0 side by side on the same computer.
4.      Powershell 3.0 and 4.0 cannot be run side by side on the same computer.
NOTE: When you install either Powershell 3.0 or 4.0, they will replace Powershell 1.0.


Sunday, August 6, 2017

Lisa's Workbook - Problem from HackerRank

Problem Statement:

Lisa just got a new math workbook. A workbook contains exercise problems, grouped into chapters.
  • There are  chapters in Lisa's workbook, numbered from  to .
  • The -th chapter has  problems, numbered from  to .
  • Each page can hold up to  problems. There are no empty pages or unnecessary spaces, so only the last page of a chapter may contain fewer than  problems.
  • Each new chapter starts on a new page, so a page will never contain problems from more than one chapter.
  • The page number indexing starts at .
Lisa believes a problem to be special if its index (within a chapter) is the same as the page number where it's located. Given the details for Lisa's workbook, can you count its number of special problems?
Note: See the diagram in the Explanation section for more details.
Input Format
The first line contains two integers  and  — the number of chapters and the maximum number of problems per page respectively. 
The second line contains  integers , where  denotes the number of problems in the -th chapter.
Constraints
Output Format
Print the number of special problems in Lisa's workbook.
Sample Input
5 3  
4 2 6 1 10
Sample Output
4
Explanation
The diagram below depicts Lisa's workbook with  chapters and a maximum of  problems per page. Special problems are outlined in red, and page numbers are in yellow squares.
There are  special problems and thus we print the number  on a new line.

Problem solution using python 2.7.6:

PageIndex = 1
NumberOfSpecialProblem = 0 #If the problem index is same as the page number
n,k = map(int,raw_input().split()) #Input number of chapters and number of problems per page
t = map(int,raw_input().split()[:n]) #Input number of problems per chapter
for i in t:
    GetFullPage,RemainingProblem = divmod(i, k)
    TotalPagesPerChapter = GetFullPage + (1 if RemainingProblem else 0)
    #Get problem index by it's number
    ProblemIndex = iter(range(1,i+1))
    for _ in range(TotalPagesPerChapter):
        Problem_On_EachPage = [next(ProblemIndex,None) for _ in range(k)]
        #print Problem_On_EachPage
        if PageIndex in Problem_On_EachPage:
            NumberOfSpecialProblem +=1
        PageIndex +=1
print NumberOfSpecialProblem

Saturday, August 5, 2017

Service Lane - Problem from HackerRank

Problem Statement:

Calvin is driving his favorite vehicle on the 101 freeway. He notices that the check engine light of his vehicle is on, and he wants to service it immediately to avoid any risks. Luckily, a service lane runs parallel to the highway. The length of the service lane is  units. The service lane consists of  segments of equal length and different width.
Calvin can enter to and exit from any segment. Let's call the entry segment as index  and the exit segment as index . Assume that the exit segment lies after the entry segment () and . Calvin has to pass through allsegments from index  to index  (both inclusive).
Paradise Highway
Calvin has three types of vehicles - bike, car, and truck - represented by  and , respectively. These numbers also denote the width of the vehicle.
You are given an array  of length , where  represents the width of the th segment of the service lane. It is guaranteed that while servicing he can pass through at most  segments, including the entry and exit segments.
  • If , only the bike can pass through the th segment.
  • If , the bike and the car can pass through the th segment.
  • If , all three vehicles can pass through the th segment.
Given the entry and exit point of Calvin's vehicle in the service lane, output the type of the largest vehicle which can pass through the service lane (including the entry and exit segments).
Input Format
The first line of input contains two integers,  and , where  denotes the length of the freeway and  the number of test cases. The next line has  space-separated integers which represent the  array.
 test cases follow. Each test case contains two integers,  and , where  is the index of the segment through which Calvin enters the service lane and  is the index of the lane segment through which he exits.
Constraints
Output Format
For each test case, print the number that represents the largest vehicle type that can pass through the service lane.
Note: Calvin has to pass through all segments from index  to index  (both inclusive).

Problem Solution using python 2.7.6:

N, T = map(int,raw_input().split(' '))
width = map(int,raw_input().split()[:N])
ans =[]
while T:   
    i,j = map(int,raw_input().split())
    ans.append(min(width[i:j+1]))
    T -=1
for r in ans: print r