Author Topic: Programming Megathread  (Read 115080 times)

printf("#%x", (0x10000*red) | (0x100*green) | blue);
That's cheating. :P

Here's the "manual" version.
Code: [Select]
rgb = raw_input("Please input space-delimeted RGB values: ")
#rgb = "0 0 0"
val = rgb.split()

h = ["", "", "", "", "", ""]
iVal = [0, 0, 0]

iVal[0] = int(val[0])
iVal[1] = int(val[1])
iVal[2] = int(val[2])

hTable = ["A", "B", "C", "D", "E", "F"]

for x in range(0, 3):
if(iVal[x]/16 < 10):
h[x] = str(iVal[x]/16)
else:
h[x] = hTable[iVal[x]/16 - 10]
if(iVal[x] % 16 < 10):
h[x] += str(iVal[x] % 16)
else:
h[x] += hTable[iVal[x] % 16 - 10]

hex = '#'
hex += h[0]
hex += h[1]
hex += h[2]

print("Hex color code: " + hex)

That's cheating. :P

Depends what you're trying to do. I used default formatting for a textual representation, but I generated the hex with math just fine. Splitting hairs about how you convert data to text is kind of like me calling you out for using int() to generate a number from a string :p

Depends what you're trying to do. I used default formatting for a textual representation, but I generated the hex with math just fine. Splitting hairs about how you convert data to text is kind of like me calling you out for using int() to generate a number from a string :p
I was just joking. ;)


printf("#%x", (0x10000*red) | (0x100*green) | blue);
that's a great way to turn the values into the hex string, but you start out with a string, not just three integers
which I obviously didn't say explicitly

anyway, on a related note
if you know me, you know I love regular expressions (or maybe you don't know that whether you know me or not)
so I made one for RGB values
(?:rgba?)?\(?(\d{1,3}),?\s*(\d{1,3}),?\s*(\d{1,3})(?:,?\s*(\d{1,3}))?\)?
question mark count: 10

it will match all of the following:
244 55 66
244, 55, 66
244,55,66
(244, 55, 66)
rgb(244, 55 66)
rgba(244, 55,66, 0)

three groups, with the values 244, 55, and 66. optionally the alpha value may be there, and in the one where it is, it's 0
it's just a regular expression though, and it does not care one bit if you have a fourth value without having typed "rgba," nor if you type "rgba" with only three values
however if that first bit is present it must be either "rgb" or "rgba" because while it would certainly be possible to allow you to leave some out, then it would look like (?:r?g?b?a?)? and honestly who wants to see that many question marks??????
now that I think about it I also could've gone with something like (?:[rgba]{1,4})? but then you'd be able to type junk like "rrrr(1, 2, 3)" and quite frankly I wouldn't stand for that
regex for hex codes would be a lot simpler

also bluetoothboy 2 things:
1. yours can only go from rgb to hex but what about the other way around
2. you should move on to python 3

Here's hex string into an rgb structure
Code: [Select]
void hexToTuple(const char *hex, Tuple &rgb)
{
long hexI = strtol(hex, NULL, 16);
rgb.r = (hexI >> 16) & 0xFF;
rgb.g = (hexI >> 8) & 0xFF;
rgb.b = hexI & 0xFF;
}

I could have been far more... efficient using a familiar language (like C#, that's my jam), but for some reason I decide to try Python, which I've not used before. It's pretty neat seeing all the ways you guys tackled the problem though.

Also I was using Python 2 because I was using an iPad IDE (I have no laptop). A Python 3 would have cost me money. Not worth it for this.
« Last Edit: February 04, 2016, 11:52:41 AM by BluetoothBoy »

Anyone have any books they recommend for C and/or C#?  Preferably on Amazon kindle unlimited but open to suggestions, looking to read moar.

Why C# or C, those are two really different languages. I wish I could force myself to do more stuff in C though, you can do some really cool stuff with it.

I've never really been a huge fan of learning through books for programming but I've heard a lot of good things about anything by O'Reilly. They have books on pretty much everything too.


I could have been far more... efficient using a familiar language (like C#, that's my jam), but for some reason I decide to try Python, which I've not used before. It's pretty neat seeing all the ways you guys tackles the problem though.

Also I was using Python 2 because I was using an iPad IDE (I have no laptop). A Python 3 would have cost me money. Not worth it for this.

I'm a pretty big fan of C#, I really need to start doing more stuff with it. I always start some small stupid project get some part of it done and then forget about it.

I hate obfuscated javascript code
mediafire rarely works for me*, and for SOME godforsaken reason people continue to use it in modern times, so I'm trying to figure out a way to deal with these two problems
my best bet right now is to find a way to get the link directly to the file, but this is made very difficult because the code is obfuscated and otherwise there's no obvious way to do it

*pressing the download button just frickin refreshes the page after sitting there for a few seconds. wtf is that about. I've tried logging in and turning off my adblocker, but nothing works. it just seems like it really doesn't want me to download certain files

You mean actually obfuscated, or just minified?
A lot of people run their code through scripts that remove whitespace and replace identifier names with single letters.
The intent is to save bandwidth when you send the script to clients. Not sure how much it actually helps these days, but it's a commonly recommended step in speeding up web pages and a lot of people do it

some parts seem intentionally obfuscated, like this:

function DLP_mOnDownload(auv) {
    var atp = avd[Sp];
    atp(auv);
    return false;
}


that is the function that's called when the download button is clicked. Sp starts at 0, and avd[0] is weird
the auv argument is the button element itself
I also can't imagine why this function returns false. it doesn't seem to ever get used outside of the onclick things, so idk why they bothered returning anything at all

here's avd[0]:

avd[0] = function(atF) {
    var atU = atF.getAttribute('href');
    Ku(5, 'Retry Download: ' + atU);
    var auR = atF;
    var auL = $(atF).parent();
    var aup = $(atF).attr('href');
    var atN = aup.split('/');
    var aul = atN[atN.length - 3];
    $(auL).html('<span class="dl-text">Your download is starting...</span>');
    $('body').addClass('download_in_progress');
    atV();
    setTimeout(function() {
        $(auR).html('Still not downloading? <span>Repair your download.</span>').addClass('warning').attr('target', '_self').attr('href', '/download_repair.php?qkey=[I removed this]&dkey=' + aul.substring(0, 11) + '&origin=normal_download_click_repair');
        $(auL).children().replaceWith($(auR));
        Sp++;
    }, 4000);
    return true;
};


(I removed a try/catch block, because it only used a different method to do the same thing, in the case of a bad browser)
as you can see it increments Sp at the end, so the next time the button is clicked it'll call a different function, but avd[1] is just this:

avd[1] = function(atF) {
    return true;
};


at this point, I have to assume that either Ku() or atV() contains what I'm looking for, but god knows how long I'm going to have to keep going
edit: atV() is irrelevant and Ku() doesn't seem to be defined in this file. fml
« Last Edit: February 06, 2016, 11:17:07 AM by Foxscotch »

Anyone have any good recommendations for a style guide for C# to follow? I imagine strictly following one will make debugging a lot easier and help me finish projects since I'll be able to go back and not forget what I was thinking when I wrote what I wrote.

I also can't imagine why this function returns false. it doesn't seem to ever get used outside of the onclick things, so idk why they bothered returning anything at all
return false prevents event handlers from doing whatever they would normally do
like stopping a submit button from submitting form/refreshing page

Anyone have any good recommendations for a style guide for C# to follow? I imagine strictly following one will make debugging a lot easier and help me finish projects since I'll be able to go back and not forget what I was thinking when I wrote what I wrote.
if google has a style guide for a language, that's usually the one I use
one exception is python, where I follow PEP 8 instead
return false prevents event handlers from doing whatever they would normally do
like stopping a submit button from submitting form/refreshing page
shouldn't you use Event.preventDefault() for that? it's a lot more intuitive...

shouldn't you use Event.preventDefault() for that? it's a lot more intuitive...
I guess that used to not be a thing? so people just keep using return false out of habit