Funny String

Problem Statement

Suppose you have a string S which has length N and is indexed from 0 to N1. String R is the reverse of the string S. The string S is funny if the condition |SiSi1|=|RiRi1| is true for every i from 1 to N1.

(Note: Given a string str, stri denotes the ascii value of the ith character (0-indexed) of str. |x| denotes the absolute value of an integer x)

Input Format

First line of the input will contain an integer T. T testcases follow. Each of the next T lines contains one string S.

Constraints

  • 1<=T<=10
  • 2<=length of S<=10000

Output Format

For each string, print Funny or Not Funny in separate lines.

Sample Input

2
acxz
bcxz

Sample Output

Funny
Not Funny

Explanation

Consider the 1st testcase acxz

here

|c-a| = |x-z| = 2
|x-c| = |c-x| = 21
|z-x| = |a-c| = 2

Hence Funny.

Consider the 2nd testcase bcxz

here

|c-b| != |x-z|

Hence Not Funny.

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    int t,count;
    string s,r;
    cin>>t;
    while(t--)
    {
        count=0;
        r.clear();
        s.clear();
        cin>>s;
        r=s;
        reverse(r.begin(),r.end());
        for(int i=1;i<s.length();i++)
        {
            if(abs(s[i]-s[i-1])==abs(r[i]-r[i-1]))
            {
                count++;
            }
        }
        if(count==(s.length()-1))
            cout<<"Funny"<<endl;
        else cout<<"Not Funny"<<endl;
    }
    return 0;
}


Pangrams

Problem Statement

Roy wanted to increase his typing speed for programming contests. So, his friend advised him to type the sentence “The quick brown fox jumps over the lazy dog” repeatedly, because it is a pangram. (Pangrams are sentences constructed by using every letter of the alphabet at least once.)

After typing the sentence several times, Roy became bored with it. So he started to look for other pangrams.

Given a sentence s, tell Roy if it is a pangram or not.

Input Format Input consists of a line containing s.

Constraints
Length of s can be at most 103 (1|s|103) and it may contain spaces, lower case and upper case letters. Lower case and upper case instances of a letter are considered the same.

Output Format Output a line containing pangram if s is a pangram, otherwise output not pangram.

Sample Input #1

We promptly judged antique ivory buckles for the next prize    

Sample Output #1

pangram

Sample Input #2

We promptly judged antique ivory buckles for the prize    

Sample Output #2

not pangram

Explanation
In the first test case, the answer is pangram because the sentence contains all the letters of the English alphabet.

Solution in Python:

  
# Enter your code here. Read input from STDIN. Print output to STDOUT
print 'pangram' if set(raw_input().lower().replace(' ', '')) == set('abcdefghijklmnopqrstuvwxyz') else 'not pangram'

StringStream

Problem Statement

stringstream is a stream class to operate on strings. It basically implements input/output operations on memory (string) based streams. stringstream can be helpful in different type of parsing. The following operators/functions are commonly used here

  • Operator >> Extracts formatted data.
  • Operator << Inserts formatted data.
  • Method str() Gets the contents of underlying string device object.
  • Method str(string) Sets the contents of underlying string device object.

Its header file is sstream.

One common use of this class is to parse comma-separated integers from a string (e.g., “23,4,56”).

stringstream ss("23,4,56");
char ch;
int a, b, c;
ss >> a >> ch >> b >> ch >> c;  // a = 23, b = 4, c = 56

You have to complete the function vector parseInts(string str). str will be a string consisting of comma-separated integers, and you have to return a vector of int representing the integers.

Input Format

The first and only line consists of n comma-separated integers.

Output Format

Print the integers after parsing it.

P.S.: I/O will be automatically handled. You need to complete the function only.

Sample Input

23,4,56

Sample Output

23
4
56

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <sstream>
using namespace std;

vector parseInts(string str) {
   // Complete this function
    stringstream ss(str);
    char ch;
    int temp;
    vector res;
    while(ss>>temp)
    { 
        res.push_back(temp);  
        if (ss.peek() == ',')
        ss.ignore();
    }
    return res;
     
}

int main() {
    string str;
    cin >> str;
    vector integers = parseInts(str);
    for(int i = 0; i < integers.size(); i++) {
        cout << integers[i] << "\n";
    }
    
    return 0;
}


Strings

Problem Statement

C++ provides a nice alternative data type to manipulate strings, and the data type is conveniently called string. Some of its widely used features are the following:

  • Declaration:
    string a = "abc";
    
  • Size:
    int len = a.size();
    
  • Concatenate two strings:
    string a = "abc";
    string b = "def";
    string c = a + b; // c = "abcdef".
    
  • Assessing ith element:
    string s = "abc";
    char   c0 = s[0];   // c0 = 'a'
    char   c1 = s[1];   // c1 = 'b'
    char   c2 = s[2];   // c2 = 'c'
    
    s[0] = 'z';         // s = "zbc"
    

P.S.: We will use cin/cout to read/write a string.

Input Format

You are given two strings, a and b, separated by a new line. Each string will consist of lower case Latin characters (‘a’-‘z’).

Output Format

In the first line print two space-separated integers, representing the length of a and b respectively.
In the second line print the string produced by concatenating a and b (a+b).
In the third line print two space-separated strings, a and b. a and b are the same as a and b, respectively, except that their first characters are swapped.

Sample Input

abcd
ef

Sample Output

4 2
abcdef
ebcd af

Explanation

  • a=abcd
  • b=ef
  • |a|=4
  • |b|=2
  • a+b=abcdef
  • a=ebcd
  • b=af

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main() {
   // Complete the program
    string a,b;
    cin>>a>>b;
    cout<<a.size()<<" "<<b.size()<<endl;
    cout<<a+b<<endl;
    char t=a[0];
    a[0]=b[0];
    b[0]=t;
    cout<<a<<" "<<b;
    return 0;
}