Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
stephen-maina / Codility PermMissingElem Solution
Created April 12, 2015 13:47
Codility PermMissingElem Solution
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Collectors;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int first=0;
int last=A.length;
@stephen-maina
stephen-maina / Email functionalitoes javamail
Last active November 21, 2022 19:51
Basic Email activities using javamail and saving of files on a cloud storage facility eg. AWS
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
@stephen-maina
stephen-maina / WebSockets
Created April 12, 2015 13:53
Using AtmosphereFramework with JerseyFramework for asynchronous communicationn with one URL, using stackoverflow as inspiration for posts
package com.api.services.websockets;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import javax.servlet.http.HttpServletRequest;
@stephen-maina
stephen-maina / AWS upload file
Last active August 29, 2015 14:19
AWS upload file
// save uploaded file to a defined location on the server
private Response saveFile(InputStream uploadedInputStream, String fileName,
String name, String description, String albumId, String userId) {
try {
File tempFile = File.createTempFile("doc-" + userId + "-"
+ albumId, ".tmp");
// tempFile.createNewFile();
tempFile.deleteOnExit();
File thumbFile = File.createTempFile("doc-" + userId + "-"
+ albumId + "thumb", ".tmp");
@stephen-maina
stephen-maina / AWS delete file
Created April 12, 2015 14:38
AWS Delete File
public void deleteAWS(String link) throws IOException {
AmazonS3 s3Conn = new AmazonS3Client(
new PropertiesCredentials(
Document.class
.getResourceAsStream("/com/api/AwsCredentials.properties")));
DeleteObjectsRequest multiDelete = new DeleteObjectsRequest(
"schoolmart-document");
List<KeyVersion> keys = new ArrayList<DeleteObjectsRequest.KeyVersion>();
@stephen-maina
stephen-maina / TapeEquilibrium
Created April 12, 2015 17:50
TapeEquilibrium Codility
import java.lang.Math;
import java.util.stream.IntStream;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length==0)
{
return 0;
}
if(A.length==1){
@stephen-maina
stephen-maina / PermCheck lesson 2 codility
Created April 13, 2015 05:46
PermCheck lesson 2 codility
import java.util.stream.IntStream;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length==0){
return 0;
}
int[] sorted=IntStream.of(A).sorted().toArray();
for(int index=0;index<sorted.length;index++){
@stephen-maina
stephen-maina / PermMissingElem Codility lesson 2
Created April 13, 2015 05:59
PermMissingElem Codility lesson 2
import java.util.stream.IntStream;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length==0){
return 1;
}
int []sorted = IntStream.of(A).sorted().toArray();
if(sorted[sorted.length-1]!=sorted.length+1){
return sorted.length+1;
@stephen-maina
stephen-maina / MissingInteger Codility Lesson 2
Created April 13, 2015 12:35
MissingInteger Codility Lesson 2
import java.util.stream.IntStream;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length==1){
return A[0]+1;
}
int[] sorted=IntStream.of(A).sorted().toArray();
int temp=sorted[0];
for(int index=1;index<sorted.length;index++){
@stephen-maina
stephen-maina / MaxCounters Codility lesson 2
Created April 13, 2015 14:48
MaxCounters Codility...not fast enough though.
class Solution {
public int[] solution(int N, int[] A) {
// write your code in Java SE 8
int length=A.length;
int max=N+1;
int []result=new int[N];
int maxValue=0;
for(int index=0;index<length;index++){