Suppose we are given an integer value, a long value, a character value, a float value, and a double value as inputs. We have to print the values that are given to us as input maintaining their precision.So, if the input is like integer value = 15, long value = 59523256297252, character value = 'y', float value = 367.124, double value = 6464292.312621, then the output will be15 59523256297252 y 367.124 6464292.31262To solve this, we will follow these steps −Print the values given as input in separate lines.ExampleLet us see the following implementation to get better understanding −#include #include ... Read More
Suppose we are in charge of building a library system that monitors and queries various operations at the library. We are now asked to implement three different commands that perform the following −By using command 1, we can record the insertion of a book with y pages at shelf x.By using command 2, we can print the page number of the y-th book at shelf x.By using command 3, we can print the number of books on shelf x.The commands are given to us as a 2D array in this format {command type, x, y}. If there is no y ... Read More
Suppose we are given two integers k and n. Our task is to perform three operations; bitwise AND, bitwise OR, and bitwise XOR between all pairs of numbers up to range n. We return the maximum value of all three operations between any two pairs of numbers that is less than the given value k.So, if the input is like n = 5, k = 5, then the output will be 4 3 4.The greatest value of AND, OR, and XOR operations between all pairs of numbers that are less than 5 are 4, 3, and 4 respectively. We can ... Read More
Suppose we have two values k and n. Consider a random permutation say p1, p2, ..., pn of first n natural numbers numbers 1, 2, ..., n and calculate the value F, such that F = (X2+...+Xn-1)k, where Xi is an indicator random variable, which is 1 when one of the following two conditions holds: pi-1 < pi > pi+1 or pi-1 > pi < pi+1 and Xi is 0 otherwise. We have to find the expected value of F.So, if the input is like k = 1 n = 1000, then the output will be 1996/3To solve this, we ... Read More
Suppose we have a value n. We have to print Decimal, Octal, Hexadecimal and Binary equivalent of first n numbers (1 to n) in four different columns. As we know, we can express the numbers with prefix characters d, o, X and b for decimal, octal, hexadecimal and decimal respectively.So, if the input is like n = 10, then the output will be1 1 1 1 2 2 2 10 3 3 3 11 4 4 4 100 5 5 5 101 6 6 6 ... Read More
Suppose we have two numbers n and m, m will be multiple of n. We have to draw a door mat pattern with a word say "WELCOME" in the middle. The mat size will be n x m. We have to make this mat using dots(.), hyphens (-), pipe symbols (|) and the text at middle.So, if the input is like n = 5 m = 15, then the output will be------.|.------ ---.|..|..|.--- ----WELCOME---- ---.|..|..|.--- ------.|.------To solve this, we will follow these steps −for i in range 1 to n-1, increase by 2, doprint(integer of ((m-i*3)/2) number of '-', then ... Read More
Suppose we have a string s and width w. We have to wrap this text into a paragraph with width w. This can be done very easily with fill() function present inside textwrap library. So we have to import textwrap library first.So, if the input is like s = "The quick brown fox jumps over the lazy dog" w = 9, then the output will beThe quickbrown foxjumpsover thelazy dogTo solve this, we will follow these steps −take the string into stake width into wcall textwrap.fill(s, w) by passing s as the first argument, and w as the second argumentExampleLet ... Read More
Suppose we have a number n. We have to draw a diamond pattern with asterisks with 2n-1 lines. First 1 to n lines contain 1 to n number of asterisks, and next they are decreasing from n-1 to 1.So, if the input is like n = 5, then the output will be * * * * * * * * * * * * * * * * * * * * * * * * *To solve this, we will follow these steps −for i in range ... Read More
Suppose we have a string s. We have to check whether the string contains the following or not.NumbersLowercase lettersUppercase lettersNote − There may be some other symbols, but these three must be thereSo, if the input is like s = "p25KDs", then the output will be TrueTo solve this, we will follow these steps −arr := an array of size 3 and fill with Falsefor each character c in s, doif c is alphanumeric, thenarr[0] := Trueif c is in lowercase, thenarr[1] := Trueif c is in uppercase, thenarr[2] := Truereturn true when all items of arr are trueExampleLet us ... Read More
Suppose we have a string s and a substring t. We have to count how many times t occurs in s.So, if the input is like s = "abaabcaabababaab", t = "aab", then the output will be 3 because the substrings are ab(aab)c(aab)abab(aab).To solve this, we will follow these steps −cnt := 0for i in range 0 to (size of s - size of t), doif substring of s[from index i to i + size of t - 1] is same as t, thencnt := cnt + 1return cntExampleLet us see the following implementation to get better understandingdef solve(s, t): ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP