Find Running Sum of 1D Array in Python

Arnab Chakraborty
Updated on 17-May-2021 12:13:36

4K+ Views

Suppose we have an array nums. The running sum of an array as rs[i] is sum of all elements from nums[0] to nums[i]. Finally return the entire running sum of nums.So, if the input is like nums = [8, 3, 6, 2, 1, 4, 5], then the output will be [8, 11, 17, 19, 20, 24, 29], becausers[0] = nums[0] rs[1] = sum of nums[0..1] = 8 + 3 = 11 rs[2] = sum of nums[0..2] = 8 + 3 + 6 = 17 and so onTo solve this, we will follow these steps −n:= size of numsrs:= [nums[0]]for i ... Read More

Find Final Prices with Special Discount in Python

Arnab Chakraborty
Updated on 17-May-2021 12:13:13

1K+ Views

Suppose we have an array called prices where prices[i] represents price of the ith item in a shop. There is a special offer going on, if we buy the ith item, then we will get a discount equivalent to prices[j] where j is the minimum index such that j > i and price of jth item is less or same as price of ith item (i.e. prices[j] = prices[j], thenprices[i] := prices[i] - prices[j]come out from the loopotherwise, j := j + 1return pricesExample (Python)Let us see the following implementation to get better understanding − Live Demodef solve(prices):    for i ... Read More

Delete N Nodes After M Nodes from a Linked List in Python

Arnab Chakraborty
Updated on 17-May-2021 12:12:32

317 Views

Suppose we are given a linked list that has the start node as "head", and two integer numbers m and n. We have to traverse the list and delete some nodes such as the first m nodes are kept in the list and the next n nodes after the first m nodes are deleted. We perform this until we encounter the end of the linked list. We start from the head node, and the modified linked list is to be returned.The linked list structure is given to us as −Node    value :    next : So, if the ... Read More

Retrieve Azure VM RAM and CPU Size Using PowerShell

Chirag Nagrekar
Updated on 17-May-2021 12:08:03

7K+ Views

Azure VM RAM and CPU size depend on the hardware profile chosen for the VM. In this example, we will retrieve VM (TestMachine2k16) hardware profile and then we can find how much RAM or CPU is allocated to it.To get the Size of the Azure VM, PS C:\> $azvm = Get-AzVM -VMName 'TestMachine2k16' PS C:\> $azvm.HardwareProfile.VmSizeOutputStandard_DS2_v2You can check the above size on the Microsoft Azure website to know how much RAM and CPU are associated with it and another way using the PowerShell by using the Get-AZVmSize command.PS C:\> $vmsize = $azvm.HardwareProfile.VmSize PS C:\> Get-AzVMSize -VMName $azvm.Name -ResourceGroupName $azvm.ResourceGroupName | ... Read More

Retrieve Azure VM Subscription Name Using PowerShell

Chirag Nagrekar
Updated on 17-May-2021 12:07:25

2K+ Views

Once you are connected to the Azure Account, there are possibilities that the Get-AzVM will display the VMs from all the Azure Subscriptions.To find the specific Azure VM Subscription name, we will first retrieve the details of the VM using the Get-AzVM and it has an ID property that contains the Subscription ID and from the subscription ID property, we can retrieve the name of the Azure Subscription Name.PS C:\> $azvm = Get-AzVM -Name TestMachine2k16 PS C:\> $subid = ($azvm.Id -split '/')[2] PS C:\> (Get-AzSubscription -SubscriptionId $subid).NameThe above will retrieve the name of the subscription.

Retrieve Azure VM Resource Group Using PowerShell

Chirag Nagrekar
Updated on 17-May-2021 12:06:52

2K+ Views

To retrieve the azure VM resource group using PowerShell, we first need to retrieve the Azure VM details using Get-AZVm and then we can use its property called ResourceGroup.Before getting the details of the Azure VM, make sure that you are connected to the Azure Account using the Connect-AzAccount command.In this example, we are going to retrieve the Azure VM named TestMachine2k16 to retrieve the VM details.$azvm = Get-AzVM -Name TestMachine2k16To get the resource group name, use its property ResourceGroupName.PS C:\> $azvm.ResourceGroupName ANSIBLETESTRG

Eject USB Device from System using PowerShell

Chirag Nagrekar
Updated on 17-May-2021 12:05:55

5K+ Views

To eject the USB device from the system, we first need to get the USB device using PowerShell. WMI class Win32_Volume will help us to find the USB device.We know that all removal devices using the DriveType '2'. So we will filter out the USB device among the listed devices.PS C:\> $usbdev = gwmi win32_volume | where{$_.DriveType -eq '2'}The below commands will helpful to unallocated the USB from the system.PS C:\> $usbdev.DriveLetter = $null PS C:\> $usbdev.Put()OutputPath : \localhost\root\cimv2:Win32_Volume.DeviceID="\\?\Volume{6e4d6f1e-a8c2-11eb-9493-005056c00008}\" RelativePath : Win32_Volume.DeviceID="\\?\Volume{6e4d6f1e-a8c2-11eb-9493-005056c00008}\" Server : localhost NamespacePath : root\cimv2 ClassName : Win32_Volume IsClass : False IsInstance : True IsSingleton : FalseAnd ... Read More

Check USB Device Connection Using PowerShell

Chirag Nagrekar
Updated on 17-May-2021 12:01:49

3K+ Views

To retrieve the USB-connected devices using Powershell, we need to retrieve all the drives using the WMI object or CIM Instance and need to filter the win32_diskdrive class with the USB as shown below.So basically, USB devices and other removable devices have the drivetype '2'. You can search with the InterfaceType or the DriveType.WMI commands, gwmi win32_diskdrive | where{$_.Interfacetype -eq "USB"}Alternatively, With the CIM commands, Get-CimInstance -ClassName Win32_DiskDrive | where{$_.InterfaceType -eq 'USB'}orGet-CimInstance -ClassName Win32_LogicalDisk | where{$_.DriveType -eq '2'}If there is no USB device connected to the system, there will be no output. To retrieve the USB disk on the remote ... Read More

Implement RSA Algorithm in C++

Anvi Jain
Updated on 17-May-2021 06:43:38

18K+ Views

RSA is an asymmetric cryptography algorithm which works on two keys-public key and private key.AlgorithmsBegin    1. Choose two prime numbers p and q.    2. Compute n = p*q.    3. Calculate phi = (p-1) * (q-1).    4. Choose an integer e such that 1 < e < phi(n) and gcd(e, phi(n)) = 1; i.e., e and phi(n) are coprime.    5. Calculate d as d ≡ e−1 (mod phi(n)); here, d is the modular multiplicative inverse of e modulo phi(n).    6. For encryption, c = me mod n, where m = original message.    7. For ... Read More

Show Multiple Colorbars in Matplotlib

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:48:47

2K+ Views

To show multiple colorbars in matplotlib, we can take the following steps−Set the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Initialize a variable N for the number of sample data.Create random data1 using numpy.Display data as an image, i.e., on a 2D regular raster, with data1.Add a colorbar to a plot.Repeat steps 4, 5, and 6, with different datasets and axes.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1) N ... Read More

Advertisements