Shuffle String with Given Indices in Python

Arnab Chakraborty
Updated on 17-May-2021 12:28:54

1K+ Views

Suppose we have a string s and a list of indices ind, they are of same length. The string s will be shuffled such that the character at the position i, moves to indices[i] in the final string. We have to find the final string.So, if the input is like s = "ktoalak" ind = [0, 5, 1, 6, 2, 4, 3], then the output will be "kolkata"To solve this, we will follow these steps −fin_str := a list whose size is same as s and fill with 0for each index i and character v in s, dofin_str[ind[i]] := vjoin ... Read More

Count Odd Numbers in an Interval Range Using Python

Arnab Chakraborty
Updated on 17-May-2021 12:23:37

2K+ Views

Suppose we have two non-negative numbers left and right. We have to find the number of odd numbers between left and right (inclusive).So, if the input is like left = 3, right = 15, then the output will be 7 because there are 7 odd numbers in range, these are [3, 5, 7, 9, 11, 13, 15], there are 7 elements.To solve this, we will follow these steps −if left is odd or right is odd, thenreturn 1 + quotient of (right-left) / 2otherwise, return quotient of (right-left) / 2Example (Python)Let us see the following implementation to get better understanding ... Read More

Find Maximum Number of Water Bottles We Can Drink in Python

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

713 Views

Suppose there are n number of full water bottles, we can exchange m empty water bottles for only one full water bottle. Now drinking a full water bottle makes it an empty bottle. We have to find the maximum number of water bottles we can drink.So, if the input is like n = 9, m = 3, then the output will be 13 because initially we have 9 bottles, so after drinking all bottles, we can get 9/3 = 3 full bottles, after drinking them all we have three empty bottles and using them we can buy one and drink ... Read More

Find Number of Good Pairs in Python

Arnab Chakraborty
Updated on 17-May-2021 12:22:53

4K+ Views

Suppose we have an array nums. Here a pair (i, j) is said to be a good pair if nums[i] is same as nums[j] and i < j. We have to count the number of good pairs.So, if the input is like nums = [5, 6, 7, 5, 5, 7], then the output will be 4 as there are 4 good pairs the indices are (0, 3), (0, 4) (3, 4), (2, 5)To solve this, we will follow these steps −count:= 0n:= size of numsfor i in range 0 to n - 1, dofor j in range i+1 to n ... Read More

Reformat Date in YYYY-MM-DD Format Using Python

Arnab Chakraborty
Updated on 17-May-2021 12:22:29

2K+ Views

Suppose we have a date string in the format "Day Month Year" format where day's are like [1st, 2nd, ..., 30th, 31st], months are in [Jan, Feb, ... Nov, Dec] format and year is a four-digit numeric value in range 1900 to 2100, we have to convert this date into "YYYY-MM-DD" format.So, if the input is like date = "23rd Jan 2021", then the output will be 2021-01-23To solve this, we will follow these steps −Months:= ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]string:= split the date and form a list like [day, month, year] formatyear ... Read More

Check if Sequence Can Form Arithmetic Progression in Python

Arnab Chakraborty
Updated on 17-May-2021 12:22:01

2K+ Views

Suppose we have a list of numbers called nums. We have to check whether the elements present in nums are forming AP series or not. As we know in AP (Arithmetic Progression) series the common difference between any two consecutive elements is the same.So, if the input is like nums = [9, 1, 17, 5, 13], then the output will be True because if we sort them, it will be [1, 5, 9, 13, 17] and here common difference is 4 for each pair of elements.To solve this, we will follow these steps −nums := sort the list numsif number ... Read More

Create a New Local User in Windows using PowerShell

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

3K+ Views

To create a new local user in the Windows operating system using PowerShell, we can use the New-LocalUser cmdlet. The below command will create the TestUser with no password.New-LocalUser -Name TestUser -NoPasswordOutputName Enabled Description ---- ------- ----------- TestUser TrueTestUser account has been enabled here. To provide the password for the user, the password should be in the secure string format. We can pass the password as shown below.$pass = "Admin@123" | ConvertTo-SecureString -AsPlainText -Force New-LocalUser -Name TestUser -Password $passThe above commands will create the TestUser with the password. To add the password and account-related settings we can directly provide parameters ... Read More

Install MSI File Using Batch File in PowerShell

Chirag Nagrekar
Updated on 17-May-2021 12:17:21

3K+ Views

Let say we have one MSI file and we need to install the MSI file on the remote computers using PowerShell but that MSI file should be deployed with the Batch file and should be executed using PowerShell.In this example, we have a 7-zip MSI file and batch file we will first write the installation instructions as shown below.msiexec /i "C:\temp\7z1900-x64.msi" /quietWe have the installation MSI package located at the C:\temp location. We will save the above instruction inside the 7ZipInstaller.bat file.Now we need to call the batch file as shown below. −Wait will wait for the batch file to ... Read More

Find Average Salary Excluding Minimum and Maximum Salary in Python

Arnab Chakraborty
Updated on 17-May-2021 12:14:34

2K+ Views

Suppoe we have an array with distinct elements called salary where salary[i] is the salary of ith employee. We have to find the average salary of employees excluding the minimum and maximum salary.So, if the input is like salary = [8000, 6000, 2000, 8500, 2500, 4000], then the output will be 5125.0, as the minimum and maximum salary values are 2000 and 8500, so excluding them the average salary values are [8000, 6000, 2500, 4000] so the average is (8000 + 6000 + 2500 + 4000)/4 = 5125.To solve this, we will follow these steps −delete minimum of salary from ... Read More

Perform XOR Operation in an Array Using Python

Arnab Chakraborty
Updated on 17-May-2021 12:14:00

862 Views

Suppose we have an integer n and another integer start. We have to create an array called nums where nums[i] = start + 2*i (i start from 0) and n is the size of nums. Then find the bitwise XOR of all elements of nums.So, if the input is like n = 6, start = 2, then the output will be 14 because the array will be like [2+2*0, 2+2*1, ... 2+2*5] = [2, 4, 6, 8, 10, 12], then XOR of each element present in the array is 14.To solve this, we will follow these steps −count := startwhile ... Read More

Advertisements