Skip to content

Instantly share code, notes, and snippets.

@pfokin92 pfokin92/ex_2.ipynb
Last active Jan 20, 2020

Embed
What would you like to do?
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"from datetime import datetime"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"dict_time = [ {'date': '2018-07-18 15:45:20', 'duration':321}, {'date': '2018-07-18 15:35:31', 'duration':180}, {'date': '2018-07-18 15:30:48', 'duration': 3600}, {'date': '2018-07-18 16:10:08', 'duration':186}, {'date': '2018-07-18 16:20:08', 'duration':186}, {'date': '2018-07-18 16:25:08', 'duration':186}]"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1531917920.0, 1531917331.0, 1531917048.0, 1531919408.0, 1531920008.0, 1531920308.0]\n",
"[1531918241.0, 1531917511.0, 1531920648.0, 1531919594.0, 1531920194.0, 1531920494.0]\n"
]
}
],
"source": [
"start=[]\n",
"end=[]\n",
"count=0\n",
"def start_end(data):\n",
" for i in data:\n",
" start_time=datetime.strptime(i['date'] ,'%Y-%m-%d %H:%M:%S')\n",
" unixtime_start = time.mktime(start_time.timetuple())\n",
" unixtime_end = unixtime_start +i['duration']\n",
" start.append(unixtime_start)\n",
" end.append(unixtime_end)\n",
"start_end(dict_time)\n",
"print(start)\n",
"print(end)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'date': '2018-07-18 15:45:20', 'duration': 321, 'counter': 1}\n",
"{'date': '2018-07-18 15:35:31', 'duration': 180, 'counter': 1}\n",
"{'date': '2018-07-18 15:30:48', 'duration': 3600, 'counter': 0}\n",
"{'date': '2018-07-18 16:10:08', 'duration': 186, 'counter': 1}\n",
"{'date': '2018-07-18 16:20:08', 'duration': 186, 'counter': 1}\n",
"{'date': '2018-07-18 16:25:08', 'duration': 186, 'counter': 1}\n"
]
}
],
"source": [
"def counter(data):\n",
" for i in data:\n",
" start_time=datetime.strptime(i['date'] ,'%Y-%m-%d %H:%M:%S')\n",
" unixtime_start = time.mktime(start_time.timetuple())\n",
" unixtime_end = unixtime_start +i['duration']\n",
" count=0\n",
" for j in range(len(start)):\n",
" if unixtime_start>start[j] and unixtime_start<end[j]:\n",
" count+=1\n",
" elif unixtime_end>start[j] and unixtime_end<end[j]:\n",
" count+=1\n",
" i.update({'counter':count})\n",
" print(i)\n",
"counter(dict_time)\n"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'date': '2018-07-18 15:45:20', 'duration': 321, 'counter': 2}\n",
"{'date': '2018-07-18 15:35:31', 'duration': 180, 'counter': 2}\n",
"{'date': '2018-07-18 15:30:48', 'duration': 3600, 'counter': 2}\n",
"{'date': '2018-07-18 16:10:08', 'duration': 186, 'counter': 2}\n",
"{'date': '2018-07-18 16:20:08', 'duration': 186, 'counter': 2}\n",
"{'date': '2018-07-18 16:25:08', 'duration': 186, 'counter': 2}\n"
]
}
],
"source": [
"def counter(data):\n",
" for i in data:\n",
" start_time=datetime.strptime(i['date'] ,'%Y-%m-%d %H:%M:%S')\n",
" unixtime_start = time.mktime(start_time.timetuple())\n",
" unixtime_end = unixtime_start +i['duration']\n",
" count=0\n",
" b=0\n",
" for j in range(len(start)):\n",
" if unixtime_start>=start[j] and unixtime_start<=end[j]:\n",
" count+=1\n",
" elif unixtime_end>start[j] and unixtime_end<end[j]:\n",
" count+=1\n",
" elif unixtime_start<start[j] and unixtime_end>end[j]:\n",
" #print(unixtime_start)\n",
" #print(start[j])\n",
" for n in range(len(start)):\n",
" if unixtime_start!=start[n] and unixtime_end!=end[n]:\n",
" if start[j]>start[n] and start[j]<end[n]:\n",
" count+=1\n",
" elif end[j]>start[n] and end[j]<end[n]:\n",
" count+=1\n",
" elif b==0:\n",
" b+=1\n",
" count+=1\n",
" i.update({'counter':count})\n",
" print(i)\n",
"counter(dict_time)\n"
]
}
],
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.