Interesting task for programmers

DV
Site user since 01.05.2010
Offline
644
#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 ) Нет нерешаемых задач ( https://searchengines.guru/ru/forum/806725 ) | Перенос сайтов на Drupal 7 с любых CMS. ( https://searchengines.guru/ru/forum/531842/page6#comment_10504844 )
danforth
Site user since 18.12.2015
Offline
153
#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?

Junior Web Developer
T7
Site user since 19.09.2018
Online
53
#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
Sly32
Site user since 29.03.2012
Offline
252
#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
Site user since 14.01.2012
Offline
218
#15

Once again you're streaming parser json?

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

Gerga
Site user since 02.08.2015
Offline
94
#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
Site user since 19.09.2018
Online
53
#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

Sly32
Site user since 29.03.2012
Offline
252
#18
timo-71:
I for example, to run about in such conditions

You'd even put smaller screen)

T7
Site user since 19.09.2018
Online
53
#19
Sly32:
You'd even put smaller screen)

There you can click

DV
Site user since 01.05.2010
Offline
644
#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