Author Topic: Programming Megathread  (Read 106438 times)

this post might contain some spoilers for day 5 part 2 of the advent of code thing
This might be a stupid question, but could somebody explain Regex's to me? I know they're regular expressions for sorting and stuff, but I have no clue how they work.
regular expressions aren't for sorting, they're for getting useful information out of text and/or checking whether or not that text matches a pattern

these are the two expressions I had in that code:
.*(\w\w).*\1.*
and
.*(\w)\w\1.*
the first one checks for rule 1:
Quote from: Advent of Code
It contains a pair of any two letters that appears at least twice in the string without overlapping, like xyxy (xy) or aabcdefgaa (aa), but not like aaa (aa, but it overlaps).
the second checks for rule 2:
Quote from: Advent of Code
It contains at least one letter which repeats with exactly one letter between them, like xyx, abcdefeghi (efe), or even aaa.

I'll break them down. first one's first
.*(\w\w).*\1.*
each dot stands for any character. \w stands for any letter. I could've used \w in place of the dots, but I think it looks marginally clearer this way, and there was no reason that would cause any problems
the asterisks are modifiers for the previous token. specifically, they mean that 0 or more of the previous token should be searched for. + would mean 1 or more, ? would mean 0 or 1, blah blah blah
the parentheses create a group. here, the group contains any two letters. then there's another .*
then there's the \1 which stands for whatever was matched by the first group. \2 would check for the second group, if there was one, and so on

so what it's looking for is any number of letters, followed by a pair of letters that is repeated at any point later in the text
in other words, exactly what rule 1 defines

the second one is pretty similar
.*(\w)\w\1.*
the middle bit is where it differs. the group only contains one letter, and we're still looking for a repeat later. but instead of looking for the repeat after any number of characters, we're specifically looking for it after ONE letter

this is a very good site both for testing regular expressions and learning simple regex syntax
there are different "flavors" of regex, because some people thought more features were necessary. for instance, python supports NAMED groups, which is not on that website. that site just has the features available in javascript's version of regex, although the syntax reference does mention a couple of things JS doesn't have
here's a page that summarizes some of the differences. if the language you're using isn't in that table, it probably inherits from one of the options that is


edit: I'm pretty convinced that nobody else here uses Python as much as I do, and I imagine there's few who use it at all, but I guess I'll post this here anyway, just in case
https://www.reddit.com/r/adventofcode/comments/3xkm1u/
« Last Edit: December 20, 2015, 06:31:53 AM by Foxscotch »



I am become death

clarification: I used up an afternoon to make an image archive in the style of 4chan using html and css
« Last Edit: December 30, 2015, 05:13:09 PM by Jairo »



Wrong thread?
I thought this thread had been revamped to include web development too


I thought this thread had been revamped to include web development too
Looks like web design more than programming anything, but idk what you did

for some reason I always cringe at programming

Man I love LINQ

For those of you who don't know what LINQ is (Probably the majority) it's sort-of like SQL except for coding (in C#). Like if you for example have an array of objects named travelLocations you can do:
var selections = from location in travelLocations
                where location.planePrice < 575.36
                select location;

foreach(var location in selections)
    Console.WriteLine(location.description);


And that will give you the description of every single travel location that has a plane price of less than $575.36

My specific use of LINQ in this case was that I had a 2D array of chars (char[][]) that I was using to construct a larger string. Each one of the arrays inside of it was a seperate line, so it was an array of strings basically, except I needed to use chars because I can't individually set the chars of a string without wonky manipulation.

So I thought to myself "What's the easiest way to merge this back into a string how I want it?"
And then I tried out using LINQ
string final = String.Join("\r\n",
                           (from x in stringBuilt
                           select String.Join("", x).TrimEnd(' ')).ToArray());


It selects every char[] in the char[][], and selects itself joined together and trimmed. Then it takes all that match that and puts every single one into an array (So we have an array of strings, each one is a seperate line) and then merges them together with a newline.

That saves probably 15 lines of code and is probably similarly efficient. Yay laziness!
« Last Edit: December 30, 2015, 10:32:25 PM by Ipquarx »

Yeah I use LINQ pretty extensively
Although I tend to use it in the other format (not sure what it's called/if it has a specific name) like so:

var selections = travelLocations.Where(l => l.planePrice < 576.36).Select(l => l.location);

Not sure why, I've used both but at some point I just stuck with that one

for some reason I always cringe at programming

not to sound aggressive, but why? just curious lol

I don't know if this is uncharted territory, but does anyone here know what BASIC is?
Just wondering.

basic is a family of a bunch of similiar languages
a few different ones is what I first started with
and i use a decent amount of visual basic at work

I don't know if this is uncharted territory, but does anyone here know what BASIC is?
Just wondering.
it's the most widely understood language in the star wars universe, of course

not to sound aggressive, but why? just curious lol
it just does, I can't seem to be able to pin down why