Skip to main content

· 2 min read
Kaustubh Kulkarni

Statement > Suppose you have an array of numbers and you have to create a function that will accept 2 parameters an array and rank, you have to print the highest rank number from the array to know more read the example below.

Example :

Example input
input
arr=array(10,25,55,66,48,33,22,13,98,95,96)
rank=2
Then Output should be 2nd highest element of array i.e. 96

Example 2>

Example input 2
input
arr=array(10,25,55,66,48,33,22,13,98,95,96)
rank=3
Then Output should be 3rd highest element of array i.e. 96

Important : First, try your own logic & then see answers, if you think you have better logic than the article please drop a comment, we'll publish your code

Logic >

Basically, we have to find the nth highest element from the given array, We will use a simple method, Just sort the array in descending order and call print n-1th element (as array starts from index 0)

If you get similar question with another logic like find nth smallest element from the given array, then just sort array in ascending order

please read programs below for reference.

Program in php
<?php
function getByRank($arr,$place)
{
rsort($arr);
echo $arr[$place-1];
}
$arr = array(1,2,5,97,9,55,78,55,93,98);
getByRank($arr,2);
?>

Code in Python >

Program in Python
def getByRank(arr,pos):
arr.sort(reverse=True)
print(arr[pos-1])
arr=[12,15,96,98,55,65,33,22]
getByRank(arr,3)

java Code >

Program in java
import java.util.Collections;
import java.util.Arrays;
public class HelloWorld{
public static void main(String []args){
int arr[]={33,3,4,5};
int pos=2;
Arrays.sort(arr, Collections.reverseOrder());
System.out.println(arr[pos-1]);
}
}

· One min read
Kaustubh Kulkarni

Given a matrix, find the transpose of a matrix.

input
input 
2
4
1 2 3 4
2 9 -1 2

Where,

First-line represents the number of rows as M. Second-line represents the number of columns as N. The third line contains matrix elements of 1st row and so on.

output
Output 
1 2
2 9
3 -1
4 2

Program >

Transponse.java
// DriverMain.java
import java.util.*;
public class DriverMain {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int r = s.nextInt();
int c = s.nextInt();
int first[][] = new int[r][c];
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++){
first[i][j] = s.nextInt();
}}
for(int i = 0; i < c; i++)
{
for(int j = 0; j < r; j++)
{
System.out.print(first[j][i]+" ");
}
System.out.println(" ");
}
}
}

Output >

output
input

2
4
1 2 3 4
2 9 -1 2
Output

1 2
2 9
3 -1
4 2
Expected Output

1 2
2 9
3 -1
4 2

· One min read
Kaustubh Kulkarni

Que > Write A Python Program To Create File And Write 1 To 100 Numbers, print each number on new line

Program >

write-numbers.py
def writeNumbers(F):
temp=""
for i in range(1,101):
temp+=str(i)+"\n"
F.write(temp)
f=open('text.txt','w')
writeNumbers(f)
cmd
$ python write-numbers.py 
output.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

· One min read
Kaustubh Kulkarni

Que > Write a program to read Name and age of person from file named file.txt' and print if person is eligible for voting or not.

file.txt
Your Name,24
Program.py
def checkAge(name,age):
if (int(age)>18):
print(name+" is eligible for voting")
else:
print(name+" is not eligible for voting")
f=open("file.txt")
content=f.read()
data=content.split(",")
checkAge(data[0],data[1])

Output > Output

· One min read
Kaustubh Kulkarni

Code >

file.py
import matplotlib.pyplot as plt
import matplotlib.image as img
testImage = img.imread('https://kaustubhk24.netlify.app/imgs/wp-content/uploads/2021/05/python-programming-language-1.png')
plt.imshow(testImage)
print(testImage)

Output >

Output
[[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
...
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
...
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
...
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
...
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
...
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
...
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
...
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]]

Python

· One min read
Kaustubh Kulkarni

Code >

file.vb
import pandas as pd
data = {'Name': ['Snake', 'Prince', 'Gangsta', 'Wolf'],
'City':['Pune','Nasik','Kolhapur','Bangalore'],
'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}
df = pd.DataFrame(data)
print("Old:\n",df)
df.insert(1, "Age", [21, 23, 24, 21], True)

Output > (OLD)

Output

**Old:**
Name City Qualification
Snake Pune Msc
Prince Nasik MA
Gangsta Kolhapur Msc
Wolf Bangalore Msc

New > (OLD)

NameAgeCityQualification
Snake21PuneMsc
Prince23NasikMA
Gangsta24KolhapurMsc
Wolf21BangaloreMsc

· One min read
Kaustubh Kulkarni

Code >

file.py
import pandas as pd
data = [['Python','Basics',5], ['Python', 'OOPs',6], ['Python','Exception Handling',2] ,
['Python', 'Database',4],
['Python', 'REgular Expressions',3],
['Python', 'Data Analysis',6] ]
df = pd.DataFrame(data, columns = ['Category', 'Name','No of lecture'])
print(df )

Output >

Output
Category              Name            No of lecture
Python Basics 5
Python OOPs 6
Python Exception Handling 2
Python Database 4
Python REgular Expressions 3
Python Data Analysis 6

· One min read
Kaustubh Kulkarni

Code >

file.vb
import pandas as pd
data = [{'401': 'Python', '402': 'ISSA', '403': 'OT','404':'EAF','405':'KRAI'},
{'401':'Programming','402':'Security','403':'Maths','404':'Architecture','405':'AI'}]
dbs= pd.DataFrame(data, index =['subject','Description'])
print (dbs, "\n")

Output>

Output
 401 402 403 404 405
subject Python ISSA OT EAF KRAI
Description Programming Security Maths Architecture AI

· One min read
Kaustubh Kulkarni

Code >

file.vb
import numpy as np
a = np.ones(3, dtype = int)
print("Matrix a:", a)
b = np.ones([4, 4], dtype = int)
print("Matrix b:", b)

Output >

Output
import numpy as np
a = np.ones(3, dtype = int)
print("Matrix a:", a)
b = np.ones([4, 4], dtype = int)
print("Matrix b:", b)