Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles on Trending Technologies
Technical articles with clear explanations and examples
C Program to construct DFA accepting odd numbers of 0s and 1s
Construct deterministic finite automata (DFA) for the language L = { w : w has odd number of 0’s and w has odd number of 1’s}, over the alphabet Σ = {0, 1}.Example0111, 010101, 01110011 is an accepted string, because those strings have an odd number of 0’s and an odd number of 1’s.For the given language we will need four states to draw the main DFA which will read odd no. of 0s and 1s. We can also draw it by merging the two DFAs in which one will accept only an odd number of 0s and one accepts ...
Read MoreGolang Program to Print the Numbers in a Range (1, upper) without Using any Loops
StepsDefine a recursive function.Define a base case for that function that the number should be greater than zero.If the number is greater than 0, call the function again with the argument as the number minus 1.Print the number.Enter the upper limit: 512345Enter the upper limit: 1512..15Examplepackage main import ( "fmt" ) func printNo(number int){ if number >= 1{ printNo(number-1) fmt.Println(number) } } func main(){ var upper int fmt.Print("Enter the upper limit: ") fmt.Scanf("%d", &upper) printNo(upper) }OutputEnter the upper limit: 5 1 2 3 4 5
Read MorePython counter and dictionary intersection example
When it is required to demonstrate a counter and dictionary intersection, the Counter and dictionary can be used.Below is the demonstration of the same −Examplefrom collections import Counter def make_string(str_1, str_2): dict_one = Counter(str_1) dict_two = Counter(str_2) result = dict_one & dict_two return result == dict_one string_1 = 'Hi Mark' string_2 = 'how are yoU' print("The first string is :") print(string_1) print("The second string is :") print(string_2) if (make_string(string_1, string_2)==True): print("It is possible") else: print("It is not possible")OutputThe first string is : Hi Mark The second string is : how are ...
Read MoreFinding the 1-based index of a character in alphabets using JavaScript
ProblemWe are required to write a JavaScript function that takes in a lowercase English alphabet character. Our function should return the character’s 1-based index in the alphabets.ExampleFollowing is the code −const char = 'j'; const findCharIndex = (char = '') => { const legend = ' abcdefghijklmnopqrstuvwxyz'; if(!char || !legend.includes(char) || char.length !== 1){ return -1; }; return legend.indexOf(char); }; console.log(findCharIndex(char));Output10
Read MoreGolang Program to Determine Recursively Whether a Given Number is Even or Odd
StepsTake a number from the user and store it in a variable.Pass the number as an argument to a recursive function.Define the base condition as the number to be lesser than 2.Otherwise, call the function recursively with the number minus 2.Then, return the result and check if the number is even or odd.Print the final result.Enter a number: 124Number is even!Enter a number: 567Number is odd!Examplepackage main import ( "fmt" ) func check(n int) bool{ if n < 2 { return n % 2 == 0 } return check(n - 2) } func ...
Read MorePython dictionary, set and counter to check if frequencies can become same
When it is required to check if the frequency of a dictionary, set and counter are same, the Counter package is imported and the input is converted into a ‘Counter’. The values of a dictionary are converted to a ‘set’ and then to a list. Based on the length of the input, the output is displayed on the console.Below is the demonstration of the same −Examplefrom collections import Counter def check_all_same(my_input): my_dict = Counter(my_input) input_2 = list(set(my_dict.values())) if len(input_2)>2: print('The frequencies are not same') elif len (input_2)==2 and input_2[1]-input_2[0]>1: print('The ...
Read MorePython Program to Select the nth Largest Element from a List in Expected Linear Time
When it is required to select the nth largest element from a list in linear time complexity, two methods are required. One method to find the largest element, and another method that divides the list into two parts. This division depends on the ‘i’ value that is given by user. Based on this value, the list is split, and the largest element is determined.Below is a demonstration of the same −Exampledef select_largest(my_list, beg, end, i): if end - beg k: return select_largest(my_list, beg, pivot_val, i - k) return my_list[pivot_val] def start_partition(my_list, beg, ...
Read MoreKeys associated with Values in Dictionary in Python
When it is required to find the keys associated with specific values in a dictionary, the ‘index’ method can be used.Below is the demonstration of the same −Examplemy_dict ={"Hi":100, "there":121, "Mark":189} print("The dictionary is :") print(my_dict) dict_key = list(my_dict.keys()) print("The keys in the dictionary are :") print(dict_key) dict_val = list(my_dict.values()) print("The values in the dictionary are :") print(dict_val) my_position = dict_val.index(100) print("The value at position 100 is : ") print(dict_key[my_position]) my_position = dict_val.index(189) print("The value at position 189 is") print(dict_key[my_position])OutputThe dictionary is : {'Hi': 100, 'there': 121, 'Mark': 189} The keys in the dictionary are : ['Hi', ...
Read MoreConstructing an array of addition/subtractions relative to first array element in JavaScript
ProblemWe are required to write a JavaScript function that takes in an array of positive integers. Our function should map this array to an array of string integers.The array should contain the number we should add/subtract to the first element to achieve the corresponding element.For example[4, 3, 6, 2]should return −['+0', '-1', '+2', '-2']ExampleFollowing is the code −const arr = [4, 3, 6, 2]; const buildRelative = (arr = []) => { const res = []; let num = ''; for(let i of arr){ if(i - arr[0] >= 0){ num ...
Read MoreHow to convert data frame values to their rank in R?
To convert data frame values to their rank in R, we can follow the below steps −First of all, create a data frame.Then, find the rank of each value using rank function with lapply.Create the data frameLet's create a data frame as shown below −> x1 y1 df dfOn executing, the above script generates the below output(this output will vary on your system due to randomization) − x1 y1 1 -0.9045608 1.95315551 2 -1.6222122 -1.19880638 3 0.8618855 -0.33643175 4 -0.3547085 -0.18097356 5 -0.3043621 -0.60342210 6 0.5606001 -0.83618995 7 -1.1752752 -0.46651702 ...
Read More