Skip to content

Instantly share code, notes, and snippets.

View Oziomajnr's full-sized avatar
🏠
Working from home

Ozioma Oziomajnr

🏠
Working from home
View GitHub Profile
@Oziomajnr
Oziomajnr / SwissPairing.Kt
Last active January 27, 2023 21:28
Swiss Pairing Algorithm for Fide rules generated by ChatGPT
class Player(val name: String, var score: Int,
var blackGames: Int, var whiteGames: Int, var bye: Boolean, var forfeitWin: Boolean)
fun swissPairing(players: List<Player>, rounds: Int) {
// Sort players by score
players.sortBy { it.score }
for (round in 1..rounds) {
// Pair players with the same score
val pairs = players.filter { !it.bye && !it.forfeitWin }.groupBy { it.score }.values
@Oziomajnr
Oziomajnr / SecretaryProblemSolution.kt
Created January 9, 2022 11:04
Simulating the solution the optimal stopping problem using the 37% rule.
fun main() {
val resultArray = IntArray(100) {
0
}
for (percent in 1..100) {
for (x in 1..1000000) {
if (simulateFindingBestSecretary(percent)) {
resultArray[percent - 1]++
}
}
class PaymentService {
EventBus eventBus = EventBusFactory.getEventBus();
public void onPaymentSuccessful (Payment payment) {
PaymentEvent paymentEvent = new PaymentEvent();
paymentEvent.setPayment(payment);
//post the payment event, after this point,
//all methods that subscribed to payment event are executed Asynchronously
eventBus.post(paymentEvent);
import com.google.common.eventbus.Subscribe;
public class RecieptSender {
@Subscribe
public void sendRecieptToCustomer(PaymentSuccessfulEvent paymentSuccessfulEvent){
// Simulate sending reciept
System.out.println("Reciept sent to Customer");
}
public class PaymentEvent {
private String name = "PaymentSuccesful";
public String getName() {
return name;
}
}
public class PaymentSuccessfulEvent {
private String name = "PaymentSuccesful";
private Payment payment;
public PaymentSuccessfulEvent (Payment payment) {
this.payment = payment;
}
public String getName() {
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import java.util.concurrent.Executors;
public class EventBusFactory {
//hold the instance of the event bus here
private static EventBus eventBus = new AsyncEventBus(Executors.newCachedThreadPool());
private void onPaymentSuccessful(Payment payment) {
sendRecieptToSeller (Payment payment);
sendRecieptToBuyer (Payment payment);
}
private void sendRecieptToSeller (Payment payment) {
System.out.print("Reciept sent to seller");
}
private void sendRecieptToBuyer (Payment payment) {
@Oziomajnr
Oziomajnr / Singleton.java
Created January 2, 2018 14:44
Sample Sigleton class
/**
This class has been simplified for this article
**/
public enum TokenManager implements Constants {
INSTANCE;
private ConcurrentHashMap<String, LoggedInUser> tokenUserMap = new ConcurrentHashMap<>();
public void addToken(String token, User user) {
tokenUserMap.put(token, new LoggedInUser(user));
}
public void updateLastAccessedTime(String token) {
@Oziomajnr
Oziomajnr / ChargeEvent.json
Created November 19, 2017 06:17
Paystack charge event for transactions
{
"event":"charge.success",
"data":{
"id":302961,
"domain":"live",
"status":"success",
"reference":"qTPrJoy9Bx",
"amount":10000,
"message":null,
"gateway_response":"Approved by Financial Institution",