Sort strings by sum of ascii codes, histogram of character frequencies

Note: Please Make Sure you include in your solution the following;

Ø The Programming language you selected and why

Ø Show and attach All The codes for each work.

Ø Any thoughts/ Constraints.

Languages to be used include: C, C++, JavaScript, JQuery, PHP, Java, Android and IOS.

Question 1: ASCII Values Of Company A

At Company A we all share a set of common values; some of us have unusual ways of prioritizing them.

Below is a list of Company A values:

 Relentless Commitment to Impact

 Robust as Fudge

 Be Surprisingly Bold

 Get Back Up

 Make it Happen

 Don’t be a jerk

 Confront Grey

 Laugh together

 Grow together.

Write a program in the language of your choice that sort these values by decreasing sum of the ascii codes of the characters forming them.

Example: The sum of the ascii code of the characters forming “Company A, every person counts” is 2995.

Question 2:

There is a lot of hype around machine learning these days. But we are yet to find out a way to teach a machine from a dataset of only 3 examples.

Here are a few pairs of input/output. Figure out what the corresponding algorithm is, and implement it in the language of your choice. Assume that the input is fed to your program by the standard input and simple print the output. Question 3:

Using any programing language of your choice generate a histogram of the frequency of alpha numerical characters in the sample resume attached. The formatting is not important.

Output example:

0:…………

1:……………

2:…………..

3:…………….

4:…………………

5:……………..

6:………………….

7:……………………

8:……………………..

a:……………………………….

b:………………………………

c:………………………………

d:…………………………..

e:…………………………………

f:…………………………………

g:…………………………………..

h:…………………………………

i:………………………………………….

J:……………………………………………

K:………………………………………

I:……………………………………………

Till Y:………………….

Solution

C++ is selected as the preferred programming language here,mainly because of the following reasons.

– Access to the internals of data-types

– Concise code with STL (Standard Template Library) usage

– Near-native performance, in comparison with C

– Easy Console and File IO manipulation

– Reduces code duplication with object oriented principles

q1.cpp

#include <algorithm>

#include <cctype>

#include <iostream>

#include <vector>

using namespace std;

int asciisum(string& s)

{

int sum = 0;

for (const char c : s) {

sum += (int)c;

}

return sum;

}

int main()

{

int n;

cin >> n;

cin.ignore(256, ‘\n’);

vector<string> vs;

while (n–) {

string s;

getline(cin, s);

vs.push_back(s);

cout << s << ” has sum = ” << asciisum(s) << endl;

}

sort(vs.begin(), vs.end(), [](string& a, string& b) {

return asciisum(a) >= asciisum(b);

});

cout << endl

<< “Values sorted by decreasing order of ascii sum” << endl;

for (const string& s : vs)

cout << s << endl;

}

q2.cpp

#include <iostream>

using namespace std;

int main()

{

int ncases;

cin >> ncases;

for (int k = 0; k < ncases; ++k) {

int n;

cin >> n;

int a[n][n];

for (int i = 0; i < n; ++i)

for (int j = 0; j < n; ++j)

cin >> a[i][j];

// compute algorithm

// Sum of all elements in both diagonals of the matrix is what we need

int sum = 0;

for (int i = 0; i < n; ++i)

sum += a[i][i] + a[n – i – 1][i];

sum -= (n % 2 == 1) ? (a[n / 2][n / 2]) : 0;

cout << “Result is ” << sum << endl;

}

}

q3.cpp

#include <fstream>

#include <iostream>

#include <vector>

using namespace std;

void printdot(const int count)

{

for (int i = 0; i < count; ++i)

cout << ‘.’;

}

void histogram(vector<int> v, char lo, char up)

{

for (char c = lo; c <= up; ++c) {

cout << c << ‘:’;

printdot(v[c]);

cout << endl;

}

}

int main()

{

const int N = 256;

vector<int> freq(N);

ifstream fin;

fin.open(“q3-testcase.txt”);

if (!fin)

return -1;

// calculate frequency of each character

while (!fin.eof()) {

char c = fin.get();

if (isalnum(c))

++freq[c];

}

// print histogram for

// numbers 0-9

histogram(freq, ‘0’, ‘9’);

// lower case characters

histogram(freq, ‘a’, ‘z’);

// upper case characters

histogram(freq, ‘A’, ‘Z’);

fin.close();

}