Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

05-07-2017 , 09:02 PM
Quote:
Originally Posted by lostmypw
http://stackoverflow.com/questions/7...g-an-interview



I don't get how this is a dynamic programming problem? The guy who replied and gave a long reply didn't show any relation to subproblems. It looks like his solution is to just iterate through the points, find the distance, and then keep the points with the lowest distance for the result set.
It looks like he's performing divide and conquer to cut down the iterative steps? I agree the explanation doesn't seen to make sense to me.

Sent from my SM-G900R4 using Tapatalk
Programming homework and newbie help thread Quote
05-08-2017 , 11:54 AM
1. I am messing around with web scrapers mechanize/nokogiri and have a question.

i want my app to automate something that requires a user giving me their password/user name for example.com... then we go to example.com and do something.

how do i create different streams/paths(i don't know what the word is to describe this) for each user so that requests could be handled concurrently instead of queuing them?

(right now i am just messing around with the code i have written in Rails Console...)

2. when i am in Rails Console, which is being used from an IDE, and I use mechanize/nokogiri to log into example.com what is my IP address being shown to that site? Is it going to be from the IDE (My computer -> IDE -> Example.com)?

3. Related to #2, how do I differentiate it so that users aren't all being logged in under the same IP?
Programming homework and newbie help thread Quote
05-08-2017 , 12:40 PM
Quote:
Originally Posted by OmgGlutten!
1. I am messing around with web scrapers mechanize/nokogiri and have a question.

i want my app to automate something that requires a user giving me their password/user name for example.com... then we go to example.com and do something.

how do i create different streams/paths(i don't know what the word is to describe this) for each user so that requests could be handled concurrently instead of queuing them?

(right now i am just messing around with the code i have written in Rails Console...)
I think for this you would need to instantiate multiple agents, one for each "path" you want to travel down. You can think of each of these agents as their own incognito browser. Here's an example from docs doing a user login, then you could just create an agent_whatever for each user you have. https://github.com/sparklemotion/mec...ickr_upload.rb

I'm not sure about concurrently since the http requests are going to take some time. Maybe have them in a background job like a sidekiq worker? Not sure the best way to handle that tbh.

Quote:
Originally Posted by OmgGlutten!
2. when i am in Rails Console, which is being used from an IDE, and I use mechanize/nokogiri to log into example.com what is my IP address being shown to that site? Is it going to be from the IDE (My computer -> IDE -> Example.com)?


3. Related to #2, how do I differentiate it so that users aren't all being logged in under the same IP?
By IDE, are you meaning like RubyMine or in a browser like Cloud9? IP address issue would probably be the same regardless, just curious.

IP address is at your local network level and Mechanize can't really change that without the help of something else. One thing you could do is setup a proxy, similar to this recommendation: http://stackoverflow.com/questions/1...ddress-in-ruby.
The key line of code is this "agent.set_proxy '78.186.178.153', 8080", here's another example: https://gist.github.com/emergent/3983870. I haven't had a need for that though, so can't give more specific information.

I learned quite a bit about scraping from the Ruby Bastards book, specifically:
http://ruby.bastardsbook.com/chapters/mechanize/
http://ruby.bastardsbook.com/chapters/web-crawling/
http://ruby.bastardsbook.com/chapter...ies-mechanize/
Programming homework and newbie help thread Quote
05-08-2017 , 02:45 PM
Quote:
Originally Posted by just_grindin
It looks like he's performing divide and conquer to cut down the iterative steps? I agree the explanation doesn't seen to make sense to me.

Sent from my SM-G900R4 using Tapatalk
I think he is just doing something like this:

x = [1 2 3 4 5 6 7 8 9]
i = 5

Then splitting between 5 and 6 so you end up with:

x1 = [1 2 3 4]
x2 = [6 7 9 9]

then taking the absolute value of each(i - x1) vs each(i - x0).

He did mention this was O(n^2) (or something horrible like that), so I'm guessing he is tracing over each possible split of x, and doing the operations on each x1 and x2.

I'm not sure if that's what's being asked or if that's what the answer is saying.
Programming homework and newbie help thread Quote
05-08-2017 , 03:40 PM
Quote:
Originally Posted by OmgGlutten!
1. I am messing around with web scrapers mechanize/nokogiri and have a question.
sounds good so far...


Quote:
2. when i am in Rails Console....
there should be no reason you're using rails console. open a file, save it as whatever.rb, require mechanize, and now mess around...
Programming homework and newbie help thread Quote
05-08-2017 , 03:59 PM
Quote:
Originally Posted by daveT
I'm not sure if that's what's being asked or if that's what the answer is saying.
After much thought I've concluded the guy who replied was smoking something and the 7 or so people who upvoted and replied to him were also morons. Shame they froze that thread in such a state.
Programming homework and newbie help thread Quote
05-08-2017 , 04:25 PM
I loved the comment from Charlie Martin.

You know, unless the job was specifically for an operations research job, this strikes me as a disqualifying interview question. That is, one should gently but firmly shake the hand of the interviewer, thank them, and explain that using this kind of silly question in an interview proves they are not the sort of place one should wish to be employed

Welcome to the interview life, lostmypw. Everyone has had a few head-scratchers. Searching for answers to some these are about as useful and mind-expanding as searching for Big Foot.
Programming homework and newbie help thread Quote
05-09-2017 , 03:24 PM
In Python, is there a simple reason why a program would work perfectly fine, but then fail to work once code was put into a main() function? When I put code into such a function, I start getting an error that says "NameError: Global name 'player' not defined." If I just take all the code I put into main() out of the function, everything works fine.

I'm hoping this is just some really simple, stupid thing that I'm overlooking and that my question will therefore be clear. If not I will post the actual code and point to the areas that are causing the error.

Thanks
Programming homework and newbie help thread Quote
05-09-2017 , 03:43 PM
Please post some before / after code.
Programming homework and newbie help thread Quote
05-09-2017 , 05:42 PM
Here's the full code of the game. A lot of this is a sort of template provided by the teacher so there are a lot of things that aren't supposed to be changed.

Code:
import pygame


SCR_WID, SCR_HEI = 640, 480
class Player():
        def __init__(self):
                self.x, self.y = 16, SCR_HEI/2
                self.speed = 3
                self.padWid, self.padHei = 8, 64
                self.score = 0
                self.scoreFont = pygame.font.Font("imagine_font.ttf", 64)
       
        def scoring(self):
                scoreBlit = self.scoreFont.render(str(self.score), 1, (255, 255, 255))
                screen.blit(scoreBlit, (32, 16))
                if self.score == 10:
                        print ("player 1 wins!")
                        exit()
       
        def movement(self):
                keys = pygame.key.get_pressed()
                if keys[pygame.K_w]:
                        self.y -= self.speed
                elif keys[pygame.K_s]:
                        self.y += self.speed
       
                if self.y <= 0:
                        self.y = 0
                elif self.y >= SCR_HEI-64:
                        self.y = SCR_HEI-64
       
        def draw(self):
                pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, self.padWid, self.padHei))
                
class Player2():
        def __init__(self):
                self.x, self.y = 280, 480
                self.speed = 3
                self.padWid, self.padHei = 64, 8
                self.score = 0
                self.scoreFont = pygame.font.Font("imagine_font.ttf", 64)
       
        def scoring(self):
                scoreBlit = self.scoreFont.render(str(self.score), 1, (255, 255, 255))
                screen.blit(scoreBlit, (280, 16))
                if self.score == 10:
                        print ("player 2 wins!")
                        exit()
       
        def movement(self):
                keys = pygame.key.get_pressed()
                if keys[pygame.K_l]:
                        self.x -= self.speed
                elif keys[pygame.K_k]:
                        self.x += self.speed
       
                if self.y <= 0:
                        self.y = 0
                elif self.y >= SCR_HEI-64:
                        self.y = SCR_HEI-64

        def draw(self):
                pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, self.padWid, self.padHei))
 
class Enemy():
        def __init__(self):
                self.x, self.y = SCR_WID-16, SCR_HEI/2
                self.speed = 3
                self.padWid, self.padHei = 8, 64
                self.score = 0
                self.scoreFont = pygame.font.Font("imagine_font.ttf", 64)
       
        def scoring(self):
                scoreBlit = self.scoreFont.render(str(self.score), 1, (255, 255, 255))
                screen.blit(scoreBlit, (SCR_HEI+92, 16))
                if self.score == 10:
                        print ("Player 2 wins!")
                        exit()
       
        def movement(self):
                keys = pygame.key.get_pressed()
                if keys[pygame.K_UP]:
                        self.y -= self.speed
                elif keys[pygame.K_DOWN]:
                        self.y += self.speed
       
                if self.y <= 0:
                        self.y = 0
                elif self.y >= SCR_HEI-64:
                        self.y = SCR_HEI-64
       
        def draw(self):
                pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, self.padWid, self.padHei))
 
class Ball():
        def __init__(self):
                self.x, self.y = SCR_WID/2, SCR_HEI/2
                self.speed_x = -3
                self.speed_y = 3
                self.size = 8
       
        def movement(self):
                self.x += self.speed_x
                self.y += self.speed_y
 
                #wall col
                if self.y <= 0:
                        self.speed_y *= -1
 
                if self.x <= 0:
                        self.__init__()
                        enemy.score += 1
                        player2.score += 1
                elif self.x >= SCR_WID-self.size:
                        self.__init__()
                        self.speed_x = 3
                        player.score += 1
                        player2.score += 1
                        
                if self.y >= (SCR_HEI - self.size):
                        self.__init__()
                        self.speed_y = -3
                        enemy.score += 1
                        player.score += 1
                
                ##wall col
                #paddle col
                #player
                for n in range(-self.size, player.padHei):
                        if self.y == player.y + n:
                                if self.x <= player.x + player.padWid:
                                        self.speed_x *= -1
                                        break
                        n += 1
                #player2
                for n in range(-self.size, player2.padWid):
                        if self.y == player2.y + n:
                                if self.x >= player2.x - player2.padHei:
                                        self.speed_y *= -1
                                        break
                        n += 1
                                
                #enemy
                for n in range(-self.size, enemy.padHei):
                        if self.y == enemy.y + n:
                                if self.x >= enemy.x - enemy.padWid:
                                        self.speed_x *= -1
                                        break
                        n += 1
                ##paddle col
 
        def draw(self):
                pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, 8, 8))

def main():
    SCR_WID, SCR_HEI = 640, 480
    screen = pygame.display.set_mode((SCR_WID, SCR_HEI))
    pygame.display.set_caption("Pong")
    pygame.font.init()
    clock = pygame.time.Clock()
    FPS = 60
    bg = pygame.image.load("dragon.jpg")

    player = Player() 
    player2 = Player2()
    enemy = Enemy()
    ball = Ball()
    while True:
    #process
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print ("Game exited by user")
                exit()
    ##process
    #logic
        ball.movement()
        player.movement()
        player2.movement()
        enemy.movement()
        ##logic
        #draw
        screen.fill((0, 0, 0))
        screen.blit(bg, (100, 100))
        ball.draw()
        player.draw()
        player.scoring()
        player2.draw()
        player2.scoring()
        enemy.draw()
        enemy.scoring()
        ##draw
        #_______
        pygame.display.flip()
        clock.tick(FPS)

main()
As is, I get the error I described above, traced back to the Ball class. Specifically the part under the "player collide" comment. If I take all the code that makes up the main function out of said function, everything works fine. If it is in that function, I get the error.

It does seem weird to me that it works as is at all. I added the Player2 class, but my teacher wrote the Ball, Player, and Enemy classes, and the Ball class handled collision detection for the Player and Enemy the same way as it is here.

So I'd love to know what I can do to fix this (I'm supposed to have a main function). But, probably even more, I'd love to know why it works the one way and not the other.

Thanks.
Programming homework and newbie help thread Quote
05-09-2017 , 06:11 PM
Inside the class Ball you are referencing the instances of player, player2, etc. Because they are instantiated inside main, they are local to that scope and not global, so those classes don't know what they are. Without the main, they are instantiated as globals, so the classes can access them. I think that is the issue.
Programming homework and newbie help thread Quote
05-09-2017 , 08:38 PM
^^ That sounds about correct to me.

For your review:

Code:
>>> class Player():
...     def __init__(self):
...         self.size = 4
... 
>>> player = Player()
>>> player.size
4

>>> class Ball():
...     def __init__(self):
...         self.size = 10
...     def movement(self, other):
...         print("self (ball) = ", self.size)
...         print("other (player) = ", other.size)
... 
>>> 
>>> ball = Ball()
>>> ball.size
10

>>> ball.movement(player)
self (ball) =  10
other (player) =  4
Programming homework and newbie help thread Quote
05-09-2017 , 09:05 PM
Took me a minute but I have it now. Thanks for teaching a man to fish.
Programming homework and newbie help thread Quote
05-11-2017 , 01:44 PM
so if i have the following code in ruby

(0..9).each do |x|
#something here

how can i break entirely from running it, not just from the current iteration of x. so if x is currently 1... i want to be able to break and for x not to turn into 2.
Programming homework and newbie help thread Quote
05-11-2017 , 02:43 PM
Pretty sure that's just "break"
Programming homework and newbie help thread Quote
05-11-2017 , 03:56 PM
haha thx
Programming homework and newbie help thread Quote
05-16-2017 , 05:57 AM
Update : Betfair historical data project

Sorry, to have been slow for updates. The beta was supposed to go Live, May 5, but, it has now been pushed, to the end of this Mth.

In the interim, I got a programmer, to do this :

Take the tar files archive from Betfair, and write some import software, so, that it unpackages all the files, and brings it into SQL.

There are more than a few problems :

For each match .. say England v Australia, there might be 25 diffferent markets :

For example, number of 6's, will they score over 100 runs, 110 runs, 120 runs, will a player score a 50, apart from the main match odds mkt.

This means, that for a simple 7 hr game, the data, is about 4.3 million rows.

As the data is being streamed, there doesnt appear to be a time-stamp, for each record.

We have to find a way, to split the data into different tables, but, are waiting for the live launch, so, we dont have to re-do.

Running a SQL query, against 4.3 million rows, takes quite a while .. so, am looking for help. I would like to find, a handicapped person, who cant travel etc or maybe a retiree, who has the skills, and doesnt mind, what could be the repetitiv nature of the data analysis.

I have also asked for grad help, at local universities. I will find the right person(s) eventually.
Programming homework and newbie help thread Quote
05-16-2017 , 07:07 AM
Jesus, your post formatting tilts me.
Programming homework and newbie help thread Quote
05-16-2017 , 07:23 AM
Quote:
Originally Posted by Wolfram
Jesus, your post formatting tilts me.
Sorry.

Will work on it.

Programming homework and newbie help thread Quote
05-16-2017 , 08:03 AM
Thanks

Too many newlines makes it much harder to read.
Programming homework and newbie help thread Quote
05-16-2017 , 10:35 AM
I now have a confirmed date for launch of the site .. May 24.
Programming homework and newbie help thread Quote
05-16-2017 , 04:14 PM
say i'm running a C program in linux, and I can't modify the source code but I want the program to use a copy of a function call that I wrote instead of one in a provided library. Is there any way to use the path environment variable to achieve that?
Programming homework and newbie help thread Quote
05-16-2017 , 04:32 PM
Quote:
Originally Posted by Noodle Wazlib
say i'm running a C program in linux, and I can't modify the source code but I want the program to use a copy of a function call that I wrote instead of one in a provided library. Is there any way to use the path environment variable to achieve that?
Yes. Look into LD_PRELOAD. It's an enviroment variable that give you a way to load your custom libraries ahead of system ones. It's somewhat similar to Window's hooking methodology, although that is actually much more powerful and dangerous.
Programming homework and newbie help thread Quote
05-16-2017 , 05:02 PM
Cool, thank ya!
Programming homework and newbie help thread Quote
05-16-2017 , 05:09 PM
It's very useful. When I did a lot of C++ programming I used it for debugging or trying new things. The most common one was replacing the standard memory allocation libraries with either debug versions, or versions optimized for whatever I was trying. This way you can just re-link your trial library instead of recompiling the whole project from source. And obviously, you can use it for any 3rd party program that uses a standard library.
Programming homework and newbie help thread Quote

      
m