dev || bet - the battle of programmers and technologies

dev || bet - the battle of programmers and technologies  
 
Dogs vs cats, drivers vs pedestrians, Coca-Cola vs Pepsi, BMW vs Mercedes, sausage vs cheese, vvs vs mors, good vs evil, after all! But no, we, as always, argued about which programming language is better. In the course were the classic performance arguments with links to benchmarks that no one tested, syntactic buns that you use once a year, graphs of popularity, lists of reputable programmers using one or another language Then the conversation went smoothly into a discussion of Oximiron's battle with someone there. Well, any conversation for more than 20 minutes reduces to a discussion of the price of bitcoin.
 
Strangely, the three themes merged into one and so the idea of ​​dev || bet was born.
 
was chosen. Codewars . Not that we long chose, but it had everything you need: support for more than 20 languages, a simple interface, the ability to add a draft task available by reference.
 
Quite interesting is the project codebattle.hexlet.io , about which already wrote on Habré. But the opportunity to see the opponent's code so far seemed to us superfluous.
 
 
Because the participants used Python and jаvascript, they ran test runners on them:
 
jаvascript [/b]
'use strict';
const https = require ('https');
const currencies =[
'btc',
'eth',
];
const BASE_CURRENCY = 'usd';
const DEFAULT_CURRENCY = 'btc';
const fetchRates = (days, currency) => new Promise ((res, rej) => {
https.get (`https://min-api.cryptocompare.com/data/histoday?aggregate=1&e=CCCAGG&extraParams= CryptoCompare & limit = $ {days} & tryConversion = false & tsym = $ {BASE_CURRENCY.toUpperCase ()} & fsym = $ {currency.toUpperCase ()} `, (resp) => {
Let data = '';
"data", (chunk) => {
? data + = chunk;
});
resp.on ('end', () => {
data = JSON.parse (data);
data = data.Data;
.
.res (data.map (datum => datum['close']) .slice (? -1));
});
.
} on. , err => rej (err));
});
const fetchAllRates = async (days, currencies) => {
const prices = {};
for (let currency of currencies) {
prices[currency]= await fetchRates (days, currency);
}
const len ​​= prices[Object.keys(prices)[0]].length;
const ret =[];
for (let i = 0; i < len; i++) {
let price = {};
for (let currency of currencies) {
price[currency]= prices[currency] [i];
}
ret.push (price);
}
.
Return ret;
};
.
.string checkStash = stash => {
const vals = Object.values ​​(stash);
.for (let val of vals) {
.expect (val> = -Math.pow (1? -6), 'Invalid operation');
if (val < -Math.pow(10, -6)) {
throw new Error (`Stts: $ {JSON.stringify (stash)}}
}
}
};
.
.constantTask = (stash, task, prices) => {
console.log ('- performing task', stash, task, prices);
.const currency = task.currency || DEFAULT_CURRENCY;
.
switch (task.op) {
case 'buy':
.stash[currency]+ = task.amoun t;
stash[BASE_CURRENCY]- = task.amount * prices[currency];
break;
case 'sell':
stash[currency]- = task.amount;
stash[BASE_CURRENCY]+ = task.amount * prices[currency];
break;
case 'pass':
break;
}
return stash;
};};
const runner = async (trader, cases) => {
for (let testCase of cases) {
let prices = await fetchAllRates (testCase.days, currencies);
let stash = testCase.amount;
for (let currency of currencies) {
stash[currency]= stash[currency]|| 0;
}
console.log (`Testing amount $ {stash[BASE_CURRENCY]}, days $ {testCase.days}`);
let tasks = await trader (prices, stash[BASE_CURRENCY]);
for (let i in tasks) {
if (! tasks.hasOwnProperty (i)) {
continue;
}
let job = tasks[i];
let todo = job.length? job:[job];
for (let row of todo) {
await applyTask (stash, row, prices[i]);
}
checkStash (stash);
}
let result = Math.floor (stash[BASE_CURRENCY]* 100) /100;
console.log (`finished. Resulting amount: $ {result}`);
}
};};
runner (trader,[
{
amount: {
[BASE_CURRENCY]: 10?
},
days: 10?
},
]);

 
 
Python [/b]
import urllib2
import json
import math
currencies =[
'btc',
'eth',
]
BASE_CURRENCY = 'usd'
DEFAULT_CURRENCY = 'btc'
def fetch_rates (days, currency):
data = urllib2.urlopen (
'https://min-api.cryptocompare.com/data/histoday?aggregate=1&e=CCCAGG&extraParams=CryptoCompare&limit= {} & tryConversion = false & tsym = {} & fsym = {}' .format (
. days, BASE_CURRENCY.upper (), currency.upper ())). read ()
data = json.loads (data)['Data']
return[row['close']for row in data] [:-1]
def fetch_all_rates (days, currencies):
prices = {currency: fetch_rates (days, currency) for currency in currencies}
return[{currency: prices[currency] [i]for currency in currencies.
def check_stash (stash):
for currency in stash:
test.assert_equals (stash[currency]> = -???? True, 'Invalid operation')
if stash[currency] < -0.000001:
raise Exception ('Debts are not supported. Stash: {}'. format (stash))
def apply_task (stash, task, prices):
print '- performing task {} {} {}'. format (stash, task, prices)
currency = task['currency']if 'currency' in task else DEFAULT_CURRENCY
if task['op']== 'buy':
stash[currency]+ = task['amount']
stash[BASE_CURRENCY]- = task['amount']* prices[currency]
elif task['op']== 'sell':
stash[currency]- = task['amount']
stash[BASE_CURRENCY]+ = task['amount']* prices[currency]
elif task['op']== 'pass':
pass
return stash
def runner (trader, cases):
for testCase in cases:
prices = fetch_all_rates (testCase['days'], currencies)
stash = testCase['amount']
for currency in currencies:
if currency not in stash:
stash[currency]= 0
print 'Testing amount {}, days {}'. format (stash[BASE_CURRENCY], testCase['days'])
tasks = trader (prices, stash[BASE_CURRENCY])
for i, job in enumerate (tasks):
todo = job if isinstance (job, list) else[job]
for row in todo:
stash = apply_task (stash, row, prices[i])
check_stash (stash)
result = math.floor (stash[BASE_CURRENCY]* 100) /100
print 'finished. Resulting amount: {} '. Format (result)
runner (trader,[
{
"amount": {
BASE_CURRENCY: 100,
},
"days": 100,
}
])

 
 
The task itself is available on Codewars: www.codewars.com/kata/5a22fdffe1ce0e4c5d000031
 
And the decisions of the participants on Github: github.com/devbet/devbet_0
 
 
Thank you for attention! We are waiting for your comments, criticism and opinions.
 
And what's next? PHP vs. JS, .Net vs. Java, iOS vs. Android, React vs. Vue.js?
+ 0 -

Add comment