Pleased I knocked it out pretty quickly, possibly used too many variables but reads OK to me. Side note talking at work about hardest programming languages, I had brainfuck down as hard but Malbolge is another level of toughness.
package com.company;
import java.text.DecimalFormat;
public class XmasLights {
private final int hoursLightRequired;
private final int batteryCapacity = 1200;
private final int ledmA = 20;
private final double ledVoltage = 1.7;
private final int ledsPerCiruit = 5;
private final int batteryVoltage = 9;
public XmasLights(int hoursLightRequired) {
this.hoursLightRequired = hoursLightRequired;
}
public void go() {
int leds = amountOfLedsForTime();
resistanceNeeded();
drawCircuit(leds);
}
//part 1, input is the max amount of leds we can have for that time
public int amountOfLedsForTime() {
int maxLeds = (batteryCapacity / hoursLightRequired / ledmA) * ledsPerCiruit;
return maxLeds;
}
public void drawCircuit(int lights) {
String serialCircuit = "--|>|---|>|---|>|---|>|---|>|--";
String joiningBars = " | |";
System.out.println("Scheme:");
int ledsToDraw = lights / ledsPerCiruit;
for (int i = 0; i < ledsToDraw; i++) {
if (i == 0) {
System.out.println("*" + serialCircuit + "*");
} else {
System.out.println(serialCircuit);
}
if (i < ledsToDraw - 1) {
System.out.println(joiningBars);
}
}
}
public double resistanceNeeded() {
double voltageOverResister = batteryVoltage - (ledsPerCiruit * ledVoltage);
double ohms = voltageOverResister / (batteryCapacity / 1000d);
DecimalFormat df = new DecimalFormat("#.###");
System.out.println("Resistance needed: " + df.format(ohms));
return ohms;
}
}
Thursday, 24 December 2015
Tuesday, 22 December 2015
Lazyness
Fed up of writing to a text file everyday what I do in the day ready for tomorrow's scrum, from what I have read I can make a bat file put into windows start up folder and no more typing the same thing everyday.
It does need a couple of changes, keen to keep one file per year which then this program would create as needed once updated. It does seem a bit much on the try catch however I do not want to create new entries if I need to reboot during the work day.
Also seems strange to me that to add to the start of the file I need to write it all again.
package com.company;
import java.io.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/**
* Created by R on 22/12/2015.
*/
public class diaryWriter {
private String path = "";
private LocalDate today = LocalDate.now();
private DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("dd-MM-yyy");
private String message;
public void writeMessageToDiary() {
message = today.format(formatter)
+ "\nTested"
+ "\nTest Cases"
+ "\nRaised"
+ "\nComplete"
+ "\nNotes"
+ "\n\n";
//Only write to file if not been written to today
if ( !isTodayLatest() ) {
prependMessageToFile();
}
}
private boolean isTodayLatest() {
String firstLine = "";
try (BufferedReader bufferedReader =
new BufferedReader(new FileReader(path))) {
firstLine = bufferedReader.readLine();
} catch (IOException ex) {
ex.printStackTrace();
}
return firstLine.equals(today.format(formatter));
}
private void prependMessageToFile() {
//Read file to a string
StringBuilder fileString = new StringBuilder();
String line;
try (BufferedReader bufferedReader =
new BufferedReader(new FileReader(path))) {
while ( (line = bufferedReader.readLine()) != null) {
fileString.append(line + "\n");
}
} catch (IOException e) {
e.getStackTrace();
}
//Write complete file back to path
try (BufferedWriter bufferedWriter =
new BufferedWriter(new FileWriter(path))){
bufferedWriter
.append(fileString.insert(0, message).toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
It does need a couple of changes, keen to keep one file per year which then this program would create as needed once updated. It does seem a bit much on the try catch however I do not want to create new entries if I need to reboot during the work day.
Also seems strange to me that to add to the start of the file I need to write it all again.
package com.company;
import java.io.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/**
* Created by R on 22/12/2015.
*/
public class diaryWriter {
private String path = "";
private LocalDate today = LocalDate.now();
private DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("dd-MM-yyy");
private String message;
public void writeMessageToDiary() {
message = today.format(formatter)
+ "\nTested"
+ "\nTest Cases"
+ "\nRaised"
+ "\nComplete"
+ "\nNotes"
+ "\n\n";
//Only write to file if not been written to today
if ( !isTodayLatest() ) {
prependMessageToFile();
}
}
private boolean isTodayLatest() {
String firstLine = "";
try (BufferedReader bufferedReader =
new BufferedReader(new FileReader(path))) {
firstLine = bufferedReader.readLine();
} catch (IOException ex) {
ex.printStackTrace();
}
return firstLine.equals(today.format(formatter));
}
private void prependMessageToFile() {
//Read file to a string
StringBuilder fileString = new StringBuilder();
String line;
try (BufferedReader bufferedReader =
new BufferedReader(new FileReader(path))) {
while ( (line = bufferedReader.readLine()) != null) {
fileString.append(line + "\n");
}
} catch (IOException e) {
e.getStackTrace();
}
//Write complete file back to path
try (BufferedWriter bufferedWriter =
new BufferedWriter(new FileWriter(path))){
bufferedWriter
.append(fileString.insert(0, message).toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sunday, 20 December 2015
Luhn validator
Got a bit confused whilst testing something at work as one of the luhn validation tools at work decided to change it's mind around what the check digit should be, I wrote my own:
Luhn algorithm
package com.company;
public class luhnValidator {
public boolean isValidLuhn(String inputNumber) {
int[] numbers = stringToIntArray(inputNumber);
specialDoubler(numbers);
int total = sumIntArray(numbers);
return (total * 9) % 10 == numbers[numbers.length-1] ;
}
public int checkDigitCalc(String numberWithNoCheckDigit) {
int[] numbers = stringToIntArray(numberWithNoCheckDigit);
specialDoubler(numbers);
int total = sumIntArray(numbers);
return (total * 9) % 10;
}
private int[] stringToIntArray(String convertToNumbers) {
char[] brokenToChars = convertToNumbers.toCharArray();
int[] numbers = new int[brokenToChars.length];
for (int i = 0; i < brokenToChars.length; i++) {
numbers[i] = Character.getNumericValue(brokenToChars[i]) ;
}
return numbers;
}
/**
* Double every second number if greater than 10 add the products together
*/
private void specialDoubler(int[] numbers) {
int x2;
for (int i = numbers.length-2; i >= 0; i -= 2) {
x2 = numbers[i] * 2;
if (x2 < 10) {
numbers[i] = x2;
} else {
numbers[i] = (x2 - 10) + 1;
}
}
}
private int sumIntArray(int[] numbers) {
int total = 0;
for (int i = 0; i < numbers.length-1; i++ ) {
total += numbers[i];
}
return total;
}
}
Could still be optimised but it was fun to play with the intellij tests for jUnit, as they did help pick out a simple error where I missed out the '>' in >=.
Luhn algorithm
package com.company;
public class luhnValidator {
public boolean isValidLuhn(String inputNumber) {
int[] numbers = stringToIntArray(inputNumber);
specialDoubler(numbers);
int total = sumIntArray(numbers);
return (total * 9) % 10 == numbers[numbers.length-1] ;
}
public int checkDigitCalc(String numberWithNoCheckDigit) {
int[] numbers = stringToIntArray(numberWithNoCheckDigit);
specialDoubler(numbers);
int total = sumIntArray(numbers);
return (total * 9) % 10;
}
private int[] stringToIntArray(String convertToNumbers) {
char[] brokenToChars = convertToNumbers.toCharArray();
int[] numbers = new int[brokenToChars.length];
for (int i = 0; i < brokenToChars.length; i++) {
numbers[i] = Character.getNumericValue(brokenToChars[i]) ;
}
return numbers;
}
/**
* Double every second number if greater than 10 add the products together
*/
private void specialDoubler(int[] numbers) {
int x2;
for (int i = numbers.length-2; i >= 0; i -= 2) {
x2 = numbers[i] * 2;
if (x2 < 10) {
numbers[i] = x2;
} else {
numbers[i] = (x2 - 10) + 1;
}
}
}
private int sumIntArray(int[] numbers) {
int total = 0;
for (int i = 0; i < numbers.length-1; i++ ) {
total += numbers[i];
}
return total;
}
}
Could still be optimised but it was fun to play with the intellij tests for jUnit, as they did help pick out a simple error where I missed out the '>' in >=.
More links
https://www.ifixit.com/ actually looks handy and has my phone on there with a guide to replace the battery.
Article I liked http://allankelly.blogspot.co.uk/2015/10/software-has-diseconomies-of-scale-not.html
And a website I liked, seems simple enough and they accept bitcoin https://www.braintreepayments.com/
Article I liked http://allankelly.blogspot.co.uk/2015/10/software-has-diseconomies-of-scale-not.html
And a website I liked, seems simple enough and they accept bitcoin https://www.braintreepayments.com/
Wednesday, 16 December 2015
Quick challenge
Quick challenge after work, avoiding putting a front end on the lego app sigh.
package com.company;
import java.util.ArrayList;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String inputText = "The legend, propagated by email and message boards, purportedly demonstrates that readers can understand the "
+ "meaning of words in a sentence even when the interior letters of each word are scrambled. As long as all the necessary letters "
+ "are present, and the first and last letters remain the same, readers appear to have little trouble reading the text.";
String aPattern = "\\w+";
Pattern pattern = Pattern.compile(aPattern);
Matcher matcher = pattern.matcher(inputText);
ArrayList<String> words = new ArrayList<>();
while (matcher.find()) {
words.add(matcher.group());
}
StringBuilder outputWords = new StringBuilder(inputText.length());
String shuffledWord, middle;
char firstLetter, lastLetter;
for (String word : words) {
if(word.length() > 3) {
//build messed up words keeping edges same
firstLetter = word.charAt(0);
lastLetter = word.charAt(word.length()-1);
middle = wordShuffler(word.substring(1, word.length() - 1));
shuffledWord = firstLetter + middle + lastLetter;
} else {
shuffledWord = word;
}
outputWords.append(shuffledWord);
}
String nonWord = "\\W+";
pattern = Pattern.compile(nonWord);
matcher = pattern.matcher(inputText);
while (matcher.find()) {
outputWords.insert(matcher.start(), matcher.group());
}
System.out.println(inputText);
System.out.println(outputWords);
}
public static String wordShuffler(String wordToShuffle) {
char[] letterArray = wordToShuffle.toCharArray();
Random random = new Random();
int nextValue;
char swap;
for (int i = 0; i < letterArray.length; i++) {
swap = letterArray[i];
nextValue = random.nextInt(letterArray.length);
letterArray[i] = letterArray[nextValue];
letterArray[nextValue] = swap;
}
return new String(letterArray);
}
}
package com.company;
import java.util.ArrayList;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String inputText = "The legend, propagated by email and message boards, purportedly demonstrates that readers can understand the "
+ "meaning of words in a sentence even when the interior letters of each word are scrambled. As long as all the necessary letters "
+ "are present, and the first and last letters remain the same, readers appear to have little trouble reading the text.";
String aPattern = "\\w+";
Pattern pattern = Pattern.compile(aPattern);
Matcher matcher = pattern.matcher(inputText);
ArrayList<String> words = new ArrayList<>();
while (matcher.find()) {
words.add(matcher.group());
}
StringBuilder outputWords = new StringBuilder(inputText.length());
String shuffledWord, middle;
char firstLetter, lastLetter;
for (String word : words) {
if(word.length() > 3) {
//build messed up words keeping edges same
firstLetter = word.charAt(0);
lastLetter = word.charAt(word.length()-1);
middle = wordShuffler(word.substring(1, word.length() - 1));
shuffledWord = firstLetter + middle + lastLetter;
} else {
shuffledWord = word;
}
outputWords.append(shuffledWord);
}
String nonWord = "\\W+";
pattern = Pattern.compile(nonWord);
matcher = pattern.matcher(inputText);
while (matcher.find()) {
outputWords.insert(matcher.start(), matcher.group());
}
System.out.println(inputText);
System.out.println(outputWords);
}
public static String wordShuffler(String wordToShuffle) {
char[] letterArray = wordToShuffle.toCharArray();
Random random = new Random();
int nextValue;
char swap;
for (int i = 0; i < letterArray.length; i++) {
swap = letterArray[i];
nextValue = random.nextInt(letterArray.length);
letterArray[i] = letterArray[nextValue];
letterArray[nextValue] = swap;
}
return new String(letterArray);
}
}
Sunday, 13 December 2015
yousician
Actually impressed by an app, yousician, it actually looks good, I always wanted to learn to play then becoming an adult all the teachers only want to teach dam kids. Never mind :).
Pleased with the lego app, I got some more tests running and talking to people about should tests be run only the public methods or the private ones using reflection.
Pleased with the lego app, I got some more tests running and talking to people about should tests be run only the public methods or the private ones using reflection.
Sunday, 6 December 2015
Books and colours
Book I've been reading, Icarus deception by Seth Godin, enjoyed it, about making art is not comfortable and makes us feel vulnerable which is how you know you're making something new. I did like that Icarus could also fly too low.
Article I found interesting, Crash only software.
Sit stand desk, stand desks are normally so expensive but this looks good for the cost, varidesk.
12 years in software development article.
Been playing with a Lego picture maker and trying to get rgb values from Lego colours, after doing an average of the rgb values and using the closest average made a lot of thing purple. Delta-E 1976, fast enough with results accurate enough for what I want. My implementation, more info on it.
public static int[] getLegoColourEquiv(int[] rgb) {
int[] returnColourRGB = {1,1,1};
if(rgb == null) {
return returnColourRGB;
}
double lowestDelta = Double.MAX_VALUE;
double deltaE;
for (int[] colour : colours) {
//DeltaE76 (CIE 1976)
deltaE = Math.sqrt(
Math.pow(rgb[0] - colour[0], 2) +
Math.pow(rgb[1] - colour[1], 2) +
Math.pow(rgb[2] - colour[2], 2)
);
if(deltaE < lowestDelta) {
lowestDelta = deltaE;
returnColourRGB = Arrays.copyOf(colour,
colour.length);
}
}
return returnColourRGB;
}
Article I found interesting, Crash only software.
Sit stand desk, stand desks are normally so expensive but this looks good for the cost, varidesk.
12 years in software development article.
Been playing with a Lego picture maker and trying to get rgb values from Lego colours, after doing an average of the rgb values and using the closest average made a lot of thing purple. Delta-E 1976, fast enough with results accurate enough for what I want. My implementation, more info on it.
public static int[] getLegoColourEquiv(int[] rgb) {
int[] returnColourRGB = {1,1,1};
if(rgb == null) {
return returnColourRGB;
}
double lowestDelta = Double.MAX_VALUE;
double deltaE;
for (int[] colour : colours) {
//DeltaE76 (CIE 1976)
deltaE = Math.sqrt(
Math.pow(rgb[0] - colour[0], 2) +
Math.pow(rgb[1] - colour[1], 2) +
Math.pow(rgb[2] - colour[2], 2)
);
if(deltaE < lowestDelta) {
lowestDelta = deltaE;
returnColourRGB = Arrays.copyOf(colour,
colour.length);
}
}
return returnColourRGB;
}
Subscribe to:
Posts (Atom)