group is one of the stage of aggregation.
group operator in aggregation is used to group similar documents and can perform operation on each document separately . These operations…
Find Uncommon Characters in two Strings
In this blog, we will understand how to find uncommon characters in two strings. For example, String1=”iamcoder” and String2=”iamloser” has below uncommon characters .c, d, l, s str1=”iamcoder” # String 1str2=”iamloser” # String 2CharInStr1NtPrInStr2=” #Blank String which will have Characters in String1 which are not present in String2CharInStr2NtPrInStr1=” #Blank String which will have Characters in String2 which are not present in String1m=${#str2} ##Length of a String2n=${#str1} ##Length of a String1i=0j=0while [[ $i -lt $n ]] #loop through all characters of String1doChar=${str1:$i:1}CharPresentInStr1=echo $str2 | grep -i $Char | wc -l #checking…
Parenthesis Check in Linux
In this blog, Sharing code to check whether Parenthesis or Brackets are correct in a string or not.Consider a String”[()]” should return “Parenthesis check Passed” while String “{()[(]))”should return “Parenthesis check Failed”. CODE EXPLANATION:1. Created two String OpenBracketStr and CloseBracketStr.2. Keep on Appending based on Character present in the String. If its a opening Bracket, it will be added to OpenBracketStr and as we have received Open Bracket type, I am adding a Expected Closed Bracket by myself in String called CloseBracketStr. In case I get 5 Opening bracket consecutively…
Minimum Index of all Characters in a String
In this blog, Sharing code to find Minimum Index value of all Characters present in a String. Consider a String “allabouttechnologies”.So, mininum Index for l is 1 not 2 and for a, its 0 not 3. str=”alllabouttechnologies”declare -A dict1 ##declare a dictionaryunset dict1 ##resetting dictionarynewstr=” ## creating Blank Stringn=${#str}i=0while [[ $i -lt $n ]]doChar=${str:$i:1}echo ${newstr}CharCntInNewStr=echo ${newstr} | grep ${Char} | wc -l ##If Character is not present, inserting into dictionary Index and Character itselfif [[ $CharCntInNewStr -eq “0” ]]thendict1[${i}]=${Char}echo ${dict1[${i}]}newstr=${newstr}${str:$i:1}fii=$(( $i + 1))donefor key in “${!dict1[@]}”; doecho “$key ${dict1[$key]}”done
Get all distinct Character in a string
In this blog, Sharing code to get only unique Characters present in a string. Please do not get confused with my Previous Blog . Its a different topic all together. Consider a String “allabouttechnologies“, so the distinct or unique Characters of this String are “alboutechngis“. str=”alllabouttechnologies” ##Sample Stringnewstr=” ##Blank new Stringn=${#str} ## Length of a Stringi=0while [[ $i -le $n ]] ##looping Character by CharacterdoChar=${str:$i:1}CharCntInNewStr=echo ${newstr}| grep ${Char} | wc -l ##Checking if Character already exists in new Stringif [[ $CharCntInNewStr -eq “0” ]]thennewstr=${newstr}${str:$i:1} ## appending Character if it is…
Remove all repeated Characters in a String
In this blog, sharing the code to remove consecutively repeated characters in a String. Consider a String “allabouttechnologies” . Here, l and t are repeated Characters. So, after applying logic, it will be like “alaboutechnologies“. str=”alllabouttechnologies” ##Sample Stringnewstr=” ##new Blank Stringn=${#str} ##length of a Sample StringPrevChar=${str:0:1} ##First Character of a Stringi=1while [[ $i -le $n ]] ##Looping Character by CharacterdoNextChar=${str:$i:1}if [[ $PrevChar == $NextChar ]] ##if two consecutive Character matches, it will remove or ignore the matched one and keep repeated Character oncethenecho “Ignore this Character: “$NextCharelsenewstr=${newstr}${PrevChar} ##merging only non-repeated…
Find first repeated character
In this blog, sharing the code to find the first repeated Character in a string. Consider a String “allabouttechnologies“, here l and t are the repeated Characters but l is the first repeated Character. str=”allabouttechnologies” ## Sample Stringn=${#str} ##Length of a StringPrevChar=${str:0:1} ## Set PrevChar as first character of a Stringi=1while [[ $i -lt $n ]] ## Looping through each character of a stringdoNextChar=${str:$i:1} ## Assigning NextChar equals to current Character in the Loopif [[ $PrevChar == $NextChar ]] ## Comparing consecutive Characters of a string in a loop, if…
Count Inversions in Array
Consider an Array (2 4 1 3 5). Inversions are those pairs where a[i]>a[j] and i<j . For above mentioned array, below are the pairs of Inversions. So, total Count Inversions are 3 for this Array.(2 1)(4 1)(4 3)Below is the code for the same using shell scripting. a=(2 4 1 3 5)len=${#a[@]}lngthminusone=$(( $len -1))i=0## This while loop will run inner loop for each element.while [ $i -lt $len ] doj=0## This while loop will compare one element with rest of the elements.while [ $j -lt $lngthminusone ] doj=$(( $j…
kth smallest element in array
This is continuation of sorting of array elements. Please go through this blog . In that blog, sorting of array elements is explained along with code. In order to find out kth smallest or kth largest element in array having all distinct elements , we need to sort this array first .Once sorting is done , then it is straight forward to get kth element (smallest or largest element) using a[k-1]Here I am sharing code for smallest kth element of an array. For kth largest element is achieved through descending…
Groups in Array
Consider an array of length N (1 2 4 5 10) and k=3. Below are the groups of Arrays(1 2 4) and (5 10)There is a difference between sub-array and groups in array. They are not at all same.Sub Array of k=3 are following.(1 2 4) , (2 4 5) and (4 5 10) In this blog, we are discussing about groups in array not sub-array. a=(1 2 4 5 10)len1=${#a[@]}j=0i=1##Set the value of K herek=3##Get the number of Groups of Array a(( NoOflps=(len1+k-1)/k ))echo $NoOflps##Run the innner while loop…