Skip to content

Commit

Permalink
more math
Browse files Browse the repository at this point in the history
  • Loading branch information
rene-d committed May 26, 2018
1 parent fc8e880 commit a15d59b
Show file tree
Hide file tree
Showing 27 changed files with 652 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"ratio": "cpp",
"tuple": "cpp",
"string.h": "c",
"locale": "cpp"
"locale": "cpp",
"numeric": "cpp"
},
"files.exclude": {
"**/.git": true,
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [![HackerRank](https://hrcdn.net/hackerrank/assets/brand/h_mark_sm-30dc0e0cbd2dded63b294819ff853a90.svg)](https://www.hackerrank.com) HackerRank

[![Build Status](https://travis-ci.org/rene-d/hackerrank.svg?branch=master)](https://travis-ci.org/rene-d/hackerrank) [![641 solutions and counting](https://img.shields.io/badge/Challenges-641-blue.svg)](https://www.hackerrank.com/rene_d?hr_r=1)
[![Build Status](https://travis-ci.org/rene-d/hackerrank.svg?branch=master)](https://travis-ci.org/rene-d/hackerrank) [![649 solutions and counting](https://img.shields.io/badge/Challenges-649-blue.svg)](https://www.hackerrank.com/rene_d?hr_r=1)

[HackerRank](https://www.hackerrank.com/dashboard) is a great place to learn, improve, play with your programming skills.

Expand Down
1 change: 1 addition & 0 deletions algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ add_subdirectory(search)
add_subdirectory(dynamic-programming)
add_subdirectory(bit-manipulation)
add_subdirectory(game-theory)
add_subdirectory(algo-debugging)
7 changes: 7 additions & 0 deletions algorithms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,10 @@ Name | Preview | Code | Difficulty
---- | ------- | ---- | ----------
[Game of Stones](https://www.hackerrank.com/challenges/game-of-stones-1)|A Game of Stones|[Python](game-theory/game-of-stones-1.py)|Easy

#### [Debugging](https://www.hackerrank.com/domains/algorithms/algo-debugging)

Name | Preview | Code | Difficulty
---- | ------- | ---- | ----------
[Prime Dates](https://www.hackerrank.com/challenges/prime-date)|Find the number of prime dates in a range|[C++](algo-debugging/prime-date.cpp) [Python](algo-debugging/prime-date.py)|Medium
[Zig Zag Sequence](https://www.hackerrank.com/challenges/zig-zag-sequence)|Find a zig zag sequence of the given array.|[C++](algo-debugging/zig-zag-sequence.cpp) [Python](algo-debugging/zig-zag-sequence.py)|Medium

6 changes: 6 additions & 0 deletions algorithms/algo-debugging/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_hackerrank(prime-date prime-date.cpp)
add_hackerrank_py(prime-date.py)

dirty_cpp(prime-date)
add_hackerrank_py(zig-zag-sequence.py)
add_hackerrank(zig-zag-sequence zig-zag-sequence.cpp)
13 changes: 13 additions & 0 deletions algorithms/algo-debugging/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

### [Algorithms](https://www.hackerrank.com/domains/algorithms)
The true test of problem solving: when one realizes that time and memory aren't infinite.



#### [Debugging](https://www.hackerrank.com/domains/algorithms/algo-debugging)

Name | Preview | Code | Difficulty
---- | ------- | ---- | ----------
[Prime Dates](https://www.hackerrank.com/challenges/prime-date)|Find the number of prime dates in a range|[C++](prime-date.cpp) [Python](prime-date.py)|Medium
[Zig Zag Sequence](https://www.hackerrank.com/challenges/zig-zag-sequence)|Find a zig zag sequence of the given array.|[C++](zig-zag-sequence.cpp) [Python](zig-zag-sequence.py)|Medium

84 changes: 84 additions & 0 deletions algorithms/algo-debugging/prime-date.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Algorithms > Debugging > Prime Dates
// Find the number of prime dates in a range
//
// https://www.hackerrank.com/challenges/prime-date/problem
// challenge id: 63495
//

#include <bits/stdc++.h>
using namespace std;

int month[15];

void updateLeapYear(int year) {
if(year % 400 == 0) {
month[2] = 29;
} else if(year % 100 == 0) {
month[2] = 28;
} else if(year % 4 == 0) {
month[2] = 29;
} else {
month[2] = 28;
}
}

void storeMonth() {
month[1] = 31;
month[2] = 28;
month[3] = 31;
month[4] = 30;
month[5] = 31;
month[6] = 30;
month[7] = 31;
month[8] = 31;
month[9] = 30;
month[10] = 31;
month[11] = 30;
month[12] = 31;
}

int findLuckyDates(int d1, int m1, int y1, int d2, int m2, int y2) {
storeMonth();

int result = 0;

while(true) {
int x = d1;
x = x * 100 + m1;
x = x * 10000 + y1;
if(x % 4 == 0 || x % 7 == 0) {
result = result + 1;
}
if(d1 == d2 && m1 == m2 && y1 == y2) {
break;
}
updateLeapYear(y1);
d1 = d1 + 1;
if(d1 > month[m1]) {
m1 = m1 + 1;
d1 = 1;
if(m1 > 12) {
y1 = y1 + 1;
m1 = 1;
}
}
}
return result;
}

int main() {
string str;
int d1, m1, y1, d2, m2, y2;
getline(cin, str);
for(int i = 0; i < str.size(); i++) {
if(str[i] == '-') {
str[i] = ' ';
}
}
stringstream ss;
ss << str;
ss >> d1 >> m1 >> y1 >> d2 >> m2 >> y2;

int result = findLuckyDates(d1, m1, y1, d2, m2, y2);
cout << result << endl;
}
70 changes: 70 additions & 0 deletions algorithms/algo-debugging/prime-date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Algorithms > Debugging > Prime Dates
# Find the number of prime dates in a range
#
# https://www.hackerrank.com/challenges/prime-date/problem
# challenge id: 63495
#

import re
month = []

def updateLeapYear(year):
if year % 400 == 0:
month[2] = 29
elif year % 100 == 0:
month[2] = 28
elif year % 4 == 0:
month[2] = 29
else:
month[2] = 28

def storeMonth():
month[1] = 31
month[2] = 28
month[3] = 31
month[4] = 30
month[5] = 31
month[6] = 30
month[7] = 31
month[8] = 31
month[9] = 30
month[10] = 31
month[11] = 30
month[12] = 31

def findPrimeDates(d1, m1, y1, d2, m2, y2):
storeMonth()
result = 0

while(True):
x = d1
x = x * 100 + m1
x = x * 10000 + y1
if x % 4 == 0 or x % 7 == 0:
result = result + 1
if d1 == d2 and m1 == m2 and y1 == y2:
break
updateLeapYear(y1)
d1 = d1 + 1
if d1 > month[m1]:
m1 = m1 + 1
d1 = 1
if m1 > 12:
y1 = y1 + 1
m1 = 1
return result;

for i in range(1, 15):
month.append(31)

line = input()
date = re.split('-| ', line)
d1 = int(date[0])
m1 = int(date[1])
y1 = int(date[2])
d2 = int(date[3])
m2 = int(date[4])
y2 = int(date[5])

result = findPrimeDates(d1, m1, y1, d2, m2, y2)
print(result)
44 changes: 44 additions & 0 deletions algorithms/algo-debugging/zig-zag-sequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Algorithms > Debugging > Zig Zag Sequence
// Find a zig zag sequence of the given array.
//
// https://www.hackerrank.com/challenges/zig-zag-sequence/problem
// challenge id: 63282
//

#include <bits/stdc++.h>
using namespace std;

void findZigZagSequence(vector < int > a, int n){
sort(a.begin(), a.end());
int mid = (n - 1)/2;
swap(a[mid], a[n-1]);

int st = mid + 1;
int ed = n - 2;
while(st <= ed){
swap(a[st], a[ed]);
st = st + 1;
ed = ed - 1;
}
for(int i = 0; i < n; i++){
if(i > 0) cout << " ";
cout << a[i];
}
cout << endl;
}

int main() {
int n, x;
int test_cases;
cin >> test_cases;

for(int cs = 1; cs <= test_cases; cs++){
cin >> n;
vector < int > a;
for(int i = 0; i < n; i++){
cin >> x;
a.push_back(x);
}
findZigZagSequence(a, n);
}
}
31 changes: 31 additions & 0 deletions algorithms/algo-debugging/zig-zag-sequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Algorithms > Debugging > Zig Zag Sequence
# Find a zig zag sequence of the given array.
#
# https://www.hackerrank.com/challenges/zig-zag-sequence/problem
# challenge id: 63282
#

def findZigZagSequence(a, n):
a.sort()
mid = int((n - 1)/2)
a[mid], a[n-1] = a[n-1], a[mid]

st = mid + 1
ed = n - 2
while(st <= ed):
a[st], a[ed] = a[ed], a[st]
st = st + 1
ed = ed - 1

for i in range (n):
if i == n-1:
print(a[i])
else:
print(a[i], end = ' ')
return

test_cases = int(input())
for cs in range (test_cases):
n = int(input())
a = list(map(int, input().split()))
findZigZagSequence(a, n)
2 changes: 1 addition & 1 deletion algorithms/implementation/sherlock-and-squares.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# https://www.hackerrank.com/challenges/sherlock-and-squares/problem
#


def squares(a, b):
# Complete this function

Expand All @@ -26,6 +25,7 @@ def squares(a, b):
break
return nb


if __name__ == "__main__":
q = int(input().strip())
for a0 in range(q):
Expand Down
8 changes: 7 additions & 1 deletion mathematics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,26 @@ Name | Preview | Code | Difficulty
[Breaking Sticks](https://www.hackerrank.com/challenges/breaking-sticks)|Find the length of the longest sequence of moves.|[C++](number-theory/breaking-sticks.cpp)|Medium
[Cheese and Random Toppings](https://www.hackerrank.com/challenges/cheese-and-random-toppings)|How many ways are there to choose exactly R toppings from N toppings?|[Python](number-theory/cheese-and-random-toppings.py)|Easy
[Easy GCD](https://www.hackerrank.com/challenges/easy-gcd-1)|Find the maximum number less than K such that GCD of all numbers is still more than one!|[Python](number-theory/easy-gcd-1.py)|Medium
[Satisfactory Pairs](https://www.hackerrank.com/challenges/pairs-again)|How many pairs of integers give a solution?|[C++](number-theory/pairs-again.cpp)|Hard
[Manasa loves Maths](https://www.hackerrank.com/challenges/manasa-loves-maths)|Find out if any permutation of the given number is divisible by 8.|[Python](number-theory/manasa-loves-maths.py)|Medium
[Largest Non-Coprime Submatrix](https://www.hackerrank.com/challenges/largest-coprime-submatrix)|Given a matrix find the largest coprime submatrix.|[C++](number-theory/largest-coprime-submatrix.cpp)|Hard
[John and GCD list](https://www.hackerrank.com/challenges/john-and-gcd-list)|Help John in making a list from GCD list|[Python](number-theory/john-and-gcd-list.py)|Easy
[Easy math](https://www.hackerrank.com/challenges/easy-math)|Help Johnny in figuring out the value of Y|[Python](number-theory/easy-math.py)|Medium
[Binomial Coefficients](https://www.hackerrank.com/challenges/binomial-coefficients)|Calculate how many binomial coefficients of n become 0 after modulo by P.|[Python](number-theory/binomial-coefficients.py)|Medium
[Lucy and Flowers](https://www.hackerrank.com/challenges/lucy-and-flowers)|Help Lucy's father with a computation involving flowers|[C++](number-theory/lucy-and-flowers.cpp)|Medium
[nCr](https://www.hackerrank.com/challenges/ncr)|Given n and r, in how many ways can r items be chosen from n items?|[Python](number-theory/ncr.py)|Expert

#### [Combinatorics](https://www.hackerrank.com/domains/mathematics/combinatorics)

Name | Preview | Code | Difficulty
---- | ------- | ---- | ----------
[nCr table](https://www.hackerrank.com/challenges/ncr-table)|Help Jim calculating nCr values|[Python](combinatorics/ncr-table.py)|Medium
[nCr table](https://www.hackerrank.com/challenges/ncr-table)|Help Jim calculating nCr values|[C++](combinatorics/ncr-table.cpp) [Python](combinatorics/ncr-table.py)|Medium
[Coinage](https://www.hackerrank.com/challenges/coinage)|Find the number of ways to pay a given amount, given a set of coins with prescribed denominations.|[Python](combinatorics/coinage.py)|Medium
[Building a List](https://www.hackerrank.com/challenges/building-a-list)|Generate all possible combinations of a string|[Python](combinatorics/building-a-list.py)|Medium
[Merge List](https://www.hackerrank.com/challenges/merge-list)|Help Shashank in merging two list.|[Python](combinatorics/merge-list.py)|Medium
[A Chocolate Fiesta](https://www.hackerrank.com/challenges/a-chocolate-fiesta)|Find the number of even subsets in the given set of numbers.|[Python](combinatorics/a-chocolate-fiesta.py)|Easy
[Sherlock and Pairs](https://www.hackerrank.com/challenges/sherlock-and-pairs)|Count the number of pairs that satisfy a given constraint.|[Python](combinatorics/sherlock-and-pairs.py)|Medium
[Picking Cards](https://www.hackerrank.com/challenges/picking-cards)|How many ways can you pick up all the cards from a table?|[Python](combinatorics/picking-cards.py)|Easy

#### [Algebra](https://www.hackerrank.com/domains/mathematics/algebra)

Expand All @@ -98,6 +103,7 @@ Name | Preview | Code | Difficulty
[Number Groups](https://www.hackerrank.com/challenges/number-groups)|Find the sum of consecutive odd number groups.|[Python](algebra/number-groups.py)|Easy
[Tell the Average](https://www.hackerrank.com/challenges/tell-the-average)|Tell me average of all list value.|[Python](algebra/tell-the-average.py)|Medium
[Wet Shark and 42](https://www.hackerrank.com/challenges/wet-shark-and-42)|Help Wet Shark escape the gods of 42.|[Python](algebra/wet-shark-and-42.py)|Easy
[Sherlock and Square](https://www.hackerrank.com/challenges/sherlock-and-square)|Help Sherlock in finding the total side lengths of squares.|[Python](algebra/sherlock-and-square.py)|Hard
[Manasa and Sub-sequences ](https://www.hackerrank.com/challenges/manasa-and-sub-sequences)|Help Manasa in getting candies|[Python](algebra/manasa-and-sub-sequences.py)|Medium

#### [Geometry](https://www.hackerrank.com/domains/mathematics/geometry)
Expand Down
1 change: 1 addition & 0 deletions mathematics/algebra/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ add_hackerrank_py(number-groups.py)
add_hackerrank_py(shashank-and-list.py)
add_hackerrank_py(tell-the-average.py)
add_hackerrank_py(wet-shark-and-42.py)
add_hackerrank_py(sherlock-and-square.py)
1 change: 1 addition & 0 deletions mathematics/algebra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ Name | Preview | Code | Difficulty
[Number Groups](https://www.hackerrank.com/challenges/number-groups)|Find the sum of consecutive odd number groups.|[Python](number-groups.py)|Easy
[Tell the Average](https://www.hackerrank.com/challenges/tell-the-average)|Tell me average of all list value.|[Python](tell-the-average.py)|Medium
[Wet Shark and 42](https://www.hackerrank.com/challenges/wet-shark-and-42)|Help Wet Shark escape the gods of 42.|[Python](wet-shark-and-42.py)|Easy
[Sherlock and Square](https://www.hackerrank.com/challenges/sherlock-and-square)|Help Sherlock in finding the total side lengths of squares.|[Python](sherlock-and-square.py)|Hard
[Manasa and Sub-sequences ](https://www.hackerrank.com/challenges/manasa-and-sub-sequences)|Help Manasa in getting candies|[Python](manasa-and-sub-sequences.py)|Medium

13 changes: 13 additions & 0 deletions mathematics/algebra/sherlock-and-square.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Mathematics > Algebra > Sherlock and Square
# Help Sherlock in finding the total side lengths of squares.
#
# https://www.hackerrank.com/challenges/sherlock-and-square/problem
# https://www.hackerrank.com/contests/w11/challenges/sherlock-and-square
# challenge id: 4429
#

MOD = 1000000007

for _ in range(int(input())):
n = int(input())
print((pow(2, n + 1, MOD) + 2) % MOD)
3 changes: 3 additions & 0 deletions mathematics/combinatorics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ add_hackerrank_py(a-chocolate-fiesta.py)
add_hackerrank_py(ncr-table.py)
add_hackerrank_py(building-a-list.py)
add_hackerrank_py(coinage.py)
add_hackerrank(ncr-table ncr-table.cpp)
add_hackerrank_py(sherlock-and-pairs.py)
add_hackerrank_py(picking-cards.py)
4 changes: 3 additions & 1 deletion mathematics/combinatorics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ Without mathematics, there's nothing you can do. Everything around you is mathem

Name | Preview | Code | Difficulty
---- | ------- | ---- | ----------
[nCr table](https://www.hackerrank.com/challenges/ncr-table)|Help Jim calculating nCr values|[Python](ncr-table.py)|Medium
[nCr table](https://www.hackerrank.com/challenges/ncr-table)|Help Jim calculating nCr values|[C++](ncr-table.cpp) [Python](ncr-table.py)|Medium
[Coinage](https://www.hackerrank.com/challenges/coinage)|Find the number of ways to pay a given amount, given a set of coins with prescribed denominations.|[Python](coinage.py)|Medium
[Building a List](https://www.hackerrank.com/challenges/building-a-list)|Generate all possible combinations of a string|[Python](building-a-list.py)|Medium
[Merge List](https://www.hackerrank.com/challenges/merge-list)|Help Shashank in merging two list.|[Python](merge-list.py)|Medium
[A Chocolate Fiesta](https://www.hackerrank.com/challenges/a-chocolate-fiesta)|Find the number of even subsets in the given set of numbers.|[Python](a-chocolate-fiesta.py)|Easy
[Sherlock and Pairs](https://www.hackerrank.com/challenges/sherlock-and-pairs)|Count the number of pairs that satisfy a given constraint.|[Python](sherlock-and-pairs.py)|Medium
[Picking Cards](https://www.hackerrank.com/challenges/picking-cards)|How many ways can you pick up all the cards from a table?|[Python](picking-cards.py)|Easy

Loading

0 comments on commit a15d59b

Please sign in to comment.