COMPETITIVE PROGRAMMING ESSENTIALS
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=a. split (" ")
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.
Your task is to find the minimum number of required deletions.'''
SOLUTION:
#include<iostream>
#include<string.h>
using namespace std;
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:
3. The included code stub will read an integer,n , from STDIN.
Without using any string methods, try to print the following:
Note that "" represents the consecutive values in between.
Example
n=5
Print the string .12345
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>
using namespace std;
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
}
cout<<"Array is sorted in "<< c<< " swaps.";
cout<<endl<<"First Element: "<<a[0];
cout<<endl<<"Last Element: "<<a[n-1];
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):
if((n>=2 and n<5) or n>20):
print ("Not Weird")
elif(n>=6 and n<=20):
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 // .
No rounding or formatting is necessary.
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 first line contains the sum of the two numbers.
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
i=random. randrange (10)
print(i)
a=int(input("enter the a value"))
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:
• If is odd, print Weird
• If is even and in the inclusive range of to , print Not Weird
• If is even and in the inclusive range of to , print Weird
• If is even and greater than , print Not Weird
#!/bin/python3
SOLUTION:
import math
import os
import random
import re import sys
if __name__ == '__main__': n = int(input()) if n % 2 == 1:
print("Weird") elif n % 2 == 0 and 2 <= n <= 5
: print("Not Weird") elif n % 2 == 0 and 6 <= n <= 20:
print("Weird") else: print("Not Weird")
Input (stdin)
24 Your
output:
Not Weird
P9