Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 548 549 [550] 551 552 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 882365 times)

Avis-Mergulus

  • Bay Watcher
  • This adorable animal can't work.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8235 on: October 19, 2015, 06:10:22 am »

Avis-Mergulus:
What does it say you messed up with?
"Incorrect answer", so at least it didn't crash. I have no way of knowing what was in the tests, though.
Logged
“See this Payam!” cried the gods, “He deceives us! He cruelly abuses our lustful hearts!”

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #8236 on: October 19, 2015, 07:38:23 am »

I'm using Selenium to scrape my university's antique, dynamic, non-restful, "secure" course finder (ie, a part of the site that DOES NOT NEED AUTH)...

It's so horrible, yet I keep finding parts that are even more horrible. I don't even, anymore

this HTML code is for this:


look upon these works, ye mortals, and despair

(yes, that is a span within a td within a tr within a table within a td within a tr within a table just for kicks)
(yes, they used a '$' in a CSS selector)
(nothing like 'win0divNYU_CLS_DTL_CLASS_NBR$1' to brighten up your CSS)

edit: "table>tbody>tr>td>table>tbody>tr>td>table>tbody>tr>td"
« Last Edit: October 19, 2015, 07:50:06 am by Skyrunner »
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

strawberry-wine

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8237 on: October 19, 2015, 10:39:44 am »

this HTML code

I wonder which elder god they hired to design that
Logged

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8238 on: October 19, 2015, 12:47:34 pm »

I sincerely hope that those selectors are automatically generated by something.  Some kind of application framework, probably.

I've only had bad experiences with full stack application frameworks...
Logged
Through pain, I find wisdom.

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8239 on: October 19, 2015, 01:09:29 pm »

It's university software. It's a given that the code will be bad.

It may also date from a time during which table-based layouts were the shiz.
Logged

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #8240 on: October 19, 2015, 01:13:04 pm »

Spoiler (click to show/hide)

Like so? Not going to lie this program is making me smile in frustration and i'm going to be asking how to do this again next class.

That's exactly what people were referring to when they said repeating code fragments is bad. The first thing to wrap your head around when figuring out how programming works, and it can be a little difficult at first, is how to abstract out repeated instructions. Computer programs, especially big ones, spend a lot of time doing things over and over. Your job is to figure out a concise way of telling the computer what you want it to do, so you don't have to copy-paste those same commands again and again. Doing boring, repetitive things is the computer's job, not yours.

The first thing I would do when writing this program is define something like this at the top of the file:

Code: [Select]
const char* options[] = {"Rock", "Paper", "Scissors"};
This is an array of strings, containing the names of the choices the player could make ("const" just means you can't change it on accident later on). Why would we create this? Well, now we can do something like this further down:

Code: [Select]
compChoice--;
cout << "Your opponent picked: " << options[compChoice] << endl;

I'm assuming you exit the loop immediately if the user selects 0, otherwise the program would crash here due to accessing array index -1. Next, we need to abstract out the information that tells us whether the player won or not. How do we do that? Well, we could do it algorithmically, but there's so little information here that we can get away with just defining it the same way we did before. So let's add a second data definition that looks like this:

Code: [Select]
const char* options[] = {"Rock", "Paper", "Scissors"};
const int beats[] = {2, 0, 1};

Okay, lemme explain this. This is another array (of numbers this time), and it tells us what beats what. The number at a given index tells us what the choice at that index beats. So, for example, at index 0 in the array we have the number 2. This means "option 0 beats option 2". Look at the names above, and we see that that's true: Rock beats Scissors. Next, we have "option 1 beats option 0". Paper beats Rock. Last, we have "option 2 beats option 1". Scissors beats Paper.

So, now we can add a block further down like this:

Code: [Select]
compChoice = rand() % 3;
if(beats[userChoice] == compChoice) {
cout << "You win!" << endl;
}
else if(beats[compChoice] == userChoice) {
cout << "You lose..." << endl;
}
else {
cout << "Draw" << endl;
}

Now you have all the logic you need to tell if the player won or not, and your program is about one tenth of the size it was before. I think I need to start listening in history class now, so I'll let you try to figure this out. Let me know if you have any questions.

(Note that compValue is set to a value between 0 and 2 here, whereas in your version it was 1-3)
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8241 on: October 19, 2015, 01:22:45 pm »

That's a pretty cool idea for the "beats" array.

Code: [Select]
compChoice = rand() % 3;
if(beats[userChoice] == compChoice) {
cout << "You win!" << endl;
}
else if(beats[compChoice] == userChoice) {
cout << "You lose..." << endl;
}
else {
cout << "Draw" << endl;
}

But you inverted win and lose here. If the user chooses "0" (Rock), then beats[userChoice] == "2" (Scissors). You then have that if the computer picks "2" (Scissors) then the program prints "You win". i.e player (rock) beat computer (scissors).

If you write out the logic checks in the other order it's clearer what's happening, because the logic reads closer to English word order, which reduces the chance of getting confused (obviously set up the array correctly for this):

Code: [Select]
compChoice = rand() % 3;
if(compChoice == beats[userChoice]) {
cout << "You lose..." << endl;
}
else if(userChoice == beats[compChoice]) {
cout << "You win!" << endl;
}
else {
cout << "Draw" << endl;
}

Now, it reads as almost-English: "if compChoice beats userChoice print you lose" and "if userChoice beats compChoice print you win".
« Last Edit: October 19, 2015, 05:19:32 pm by Reelya »
Logged

Avis-Mergulus

  • Bay Watcher
  • This adorable animal can't work.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8242 on: October 19, 2015, 03:03:38 pm »

Code: [Select]
def count_matrix(matrix, query):
    matrixFragment = matrix[query[1]-1:query[3]]
    count = 0
    for line in matrixFragment:
        count += sum(line[query[0]-1:query[2]])
    return count
   
initText = open('input.txt')
Lines = initText.readlines()

param = list(map(int, Lines.pop(0).split()))
matrix = [x.split() for x in Lines[:param[0]]]
matrix = [list(map(int, x)) for x in matrix]
queries = [x.split() for x in Lines[param[0]:param[0]+param[2]]]
queries = [list(map(int, x)) for x in queries]

for query in queries:
    print(count_matrix(matrix, query))
So I figured out what was wrong here, but now it's a little too slow. I always have trouble with optimization because I generally have little idea what's fast and what's slow, so what exactly is the slowest part in my code here? The generators or the looping sum thingy? Would substituting the array of arrays with a non-nested array and some index trickery be faster?
Logged
“See this Payam!” cried the gods, “He deceives us! He cruelly abuses our lustful hearts!”

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8243 on: October 19, 2015, 03:27:15 pm »

Now, it reads as almost-English: "if compChoice beats userChoice print you lose" and "if userChoice beats compChoice print you win".

Clearly there is a naming problem here. You can interpret beats[userChoice] either as "what userChoice beats" or as "what beats userChoice". As good programming practice I suggest encapsulating the whole thing in a two-argument function: int winner(int choice1, int choice2), that returns 1 when choice1 wins, 2 when choice2 wins, and 0 for a tie.
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8244 on: October 19, 2015, 03:43:09 pm »

Another way would be to make choice an object and have a "beats" method.

if(compChoice.beats(userChoice))
{
    // you lose!
}

is pretty unambiguous. But i guess if you have a "pick winner" function then you can do a switch statement instead of if statements

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8245 on: October 19, 2015, 03:44:25 pm »

Why would you make compChoice an object?  That seems rather unnecessary for RPS, guys.
We do not need 100 lines of code to print "Hello World".
« Last Edit: October 19, 2015, 03:46:45 pm by TheBiggerFish »
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8246 on: October 19, 2015, 04:03:57 pm »

Yeah, making it an object is probably overkill. It works as a demonstration of how to solve other problems, obviously, but a big part of programming is finding the best tools for a job.
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8247 on: October 19, 2015, 04:24:28 pm »

But in this case, it would literally be only a few extra lines of code

struct choice
{
    int val;
    bool beats(val2) { return val == beats[val2]; }
}

making a struct hardly breaks the bank with complexity. it's basically the same overhead as making a function, except nobody ever goes "whoa, cool down with making the functions, you're over complexifying things!"

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8248 on: October 19, 2015, 04:28:23 pm »

But in this case, it would literally be only a few extra lines of code

struct choice
{
    int val;
    bool beats(val2) { return val == beats[val2]; }
}

making a struct hardly breaks the bank with complexity. it's basically the same overhead as making a function, except nobody ever goes "whoa, cool down with making the functions, you're over complexifying things!"
Weeeeeelllllll.
You can probably have someone do that.
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

nogoodnames

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8249 on: October 19, 2015, 04:33:54 pm »

We are talking about a school project here. Introducing new concepts that haven't been covered in the class probably isn't helping.
Logged
Life is, in a word, volcanoes.
                        - Random human lord
Pages: 1 ... 548 549 [550] 551 552 ... 796