Skip to content

Commit 3628a98

Browse files
committed
Added challenge 14
1 parent b34e2d2 commit 3628a98

File tree

5 files changed

+850161
-0
lines changed

5 files changed

+850161
-0
lines changed

014-aLongTime/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Welcome @Challengers to the 14th #code4bytes challenge!
2+
3+
I'm kinda sick so I don't have the energy to come up with a story as to why we're doing what we're doing like I usually do, so let's get right into it!
4+
5+
You are given a file called `time.log`. Each line of that file contains **2 timestamps** (*1 timestamp combination*) and **1 unique code**.
6+
The codes are merely there to make grading a lot easier, consider them an *ID* to each timestamp!
7+
You may assume the first timestamp is **always** earlier (smaller if converted to UNIX time code) than the second timestamp.
8+
Here are your challenges:
9+
10+
```
11+
Find the code of the timestamp combination with the smallest delta.
12+
```
13+
```
14+
Find the code of the timestamp combination with the largest delta.
15+
```
16+
```
17+
Find the code of the timestamp combination which is closest to the average of all the timestamp deltas.
18+
```
19+
20+
To this post I have also attached a file called `example_time.log`. This is where you can verify if your code works!
21+
Here are the results of running my code against that file:
22+
```
23+
shortest code: XHtGpBzBcz
24+
longest code: HNUKalXyRW
25+
code closest to average: lHrAwUQAmJ
26+
```
27+
28+
- 1st place: 500.000 bytes!
29+
- 2nd place: 300.000 bytes!
30+
- 3rd place: 150.000 bytes!
31+
- Runner up: 75.000 bytes!
32+
33+
Good luck, have fun and don't cheat!

014-aLongTime/base_solution.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from datetime import datetime
2+
from tqdm import tqdm
3+
4+
deltas = []
5+
codes = []
6+
7+
8+
9+
def splitString(s):
10+
a = line.split(" - ")
11+
b = a[1].split(" | ")
12+
13+
return a[0].rstrip(), b[0].rstrip(), b[1].rstrip()
14+
15+
def toUnixTimestamp(s):
16+
return int(datetime.strptime(s, '%Y-%m-%d %H:%M:%S').strftime("%s"))
17+
18+
f = open("example_time.log")
19+
lines = f.readlines()
20+
21+
for line in tqdm(lines):
22+
t1, t2, code = splitString(line)
23+
24+
t1 = toUnixTimestamp(t1)
25+
t2 = toUnixTimestamp(t2)
26+
27+
delta = t2 - t1
28+
29+
deltas.append(delta)
30+
codes.append(code)
31+
32+
deltas, codes = (list(t) for t in zip(*sorted(zip(deltas, codes))))
33+
34+
shortestCode = codes[0]
35+
longestCode = codes[-1]
36+
37+
print("shortest code: ", shortestCode)
38+
print("longest code: ", longestCode)
39+
40+
average = sum(deltas)/len(deltas)
41+
42+
closestToAverage = min(deltas, key=lambda x:abs(x-average))
43+
print("code closest to average: ", codes[deltas.index(closestToAverage)])

014-aLongTime/example_time.log

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
1971-12-04 05:22:59 - 2013-07-09 10:25:05 | nHFAtGDDoq
2+
1997-10-07 17:04:55 - 1998-06-29 15:33:29 | mbjQiPqdns
3+
2001-02-06 04:01:10 - 2006-06-11 20:07:21 | iNLZopSUCw
4+
2013-07-28 14:13:44 - 2014-05-20 22:33:28 | JyMPWiBbJp
5+
1979-08-18 12:47:52 - 2014-10-16 00:27:08 | eKnZxbUydv
6+
1981-08-11 17:24:11 - 1994-07-17 15:49:13 | YXLwGiixVm
7+
1981-04-06 09:18:53 - 1999-07-05 07:39:04 | vnuvYXZSNF
8+
1980-05-30 21:18:32 - 2020-07-26 00:15:54 | LcSFwCUlpD
9+
2017-01-28 08:02:02 - 2020-01-15 07:20:30 | EvQqqehzXi
10+
1984-11-16 20:36:33 - 2001-05-25 11:51:22 | vLzsRudUAU
11+
1975-04-28 20:36:26 - 2005-03-04 14:40:29 | OYWhrDizFn
12+
1971-09-11 07:08:53 - 2002-04-28 07:18:21 | lkyWYVAEFD
13+
1985-06-06 12:24:59 - 1987-01-16 10:30:19 | QhuVYBhfTH
14+
1999-08-07 02:23:51 - 2008-05-23 14:43:10 | aOvgIyAgrA
15+
1973-03-25 15:06:29 - 2005-05-28 09:28:42 | GGDOKmxohR
16+
1972-01-04 19:25:51 - 2015-01-02 17:58:54 | UlFmTXkvvg
17+
1972-10-10 20:02:43 - 2010-04-16 04:44:40 | gCmQfaBZDr
18+
1977-02-11 08:30:18 - 1994-10-07 00:59:51 | lHrAwUQAmJ
19+
1975-07-27 20:36:39 - 2013-02-08 00:49:07 | pLgMmvtBoX
20+
2007-09-30 18:52:11 - 2011-09-05 23:57:00 | uYNEAFitWl
21+
1979-12-17 06:46:41 - 2006-07-16 18:18:30 | jPGTOgFPwU
22+
1971-03-16 07:47:35 - 1988-12-03 03:05:57 | TReEjnzgme
23+
1991-06-28 04:53:12 - 2019-07-29 01:21:57 | HKjLspzOlC
24+
2002-07-22 03:27:12 - 2016-04-04 20:08:33 | TAonRqnkEt
25+
1986-10-20 08:54:18 - 2017-07-20 22:24:10 | RdAvPXqsXc
26+
1991-06-28 01:16:49 - 1992-01-05 18:03:33 | tfFQaamOVJ
27+
1976-04-04 04:30:54 - 1989-11-23 11:32:24 | yYrEHGKghV
28+
2019-06-08 13:19:03 - 2019-07-26 09:50:11 | XHtGpBzBcz
29+
1980-09-10 16:23:45 - 1999-03-22 13:23:06 | aShFGMtgnR
30+
1979-01-07 19:08:52 - 1982-02-03 09:54:38 | mjlBXdxorg
31+
2006-01-21 05:00:48 - 2014-03-13 00:41:31 | fiIiUxInlT
32+
1992-12-18 06:53:25 - 2001-04-23 17:37:14 | stSepfjazG
33+
1989-06-24 05:49:19 - 1997-06-06 15:18:11 | YodiKmbnrM
34+
1985-05-11 17:19:31 - 2011-01-31 06:56:22 | wYlCHZyurq
35+
1985-02-03 17:42:47 - 1988-04-12 08:54:48 | ysRxFZYlmM
36+
1996-12-08 23:20:30 - 2015-12-26 19:51:23 | LcmsGCSoUZ
37+
1972-03-30 23:22:54 - 2002-05-01 13:14:34 | UMLfhmkTQX
38+
1982-01-29 12:50:47 - 1984-09-15 02:53:32 | jmMoAvmmkD
39+
1980-07-10 22:16:52 - 2019-10-06 21:12:29 | qzABZfhvkg
40+
1982-05-29 23:15:46 - 2015-09-03 01:59:46 | bdSDmoOqrc
41+
2006-03-29 16:47:35 - 2010-04-11 23:34:36 | awOfeDDNzI
42+
1996-11-01 20:00:27 - 1997-07-02 11:45:05 | aQcvAEGKeq
43+
1973-07-16 05:12:18 - 1981-01-25 12:55:54 | BafhkIIQoI
44+
1987-06-20 15:58:48 - 1990-06-19 10:54:43 | XDingQQxtF
45+
1980-02-22 11:15:26 - 1980-06-09 12:17:28 | NEXnRRQsyR
46+
1970-03-13 03:32:42 - 2017-01-02 09:51:25 | HNUKalXyRW
47+
2017-06-18 15:58:40 - 2020-04-14 15:27:36 | BoeASxkAMg
48+
1981-11-14 18:20:31 - 2014-09-18 03:50:17 | zLlhJgQgoA
49+
2010-02-26 17:11:41 - 2013-07-14 10:27:21 | euTQOmvALO
50+
2000-09-15 08:44:56 - 2006-02-03 23:47:46 | OmEajHkpdn

014-aLongTime/generate.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import random
2+
from datetime import datetime
3+
import string
4+
from tqdm import tqdm
5+
6+
LEN = 1700000
7+
MAX = 1600612413
8+
9+
file = open("time.log", "a+")
10+
11+
12+
def randomString():
13+
return ''.join(random.choice(string.ascii_letters) for i in range(10))
14+
15+
16+
def toFormatted(smallest, biggest):
17+
dtf = '%Y-%m-%d %H:%M:%S'
18+
smallest = datetime.utcfromtimestamp(smallest).strftime(dtf)
19+
biggest = datetime.utcfromtimestamp(biggest).strftime(dtf)
20+
21+
return "{} - {} | {}\n".format(smallest, biggest, randomString())
22+
23+
for _ in tqdm(range(round(LEN/2))):
24+
25+
d1 = random.randrange(MAX)
26+
d2 = random.randrange(MAX)
27+
28+
if d1 < d2:
29+
f = toFormatted(d1, d2)
30+
else:
31+
f = toFormatted(d2, d1)
32+
33+
file.write(f)
34+
35+
file.close()

0 commit comments

Comments
 (0)