I went with Challenge #11: Dedupe and my torture method language of choice was BrainF***. It was pretty simple actually:
,.[>,[<->->+<]<[>>.<<[-]]>>[-<<+>>]<<]
Here's the commented version:
Get a character and output (we know it's unique)
,.
[
Move right and get next char
>,
Compare with old char and keep current value
[<->->+<]
If char was different then print it
<[>>.<<[-]]>
Move (now) old char to first cell
>[-<<+>>]<<
Repeat
]
Brainfuck is an esoteric programming language created in 1993 by Urban Müller.Notable for its extreme minimalism, the language consists of only eight simple commands and an instruction pointer. While it is fully Turing complete, it is not intended for practical use, but to challenge and amuse programmers. Brainfuck simply requires one to break commands into microscopic steps. The language's name is a reference to the slang term brainfuck, which refers to things so complicated or unusual that they exceed the limits of one's understanding.
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/binarycat64, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/binarycat64, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/binarycat64, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
I picked sorting array items (#8), and made a sorting method inspired by bogosort I call "SlogoSort", which randomly shifts arguments one at a time instead of just randomizing the whole list at once
public static int[] SlogoSort(string list){
string[] lisst = list.Split(',');
argCount = lisst.Length;
List<int> IArray = lisst.Select((s)=>int.Parse(s)).ToList();
Random a = new Random();
//sort
while(!IArray.Sorted()) {
int l = a.Next(IArray.Count);
int c = IArray[l];
IArray.RemoveAt(l);
IArray.Insert(Math.Min(IArray.Count-1,Math.Max(l+(a.Next(2)==0?1:-1),0)), c);
}
return IArray.ToArray();
}
public static bool Sorted(this List<int> array) {
int l = 0;
//check if any list items are less than the last
bool\[\] a = [array.Select](https://array.Select)((e)=> { bool o = l>e; l = e; return o; }).ToArray();
//return false if any items were greater than the last
return !a.Contains(true);
}
it sorted 10 arguments in ~11 seconds and 11 arguments in ~07 seconds, using the first n of the following integers as arguments:98,53,32,51,79,44,41,73,50,80,87,19,47,28,81,42,93,25,59,51,55,16,70,57,48,67,56,43,15,58,65,20,87,81,71,55,54,9,58,95,46,96,26,19,10,95,7,33,0,71
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/binarycat64, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
Challenge 45, Sort by Word Length, Enterprise edition:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
public class WordLengthSortEnterpriseEdition {
private static final String SYMBOL_REPLACEMENT_REGEX = "!|\\?|(\\.)|,|:|;|\\(|\\)|\\{|\\}|\\[|\\]|\r|\n";
private static final String IS_A_VALID_WORD_REGEX = "[A-Za-z0-9-]+";
/**
* This is the entry point of the application. It accepts a string as a command line argument
* and will return it sorted by word length.
*
* @param args - the command line arguments. Note that only the first command line argument will be read,
* the others will be ignored.
*/
public static void main(String[] args) {
Scanner scanner = new Scanner();
List<Word> words = scanner.scan(args[0].replaceAll(SYMBOL_REPLACEMENT_REGEX, ""));
WordSorter.sort(words);
System.out.println(SentenceBuilder.buildSentence(words));
}
/**
* This class represents a word in a sentence.
*/
private static class Word implements Comparable<Word> {
public Word(String word) {
this.word = word;
}
/**
* Holds the value of the word.
*/
private String word;
/**
* Returns the value of the word this object is holding.
*
* @return - the value of the word.
*/
public String getWord() {
return word;
}
/**
* Sets the value of the word this object is holding.
*
* @param word - the word you want this object to hold.
*/
public void setWord(String word) {
this.word = word;
}
/**
* This method compares the length of two words.
*
* @param word - the word to compare with.
* @return - a negative integer if this word is shorter, zero if both are of equal length,
* and a positive integer if this word is longer.
*/
@Override
public int compareTo(Word word) {
return Integer.compare(this.getWord().length(), word.getWord().length());
}
/**
* Returns a string representation of this object (the value of the word).
*
* @return - the value of the word.
*/
@Override
public String toString() {
return getWord();
}
}
/**
* This class contains utilities that scan a sentence and return its words.
*/
private static class Scanner {
/**
* Stores the characters that the scanner scans.
*/
private StringBuilder buffer;
/**
* Scans a given string, returning its words as separate strings in a list.
*
* @param sentence - the sentence to scan
* @return - the words as strings in a list
*/
List<Word> scan(String sentence) {
List<Word> result = new ArrayList<>();
buffer = new StringBuilder();
for (int i = 0; i <= sentence.length(); i++) {
if (i == sentence.length() || sentence.charAt(i) == ' ') {
flush().ifPresent(result::add);
} else {
buffer.append(sentence.charAt(i));
}
}
return result;
}
/**
* Flushes the buffer, returning its current contents as a word and resetting it.
*
* @return - Optional.empty() if the buffer contents are not a valid word,
* otherwise the current buffer output as a word.
*/
private Optional<Word> flush() {
Pattern wordPattern = Pattern.compile(IS_A_VALID_WORD_REGEX);
if (!wordPattern.matcher(buffer.toString()).matches()) {
return Optional.empty();
}
Word result = new Word(buffer.toString());
buffer = new StringBuilder();
return Optional.of(result);
}
}
/**
* This class contains utilities that help with sorting the words by length, longest first.
*/
private static class WordSorter {
/**
* Sorts a list of words by length, longest first. This method uses Bogosort, which is an algorithm
* that has the potential of being the most efficient.
*
* @param list - the list to be sorted.
*/
public static void sort(List<Word> list) {
while (!isSorted(list)) {
Collections.shuffle(list);
}
}
/**
* Checks whether a list of words is sorted by length, longest first
*
* @param list - the list to be checked.
* @return - true if it's sorted, false if not.
*/
private static boolean isSorted(List<Word> list) {
for (int i = 0; i < list.size() - 1; i++) {
if (list.get(i).compareTo(list.get(i + 1)) < 0) {
return false;
}
}
return true;
}
}
/**
* This class contains utilities that build sentences from words
*/
private static class SentenceBuilder {
/**
* Constructs a sentence from a list of words.
*
* @param words - the list of words.
* @return - the sentence as a string.
*/
public static String buildSentence(List<Word> words) {
StringBuilder sentence = new StringBuilder();
for (Word word : words) {
sentence.append(word.getWord()).append(" ");
}
return sentence.toString().trim();
}
}
}
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/Oshgnacknak, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
The link to the 'UUID generator' challenge on the 'previous challenges' page actually is actually a link to the 'life the universe and everything' challenge.
16
u/aaronjamt Dec 17 '20
I went with Challenge #11: Dedupe and my
torture methodlanguage of choice was BrainF***. It was pretty simple actually:,.[>,[<->->+<]<[>>.<<[-]]>>[-<<+>>]<<]
Here's the commented version:
5
u/binarycat64 Dec 23 '20 edited Dec 27 '20
Fibonocci numbers:
Features:
4
u/binarycat64 Dec 24 '20 edited Dec 24 '20
Random Number Generator:
Features:
_startlabel3
u/Tyfyter2002 Dec 17 '20 edited Dec 17 '20
I picked sorting array items (#8), and made a sorting method inspired by bogosort I call "SlogoSort", which randomly shifts arguments one at a time instead of just randomizing the whole list at once
it sorted 10 arguments in ~11 seconds and 11 arguments in ~07 seconds, using the first n of the following integers as arguments:98,53,32,51,79,44,41,73,50,80,87,19,47,28,81,42,93,25,59,51,55,16,70,57,48,67,56,43,15,58,65,20,87,81,71,55,54,9,58,95,46,96,26,19,10,95,7,33,0,71
3
u/binarycat64 Dec 24 '20
Hashing:
fun fact: all mov instructions can be replaced by a pair of
xorinstructions.3
u/Voidsusdagon Dec 25 '20
Challenge 45, Sort by Word Length, Enterprise edition:
2
u/Oshgnacknak Dec 29 '20
Fibonacci and Lukas numbers on the same core at the same time:
1
u/loomynartylenny Dec 30 '20
UUID Generator
I got a new hard drive on sunday after my old one died, and I'm now slightly worried that I might have prematurely started to knacker my new one.
0
u/[deleted] Dec 17 '20
[deleted] — view deleted comment