Skip to content

Commit 21d0ced

Browse files
author
HUMANIAM
committed
solutions of ds and algo specialization
0 parents  commit 21d0ced

File tree

500 files changed

+1461246
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

500 files changed

+1461246
-0
lines changed

Algorithms Toolbox/W1/APlusB.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <iostream>
2+
3+
int main(){
4+
int a = 0;
5+
int b = 0;
6+
int sum = 0;
7+
std::cin >> a;
8+
std::cin >> b;
9+
sum = a + b;
10+
std::cout << sum;
11+
return 0;
12+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using std::vector;
6+
using std::cin;
7+
using std::cout;
8+
/*
9+
int Maxvalue(const vector<int>& numbers, int sz)
10+
{
11+
int maxindex = 0;
12+
13+
for(int i=1; i<sz; i++)
14+
if(numbers[maxindex] < numbers[i])
15+
maxindex = i;
16+
17+
return maxindex;
18+
}
19+
20+
long long MaxPairwiseProduct( vector<int>& numbers)
21+
{
22+
int n = numbers.size();
23+
24+
int maxindex1 = Maxvalue(numbers , n);
25+
int maxvalue1 = numbers[maxindex1];
26+
numbers.erase(numbers.begin() + maxindex1);
27+
28+
int maxindex2 = Maxvalue(numbers , n-1);
29+
int maxvalue2 = numbers[maxindex2];
30+
31+
return (1LL * maxvalue1 * maxvalue2);
32+
}
33+
*/
34+
35+
long long MaxPairwiseProduct(const vector<int>& numbers) {
36+
int n = numbers.size();
37+
38+
int max_index1 = 0;
39+
for (int i = 1; i < n; ++i)
40+
if ((numbers[i] > numbers[max_index1]))
41+
max_index1 = i;
42+
43+
int max_index2 = -1;
44+
for (int j = 0; j < n; ++j)
45+
if ((numbers[j] != numbers[max_index1]) && ((max_index2 == -1) || (numbers[j] > numbers[max_index2])))
46+
max_index2 = j;
47+
48+
return ((long long)(numbers[max_index1])) * numbers[max_index2];
49+
}
50+
int main() {
51+
52+
int n;
53+
cin >> n;
54+
vector<int> numbers(n);
55+
for (int i = 0; i < n; ++i) {
56+
cin >> numbers[i];
57+
}
58+
59+
long long result = MaxPairwiseProduct(numbers);
60+
cout << result << "\n";
61+
62+
63+
return 0;
64+
}
1.01 MB
Binary file not shown.
13.1 KB
Binary file not shown.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <cstdlib>
2+
#include <iostream>
3+
#include <vector>
4+
5+
using std::vector;
6+
using std::cin;
7+
using std::cout;
8+
9+
long long MaxPairwiseProduct(const vector<int>& numbers) {
10+
long long result = 0;
11+
int n = numbers.size();
12+
for (int i = 0; i < n; ++i) {
13+
for (int j = i + 1; j < n; ++j) {
14+
if (((long long)(numbers[i])) * numbers[j] > result) {
15+
result = ((long long)(numbers[i])) * numbers[j];
16+
}
17+
}
18+
}
19+
return result;
20+
}
21+
22+
long long MaxPairwiseProductFast(const vector<int>& numbers) {
23+
int n = numbers.size();
24+
25+
int max_index1 = 0;
26+
for (int i = 1; i < n; ++i)
27+
if (numbers[i] > numbers[max_index1])
28+
max_index1 = i;
29+
30+
int max_index2 = -1;
31+
for (int j = 0; j < n; ++j)
32+
if ((j != max_index1) && ((max_index2 == -1) || (numbers[j] > numbers[max_index2])))
33+
{
34+
max_index2 = j;
35+
36+
}
37+
return ((long long)(numbers[max_index1])) * numbers[max_index2];
38+
}
39+
40+
int main() {
41+
while (true) {
42+
int n = rand() % 1000 + 2;
43+
cout << n << "\n";
44+
vector<int> a;
45+
for (int i = 0; i < n; ++i) {
46+
a.push_back(rand() % 100000);
47+
}
48+
for (int i = 0; i < n; ++i) {
49+
cout << a[i] << ' ';
50+
}
51+
cout << "\n";
52+
long long res1 = MaxPairwiseProduct(a);
53+
long long res2 = MaxPairwiseProductFast(a);
54+
if (res1 != res2) {
55+
cout << "Wrong answer: " << res1 << ' ' << res2 << "\n";
56+
break;
57+
}
58+
else {
59+
cout << "OK\n";
60+
}
61+
}
62+
63+
int n;
64+
cin >> n;
65+
vector<int> numbers(n);
66+
67+
for (int i = 0; i < n; ++i) {
68+
cin >> numbers[i];
69+
}
70+
71+
72+
long long result = MaxPairwiseProductFast(numbers);
73+
long long result1 = MaxPairwiseProduct(numbers);
74+
cout << result << "\n";
75+
cout << result1<< "\n";
76+
return 0;
77+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.Scanner;
2+
3+
public class Fibonacci {
4+
private static long calc_fib(int n) {
5+
if (n <= 1)
6+
return n;
7+
8+
return calc_fib(n - 1) + calc_fib(n - 2);
9+
}
10+
11+
public static void main(String args[]) {
12+
Scanner in = new Scanner(System.in);
13+
int n = in.nextInt();
14+
15+
System.out.println(calc_fib(n));
16+
}
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env ruby
2+
# by Andronik Ordian
3+
4+
def calc_fib(n)
5+
return n if n <= 1
6+
# slow
7+
# fix me
8+
calc_fib(n - 1) + calc_fib(n - 2)
9+
end
10+
11+
if __FILE__ == $0
12+
n = gets.to_i
13+
puts "#{calc_fib(n)}"
14+
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <iostream>
2+
#include <cassert>
3+
4+
// The following code calls a naive algorithm for computing a Fibonacci number.
5+
//
6+
// What to do:
7+
// 1. Compile the following code and run it on an input "40" to check that it is slow.
8+
// You may also want to submit it to the grader to ensure that it gets the "time limit exceeded" message.
9+
// 2. Implement the fibonacci_fast procedure.
10+
// 3. Remove the line that prints the result of the naive algorithm, comment the lines reading the input,
11+
// uncomment the line with a call to test_solution, compile the program, and run it.
12+
// This will ensure that your efficient algorithm returns the same as the naive one for small values of n.
13+
// 4. If test_solution() reveals a bug in your implementation, debug it, fix it, and repeat step 3.
14+
// 5. Remove the call to test_solution, uncomment the line with a call to fibonacci_fast (and the lines reading the input),
15+
// and submit it to the grader.
16+
17+
int fibonacci_naive(int n) {
18+
if (n <= 1)
19+
return n;
20+
21+
return fibonacci_naive(n - 1) + fibonacci_naive(n - 2);
22+
}
23+
24+
int fibonacci_fast(int n) {
25+
// write your code here
26+
if (n <= 1)
27+
return n;
28+
29+
int f1=0, f2=1, temp;
30+
31+
for(int i=2; i<n; i++)
32+
{
33+
temp = f2;
34+
f2 = f1+f2;
35+
f1=temp;
36+
37+
}
38+
return f1+f2;
39+
}
40+
41+
void test_solution() {
42+
assert(fibonacci_fast(3) == 2);
43+
assert(fibonacci_fast(10) == 55);
44+
for (int n = 0; n < 20; ++n)
45+
assert(fibonacci_fast(n) == fibonacci_naive(n));
46+
}
47+
48+
int main() {
49+
int n = 0;
50+
51+
std::cin >> n;
52+
53+
std::cout << fibonacci_fast(n) << '\n';
54+
55+
return 0;
56+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
-- by Kirill Elagin
2+
3+
import Control.Exception (assert)
4+
5+
-- The following code calls a naive algorithm for computing a Fibonacci number.
6+
--
7+
-- What to do:
8+
-- 1. Compile the following code and run it on an input "40" to check that it is slow.
9+
-- You may also want to submit it to the grader to ensure that it gets the "time limit exceeded" message.
10+
-- 2. Implement the fibonacci_fast procedure.
11+
-- 3. Remove the line that prints the result of the naive algorithm, comment the lines reading the input,
12+
-- uncomment the line with a call to test_solution, compile the program, and run it.
13+
-- This will ensure that your efficient algorithm returns the same as the naive one for small values of n.
14+
-- 4. If test_solution reveals a bug in your implementation, debug it, fix it, and repeat step 3.
15+
-- 5. Remove the call to test_solution, uncomment the line with a call to fibonacci_fast (and the lines reading the input),
16+
-- and submit it to the grader.
17+
18+
fibonacci_naive :: Int -> Int
19+
fibonacci_naive 0 = 0
20+
fibonacci_naive 1 = 1
21+
fibonacci_naive n = fibonacci_naive (n - 1) + fibonacci_naive (n - 2)
22+
23+
fibonacci_fast :: Int -> Int
24+
fibonacci_fast n = 0 -- write your code here
25+
26+
test_solution :: IO ()
27+
test_solution = and tests `seq` return ()
28+
where
29+
tests =
30+
[ assert (fibonacci_fast 3 == 2) True
31+
, assert (fibonacci_fast 10 == 55) True
32+
] ++ map (\i -> assert (fibonacci_fast i == fibonacci_naive i) True) [0..20]
33+
34+
main :: IO ()
35+
main = do
36+
[w] <- fmap words getLine
37+
let n = read w
38+
39+
print $ fibonacci_naive n
40+
--test_solution
41+
--print $ fibonacci_fast n
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Uses python3
2+
def calc_fib(n):
3+
if (n <= 1):
4+
return n
5+
6+
return calc_fib(n - 1) + calc_fib(n - 2)
7+
8+
n = int(input())
9+
print(calc_fib(n))

0 commit comments

Comments
 (0)