Skip to content

Instantly share code, notes, and snippets.

trait EnoughParticipants {
/** Given probabilities of each participant attending in "probabilities",
* calculate the probability that at least "needed" participants will participate.
*
* The probability has to be accurate to the precision of 0.001.
*/
def calculate(probabilities: List[Double], needed: Int): Double
}
class EnoughParticipantsMonteCarlo extends EnoughParticipants {
override def calculate(probabilities: List[Double], needed: Int): Double = {
(1 to attempts).par.map(i => probabilities.map(p => dice(p)).sum).count(successful => successful >= needed) / attempts.toDouble
}
val Precision = 0.001
val attempts = Math.ceil(Math.pow(1 / Precision, 2)).toInt
@Megaprog
Megaprog / config.properties
Last active October 28, 2019 11:01
How to append a port to host from ansible inventory
hazelcast.hosts={{ groups.api|zip_longest([], fillvalue=':5701')|map('join')|join(',') }}
endpoint.hosts={{ ['http://']|product(groups.query_gui)|map('join')|product([':8099'])|map('join')|join(',') }}
@Megaprog
Megaprog / Rfc3339Test.java
Last active November 22, 2023 17:11
RFC3339 parsing by java.time (only parsing is possible)
package rfc3339;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;
@Megaprog
Megaprog / test.rs
Created November 8, 2019 13:47
Rust test deal with panic
#[test]
fn test_something_interesting() {
run_test(|| {
let true_or_false = do_the_test();
assert!(true_or_false);
})
}
fn run_test<T>(test: T) -> ()
where T: FnOnce() -> () + panic::UnwindSafe
use std::sync::{Arc, Mutex, MutexGuard};
use std::collections::HashMap;
use std::ops::Deref;
pub struct Inner<'a>(MutexGuard<'a, HashMap<i32, Vec<i32>>>, i32);
impl<'a> Deref for Inner<'a> {
type Target = Vec<i32>;
fn deref(&self) -> &Self::Target {
self.0.get(&self.1).unwrap()
@Megaprog
Megaprog / SimpleMessageListenerContainerLifeCycle.java
Last active July 13, 2021 02:22
How to start and stop SimpleMessageListenerContainer and wait a result
val events = CopyOnWriteArrayList<UserSubscriptionExternalActivationEvent>()
val listenerContainer = rabbitConfig.bind<UserSubscriptionExternalActivationEvent>(
routingKey.externalActivation,
TopicExchange(properties.template.exchange),
) {
println("result is $it")
events.add(it)
Mono.empty()
}
listenerContainer.start()
from sqlalchemy import create_engine
engine = create_engine("postgresql://<name>:<password>@postgres-context-slave.spb.play.dc:5433/context", echo=True, connect_args={"options": "-c TimeZone=UTC"})
import psycopg2.extras
# del os.environ["PGTZ"]
# os.environ["PGTZ"]="UTC"
# os.environ["PGTZ"]="Europe/Moscow"
conn = psycopg2.connect(
# host="postgres-corecms-slave.spb.play.dc",
host="postgres-context-slave.spb.play.dc",
port=5433,
dbname="context",
@Megaprog
Megaprog / supervisorScope_vs_SupervisorJob
Created December 1, 2021 10:47
CoroutineScope(coroutineContext + SupervisorJob()) and supervisorScope() look simular but behaviur is completly different. If you add SupervisorJob() then all jobs will be out of scope and when nobody will wait for them after scope finishes
fun main() {
runBlocking {
repeat(10) {
try {
// CoroutineScope(coroutineContext + SupervisorJob()).run {
supervisorScope {
listOf(
async {
throw IllegalStateException("ERROR OCCURRED.")
},