While analyzing data using Python data structures we will eventually come across the need for accessing key and value in a dictionary. There are various ways to do it in this article we will see some of the ways.With for loopUsing a for loop we can access both the key and value at each of the index position in dictionary as soon in the below program.Example Live DemodictA = {1:'Mon', 2:'Tue', 3:'Wed', 4:'Thu', 5:'Fri'} #Given dictionary print("Given Dictionary: ", dictA) # Print all keys and values print("Keys and Values: ") for i in dictA : print(i, dictA[i])OutputRunning the above code ... Read More
When we use a Python list, will be required to access its elements at different positions. In this article we will see how to get the index of specific elements in a list.With list.IndexThe below program sources the index value of different elements in given list. We supply the value of the element as a parameter and the index function returns the index position of that element.Example Live DemolistA = [11, 45, 27, 8, 43] # Print index of '45' print("Index of 45: ", listA.index(45)) listB = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] # Print index of 'Wed' print("Index of ... Read More
This example demonstrates how How to make a GridLayout fit screen size in Android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.javapackage com.app.sample; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.widget.GridLayout; import android.widget.ImageView; ... Read More
A Binary number is a number that has only two bits 0 and 1.Gray code is a special type of binary number that has a property that two successive number of the code cannot differ more than one bit. This property of gray code makes it useful more K-maps, error correction, communication, etc.This makes the conversion of binary to gray code necessary. So, let’s see an algorithm to convert binary to gray code using recursion.ExampleLet’s take an example of gray code coInput : 1001 Output : 1101AlgorithmStep 1 : Do with input n : Step 1.1 : if n ... Read More
In this problem, we are given the binary representation of a number and we have to find the binary representation of the previous number i.e. the number that is resulted after subtracting one from the given number.Binary representation of a number is changing the base of the number to base 2 and representing the number using only 0 or 1.For example, Binary representation of 23 is 10111.So, here we would be given a number, let's say n in binary form. And we have to find the binary representation of n-1.To solve this problem, we need to know the basics of ... Read More
We can define styles for tables using CSS. The following properties are used to style and its elements −borderThe CSS border property is used to define border-width, border-style and border-color.border-collapseThis property is used to specify whether a elements should have a shared or separate border.captionThe caption-side property is used to vertically position the table caption box.empty-cellsThis property is used to specify the display of empty cells of a table.table-layout To define the algorithm to be used by the browser for laying out rows, columns and cells of a table.ExampleThe following examples illustrate styling of tables − Live Demo ... Read More
In this tutorial, we will be discussing a program to find the rate percentage from compound interest of the consecutive years.For this, we will be provided with two integers say A and B that are the interests of two consecutive years. Our task is to find the rate of interest for the given values.Finding the relation between the given values and eliminating the principal amount, we get the formula asrate = ((B-A)*100)/AExample#include using namespace std; //calculating the rate of interest float calc_rate(int N1, int N2) { float rate = (N2 - N1) * 100 / float(N1); return ... Read More
In this tutorial, we will be discussing a program to find the roots of the Quadratic equation.Given a quadratic equation of the form ax2 + bx + c. Our task is to find the roots x1 and x2 of the given equation.For this, we are using the deterministic method, in thisD = √b2 - 4acthen the roots of the equation will bex1 = (-b + D)/2a ,andx2 = (-b - D)/2aExample#include #include #include //calculating the roots of equation void calc_roots(int a, int b, int c) { if (a == 0) { printf("Invalid Equation"); ... Read More
In this tutorial, we will be discussing a program to find the side of the octagon inscribed within a given square.For this, we will be given with the side of a square and our task is to find the side of the largest octagon that can be inscribed in it.Finding the relation between the sides of the square and the octagon, we find the formula for the side of the octagonside of square/(√2 + 1)Example#include using namespace std; //calculating the side of the octagon float calc_oside(float a) { if (a < 0) return -1; ... Read More
In this tutorial, we will be discussing a program to find the smallest element among the provided three elements.We will be provided with three elements/ integers and our task is to compare them and find the smallest element/ integer among them.Example#include using namespace std; int main() { int a = 45, b = 72, c = 10; if (a