Let’s Code in Python for HackerRank: Sorting Problem

You are given data in a tabular format. The data contains  rows, and each row contains  space separated elements.
You can imagine the  items to be different attributes, (like height, weight, energy, etc.) and each of the  rows as an instance or a sample.
Your task is to sort the table on the th attribute and print the final resulting table.
Note: If two attributes are the same for different rows, print the row that appeared first in the input.
Input Format
The first line contains  and  separated by a space.
The next  lines each contain  elements.
The last line contains .
Output Format
Print the  lines of the sorted table. Each line should contain the space separated elements. Check the sample below for clarity.
Sample Input
5 3
10 2 5
7 1 0
9 9 9
1 23 12
6 5 9
1
Sample Output
7 1 0
10 2 5
6 5 9
9 9 9
1 23 12
#!/bin/usr
# Coded by @VK_Intel for HackerRan

N, M = map(int, input().split(" "))
rows = [input() for i in range(N)]
K = int(input())

class C:
@staticmethod
def func():
for row in sorted(rows, key=lambda row: int(row.split()[K])):
print(row)
return
if __name__ == "__main__":
try:
elem = C()
elem.func()
except Error as e:
print("Error", e)
raise

Let’s Learn: C++ & Structs

Source: hackerrank.com

struct is a way to combine multiple fields to represent a composite data structure, which further lays the foundation for Object Oriented Programming. For example, we can store details related to a student in a struct consisting of his age (int), first_name (string), last_name (string) and standard (int).

struct can be represented as
struct NewType {
type1 value1;
type2 value2;
.
.
.
typeN valueN;
};
You have to create a struct, named Student, representing the student’s details, as mentioned above, and store the data of a student.
Input Format
Input will consist of four lines.
The first line will contain an integer, representing age.
The second line will contain a string, consisting of lower-case Latin characters (‘a’-‘z’), representing the first_name of a student.
The third line will contain another string, consisting of lower-case Latin characters (‘a’-‘z’), representing the last_name of a student.
The fourth line will contain an integer, representing the standard of student.
Note: The number of characters in first_name and last_name will not exceed 50.
Output Format
Output will be of a single line, consisting of agefirst_namelast_name and standard, each separated by one white space.
P.S.: I/O will be handled by HackerRank.
Solution:
#include 
#include
#include
#include
#include
using namespace std;

struct Student{
unsigned int age;
char first_name[51];
char last_name[51];
unsigned int standard;
};

int main() {
Student st;
cin >> st.age >> st.first_name >> st.last_name >> st.standard;
cout << st.age << " " << st.first_name << " " << st.last_name << " " << st.standard;

return 0;
}


Let’s Learn: eval() in Python

The eval() expression is a very powerful built-in function of Python. It helps in evaluating an expression. The expression can be a Python statement, or a code object.
For example:
>>> eval("9 + 5")
14
>>> x = 2
>>> eval("x + 3")
5
Here, eval() can also be used to work with Python keywords or defined functions and variables. These would normally be stored as strings.
For example:
>>> type(eval("len"))


Without eval()

>>> type("len")

Task
You are given an expression in a line. Read that line as a string variable, such as var, and print the result using eval(var).
#Python3 Solution by VK
N = input()
class C:
@staticmethod
def m(N):
return eval(N)
if __name__ == "__main__":
try:
j = C()
j.m(N)
except IOError as e:
print("I/O error({0}): {1}".format(e.errno, e.strerror))
raise