0% found this document useful (0 votes)
2 views

Code

The document contains a series of programming tasks that require writing functions or programs to solve various problems, such as calculating probabilities, summing diagonal elements of a matrix, removing vowels from a string, checking for matching parentheses, finding prime numbers, converting numbers to a specific format, sorting sentences based on word occurrences, and more. Each question includes example inputs and outputs to illustrate the expected functionality. The tasks cover a range of programming concepts and algorithms suitable for practice.

Uploaded by

2021062023
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Code

The document contains a series of programming tasks that require writing functions or programs to solve various problems, such as calculating probabilities, summing diagonal elements of a matrix, removing vowels from a string, checking for matching parentheses, finding prime numbers, converting numbers to a specific format, sorting sentences based on word occurrences, and more. Each question includes example inputs and outputs to illustrate the expected functionality. The tasks cover a range of programming concepts and algorithms suitable for practice.

Uploaded by

2021062023
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Question 1

Your task is to complete a function “count_heads()” that takes


two inputs N and R. The function should return the probability of
getting exactly R heads on N successive tosses of a fair coin. A
fair coin has an equal probability of landing a head or a tail (i.e.
0.5) on each toss.
Example 1
e Input: 11
» Output: 0.500000
Example 2
Input: 4 3
Output: 0.250000
Question 2
Write a program that will print the sum of diagonal elements of a
10X10 matrix. The program will take a total of 100 numbers as
input (10 numbers will be input per line and each number will be
separated by a space).
Example 1
input:
1234567890
0123456780
3456789640
2345678932
3456743213
3456244246
2346246235
2356246235
2462143352
3352462146

Output: 42
Example 2
Input:
12233445566 778899100
10018877 665544332211
8888 1665544332211100
88776614433221110099
77 66 5544 122 118899 100
66 5544 33221778899 100
4433221110099 177 66 55
332211100998877 15544
2211100998877 66 551 33
10011 223344559988771
Output: 10
Question 3
Write a program that will take one string as input. The program
will then remove vowels a, €, i, 0, and u (in lower or upper case)
from the string. If there are two or more vowels that occur
together then the program shall ignore all of those vowels.
Example 1
e Input: Cat
e Output: Ct
Example 2
Input: Compuuter
o Output: Cmpuutr
Question 4
Write a program that will take a string as input. The program will
then determine whether each left parenthesis ‘(" has a matching
right parenthesis *)’ and also all the )" has a consecutive ‘(". If so,
the program will print 0 else the program will print 1.
Example 1
« Input: HELLO AND (WELCOME (TO THE) TCEA
(CONTEST) TODAY)IS (SATURDAY())
e Output: 0
Example 2
« Input: (9%(7-2)*(1*5)
e Output: 0
Question 5
Write a program to find out and display prime numbers from the
given list of integers. The program will accept input in two lines.
First-line contains a number indicating the total number of
integers in the list and the second line contains integers
separated by spaces.
Example 1
Input: 5
46937
e Output: 37
Example 2
e Input: 10
81031271511217 26
e Output: 3711217
Question 6
Write a program that will take a number as input. The program
will convert the inputted number to a format, which will contain
an integer part and a fraction part. The fraction part needs to be
reduced to its lowest representation. The input will not contain
any repeating decimals e.g. 1.3333...33. The output will be :
The integer part of the number ++fraction using a’/’
Example 1
Input: 2.95
» Output: 219/20
Example 2
e Input: 3.08
» Output: 32/2
Question 7
Given a sentence with numbers representing a word's location in
the sentence, embedded within each word, and return the sorted
sentence.
Note: We are using a maximum of 0-9 numbers only for 1
sentence
Example 1
e Input: is1 ThiOs T3est 2a
o Output: This is a Test
Example 2
« Input: t20 j3oin 4WonderBiz I0 Technolog5ies want
» Output: | want to join WonderBiz Technologies
Question 8
Write a program that takes an integer M and M integer array
elements as input. The program needs to find the minimum
difference between two elements in the integer array. The
program then needs to print all those pairs of elements that have
the minimum difference. If more than one pair has the minimum
difference, then the program should print the output in the
ascending order, if an element exists in two or more pairs, then it
should be printed two times or more.
Example 1
e Input: 4
55443322
» Output: 22 33 33 44 44 55
« Explanation: The minimum difference between two elements
is 11. Hence the pairs are printed in the ascending order. Here
33 and 44 appear in two different pairs; hence both are printed
twice.
Example 2
e Input: 5
199 22 441001
e Output: 122
Question 9
Write a program to convert a number to binary format.
Example 1
e Input: 0
e Output: 0
Example 2
Input: 1
e Output: 1

Question 10
Write a program that receives a word A and some texts as input.
You need to output the texts (without modifying them) in the
ascending order of the number of occurrences of the word A in
the texts. The input is as follows: an integer M(between 1 and
100, inclusive), followed by the word A in the next line, and some
text in each of the M next lines.
Note: The texts and the word A contain only lowercase Latin
“ww
letters (a,b,c...,z) and blank spaces (“”). The maximum size of the
texts and the word A is 100 Characters. Every text has a different
number of occurrences of the word A.
Note 2:you must print one text per line without modifying the
texts.
Example 1
e Input: 2
Java
| hate java
Python is a good programming language
» Output: Python is a good programming language
| hate java
Example 2
Input: 3
python
| like to code in python
python is named after a show name monty python and
not after the snake python
| think python is good i think python is important than
php
» Output: i like to code in python
i think python is good i think python is important than
php
python is named after a show name monty python and
not after the snake python

Question 1

Your task is to complete a function “count_heads()” that takes


two inputs N and R. The function should return the probability of
getting exactly R heads on N successive tosses of a fair coin. A
fair coin has an equal probability of landing a head or a tail (i.e.
0.5) on each toss.

Example 1

e Input: 11
» Output: 0.500000

Example 2

e Input: 4 3
» Output: 0.250000

Question 2

Write a program that will print the sum of diagonal elements of a


10X10 matrix. The program will take a total of 100 numbers as
input (10 numbers will be input per line and each number will be
separated by a space).

Example 1

elnput: 1234567890
0123456780
3456789640
2345678932
3456743213
3456244246
2346246235
2356246235
2462143352
3352462146

o Output: 42

Example 2

Input: 12233445566 778899100


10018877 665544332211
8888 1665544332211100
88776614433221110099
77 66 5544 122 118899 100
66 5544 33221778899 100
4433221110099 177 66 55
332211100998877 15544
2211100998877 66 551 33
10011 223344559988771

e Output: 10

Question 3

Write a program that will take one string as input. The program
will then remove vowels a, €, i, 0, and u (in lower or upper case)
from the string. If there are two or more vowels that occur
together then the program shall ignore all of those vowels.

Example 1

e Input: Cat
e Output: Ct

Example 2

Input: Compuuter
o Output: Cmpuutr

Question 4

Write a program that will take a string as input. The program will

then determine whether each left parenthesis ‘(" has a matching

right parenthesis *)’ and also all the )" has a consecutive ‘(". If so,
the program will print 0 else the program will print 1.

Example 1

« Input: HELLO AND (WELCOME (TO THE) TCEA


(CONTEST) TODAY)IS (SATURDAY())
e Output: 0

Example 2

« Input: (9%(7-2)*(1*5)
e Output: 0

Question 5

Write a program to find out and display prime numbers from the
given list of integers. The program will accept input in two lines.
First-line contains a number indicating the total number of
integers in the list and the second line contains integers
separated by spaces.

Example 1

Input: 5
46937
e Output: 37

Example 2

e Input: 10
81031271511217 26
e Output: 3711217

Question 6

Write a program that will take a number as input. The program


will convert the inputted number to a format, which will contain
an integer part and a fraction part. The fraction part needs to be
reduced to its lowest representation. The input will not contain
any repeating decimals e.g. 1.3333...33. The output will be :

The integer part of the number ++fraction using a’/’


Example 1

Input: 2.95
» Output: 219/20

Example 2

e Input: 3.08
» Output: 32/25

Question 7

Given a sentence with numbers representing a word's location in


the sentence, embedded within each word, and return the sorted
sentence.

Note: We are using a maximum of 0-9 numbers only for 1


sentence

Example 1

e Input: is1 ThiOs T3est 2a


o Output: This is a Test

Example 2
« Input: t20 j3oin 4WonderBiz I0 Technolog5ies want
» Output: | want to join WonderBiz Technologies

Question 8

Write a program that takes an integer M and M integer array


elements as input. The program needs to find the minimum
difference between two elements in the integer array. The
program then needs to print all those pairs of elements that have
the minimum difference. If more than one pair has the minimum
difference, then the program should print the output in the
ascending order, if an element exists in two or more pairs, then it
should be printed two times or more.

Example 1

e Input: 4
55443322
» Output: 22 33 33 44 44 55
« Explanation: The minimum difference between two elements
is 11. Hence the pairs are printed in the ascending order. Here
33 and 44 appear in two different pairs; hence both are printed
twice.

Example 2

e Input: 5
199 22 441001
e Output: 122

Question 9

Write a program to convert a number to binary format.


Example 1

e Input: 0
e Output: 0

Example 2

Input: 1
e Output: 1

n= (input())

—nn

s=

n:
(n&1):

s+='1"'
s+='0"
n>>=1
(s[::-11)

Question 10

Write a program that receives a word A and some texts as input.


You need to output the texts (without modifying them) in the
ascending order of the number of occurrences of the word A in
the texts. The input is as follows: an integer M(between 1 and
100, inclusive), followed by the word A in the next line, and some
text in each of the M next lines.

Note: The texts and the word A contain only lowercase Latin

“ww

letters (a,b,c...,z) and blank spaces (“”). The maximum size of the
texts and the word A is 100 Characters. Every text has a different

number of occurrences of the word A.

Note 2:you must print one text per line without modifying the
texts.

Example 1

e Input: 2
Java
| hate java
Python is a good programming language
» Output: Python is a good programming language
| hate java

Example 2

eo Input: 3
python
| like to code in python
python is named after a show name monty python and
not after the snake python
| think python is good i think python is important than
php
» Output: i like to code in python
i think python is good i think python is important than
php
python is named after a show name monty python and
not after the snake python

#include<bits/stdc++.h>

S53
map<
cmp (pair< a,
pair< 9 >& b)

a.second < b.second;


sort(map< 5 >& M)

vector<pair< 0 > > A;


( & it : M) {
A.push_back (it);
}
sort(A. 0, A.end(), cmp);
( & it : A) {
( i=0;i<m2[it. first] ;i++)
cout << it.first <<endl; } } count (

(w==s) @43
C3

main()

n;getline(cin,s);n=stoi(s);

getline(cin,s);
transform(s. OPISH OPISH (),::tolower);
vector<string> v(n);
vector<int> a(n);
map< 5 > mj
( i=0;i<n;i++)
{
getline(cin,v[i]);
transform(v[i]. O),v[il. O),v[il. (),::tolower);
m2[v[i]]++;
m[v[i]l=count(v[i]);
}

sort(m)

You might also like