codeparrot/xlcost-text-to-code huggingface dataset:
Latin alphabet cipher | function for calculating the encryption ; Driver Code
def cipher ( str ) : NEW_LINE INDENT for i in range ( len ( str ) ) : NEW_LINE INDENT if str [ i ] . isalpha ( ) == 0 and str [ i ] != " ▁ " : NEW_LINE INDENT print ( " Enter ▁ only ▁ alphabets ▁ and ▁ space " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " Encrypted ▁ Code ▁ using ▁ Latin ▁ Alphabet " ) NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT if str [ i ] >= " A " and str [ i ] <= " Z " : NEW_LINE INDENT print ( ord ( str [ i ] ) - ord ( " A " ) + 1 , end = " ▁ " ) NEW_LINE DEDENT elif str [ i ] >= " a " and str [ i ] <= ' z ' : NEW_LINE INDENT print ( ord ( str [ i ] ) - ord ( " a " ) + 1 , end = " ▁ " ) NEW_LINE DEDENT if str [ i ] == " ▁ " : NEW_LINE INDENT print ( str [ i ] ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " geeksforgeeks " NEW_LINE cipher ( str ) NEW_LINE DEDENT
Count palindrome words in a sentence | Function to check if a word is palindrome ; Function to count palindrome words ; splitting each word as spaces as delimiter and storing it into a list ; Iterating every element from list and checking if it is a palindrome . ; if the word is a palindrome increment the count . ; Driver code
def checkPalin ( word ) : NEW_LINE INDENT if word . lower ( ) == word . lower ( ) [ : : - 1 ] : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT def countPalin ( str ) : NEW_LINE INDENT count = 0 NEW_LINE listOfWords = str . split ( " ▁ " ) NEW_LINE for elements in listOfWords : NEW_LINE INDENT if ( checkPalin ( elements ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT countPalin ( " Madam ▁ Arora ▁ teaches ▁ malayalam " ) NEW_LINE countPalin ( " Nitin ▁ speaks ▁ malayalam " ) NEW_LINE
Remove the forbidden strings | pre [ ] keeps record of the characters of w that need to be changed ; stores the forbidden strings ; Function to check if the particula r substring is present in w at position ; If length of substring from this position is greater than length of w then return ; n and n1 are used to check for substring without considering the case of the letters in w by comparing the difference of ASCII values ; If same == true then it means a substring was found starting at position therefore all characters from position to length of substring found need to be changed therefore they needs to be marked ; Function implementing logic ; To verify if any substring is starting from index i ; Modifying the string w according to th rules ; This condition checks if w [ i ] = upper ( letter ) ; This condition checks if w [ i ] is any lowercase letter apart from letter . If true replace it with letter ; This condition checks if w [ i ] is any uppercase letter apart from letter . If true then replace it with upper ( letter ) . ; number of forbidden strings ; given string ; letter to replace and occur max number of times ; Calling function
pre = [ False ] * 100 NEW_LINE s = [ None ] * 110 NEW_LINE def verify ( position , index ) : NEW_LINE INDENT l = len ( w ) NEW_LINE k = len ( s [ index ] ) NEW_LINE if ( position + k > l ) : NEW_LINE INDENT return NEW_LINE DEDENT same = True NEW_LINE for i in range ( position , position + k ) : NEW_LINE INDENT ch = w [ i ] NEW_LINE ch1 = s [ index ] [ i - position ] NEW_LINE if ( ch >= ' a ' and ch <= ' z ' ) : NEW_LINE INDENT n = ord ( ch ) - ord ( ' a ' ) NEW_LINE DEDENT else : NEW_LINE INDENT n = ord ( ch ) - ord ( ' A ' ) NEW_LINE DEDENT if ( ch1 >= ' a ' and ch1 <= ' z ' ) : NEW_LINE INDENT n1 = ord ( ch1 ) - ord ( ' a ' ) NEW_LINE DEDENT else : NEW_LINE INDENT n1 = ord ( ch1 ) - ord ( ' A ' ) NEW_LINE DEDENT if ( n != n1 ) : NEW_LINE INDENT same = False NEW_LINE DEDENT DEDENT if ( same == True ) : NEW_LINE INDENT for i in range ( position , position + k ) : NEW_LINE INDENT pre [ i ] = True NEW_LINE DEDENT return NEW_LINE DEDENT DEDENT def solve ( ) : NEW_LINE INDENT l = len ( w ) NEW_LINE p = ord ( letter ) - ord ( ' a ' ) NEW_LINE for i in range ( l ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT verify ( i , j ) NEW_LINE DEDENT DEDENT for i in range ( l ) : NEW_LINE INDENT if ( pre [ i ] == True ) : NEW_LINE INDENT if ( w [ i ] == letter ) : NEW_LINE INDENT w [ i ] = ' b ' if ( letter == ' a ' ) else ' a ' NEW_LINE DEDENT elif ( w [ i ] == str ( ord ( ' A ' ) + p ) ) : NEW_LINE INDENT w [ i ] = ' B ' if ( letter == ' a ' ) else ' A ' NEW_LINE DEDENT elif ( w [ i ] >= ' a ' and w [ i ] <= ' z ' ) : NEW_LINE INDENT w [ i ] = letter NEW_LINE DEDENT elif ( w [ i ] >= ' A ' and w [ i ] <= ' Z ' ) : NEW_LINE INDENT w [ i ] = chr ( ord ( ' A ' ) + p ) NEW_LINE DEDENT DEDENT DEDENT print ( ' ' . join ( w ) ) NEW_LINE DEDENT n = 3 NEW_LINE s [ 0 ] = " etr " NEW_LINE s [ 1 ] = " ed " NEW_LINE s [ 2 ] = " ied " NEW_LINE w = " PEtrUnited " NEW_LINE w = list ( w ) NEW_LINE letter = ' d ' NEW_LINE solve ( ) NEW_LINE
Round the given number to nearest multiple of 10 | function to round the number ; Smaller multiple ; Larger multiple ; Return of closest of two ; driver code
def round ( n ) : NEW_LINE INDENT a = ( n // 10 ) * 10 NEW_LINE b = a + 10 NEW_LINE return ( b if n - a > b - n else a ) NEW_LINE DEDENT n = 4722 NEW_LINE print ( round ( n ) ) NEW_LINE
Breaking a number such that first part is integral division of second by a power of 10 | Python3 program to count ways to divide a string in two parts a and b such that b / pow ( 10 , p ) == a ; substring representing int a ; no of digits in a ; consider only most significant l1 characters of remaining string for int b ; if any of a or b contains leading 0 s discard this ; if both are equal ; driver code to test above function
def calculate ( N ) : NEW_LINE INDENT length = len ( N ) NEW_LINE l = int ( ( length ) / 2 ) NEW_LINE count = 0 NEW_LINE for i in range ( l + 1 ) : NEW_LINE INDENT s = N [ 0 : 0 + i ] NEW_LINE l1 = len ( s ) NEW_LINE t = N [ i : l1 + i ] NEW_LINE try : NEW_LINE INDENT if s [ 0 ] == '0' or t [ 0 ] == '0' : NEW_LINE INDENT continue NEW_LINE DEDENT DEDENT except : NEW_LINE INDENT continue NEW_LINE DEDENT if s == t : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT N = str ( "2202200" ) NEW_LINE print ( calculate ( N ) ) NEW_LINE
Program to check Strength of Password | Python3 program to check if a given password is strong or not . ; Checking lower alphabet in string ; Strength of password ; Driver code
def printStrongNess ( input_string ) : NEW_LINE INDENT n = len ( input_string ) NEW_LINE hasLower = False NEW_LINE hasUpper = False NEW_LINE hasDigit = False NEW_LINE specialChar = False NEW_LINE normalChars = " abcdefghijklmnopqrstu " NEW_LINE for i in range ( n ) : NEW_LINE INDENT if input_string [ i ] . islower ( ) : NEW_LINE INDENT hasLower = True NEW_LINE DEDENT if input_string [ i ] . isupper ( ) : NEW_LINE INDENT hasUpper = True NEW_LINE DEDENT if input_string [ i ] . isdigit ( ) : NEW_LINE INDENT hasDigit = True NEW_LINE DEDENT if input_string [ i ] not in normalChars : NEW_LINE INDENT specialChar = True NEW_LINE DEDENT DEDENT print ( " Strength ▁ of ▁ password : - " , end = " " ) NEW_LINE if ( hasLower and hasUpper and hasDigit and specialChar and n >= 8 ) : NEW_LINE INDENT print ( " Strong " ) NEW_LINE DEDENT elif ( ( hasLower or hasUpper ) and specialChar and n >= 6 ) : NEW_LINE INDENT print ( " Moderate " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Weak " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT input_string = " GeeksforGeeks ! @12" NEW_LINE printStrongNess ( input_string ) NEW_LINE DEDENT
String with k distinct characters and no same characters adjacent | Function to find a string of length n with k distinct characters . ; Initialize result with first k Latin letters ; Fill remaining n - k letters by repeating k letters again and again . ; Driver code
def findString ( n , k ) : NEW_LINE INDENT res = " " NEW_LINE for i in range ( k ) : NEW_LINE INDENT res = res + chr ( ord ( ' a ' ) + i ) NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( n - k ) : NEW_LINE INDENT res = res + chr ( ord ( ' a ' ) + count ) NEW_LINE count += 1 NEW_LINE if ( count == k ) : NEW_LINE INDENT count = 0 ; NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE k = 2 NEW_LINE print ( findString ( n , k ) ) NEW_LINE DEDENT
Find substrings that contain all vowels | Returns true if x is vowel . ; Function to check whether a character is vowel or not ; Function to FindSubstrings of string ; hash = set ( ) ; To store vowels ; If current character is vowel then insert into hash ; If all vowels are present in current substring ; Driver Code
def isVowel ( x ) : NEW_LINE INDENT return ( x == ' a ' or x == ' e ' or x == ' i ' or x == ' o ' or x == ' u ' ) ; NEW_LINE DEDENT def FindSubstring ( str ) : NEW_LINE INDENT start = 0 ; NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT if ( isVowel ( str [ i ] ) == True ) : NEW_LINE INDENT hash . add ( str [ i ] ) ; NEW_LINE if ( len ( hash ) == 5 ) : NEW_LINE INDENT print ( str [ start : i + 1 ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT start = i + 1 ; NEW_LINE hash . clear ( ) ; NEW_LINE DEDENT DEDENT DEDENT str = " aeoibsddaeiouudb " ; NEW_LINE FindSubstring ( str ) ; NEW_LINE
Concatenated string with uncommon characters of two strings | Python3 program Find concatenated string with uncommon characters of given strings ; res = " " result ; store all characters of s2 in map ; Find characters of s1 that are not present in s2 and append to result ; Find characters of s2 that are not present in s1 . ; Driver Code
def concatenetedString ( s1 , s2 ) : NEW_LINE INDENT m = { } NEW_LINE for i in range ( 0 , len ( s2 ) ) : NEW_LINE INDENT m [ s2 [ i ] ] = 1 NEW_LINE DEDENT for i in range ( 0 , len ( s1 ) ) : NEW_LINE INDENT if ( not s1 [ i ] in m ) : NEW_LINE INDENT res = res + s1 [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT m [ s1 [ i ] ] = 2 NEW_LINE DEDENT DEDENT for i in range ( 0 , len ( s2 ) ) : NEW_LINE INDENT if ( m [ s2 [ i ] ] == 1 ) : NEW_LINE INDENT res = res + s2 [ i ] NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " abcs " NEW_LINE s2 = " cxzca " NEW_LINE print ( concatenetedString ( s1 , s2 ) ) NEW_LINE DEDENT
Null Cipher | Function to decode the message . ; Store the decoded string ; found variable is used to tell that the encoded encoded character is found in that particular word . ; Set found variable to false whenever we find whitespace , meaning that encoded character for new word is not found ; Driver code
def decode ( string ) : NEW_LINE INDENT res = " " NEW_LINE found = False NEW_LINE for character in string : NEW_LINE INDENT if character == ' ▁ ' : NEW_LINE INDENT found = False NEW_LINE continue NEW_LINE DEDENT if not found : NEW_LINE INDENT if character >= ' A ' and character <= ' Z ' or character >= ' a ' and character <= ' z ' : NEW_LINE INDENT res += character NEW_LINE found = True NEW_LINE DEDENT DEDENT DEDENT return res . lower ( ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT input = " A ▁ Step ▁ by ▁ Step ▁ Guide ▁ for ▁ Placement ▁ Preparation ▁ by ▁ GeeksforGeeks " NEW_LINE print ( " Enciphered ▁ Message : " , decode ( input ) ) NEW_LINE DEDENT
Program to count vowels in a string ( Iterative and Recursive ) | Function to check the Vowel ; to count total number of vowel from 0 to n ; string object ; Total numbers of Vowel
def isVowel ( ch ) : NEW_LINE INDENT return ch . upper ( ) in [ ' A ' , ' E ' , ' I ' , ' O ' , ' U ' ] NEW_LINE DEDENT def countVovels ( str , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return isVowel ( str [ n - 1 ] ) ; NEW_LINE DEDENT return ( countVovels ( str , n - 1 ) + isVowel ( str [ n - 1 ] ) ) ; NEW_LINE DEDENT str = " abc ▁ de " ; NEW_LINE print ( countVovels ( str , len ( str ) ) ) NEW_LINE
Generate all rotations of a given string | Print all the rotated string . ; Concatenate str with itself ; Print all substrings of size n . Note that size of temp is 2 n ; Driver Code
def printRotatedString ( string ) : NEW_LINE INDENT n = len ( string ) NEW_LINE temp = string + string NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT print ( temp [ i + j ] , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeks " NEW_LINE printRotatedString ( string ) NEW_LINE DEDENT
Check if frequency of all characters can become same by one removal | Python3 program to get same frequency character string by removal of at most one char ; Utility method to get index of character ch in lower alphabet characters ; Returns true if all non - zero elements values are same ; get first non - zero element ; check equality of each element with variable same ; Returns true if we can make all character frequencies same ; fill frequency array ; if all frequencies are same , then return true ; Try decreasing frequency of all character by one and then check all equality of all non - zero frequencies ; Check character only if it occurs in str ; Driver code
M = 26 NEW_LINE def getIdx ( ch ) : NEW_LINE INDENT return ( ord ( ch ) - ord ( ' a ' ) ) NEW_LINE DEDENT def allSame ( freq , N ) : NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT if ( freq [ i ] > 0 ) : NEW_LINE INDENT same = freq [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( freq [ j ] > 0 and freq [ j ] != same ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def possibleSameCharFreqByOneRemoval ( str1 ) : NEW_LINE INDENT l = len ( str1 ) NEW_LINE freq = [ 0 ] * M NEW_LINE for i in range ( 0 , l ) : NEW_LINE INDENT freq [ getIdx ( str1 [ i ] ) ] += 1 NEW_LINE DEDENT if ( allSame ( freq , M ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( 0 , 26 ) : NEW_LINE INDENT if ( freq [ i ] > 0 ) : NEW_LINE INDENT freq [ i ] -= 1 NEW_LINE if ( allSame ( freq , M ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT freq [ i ] += 1 NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " xyyzz " NEW_LINE if ( possibleSameCharFreqByOneRemoval ( str1 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Check if a large number is divisible by 11 or not | Function to find that number divisible by 11 or not ; Compute sum of even and odd digit sums ; When i is even , position of digit is odd ; Check its difference is divisible by 11 or not ; Driver code
def check ( st ) : NEW_LINE INDENT n = len ( st ) NEW_LINE oddDigSum = 0 NEW_LINE evenDigSum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT oddDigSum = oddDigSum + ( ( int ) ( st [ i ] ) ) NEW_LINE DEDENT else : NEW_LINE INDENT evenDigSum = evenDigSum + ( ( int ) ( st [ i ] ) ) NEW_LINE DEDENT DEDENT return ( ( oddDigSum - evenDigSum ) % 11 == 0 ) NEW_LINE DEDENT st = "76945" NEW_LINE if ( check ( st ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ " ) NEW_LINE DEDENT
Hamming Distance between two strings | Function to calculate Hamming distance ; Driver code ; function call
def hammingDist ( str1 , str2 ) : NEW_LINE INDENT i = 0 NEW_LINE count = 0 NEW_LINE while ( i < len ( str1 ) ) : NEW_LINE INDENT if ( str1 [ i ] != str2 [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT str1 = " geekspractice " NEW_LINE str2 = " nerdspractise " NEW_LINE print ( hammingDist ( str1 , str2 ) ) NEW_LINE
Check if two strings are k | Python3 program to check if two strings are k anagram or not . ; Function to check that is k - anagram or not ; If both strings are not of equal length then return false ; Store the occurrence of all characters in a hash_array ; Count number of characters that are different in both strings ; Return true if count is less than or equal to k ; Driver Code
MAX_CHAR = 26 NEW_LINE def arekAnagrams ( str1 , str2 , k ) : NEW_LINE INDENT n = len ( str1 ) NEW_LINE if ( len ( str2 ) != n ) : NEW_LINE INDENT return False NEW_LINE DEDENT count1 = [ 0 ] * MAX_CHAR NEW_LINE count2 = [ 0 ] * MAX_CHAR NEW_LINE for i in range ( n ) : NEW_LINE INDENT count1 [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT count2 [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( MAX_CHAR ) : NEW_LINE INDENT if ( count1 [ i ] > count2 [ i ] ) : NEW_LINE INDENT count = count + abs ( count1 [ i ] - count2 [ i ] ) NEW_LINE DEDENT DEDENT return ( count <= k ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " anagram " NEW_LINE str2 = " grammar " NEW_LINE k = 2 NEW_LINE if ( arekAnagrams ( str1 , str2 , k ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Remove spaces from a given string | Function to remove all spaces from a given string ; To keep track of non - space character count ; Traverse the given string . If current character is not space , then place it at index 'count++ ; Utility Function ; Driver program
def removeSpaces ( string ) : NEW_LINE INDENT count = 0 NEW_LINE list = [ ] NEW_LINE DEDENT ' NEW_LINE INDENT for i in xrange ( len ( string ) ) : NEW_LINE INDENT if string [ i ] != ' ▁ ' : NEW_LINE INDENT list . append ( string [ i ] ) NEW_LINE DEDENT DEDENT return toString ( list ) NEW_LINE DEDENT def toString ( List ) : NEW_LINE INDENT return ' ' . join ( List ) NEW_LINE DEDENT string = " g ▁ eeks ▁ for ▁ ge ▁ eeks ▁ " NEW_LINE print removeSpaces ( string ) NEW_LINE
Given a binary string , count number of substrings that start and end with 1. | A Python3 program to count number of substrings starting and ending with 1 ; Count of 1 's in input string ; Traverse input string and count of 1 's in it ; Return count of possible pairs among m 1 's ; Driver program to test above function
def countSubStr ( st , n ) : NEW_LINE INDENT m = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( st [ i ] == '1' ) : NEW_LINE INDENT m = m + 1 NEW_LINE DEDENT DEDENT return m * ( m - 1 ) // 2 NEW_LINE DEDENT st = "00100101" ; NEW_LINE list ( st ) NEW_LINE n = len ( st ) NEW_LINE print ( countSubStr ( st , n ) , end = " " ) NEW_LINE
Given a number as a string , find the number of contiguous subsequences which recursively add up to 9 | Python 3 program to count substrings with recursive sum equal to 9 ; count = 0 To store result ; Consider every character as beginning of substring ; sum of digits in current substring ; One by one choose every character as an ending character ; Add current digit to sum , if sum becomes multiple of 5 then increment count . Let us do modular arithmetic to avoid overflow for big strings ; Driver Code
def count9s ( number ) : NEW_LINE INDENT n = len ( number ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = ord ( number [ i ] ) - ord ( '0' ) NEW_LINE if ( number [ i ] == '9' ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT sum = ( sum + ord ( number [ j ] ) - ord ( '0' ) ) % 9 NEW_LINE if ( sum == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( count9s ( "4189" ) ) NEW_LINE print ( count9s ( "1809" ) ) NEW_LINE DEDENT
Generate n | Python3 program to generate n - bit Gray codes ; This function generates all n bit Gray codes and prints the generated codes ; base case ; ' arr ' will store all generated codes ; start with one - bit pattern ; Every iteration of this loop generates 2 * i codes from previously generated i codes . ; Enter the prviously generated codes again in arr [ ] in reverse order . Nor arr [ ] has double number of codes . ; append 0 to the first half ; append 1 to the second half ; prcontents of arr [ ] ; Driver Code
import math as mt NEW_LINE def generateGrayarr ( n ) : NEW_LINE INDENT if ( n <= 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT arr = list ( ) NEW_LINE arr . append ( "0" ) NEW_LINE arr . append ( "1" ) NEW_LINE i = 2 NEW_LINE j = 0 NEW_LINE while ( True ) : NEW_LINE INDENT if i >= 1 << n : NEW_LINE INDENT break NEW_LINE DEDENT for j in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT arr . append ( arr [ j ] ) NEW_LINE DEDENT for j in range ( i ) : NEW_LINE INDENT arr [ j ] = "0" + arr [ j ] NEW_LINE DEDENT for j in range ( i , 2 * i ) : NEW_LINE INDENT arr [ j ] = "1" + arr [ j ] NEW_LINE DEDENT i = i << 1 NEW_LINE DEDENT for i in range ( len ( arr ) ) : NEW_LINE INDENT print ( arr [ i ] ) NEW_LINE DEDENT DEDENT generateGrayarr ( 3 ) NEW_LINE
N Queen in O ( n ) space | Python code to for n Queen placement ; Helper Function to check if queen can be placed ; Function to display placed queen ; Function to check queens placement ; Driver Code
class GfG : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . MAX = 10 NEW_LINE self . arr = [ 0 ] * self . MAX NEW_LINE self . no = 0 NEW_LINE DEDENT def breakLine ( self ) : NEW_LINE INDENT print ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " ) NEW_LINE DEDENT def canPlace ( self , k , i ) : NEW_LINE INDENT for j in range ( 1 , k ) : NEW_LINE INDENT if ( self . arr [ j ] == i or ( abs ( self . arr [ j ] - i ) == abs ( j - k ) ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def display ( self , n ) : NEW_LINE INDENT self . breakLine ( ) NEW_LINE self . no += 1 NEW_LINE print ( " Arrangement ▁ No . " , self . no , end = " ▁ " ) NEW_LINE self . breakLine ( ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if self . arr [ i ] != j : NEW_LINE INDENT print ( " TABSYMBOL _ " , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " TABSYMBOL Q " , end = " ▁ " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT self . breakLine ( ) NEW_LINE DEDENT def nQueens ( self , k , n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if self . canPlace ( k , i ) : NEW_LINE INDENT self . arr [ k ] = i NEW_LINE if k == n : NEW_LINE INDENT self . display ( n ) NEW_LINE DEDENT else : NEW_LINE INDENT self . nQueens ( k + 1 , n ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE obj = GfG ( ) NEW_LINE obj . nQueens ( 1 , n ) NEW_LINE DEDENT
Numbers that are bitwise AND of at least one non | Python3 implementation of the approach ; Set to store all possible AND values . ; Starting index of the sub - array . ; Ending index of the sub - array . res = 2147483647 Integer . MAX_VALUE ; AND value is added to the set . ; The set contains all possible AND values .
A = [ 11 , 15 , 7 , 19 ] NEW_LINE N = len ( A ) NEW_LINE Set = set ( ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( i , N ) : NEW_LINE INDENT res &= A [ j ] NEW_LINE Set . add ( res ) NEW_LINE DEDENT DEDENT print ( Set ) NEW_LINE
Collect all coins in minimum number of steps | recursive method to collect coins from height array l to r , with height h already collected ; if l is more than r , no steps needed ; loop over heights to get minimum height index ; choose minimum from , 1 ) collecting coins using all vertical lines ( total r - l ) 2 ) collecting coins using lower horizontal lines and recursively on left and right segments ; method returns minimum number of step to collect coin from stack , with height in height [ ] array ; Driver code
def minStepsRecur ( height , l , r , h ) : NEW_LINE INDENT if l >= r : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT m = l NEW_LINE for i in range ( l , r ) : NEW_LINE INDENT if height [ i ] < height [ m ] : NEW_LINE INDENT m = i NEW_LINE DEDENT DEDENT return min ( r - l , minStepsRecur ( height , l , m , height [ m ] ) + minStepsRecur ( height , m + 1 , r , height [ m ] ) + height [ m ] - h ) NEW_LINE DEDENT def minSteps ( height , N ) : NEW_LINE INDENT return minStepsRecur ( height , 0 , N , 0 ) NEW_LINE DEDENT height = [ 2 , 1 , 2 , 5 , 1 ] NEW_LINE N = len ( height ) NEW_LINE print ( minSteps ( height , N ) ) NEW_LINE