Competitive Programming Essentials: Problem Statement 1
Competitive Programming Essentials: Problem Statement 1
Competitive Programming Essentials: Problem Statement 1
DATE:
PROBLEM STATEMENT 1:
split and join
You are given a string. Split the string on a " " (space) delimiter and join using a - hyphen.
SOLUTION:
a=input ()
a="-”. join(a)
print(a)
Input
this is a string
Output:
this-is-a-string
DATE:
PROBLEM STATEMENT 2:
ALTERNATING CHARACTERS
'''You are given a string containing characters and only. Your task is to change it into a string such that
there are no matching adjacent characters. To do this, you are allowed to delete zero or more
characters in the string.
SOLUTION:
#include<iostream>
#include<string.h>
P1
int main()
int n,i,c;
char a[100000];
cin>>n;
while(n>0)
c=0;
cin>>a;
for(i=0;i<strlen(a);i++)
if(a[i]==a[i+1])
c++;
cout<<c<<endl;
n--;
Input:
AAAA
BBBBB
ABABABAB
BABABA
AAABBB
Output:
P2
3
DATE:
PROBLEM STATEMENT 3:
Example
n=5
SOLUTION:
a=int(input())
for i in range(1,a+1):
print(i,end="")
Sample Input:
Sample Output :
123
DATE:
PROBLEM STATEMENT 4:
4. BUBBLE SORT
P3
Given an array of integers, sort the array in ascending order using the Bubble Sort algorithm above.
Once sorted, print the following three lines:
Array is sorted in numSwaps swaps., where is the number of swaps that took place.
First Element: firstElement, where is the first element in the sorted array.
Last Element: lastElement, where is the last element in the sorted array.
Hint: To complete this challenge, you must add a variable that keeps a running tally of all swaps that
occur during execution.
SOLUTION:
#include<iostream>
int main()
int n,a[100000],i,j,temp,c=0;
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
c++;
P4
}
Input (stdin)
123
Expected Output:
Array is sorted in 0 swaps.
First Element: 1
Last Element: 3
DATE:
PROBLEM STATEMENT 5:
5. IF-ELSE
SOLUTION:
n=input()
if(n%2==0):
P5
print("Weird")
else:
print("Weird")
Sample Input:
24
Sample Output :
Not Weird
DATE:
PROBLEM STATEMENT 6:
6. DIVISION
The provided code stub reads two integers, and, from STDIN.
Add logic to print two lines. The first line should contain the result of integer division // The second
line should contain the result of float division // .
SOLUTION:
n=input();
a=input();
r=float(n)/a;
print n/a;
print r;
Sample Input 0
Sample Output 0
1
1.33333333333
P6
DATE:
PROBLEM STATEMENT 7:
7. The provided code stub reads and integer r , n , from STDIN. For all non-negative integers i<n, print
i^2.
SOLUTION:
n=input();
i=0;
while i<n:
print i*i;
i=i+1;
Sample Input
Sample Output
0
16
DATE:
PROBLEM STATEMENT 8:
8. The provided code stub reads two integers from STDIN, a and b . Add code to print three lines where:
The second line contains the difference of the two numbers (first - second).
P7
The third line contains the product of the two numbers.
SOLUTION:
n=input();
a=input();
print n+a;
print n-a;
print n*a;
Sample Input
Sample Output
5
DATE:
PROBLEM STATEMENT 9:
9.for a given input random numbers, user have to guess a number and if the number is present in the
list print RIGHT ELSE WRONG.
SOLUTION:
import random
print(i)
if(i==a):
print("right")
else:
P8
print("wrong")
INPUT:
OUTPUT:
[1, 2, 5, 1, 3, 3, 1, 9, 8, 7] RIGHT
DATE:
PROBLEM STATEMENT-10
Given an integer, , perform the following conditional actions:
#!/bin/python3
SOLUTION:
import math
import os
import random
Input (stdin)
24 Your
output:
Not Weird
P9