Skip to content

Commit

Permalink
colorized hrinit.sh and javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
rene-d committed Jun 26, 2018
1 parent d547a7d commit 87fc2fd
Show file tree
Hide file tree
Showing 15 changed files with 357 additions and 8 deletions.
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) [![765 solutions and counting](https://img.shields.io/badge/Challenges-765-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) [![771 solutions and counting](https://img.shields.io/badge/Challenges-771-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/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Name | Preview | Code | Difficulty
[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
[Sherlock and Array](https://www.hackerrank.com/challenges/sherlock-and-array)|Check whether there exists an element in the array such that sum of elements on its left is equal to the sum of elements on its right.|[Python](search/sherlock-and-array.py)|Easy

#### [Greedy](https://www.hackerrank.com/domains/algorithms/greedy)

Expand Down
1 change: 1 addition & 0 deletions algorithms/search/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,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)
add_hackerrank_py(sherlock-and-array.py)
1 change: 1 addition & 0 deletions algorithms/search/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ Name | Preview | Code | Difficulty
[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](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](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](pairs.py)|Medium
[Sherlock and Array](https://www.hackerrank.com/challenges/sherlock-and-array)|Check whether there exists an element in the array such that sum of elements on its left is equal to the sum of elements on its right.|[Python](sherlock-and-array.py)|Easy

30 changes: 30 additions & 0 deletions algorithms/search/sherlock-and-array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Algorithms > Search > Sherlock and Array
# Check whether there exists an element in the array such that sum of elements on its left is equal to the sum of elements on its right.
#
# https://www.hackerrank.com/challenges/sherlock-and-array/problem
# https://www.hackerrank.com/contests/101may14/challenges/sherlock-and-array
# challenge id: 2490
#


def solve(a):
i = 0
j = len(a) - 1
l = r = 0

while i < j:
if l < r:
l += a[i]
i += 1
else:
r += a[j]
j -= 1

return "YES" if l == r else "NO"


for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
result = solve(a)
print(result)
50 changes: 43 additions & 7 deletions hrinit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,39 @@
import time


class Colors:
""" Terminal colors """
BLACK = "\033[0;30m"
RED = "\033[0;31m"
GREEN = "\033[0;32m"
BROWN = "\033[0;33m"
BLUE = "\033[0;34m"
PURPLE = "\033[0;35m"
CYAN = "\033[0;36m"
LIGHT_GRAY = "\033[0;37m"
DARK_GRAY = "\033[1;30m"
LIGHT_RED = "\033[1;31m"
LIGHT_GREEN = "\033[1;32m"
YELLOW = "\033[1;33m"
LIGHT_BLUE = "\033[1;34m"
LIGHT_PURPLE = "\033[1;35m"
LIGHT_CYAN = "\033[1;36m"
LIGHT_WHITE = "\033[1;37m"
BOLD = "\033[1m"
FAINT = "\033[2m"
ITALIC = "\033[3m"
UNDERLINE = "\033[4m"
BLINK = "\033[5m"
NEGATIVE = "\033[7m"
CROSSED = "\033[9m"
END = "\033[0m"
# cancel SGR codes if we don't write to a terminal
if not __import__("sys").stdout.isatty():
for c in dir():
if isinstance(c, str) and c[0:2] != "__":
locals()[c] = ""


class HackerRankParser():

def __init__(self, debug=False, rootdir=None):
Expand Down Expand Up @@ -76,11 +109,11 @@ def feed(self, data, ignore_path=False):
self.url2 = None

def info(self):
print("key :", self.model['slug'])
print("name :", self.model['name'])
print("domain :", self.path_name)
print("preview :", self.model['preview'])
print("lang :", ','.join(self.model['languages']))
print(Colors.LIGHT_BLUE + "key :" + Colors.END, self.model['slug'])
print(Colors.LIGHT_BLUE + "name :" + Colors.END, self.model['name'])
print(Colors.LIGHT_BLUE + "domain :" + Colors.END, self.path_name)
print(Colors.LIGHT_BLUE + "preview :" + Colors.END, self.model['preview'])
print(Colors.LIGHT_BLUE + "lang :" + Colors.END, ','.join(self.model['languages']))

def gen_stub(self, lang, overwrite=False, hpp=False, editor=True, add_test=True):
""" create a file based on the hackerrank template with a significant header """
Expand Down Expand Up @@ -319,18 +352,21 @@ def downloads(self, overwrite=False, testcases=True, statement=False):
def main():

lines = [
Colors.GREEN,
"===============================================================================",
",--. ,--. ,--. ,------. ,--. ",
"| '--' | ,--,--. ,---.| |,-. ,---. ,--.--.| .--. ' ,--,--.,--,--, | |,-. ",
"| .--. |' ,-. || .--'| /| .-. :| .--'| '--'.'' ,-. || \\| / ",
"| | | |\\ '-' |\\ `--.| \\ \\\\ --.| | | |\\ \\ \\ '-' || || || \\ \\ ",
"`--' `--' `--`--' `---'`--'`--'`----'`--' `--' '--' `--`--'`--''--'`--'`--' ",
"===============================================================================",
Colors.END,
]
for i in lines:
print(i)

parser = argparse.ArgumentParser(description='Intialize a HackerRank challenge.')
parser = argparse.ArgumentParser(
description='Intialize a ' + Colors.LIGHT_BLUE + 'HackerRank' + Colors.END + ' challenge.')
parser.add_argument('url', help="Challenge URL")
parser.add_argument('-v', '--verbose', help="Verbose mode", action='store_true')
parser.add_argument('-d', '--debug', help="Debug mode", action='store_true')
Expand Down Expand Up @@ -383,7 +419,7 @@ def main():

else:
# contest challenge ?
t = re.search(r"www\.hackerrank\.com/contests/([^/]+)/challenges/([\w\d\-]+)", args.url)
t = re.search(r"www\.hackerrank\.com/contests/([^/]+)/challenges/([\w\d\-]+)", args.url) # noqa
if t:
contest = t.group(1)
challenge = t.group(2)
Expand Down
Binary file modified testcases.tar.xz
Binary file not shown.
5 changes: 5 additions & 0 deletions tutorials/10-days-of-javascript/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ add_hackerrank_js(js10-if-else.js)
add_hackerrank_js(js10-switch.js)
add_hackerrank_js(js10-loops.js)
add_hackerrank_js(js10-arrays.js)
add_hackerrank_js(js10-try-catch-and-finally.js)
add_hackerrank_js(js10-throw.js)
add_hackerrank_js(js10-objects.js)
add_hackerrank_js(js10-count-objects.js)
add_hackerrank_js(js10-class.js)
5 changes: 5 additions & 0 deletions tutorials/10-days-of-javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ Name | Preview | Code | Difficulty
[Day 2: Conditional Statements: Switch](https://www.hackerrank.com/challenges/js10-switch)|Practice using Switch statements.|[Javascript](js10-switch.js)|Easy
[Day 2: Loops](https://www.hackerrank.com/challenges/js10-loops)|Learn For, While and Do-While loops in Javascript.|[Javascript](js10-loops.js)|Easy
[Day 3: Arrays](https://www.hackerrank.com/challenges/js10-arrays)|Output the 2nd largest number in an array in JavaScript.|[Javascript](js10-arrays.js)|Easy
[Day 3: Try, Catch, and Finally](https://www.hackerrank.com/challenges/js10-try-catch-and-finally)|Learn to use `try`, `catch`, and 'finally' in JavaScript.|[Javascript](js10-try-catch-and-finally.js)|Easy
[Day 3: Throw](https://www.hackerrank.com/challenges/js10-throw)|Practice throwing errors` in JavaScript.|[Javascript](js10-throw.js)|Easy
[Day 4: Create a Rectangle Object](https://www.hackerrank.com/challenges/js10-objects)|Create an object with certain properties in JavaScript.|[Javascript](js10-objects.js)|Easy
[Day 4: Count Objects](https://www.hackerrank.com/challenges/js10-count-objects)|Iterate over the elements in an array and perform an action based on each element's properties.|[Javascript](js10-count-objects.js)|Easy
[Day 4: Classes](https://www.hackerrank.com/challenges/js10-class)|Practice using JavaScript classes.|[Javascript](js10-class.js)|Easy
[Day 8: Create a Button](https://www.hackerrank.com/challenges/js10-create-a-button)|Create a button.|[HTML](js10-create-a-button.html)|Easy

34 changes: 34 additions & 0 deletions tutorials/10-days-of-javascript/js10-class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Tutorials > 10 Days of Javascript > Day 4: Classes
// Practice using JavaScript classes.
//
// https://www.hackerrank.com/challenges/js10-class/problem
// challenge id: 21855
//

/*
* Implement a Polygon class with the following properties:
* 1. A constructor that takes an array of integer side lengths.
* 2. A 'perimeter' method that returns the sum of the Polygon's side lengths.
*/
class Polygon {
constructor(sides) {
this.sides = sides;
}

perimeter()
{
let sum = 0;
for (const s of this.sides)
sum += s;
return sum;
}
}

// (skeliton_tail) ----------------------------------------------------------------------
const rectangle = new Polygon([10, 20, 10, 20]);
const square = new Polygon([10, 10, 10, 10]);
const pentagon = new Polygon([10, 20, 30, 40, 43]);

console.log(rectangle.perimeter());
console.log(square.perimeter());
console.log(pentagon.perimeter());
60 changes: 60 additions & 0 deletions tutorials/10-days-of-javascript/js10-count-objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Tutorials > 10 Days of Javascript > Day 4: Count Objects
// Iterate over the elements in an array and perform an action based on each element's properties.
//
// https://www.hackerrank.com/challenges/js10-count-objects/problem
// challenge id: 21013
//

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});

process.stdin.on('end', _ => {
inputString = inputString.trim().split('\n').map(string => {
return string.trim();
});

main();
});

function readLine() {
return inputString[currentLine++];
}
// (skeliton_head) ----------------------------------------------------------------------

/*
* Return a count of the total number of objects 'o' satisfying o.x == o.y.
*
* Parameter(s):
* objects: an array of objects with integer properties 'x' and 'y'
*/
function getCount(objects) {

var count = 0;
for (const o of objects)
if (o.x == o.y) count++;

return count;
}

// (skeliton_tail) ----------------------------------------------------------------------
function main() {
const n = +(readLine());
let objects = [];

for (let i = 0; i < n; i++) {
const [a, b] = readLine().split(' ');

objects.push({x: +(a), y: +(b)});
}

console.log(getCount(objects));
}
57 changes: 57 additions & 0 deletions tutorials/10-days-of-javascript/js10-objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Tutorials > 10 Days of Javascript > Day 4: Create a Rectangle Object
// Create an object with certain properties in JavaScript.
//
// https://www.hackerrank.com/challenges/js10-objects/problem
// challenge id: 21012
//

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});

process.stdin.on('end', _ => {
inputString = inputString.trim().split('\n').map(string => {
return string.trim();
});

main();
});

function readLine() {
return inputString[currentLine++];
}
// (skeliton_head) ----------------------------------------------------------------------

/*
* Complete the Rectangle function
*/
function Rectangle(a, b) {

return {
length: a,
width: b,
perimeter: (a + b) * 2,
area: a * b
};
}

// (skeliton_tail) ----------------------------------------------------------------------
function main() {
const a = +(readLine());
const b = +(readLine());

const rec = new Rectangle(a, b);

console.log(rec.length);
console.log(rec.width);
console.log(rec.perimeter);
console.log(rec.area);
}
59 changes: 59 additions & 0 deletions tutorials/10-days-of-javascript/js10-throw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Tutorials > 10 Days of Javascript > Day 3: Throw
// Practice throwing errors` in JavaScript.
//
// https://www.hackerrank.com/challenges/js10-throw/problem
// challenge id: 20998
//

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});

process.stdin.on('end', _ => {
inputString = inputString.trim().split('\n').map(string => {
return string.trim();
});

main();
});

function readLine() {
return inputString[currentLine++];
}
// (skeliton_head) ----------------------------------------------------------------------

/*
* Complete the isPositive function.
* If 'a' is positive, return "YES".
* If 'a' is 0, throw an Error with the message "Zero Error"
* If 'a' is negative, throw an Error with the message "Negative Error"
*/
function isPositive(a) {

if (a == 0) throw Error("Zero Error");
if (a < 0) throw Error("Negative Error");
return "YES";
}

// (skeliton_tail) ----------------------------------------------------------------------
function main() {
const n = +(readLine());

for (let i = 0; i < n; i++) {
const a = +(readLine());

try {
console.log(isPositive(a));
} catch (e) {
console.log(e.message);
}
}
}
Loading

0 comments on commit 87fc2fd

Please sign in to comment.