Find Max Values of Sublists of Size K in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:43:55

190 Views

Suppose we have a list nums and another value k, we have to find the maximum values of each sublist of size k.So, if the input is like nums = [12, 7, 3, 9, 10, 9] k = 3, then the output will be [12, 9, 10, 10]To solve this, we will follow these steps −if k > size of nums, thenreturn a blank listres := a new listtemp := nums[0]temp := npoint := 0for i in range 0 to k − 1, doif nums[i] > temp, thentemp := nums[i]point := iinsert temp at the end of resfor i in ... Read More

Find Length of Longest Sublist Whose Sum is 0 in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:41:36

139 Views

Suppose we have a list with only two values 1 and −1. We have to find the length of the longest sublist whose sum is 0.So, if the input is like nums = [1, 1, −1, 1, 1, −1, 1, −1, 1, −1], then the output will be 8, as the longest sublist is [−1, 1, 1, −1, 1, −1, 1, −1] whose sum is 0.To solve this, we will follow these steps −table := a new empty mapcs := 0, max_diff := 0for i in range 0 to size of nums − 1, docs := cs + nums[i]if cs ... Read More

Find Length of Longest Substring with K Distinct Characters in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:39:26

753 Views

Suppose we have a number k and another string s, we have to find the size of the longest substring that contains at most k distinct characters.So, if the input is like k = 3 s = "kolkata", then the output will be 4, as there are two longest substrings with 3 distinct characters these are "kolk" and "kata", which have length 4.To solve this, we will follow these steps −ans := 0, left := 0table := a new mapfor right is in range 0 to size of s − 1, dotable[s[right]] := 1 + (s[right] if exists otherwise 0)if ... Read More

Find Length of Longest Substring with Even Vowel Counts in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:36:37

418 Views

Suppose we have a string s (lowercase), we have to find the length of the longest substring where each vowel occurs even number of times.So, if the input is like s = "anewcoffeepot", then the output will be 10, as the substring "wcoffeepot" has two vowels "o" and "e", both of which occurs two times.To solve this, we will follow these steps −vowels := a map assigning vowels and numeric values as {a:0, e:1, i:2, o:3, u:4}prefix := an empty map and insert a key-value pair (0, −1) into itmask := 0, n := size of s, res := 0for ... Read More

Length of Longest Palindromic Substring After Single Rotation in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:34:46

219 Views

Suppose we have a string s, which we can rotate at any point exactly once. We have to find the length of the longest palindromic substring we can get by doing this operation.So, if the input is like s = "elklev", then the output will be 7, as we can rotate between "el" and "klev" to get "levelk". So here the longest palinfromic substring length is 5.To solve this, we will follow these steps −s2 := concatenate s twicemax_len := 0for x in range 0 to size of s − 1, dofor y in range 0 to size of s, ... Read More

Find Longest Prefix That is Also a Suffix in C++

Arnab Chakraborty
Updated on 15-Dec-2020 12:33:08

543 Views

Suppose we have a string s, we have to find the longest prefix of s, which is also a suffix (excluding itself). If there is no such prefix, then simply return blank string.So, if the input is like "madam", then the output will be "m", it has 4 prefixes excluding itself. These are "m", "ma", "mad", "mada" and 4 suffixes like "m", "am", "dam", "adam". The largest prefix which is also suffix is given by "m".To solve this, we will follow these steps −Define a function lps(), this will take s, n := size of sDefine an array ret of ... Read More

Remove Connected Remote Desktop User Sessions Using PowerShell

Chirag Nagrekar
Updated on 15-Dec-2020 08:00:28

4K+ Views

We can remove connected RDP sessions using PowerShell and for that, we can use the cmd command “reset session” in PowerShell.  Let’s see the supported parameters for it.ExamplePS C:\> reset session /? Reset the session subsytem hardware and software to known initial values. RESET SESSION {sessionname | sessionid} [/SERVER:servername] [/V] sessionname         Identifies the session with name sessionname. sessionid           Identifies the session with ID sessionid. /SERVER:servername  The server containing the session (default is current). /V                  Display additional information.We can provide here session ... Read More

Get Connected Remote Desktop Users on Computers Using PowerShell

Chirag Nagrekar
Updated on 15-Dec-2020 07:57:30

3K+ Views

To get the user sessions on the remote computers using PowerShell, we need to use the cmd query command. First of all, we will get the user sessions on the local computer using the below command.Examplequery sessionOutputLet’s see what are other supported parameters for the query session command.ExamplePS C:\> query session /? Display information about Remote Desktop Services sessions. QUERY SESSION [sessionname | username | sessionid]               [/SERVER:servername] [/MODE] [/FLOW] [/CONNECT] [/COUNTER] [/VM] sessionname         Identifies the session named sessionname. username            Identifies the session ... Read More

Convert Dictionary to Hashtable in PowerShell

Chirag Nagrekar
Updated on 15-Dec-2020 07:54:24

1K+ Views

Like any other data type conversion in PowerShell, we can convert Dictionary to hashtable in a similar way.  We have a below Dictionary for the example called $CityData.Key     Value ---     ----- India      91 Austria    43Its datatype is Dictionary,ExamplePS C:\> $citydata.GetType() | ft -AutoSizeOutputIsPublic IsSerial Name         BaseType -------- -------- ----         -------- True     True     Dictionary`2 System.ObjectTo convert it to the hashtable,$hash = [Hashtable]$citydataOr$hash = [System.Collections.Hashtable]$CityDataDatatype:PS C:\> $hash | ft -AutoSizeOutputName    Value ----    ----- Austria 43 India   91

Difference Between Dictionary and Hashtable in PowerShell

Chirag Nagrekar
Updated on 15-Dec-2020 07:52:52

1K+ Views

PowerShell programmers generally prefer the Hashtable over the Dictionary although there are some advantages of using Dictionary. See the difference below.a. Hashtable is easy to declare while the Dictionary is a little complex compared to Hashtable. For example, To create a hashtable, $hash = @{    'Country' = 'India'    'Code' = '91' }To create a Dictionary, $citydata = New-Object System.Collections.Generic.Dictionary"[String, Int]" $citydata.Add('India', 91) b. Hashtable is included in the namespace called Collections while Dictionary is included in the namespace called System.Collections.Generic namespace. Hashtable is non-generic so it can be a collection of different data types and Dictionary belongs to a generic class so it is ... Read More

Advertisements