spiroso
BAN USER
import java.util.*;
public class Primes {
Map<Integer, Integer> cache = new HashMap<Integer, Integer>();
public static void main(String[] args) {
Primes primes = new Primes();
primes.allPrimesTo(20);
System.out.println(primes.cache.get(13));
System.out.println(primes.cache.get(5));
}
int allPrimesTo(int n) {
int sum = 0;
for(int i=2; i<=n; i++) {
if(isPrime(i)) {
sum+=i;
cache.put(i, sum);
}
}
return sum;
}
boolean isPrime(int n) {
if(n ==2 ) return false;
for(int i=2; i<n; i++) {
if(n%i==0) return false;
}
return true;
}
}
class Solution {
boolean found(int i, int[] c1, int[] c2) {
int sum = 0;
for(int w=0; w<c1.length; w++) {
if(c1[w]-c2[w]!=0) return false;
}
return true;
}
public List<Integer> findAnagrams(String s, String p) {
List<Integer> response = new ArrayList<>();
if(s == null || p == null || s.length()==0 || p.length() == 0) return response;
char[] ch = p.toCharArray();
char[] chs = s.toCharArray();
int[] l = new int[128];
for (char c : ch) {
l[c - 'a']++;
}
for (int i = 0; i < chs.length-ch.length+1; i++) {
int count = 0;
int[] window = new int[p.length()];
int[] window2 = new int[p.length()];
int[] c1 = new int[128];
int[] c2 = new int[128];
for (int j = i; j < p.length() + i; j++) {
c1[chs[(i + (j - i))]-'a']++;
c2[ch[((j - i))]-'a']++;
//window[(j - i)] = chs[(i + (j - i))] - ch[j - i];
}
if(found(i, c1, c2)) {
response.add(i);
}
}
return response;
}
}
//With a test
public class App {
static class Order {
int id;
List<String> items;
public Order(int id, List<String> items) {
this.id = id;
this.items = items;
}
}
public static void main(String[] args) {
List<Order> orders = new ArrayList<>();
List<String> l1 = new ArrayList<>();
List<String> l2 = new ArrayList<>();
List<String> l3 = new ArrayList<>();
l1.add("A");
l1.add("B");
l2.add("B");
l2.add("C");
l3.add("K");
l3.add("P");
l3.add("L");
l3.add("A");
orders.add(new Order(1, l1));
orders.add(new Order(2, l2));
orders.add(new Order(3, l3));
Map<String, List<Integer>> maps = new HashMap<>();
orders.forEach(order -> {
order.items.forEach(item -> {
maps.putIfAbsent(item, new ArrayList<>());
maps.get(item).add(order.id);
});
});
maps.entrySet().stream().forEach(item -> {
System.out.println(item.getKey()+":"+item.getValue());
});
}
}
import java.util.LinkedList;
import java.util.List;
public class Test {
public static void main(String[] args) {
Notification notification = new Notification();
new UserNotification(notification, "ob1");
new UserNotification(notification, "ob2");
new UserNotification(notification, "ob3");
new UserNotification(notification, "ob4");
notification.setState("hello");
notification.setState("sjgs");
}
}
abstract class Observer {
Notification notification;
abstract void alert();
}
class Notification {
private String state;
private List<Observer> observers = new LinkedList<>();
void attach(Observer observer) {
this.observers.add(observer);
}
void setState(String state) {
this.state = state;
notifyObservers();
}
private void notifyObservers() {
observers.forEach(observer -> observer.alert());
}
public String getState() {
return state;
}
}
class UserNotification extends Observer {
private String user;
public UserNotification(Notification notification, String user) {
super.notification = notification;
this.user = user;
notification.attach(this);
}
@Override
void alert() {
System.out.println("User: " + this.user + " notified about " + super.notification.getState());
}
}
import java.util.*;
import java.util.stream.IntStream;
public class Test {
public static void main(String[] args) {
List<Customer> customers = new ArrayList<>();
List<Purchace> purchases = new ArrayList<>();
IntStream.range(1, 101).forEach(item -> {
Purchace purchace =new Purchace(new Customer(item), (int)(Math.random()*1000));
purchases.add(purchace);
});
List<PercentageDS> purc = new ArrayList<>(3);
PercentageDS ds = new PercentageDS((purchases.size()));
purc.add(ds);
int index = 0;
for(Purchace purchace:purchases) {
try {
purc.get(index).add(purchace);
} catch (RuntimeException e) {
index++;
}
}
PriorityQueue<Purchace> ddds = purc.get(0).customers;
while(!ddds.isEmpty()) {
System.out.println(ddds.poll());
}
}
}
class PercentageDS {
int capacity;
PriorityQueue<Purchace> customers;
public PercentageDS(int capacity) {
this.capacity = capacity;
this.customers = new PriorityQueue<>(capacity);
}
void add(Purchace customer) throws RuntimeException{
if(this.capacity == this.customers.size())
throw new RuntimeException("Max reached");
this.customers.add(customer);
}
}
class Customer {
private int id;
public Customer(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Customer [id=");
builder.append(id);
builder.append("]");
return builder.toString();
}
}
class Purchace implements Comparable<Purchace>{
private Customer customer;
Integer purchase;
public Purchace(Customer customer, int purchase) {
this.customer = customer;
this.purchase = purchase;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public int getPurchase() {
return purchase;
}
public void setPurchase(int purchase) {
this.purchase = purchase;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Purchace [customer=");
builder.append(customer);
builder.append(", purchase=");
builder.append(purchase);
builder.append("]");
return builder.toString();
}
@Override
public int compareTo(Purchace o) {
// TODO Auto-generated method stub
return this.purchase.compareTo(o.purchase);
}
}
Just another solution
- spiroso March 24, 2019