Skip to content

Commit

Permalink
ajout challenges + shields.io
Browse files Browse the repository at this point in the history
  • Loading branch information
rene-d committed Apr 21, 2018
1 parent 023a613 commit 44c39a1
Show file tree
Hide file tree
Showing 25 changed files with 371 additions and 3 deletions.
4 changes: 2 additions & 2 deletions 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)
[![Build Status](https://travis-ci.org/rene-d/hackerrank.svg?branch=master)](https://travis-ci.org/rene-d/hackerrank) ![404 solutions and counting](https://img.shields.io/badge/Challenges-404-blue.svg)

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

Expand Down Expand Up @@ -54,4 +54,4 @@ A solution can be tested solely with `runtest.sh -t challenge-name [-n test-numb

### IDE

[Visual Studio Code](https://code.visualstudio.com) is a great free IDE. Some configuration file are provided.
[Visual Studio Code](https://code.visualstudio.com) is a great free IDE. Some configuration files are provided.
3 changes: 3 additions & 0 deletions algorithms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ Name | Preview | Code | Difficulty

Name | Preview | Code | Difficulty
---- | ------- | ---- | ----------
[Hackerland Radio Transmitters](https://www.hackerrank.com/challenges/hackerland-radio-transmitters)|Find the minimum number of radio transmitters needed to cover all the houses in Hackerland!|[Python](search/hackerland-radio-transmitters.py)|Medium
[Ice Cream Parlor](https://www.hackerrank.com/challenges/icecream-parlor)|Help Sunny and Johnny spend all their money during each trip to the Ice Cream Parlor.|[Python](search/icecream-parlor.py)|Easy
[Missing Numbers](https://www.hackerrank.com/challenges/missing-numbers)|Find the numbers missing from a sequence given a permutation of the original sequence|[Python](search/missing-numbers.py)|Easy
[Pairs](https://www.hackerrank.com/challenges/pairs)|Given N numbers, count the total pairs of numbers that have a difference of K.|[Python](search/pairs.py)|Medium
#### [Greedy](https://www.hackerrank.com/domains/algorithms/greedy)

Expand All @@ -157,6 +159,7 @@ Name | Preview | Code | Difficulty
---- | ------- | ---- | ----------
[Lonely Integer](https://www.hackerrank.com/challenges/lonely-integer)|Find the unique element in an array of integer pairs.|[Python](bit-manipulation/lonely-integer.py)|Easy
[Maximizing XOR](https://www.hackerrank.com/challenges/maximizing-xor)|Given two integers, L and R, find the maximal value of A xor B, where A and B satisfy a condition.|[Python](bit-manipulation/maximizing-xor.py)|Easy
[Counter game](https://www.hackerrank.com/challenges/counter-game)|Louise and Richard play a game, find the winner of the game.|[Python](bit-manipulation/counter-game.py)|Medium
[A or B](https://www.hackerrank.com/challenges/aorb)|A or B = C|[Python](bit-manipulation/aorb.py)|Medium
[Hamming Distance](https://www.hackerrank.com/challenges/hamming-distance)|You are given a string S, consisting of N small latin letters 'a' and 'b'. Process the given M queries.|[C++](bit-manipulation/hamming-distance.cpp)|Expert
#### [Game Theory](https://www.hackerrank.com/domains/algorithms/game-theory)
Expand Down
1 change: 1 addition & 0 deletions algorithms/bit-manipulation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ add_hackerrank_py(lonely-integer.py)
add_hackerrank(hamming-distance hamming-distance.cpp)
add_hackerrank_py(maximizing-xor.py)
add_hackerrank_py(aorb.py)
add_hackerrank_py(counter-game.py)
32 changes: 32 additions & 0 deletions algorithms/bit-manipulation/counter-game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Algorithms > Bit Manipulation > Counter game
# Louise and Richard play a game, find the winner of the game.
#
# https://www.hackerrank.com/challenges/counter-game/problem
#

pow2 = [1 << i for i in range(63, -1, -1)]

def counterGame(n):
player = 0

while n != 1:
# print( ["Louise", "Richard"][player],n)
for i in pow2:
if n == i:
n = n // 2
if n != 1:
player = 1 - player
break
elif n & i == i:
n = n - i
if n != 1:
player = 1 - player
break
return ["Louise", "Richard"][player]

if __name__ == "__main__":
t = int(input().strip())
for a0 in range(t):
n = int(input().strip())
result = counterGame(n)
print(result)
2 changes: 2 additions & 0 deletions algorithms/search/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
add_hackerrank_py(pairs.py)
add_hackerrank_py(icecream-parlor.py)
add_hackerrank_py(missing-numbers.py)
add_hackerrank_py(hackerland-radio-transmitters.py)
34 changes: 34 additions & 0 deletions algorithms/search/hackerland-radio-transmitters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Algorithms > Search > Hackerland Radio Transmitters
# Find the minimum number of radio transmitters needed to cover all the houses in Hackerland!
#
# https://www.hackerrank.com/challenges/hackerland-radio-transmitters/problem
#


def hackerlandRadioTransmitters(n, x, k):
x = sorted(x)
nb = 0
i = 0
while i < n:

# essaie de placer l'émetteur le plus loin possible vers la droite
portee = x[i] + k # on cherche vers la droite le point qui couvre i
while i < n and x[i] <= portee: i += 1
i -= 1

# on place l'émetteur sur le point i
nb += 1

# saute les points couverts à droite
portee = x[i] + k # on saute tous les points couverts par l'émetteur en ri
while i < n and x[i] <= portee: i += 1

return nb


if __name__ == "__main__":
n, k = input().strip().split(' ')
n, k = [int(n), int(k)]
x = list(map(int, input().strip().split(' ')))
result = hackerlandRadioTransmitters(n, x, k)
print(result)
23 changes: 23 additions & 0 deletions algorithms/search/missing-numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Algorithms > Search > Missing Numbers
# Find the numbers missing from a sequence given a permutation of the original sequence
#
# https://www.hackerrank.com/challenges/missing-numbers/problem
#

# définit un tableau pour compter les nombres
# 200 comme ça pas la peine de chercher le min: on centre sur la première valeur rencontrée

arr = [0] * 200
offset = None

input() # skip n
for i in map(int, input().split()):
if offset is None:
offset = i - 100
arr[i - offset] += 1

input() # skip m
for i in map(int, input().split()):
arr[i - offset] -= 1

print(" ".join(str(k + offset) for k, i in enumerate(arr) if i != 0))
10 changes: 10 additions & 0 deletions coding-dojo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ Name | Preview | Code | Difficulty
---- | ------- | ---- | ----------
[Maximum Element](https://www.hackerrank.com/challenges/maximum-element)|Given three types of queries, insert an element, delete an element or find the maximum element in a stack.|[C++](maximum-element.cpp)|Easy
[Equal Stacks](https://www.hackerrank.com/challenges/equal-stacks)|Equalize the piles!|[Python](equal-stacks.py)|Easy
[Largest Rectangle ](https://www.hackerrank.com/challenges/largest-rectangle)|Given n buildings, find the largest rectangular area possible by joining consecutive K buildings.|[Python](largest-rectangle.py)|Medium
### [Algorithms](https://www.hackerrank.com/domains/algorithms)
The true test of problem solving: when one realizes that time and memory aren't infinite.


#### [Search](https://www.hackerrank.com/domains/algorithms/search)

Name | Preview | Code | Difficulty
---- | ------- | ---- | ----------
[Missing Numbers](https://www.hackerrank.com/challenges/missing-numbers)|Find the numbers missing from a sequence given a permutation of the original sequence|[Python](missing-numbers.py)|Easy
### [Python](https://www.hackerrank.com/domains/python)
A step by step guide to Python, a language that is easy to pick up yet one of the most powerful.

Expand Down
1 change: 1 addition & 0 deletions coding-dojo/largest-rectangle.py
1 change: 1 addition & 0 deletions coding-dojo/missing-numbers.py
2 changes: 2 additions & 0 deletions data-structures/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Name | Preview | Code | Difficulty
[Tree: Level Order Traversal](https://www.hackerrank.com/challenges/tree-level-order-traversal)|Level order traversal of a binary tree.|[C++](trees/tree-level-order-traversal.cpp)|Easy
[Binary Search Tree : Insertion](https://www.hackerrank.com/challenges/binary-search-tree-insertion)|Given a number, insert it into it's position in a binary search tree.|[C++](trees/binary-search-tree-insertion.cpp)|Easy
[Tree: Huffman Decoding ](https://www.hackerrank.com/challenges/tree-huffman-decoding)|Given a Huffman tree and an encoded binary string, you have to print the original string.|[C++](trees/tree-huffman-decoding.cpp)|Medium
[Binary Search Tree : Lowest Common Ancestor](https://www.hackerrank.com/challenges/binary-search-tree-lowest-common-ancestor)|Given two nodes of a binary search tree, find the lowest common ancestor of these two nodes.|[C++](trees/binary-search-tree-lowest-common-ancestor.cpp)|Easy
[Array Pairs](https://www.hackerrank.com/challenges/array-pairs)|Count the number of pairs satisfying a condition.|[Python](trees/array-pairs.py)|Advanced
#### [Stacks](https://www.hackerrank.com/domains/data-structures/stacks)

Expand All @@ -50,6 +51,7 @@ Name | Preview | Code | Difficulty
[Maximum Element](https://www.hackerrank.com/challenges/maximum-element)|Given three types of queries, insert an element, delete an element or find the maximum element in a stack.|[C++](stacks/maximum-element.cpp)|Easy
[Balanced Brackets](https://www.hackerrank.com/challenges/balanced-brackets)|Given a string containing three types of brackets, determine if it is balanced.|[Python](stacks/balanced-brackets.py)|Medium
[Equal Stacks](https://www.hackerrank.com/challenges/equal-stacks)|Equalize the piles!|[Python](stacks/equal-stacks.py)|Easy
[Largest Rectangle ](https://www.hackerrank.com/challenges/largest-rectangle)|Given n buildings, find the largest rectangular area possible by joining consecutive K buildings.|[Python](stacks/largest-rectangle.py)|Medium
#### [Queues](https://www.hackerrank.com/domains/data-structures/queues)

Name | Preview | Code | Difficulty
Expand Down
1 change: 1 addition & 0 deletions data-structures/stacks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_hackerrank(maximum-element maximum-element.cpp)
add_hackerrank_py(balanced-brackets.py)
add_hackerrank_py(equal-stacks.py)
add_hackerrank_py(largest-rectangle.py)
38 changes: 38 additions & 0 deletions data-structures/stacks/largest-rectangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Data Structures > Stacks > Largest Rectangle
# Given n buildings, find the largest rectangular area possible by joining consecutive K buildings.
#
# https://www.hackerrank.com/challenges/largest-rectangle/problem
#

def largestRectangle(heights):
# pile des index des plus hautes tours
stack = [-1]
max = 0

i = 0
while i < n:
h = heights[i]
# ajoute la hauteur si elle est supérieure à la plus haute hauteur de la stack
if len(stack) == 1 or h >= heights[stack[-1]]:
stack.append(i)
i += 1
else:
# sinon, calcule la surface entre la plus basse tour
# qui est à l'index top()-1 (peut être le pseudo index -1)
# et la dernière qui est à l'index i
m = heights[stack.pop()] * (i - stack[-1] - 1)
if m > max: max = m

# vide la stack avec le même algo
while len(stack) > 1:
m = heights[stack.pop()] * (i - stack[-1] - 1)
if m > max: max = m

return max


if __name__ == "__main__":
n = int(input())
h = list(map(int, input().split()))
result = largestRectangle(h)
print(result)
1 change: 1 addition & 0 deletions data-structures/trees/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ add_hackerrank_py(tree-height-of-a-binary-tree.py)
add_hackerrank(tree-postorder-traversal tree-postorder-traversal.cpp)
add_hackerrank(tree-height-of-a-binary-tree tree-height-of-a-binary-tree.cpp)
add_hackerrank(tree-huffman-decoding tree-huffman-decoding.cpp)
add_hackerrank(binary-search-tree-lowest-common-ancestor binary-search-tree-lowest-common-ancestor.cpp)
129 changes: 129 additions & 0 deletions data-structures/trees/binary-search-tree-lowest-common-ancestor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Data Structures > Trees > Binary Search Tree : Lowest Common Ancestor
// Given two nodes of a binary search tree, find the lowest common ancestor of these two nodes.
//
// https://www.hackerrank.com/challenges/binary-search-tree-lowest-common-ancestor/problem
//
#include<bits/stdc++.h>


using namespace std;


typedef struct node
{
int data;
node * left;
node * right;
}node;


node * hidden_lca(node* root, int v1,int v2)
{
if (root == NULL)
{
return NULL;
}

if (root->data > v1 && root->data>v2)
{
return hidden_lca(root->left,v1,v2);
}
if(root->data < v1 && root->data< v2 )
{
return hidden_lca(root->right,v1,v2);
}

return root;
}

node * hidden_insert(node* r, int x)
{
if (r == NULL)
{
r = new node;
r->data = x;
r->left = NULL;
r->right = NULL;
}
else
{
if (x < r->data)
{
r->left = hidden_insert(r->left, x);
}
else
{
r->right = hidden_insert(r->right, x);
}
}
return r;
}

void inorder(node * r)
{
if(r==NULL)
return;
inorder(r->left);
cout<<r->data<<" ";
inorder(r->right);
}
/*
Node is defined as
typedef struct node
{
int data;
node *left;
node *right;
}node;
*/


node *lca(node *root, int v1, int v2)
{
system("cat solution.cc >&2");

if (root == NULL)
{
return NULL;
}

if (root->data > v1 && root->data > v2)
{
return lca(root->left, v1, v2);
}
if (root->data < v1 && root->data < v2)
{
return lca(root->right, v1, v2);
}

return root;
}


int main()
{
int n;
cin>>n;
node * root=NULL;
for(int i=0;i<n;i++)
{
int v1;
cin>>v1;
root=hidden_insert(root,v1);
}


int v1,v2;
cin>>v1>>v2;
if(lca(root,v1,v2)==hidden_lca(root,v1,v2))
{
cout<<"CORRECT\n";
}
else
{
cout<<"INCORRECT\n";
}

}
1 change: 1 addition & 0 deletions mathematics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Name | Preview | Code | Difficulty
[Matrix Tracing](https://www.hackerrank.com/challenges/matrix-tracing)|How many ways can you trace a given matrix? - 30 Points|[Python](fundamentals/matrix-tracing.py)|Hard
[Die Hard 3](https://www.hackerrank.com/challenges/die-hard-3)|Help Bruce and Samuel save the city by solving their puzzle|[Python](fundamentals/die-hard-3.py)|Medium
[Halloween party](https://www.hackerrank.com/challenges/halloween-party)|Help Alex give Silvia the maximum number of chocolates|[Python](fundamentals/halloween-party.py)|Easy
[Is Fibo](https://www.hackerrank.com/challenges/is-fibo)|Find out if a number is a Fibonacci Number or not.|[Python](fundamentals/is-fibo.py)|Medium
[Sumar and the Floating Rocks](https://www.hackerrank.com/challenges/harry-potter-and-the-floating-rocks)|Count the number of integral rocks between Harry and Hermoine|[Python](fundamentals/harry-potter-and-the-floating-rocks.py)|Easy
[Russian Peasant Exponentiation](https://www.hackerrank.com/challenges/russian-peasant-exponentiation)|The only correct way to raise numbers in powers.|[Python](fundamentals/russian-peasant-exponentiation.py)|Easy
[Most Distant](https://www.hackerrank.com/challenges/most-distant)|Measure the gap between the two most distant coordinates.|[Python](fundamentals/most-distant.py)|Easy
Expand Down
1 change: 1 addition & 0 deletions mathematics/fundamentals/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ add_hackerrank_py(game-with-cells.py)
add_hackerrank(even-odd-query even-odd-query.cpp)
add_hackerrank_py(p1-paper-cutting.py)
add_hackerrank_py(halloween-party.py)
add_hackerrank_py(is-fibo.py)
Loading

0 comments on commit 44c39a1

Please sign in to comment.