To make a new folder using askdirectory dialog in Tkinter, we can take the following steps −Import the required modules. filedialog module is required for askdirectory method. os module is required for makedirs method.Create an instance of tkinter frame.Set the size of the frame using win.geometry method.Define a user-defined method "create_subfolder". Inside the method, call filedialog.askdirectory to select a folder and save the path in a variable, source_path.We can use askdirectory method of filedialog to open a directory. Save the path of the selected directory in a 'path' variable.Then, use os.path.join and makedirs to create a sub-folder inside the parent ... Read More
Given an array of numbers, we need to find the number of groups of size 2 and 3 that are divisible by 3. We can get the sums of two and three combination numbers and check whether they are divisible by 3 or not.Let's see an example.Inputarr = [1, 2, 3, 4]Output4 There are 4 combinations that are divisible by 3. The combinations are...[1, 2] [2, 4] [1, 2, 3] [2, 3, 4]AlgorithmInitialise the array.Write two loops to get all combinations of size two.Compute the sum of each group.If the sum is divisible by 3, then increment the count.Write three ... Read More
The digit 1 represent positive pole whereas 0 represents negative pole.The magnet will have both poles as 10 or 01. A group can be formed with the magnets that attracts each other. The magnets with different pole facing each other will be in the same group.Here, you are given N number of magnets. You need to find out number of groups can be formed with them.Whenever there are two different magnets side by side, there forms a new group. In that case increment the count of the groups.Let's see an example.Inputmagnets = ["10", "01", "01", "01", "10", "01"]Output4 There are ... Read More
Let's say you have given a binary string "10011". To make an alternate binary string, we need to flip a minimum of 2 characters as "10101".There are two possibilities for the alternate string. It will start with 0 or 1. We will check for two alternates and count the number of flips required for both.And then return the minimum of both. Let's see an example.Inputbinary = "10011"Output2 If we start the string with 0, then we have to flip 3 times. And if we start the string with 1, then we have to flip 2 times. The minimum is 2.AlgorithmInitialise ... Read More
Given a string of digits, we need to find the count of even substrings in it. Let's see an example.Inputnum = "1234"Output6The even substrings that can be formed from the given string are2 12 4 34 234 1234AlgorithmInitialise the string with digits.Initialise the count to 0.Iterate over the string.Get the current digit by subtracting the char 0 from the current char digit.Check whether the digit is even or not.If the current digit is even, then add it's index plus 1 to the count.Return the count.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getEvenSubstringsCount(char str[]) ... Read More
A Grammar that makes more than one Leftmost Derivation (or Rightmost Derivation) for the similar sentence is called Ambiguous Grammar.Example − Verify whether the following Grammar is Ambiguous or Not.E → E+E|E $\ast$ E|idSolutionFor string id + id * id, there exist two parse trees.E ⇒lm $\underline{E}$+E ⇒ id+ $\underline{E}$⇒ id+$\underline{E}$ $\ast$ E⇒ id+id $\ast$ $\underline{E}$⇒ id+id $\ast$ idE ⇒lm $\underline{E}$ $\ast$ E⇒ $\underline{E}$+E $\ast$ E⇒ id+ $\underline{E}$ $\ast$ E⇒ id+id $\ast$ $\underline{E}$⇒ id+id $\ast$ idSo, the same string is generated using two different leftmost derivations. Each is having a different parse tree.∴ Two different parse trees exist for string id + id ... Read More
Derivations mean replacing a given string’s non-terminal by the right-hand side of the production rule. The sequence of applications of rules that makes the completed string of terminals from the starting symbol is known as derivation.It can derive terminal strings, beginning with the start symbol, by repeatedly replacing a variable with some production. The language of CFG is a set of terminal symbols we can derive so. This language is called context Free Language.Derivations are denoted by ⇒.For example, consider a Grammar.G=({S}, {a, b}, P, S), where, P contains following productions −P={S→aSa |bSb | ∈}In the above, S may be ... Read More
Grammar − It is a set of rules which checks whether a string belongs to a particular language a not.A program consists of various strings of characters. But, every string is not a proper or meaningful string. So, to identify valid strings in a language, some rules should be specified to check whether the string is valid or not. These rules are nothing but make Grammar.Example − In English Language, Grammar checks whether the string of characters is acceptable or not, i.e., checks whether nouns, verbs, adverbs, etc. are in the proper sequence.Context-Free GrammarIt is a notation used to specify ... Read More
Lexical Analysis is the first step of the compiler which reads the source code one character at a time and transforms it into an array of tokens. The token is a meaningful collection of characters in a program. These tokens can be keywords including do, if, while etc. and identifiers including x, num, count, etc. and operator symbols including >, >=, +, etc., and punctuation symbols including parenthesis or commas. The output of the lexical analyzer phase passes to the next phase called syntax analyzer or parser.The syntax analyser or parser is also known as parsing phase. It takes tokens ... Read More
Minimizing means reducing the number of states in DFA. We have to detect those states of DFA whose presence or absence in DFA does not affect language accepted by DFA. These states can be eliminated from DFA.Algorithm: Minimization of DFAInput − DFA D1 with a set of states Q with a set of final states F.Output − DFA D2 which accepts the same language as D1 and having a minimum number of states as possible.MethodMake a partition 'π' of a set of states with two subsets −Final state 'F'Non-Final States 'Q-F'∴ π={F, Q−F}To apply the following procedure to make πnew ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP