Skip to content

Commit 7048cea

Browse files
committed
2 parents 490fcf5 + a512a58 commit 7048cea

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

Sorting/Selection_Sort.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def selectionSort(lst):
2+
for i in range(0, len(lst) - 1):
3+
minIndex = i
4+
for j in range(i + 1, len(lst)):
5+
if lst[j] < lst[minIndex]:
6+
minIndex = j
7+
if minIndex != i:
8+
lst[minIndex], lst[i] = lst[i], lst[minIndex]
9+
10+
11+
def main():
12+
lst = [4,1,2,8,7,2,6]
13+
print('Sorted List: ',end='')
14+
selectionSort(lst)
15+
print(lst)
16+
17+
if __name__ == '__main__':
18+
main()

String/print_transpose.java

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.util.Scanner;
2+
3+
public class print_transpose {
4+
static Scanner sc = new Scanner(System.in);
5+
void print_matrix(char[][] matrix){
6+
for (char[] C: matrix){
7+
for (char c: C){
8+
System.out.print(c + "\t");
9+
}
10+
System.out.println();
11+
}
12+
}
13+
14+
void print_matrix(char[][] matrix, int r, int c){
15+
for (int i=0; i<c; i++){
16+
for (int j=0; j<r; j++){
17+
if (i<matrix[j].length)
18+
System.out.print(matrix[j][i] + " ");
19+
else
20+
System.out.print(" ");
21+
}
22+
System.out.println();
23+
}
24+
}
25+
int find_longestWord(String sentence){
26+
int max_len = 0;
27+
for (String word : sentence.split(" ")) {
28+
if (word.length() > max_len) {
29+
max_len = word.length();
30+
}
31+
}
32+
return max_len;
33+
}
34+
35+
char[][] str_to_matrix(String sentence, char[][] matrix){
36+
int row=0, col=0;
37+
for (String word : sentence.split(" ")){
38+
col = 0;
39+
for (char c : word.toCharArray()){
40+
matrix[row][col] = c;
41+
col++;
42+
}
43+
row ++;
44+
}
45+
return matrix;
46+
}
47+
48+
public static void main(String[] args) {
49+
String ip_sent;
50+
System.out.print("Enter the input string:");
51+
ip_sent = sc.nextLine();
52+
print_transpose ob = new print_transpose();
53+
54+
// STEP 1. Start creating the matrix
55+
56+
int nr_words = ip_sent.split(" ").length; // number of words in the input sentence
57+
int longest_word_len = ob.find_longestWord(ip_sent);
58+
System.out.printf("Longest word length: %d\n", longest_word_len);
59+
char[][] matrix = new char[nr_words][longest_word_len];
60+
// ob.print_matrix(matrix);
61+
matrix = ob.str_to_matrix(ip_sent, matrix);
62+
63+
// STEP 2: Print the matrix in the transposed form
64+
ob.print_matrix(matrix, nr_words, longest_word_len);
65+
66+
System.out.println("\n----------Done---------");
67+
}
68+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Problem statement: https://www.codechef.com/LRNDSA01/problems/CARVANS
3+
"""
4+
5+
def calculate(n, max_speeds):
6+
max_speed_count = 0
7+
lowest_so_far = -1
8+
for speed in max_speeds:
9+
if lowest_so_far == -1:
10+
lowest_so_far = speed
11+
max_speed_count += 1
12+
continue
13+
if speed <= lowest_so_far: # cars having speed eqauls to or lesser than the lowest speed so far, are moving at their max speed.
14+
lowest_so_far = speed
15+
max_speed_count += 1
16+
return max_speed_count
17+
18+
19+
if __name__ == '__main__':
20+
T = int(input()) # input number of test cases.
21+
22+
for i in range(T):
23+
N = int(input()) # enter number of cars
24+
max_car_speeds = list(map(int, input().split()))
25+
res = calculate(N, max_car_speeds)
26+
print(res)
27+
28+
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if __name__ == '__main__':
2+
T = int(input()) # enter test cases
3+
for i in range(T):
4+
G = int(input()) # no. of games played
5+
for j in range(G):
6+
i,n,q = list(map(int, input().split())) # 3 space separated integers

0 commit comments

Comments
 (0)