Skip to content

Instantly share code, notes, and snippets.

@vaibhavsagar
Created September 27, 2016 21:25
Show Gist options
  • Save vaibhavsagar/46f044eda78e89e26ee1583d010090ef to your computer and use it in GitHub Desktop.
Save vaibhavsagar/46f044eda78e89e26ee1583d010090ef to your computer and use it in GitHub Desktop.
hackerrank pairs code dojo with @sranso
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"some_input = \"1 3 5 6 7\"\n",
"input_list = list(map(int, some_input.split(\" \")))\n",
"input_list\n",
"k = 2"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"some_input = \"1 3 5 6 7\"\n",
"input_set = set(map(int, some_input.split(\" \")))\n",
"k = 2"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def pairs(k, ls):\n",
" counter = 0\n",
" ls.sort()\n",
" for i, outer in enumerate(ls):\n",
" for inner in ls[i+1:]:\n",
" if inner-outer == k:\n",
" counter += 1\n",
" break\n",
" return counter\n",
"pairs(k, input_list)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'abc'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ls = 'abc'\n",
"list(enumerate(ls))\n",
"ls[0:]"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def pairs(k, input_set):\n",
" counter = 0\n",
" for item in input_set:\n",
" if item + k in input_set:\n",
" counter += 1\n",
" return counter\n",
"pairs(k, input_set)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5 2\n",
"1 5 3 4 2\n",
"3\n"
]
}
],
"source": [
"first_line = input()\n",
"k = int(first_line.split(' ')[1])\n",
"numbers_input = input()\n",
"numbers_set = set(map(int, numbers_input.split(' ')))\n",
"print(pairs(k, numbers_set))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
function processData(input) {
function pair(k, set) {
var count = 0;
function countPair(value, _value, _set) {
if ( set.has(value + k) ) {
count += 1;
}
}
set.forEach(countPair);
return count;
}
function toInt(value, _index, _array) {
return parseInt(value)
};
var split = input.split('\n');
var k = parseInt(split[0].split(' ')[1]);
var inputSet = new Set(split[1].split(" ").map(toInt));
var result = pair(k, inputSet);
return result;
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
console.log(processData(_input));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment