Interesting task for programmers

DV 643
#11

Exhaust to CSV, JSON-laziness.

 #! / Bin / sh 

wget -qO- 'https://0x.com.ua/task-1.json' | sed 's /}, {/} \
{/ G '| sed 's / \: /, / g' | sed 's / \} // g' | sed 's / \] // g' |
LC_ALL = C awk -F \, '{if ($ 4> 0.7) print $ 2 ";" $ 4} '> out.csv
VDS хостинг (http://clck.ru/0u97l) Нет нерешаемых задач (/ru/forum/806725) | Перенос сайтов на Drupal 7 с любых CMS. (/ru/forum/comment/10504844)
D 141
#12
VoV @:
If you do not ship directly into the memory of all, IMHO, only brute force and character work.

Well, it is in fact all through the bust of symbols only work you can do is buffered and can drive and zadalbyvat core siskolami reading 1 byte.

And you can just unpack everything in memory, and can read and decompress a little, passing freeing memory. In the second approach, as you can imagine a lot of benefits on low-end machines can parse large files, it is possible to interrupt the decompression half way, if for example the task of reading until until we find the required object.

For how much you have worked for your code to this file?

Разработка и поддержка высоконагруженных проектов.
T7 30
#13
VoV @:
IMHO, only brute force characters will

Solutions above was with stream_get_line, a pack of characters to the separator + preg_match. one character reading makes it easier to solve the problem.

Especially if slightly complicate the task. The json

 [
  { 
"Id": 2234,
"Scores": {
"Max": 267,
"Min": 17
}
}
{
"Id": 2235
"Name": "OOOYO",
"Price": 200319.12,
"Scores": {
"Max": 408,
"Min": 78
}
}
And we must find scores.max, for example more than 500, and the object format is flexible

Slightly changing the code


$ Read = 0;
......
if ($ c == '{') {
$ Read ++;
}
if ($ read> 0) {
. $ Str = $ c;
}
if ($ c == '}') {
$ Read--;

problem is easily solved

------------

Danforth:
or you can drive and zadalbyvat core siskolami reading 1 byte.

Yes, constructive comments, but 20 minutes working hard to jot down. To me at least. :(

png json-2.png
S3 211
#14

Here is a fully working code character by character subtraction on Python


import json
import time


def truncate_row (data):
start = time.time ()
with open ( 'res-task-1.json', 'w') as res:
res.write ( '[') with open (data) as f:
pos = 1
flag = False
js = ''
while pos:
char = f.read (pos)
if char == ']':
break
if char == '{':
flag = True
if flag:
js + = char
if char == '}':
flag = False
data = json.loads (js)
if data.get ( 'scores')> = 0.7:
res.write (json.dumps (data) + ',')
js = ''
size = res.tell ()
res.truncate (size - 1)

with open ( 'res-task-1.json', 'a') as res:
res.write ( ']')
print (f'Executing time: {time.time () - start} ')


truncate_row ( 'task-1.json')

Executing time: 0.2150096893310547

The output file is valid)

This course is very tough, can be further optimized

And the request to all - is the full listing of the program to be able to run and pochekat time)

---------- Posted 06.15.2020 at 10:47 ----------

Gerga:
On php it can be even shorter if you do not read character by character.

It will be a violation of the conditions of the problem)

HM 208
#15

Once again you're streaming parser json?

They are fully in Google https://github.com/salsify/jsonstreamingparser

Avatar 89
#16
Sly32:
It will be a violation of the conditions of the problem)

No. Is the flow reading to a particular character or length parameter, symbol reading deals not my code, and built-in functions, which of course faster.

---------- Posted 15.06.2020 at 11:10 ----------

Sly32:
Executing time: 0.2150096893310547

0.10043406486511 :)

T7 30
#17
Danforth:
For how much you have worked for your code to this file?

Terms and conditions are different. For a fair assessment at all 1 machine should be allowed.

I for example, to run about in such conditions

S3 211
#18
timo-71:
I for example, to run about in such conditions

You'd even put smaller screen)

T7 30
#19
Sly32:
You'd even put smaller screen)

There you can click

DV 643
#20
timo-71:
For a fair assessment at all 1 machine should be allowed.

And, at the same time :)

It must be some kind of wrapper, while all running scripts at once (in different languages) to compare.

Although it can be like this:

 time script1.sh> 1.log 2> & 1 & 
time php script2.php> 2.log 2> & 1 &
time python3.6 script3.py> 3.log 2> & 1 &
time java script4.class> 4.log 2> & 1 &
This idea, HZ that will be released, it is necessary to play.

To post a new comment, please log in or register