Be a Supporter!

Programming Regs Lounge

  • 422,245 Views
  • 17,911 Replies
New Topic Respond to this Topic
Diki
Diki
  • Member since: Jan. 31, 2004
  • Online!
Forum Stats
Moderator
Level 13
Programmer
Response to Programming Regs Lounge 2013-01-04 02:08:41 Reply

At 1/4/13 01:03 AM, pirateplatypus wrote: If you're looking for little challenges, Project Euler is pretty cool.

Project Euler is pretty cool, but I find most of the problems are mathematical challenges instead of programming challenges (which makes sense given the name Euler).

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Programming Regs Lounge 2013-01-06 08:49:57 Reply

http://www.php.net/archive/2012.php#id2012-11-15-1

pirateplatypus
pirateplatypus
  • Member since: Sep. 27, 2011
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to Programming Regs Lounge 2013-01-06 09:27:27 Reply

At 1/4/13 02:08 AM, Diki wrote:
At 1/4/13 01:03 AM, pirateplatypus wrote: If you're looking for little challenges, Project Euler is pretty cool.
Project Euler is pretty cool, but I find most of the problems are mathematical challenges instead of programming challenges (which makes sense given the name Euler).

I stumbled across C Puzzles while I was reading a blog.


"If loving Python is crazy then I don't want to be sane." -Diki

Thegluestickman
Thegluestickman
  • Member since: Mar. 16, 2007
  • Offline.
Forum Stats
Member
Level 13
Game Developer
Response to Programming Regs Lounge 2013-01-06 10:12:31 Reply

Rooted my phone and installed Cyanogen mod on it. So much better thsn stock android, I recommend it if you have an older phone and can't upgrade. Looks really fancy compared to gingerbread (which my phone had) and runs a helluva lot smoother.


To protect the world from devastation.
kidd25 is the best Newgrounder ever.

BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Online!
Forum Stats
Moderator
Level 13
Programmer
Response to Programming Regs Lounge 2013-01-06 13:33:04 Reply

At 1/6/13 08:49 AM, NinoGrounds wrote: http://www.php.net/archive/2012.php#id2012-11-15-1

Well at least PHP is getting some useful new features:

support for Generators
support for finally in try/catch blocks

Though I think finally should have existed to begin with.

At 1/6/13 09:27 AM, pirateplatypus wrote: I stumbled across C Puzzles while I was reading a blog.

Well this looks pretty cool, and somewhat challenging, thanks.

klyrslf
klyrslf
  • Member since: Jan. 6, 2013
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Programming Regs Lounge 2013-01-06 18:47:09 Reply

At 1/6/13 01:33 PM, Diki wrote:
At 1/6/13 08:49 AM, NinoGrounds wrote: http://www.php.net/archive/2012.php#id2012-11-15-1
Well at least PHP is getting some useful new features[...]

Too bad PHP is awful in every sense of the word.

pirateplatypus
pirateplatypus
  • Member since: Sep. 27, 2011
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to Programming Regs Lounge 2013-01-08 22:04:32 Reply

I stumbled across a nifty site. It's called Coding Bat.

It's similar to sites like Pyschools where you are presented with a problem and some examples, you write your code in the browser, then test the solutions.

The site has Python 2.x and Java. I don't speak Java so I have yet to try it out. I ran through the Python section. It was very easy but I think this is a pretty cool site for newbies.


"If loving Python is crazy then I don't want to be sane." -Diki

Diki
Diki
  • Member since: Jan. 31, 2004
  • Online!
Forum Stats
Moderator
Level 13
Programmer
Response to Programming Regs Lounge 2013-01-08 22:56:09 Reply

At 1/6/13 06:47 PM, klyrslf wrote: Too bad PHP is awful in every sense of the word.

No argument here.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Online!
Forum Stats
Moderator
Level 13
Programmer
Response to Programming Regs Lounge 2013-01-09 03:08:46 Reply

While working on a multi-threaded application in Python I ended up needing to use a mutex. I've known about them for a while, but this is the first time I've ever actually needed one. Turns out in Python they are ridiculously simple to implement, so I figured I'd make a short post about them, because why not?

What is a mutex?
Mutex stands for "mutual exclusion".
It is used with multi-threading to ensure that two threads can safely manipulate the same piece of memory. Sometimes two, or more, different threads need to access, and manipulate, some data among each other.
Since threads can run simultaneously that means that two conflicting operations could be performed simultaneously. That's not good. A mutex prevents that from happening.

When should you use a mutex?
Basically, when you're in a situation like the one described above.
Here's an example where the output buffer is used as the shared piece of memory:

import threading
import time

def one():
    while True:
        print "one"
        time.sleep(1)

def two():
    while True:
        print "two"
        time.sleep(1)

threading.Thread(None, one).start()
threading.Thread(None, two).start()

Note: Assumes Python 2.7

From the looks of it each thread will output the word "one" and "two" every second respectively.
However that is not what happens. Each thread may end up printing at the same time, resulting in both strings on the same line. Sometimes they may time correctly and put "one" and "two" on separate lines as it should. Ultimately the behaviour is erroneous.

The solution?

Use a mutex!
This just requires you to lock the mutex before manipulating shared memory, and then unlocking/releasing it afterwards. The same mutex object must be shared among the threads.
Adding a mutex to the above code looks like this:

import threading
import time

def one(mutex):
    while True:
        mutex.acquire() #LOCK
        print "one"
        time.sleep(1)
        mutex.release() #UNLOCK

def two(mutex):
    while True:
        mutex.acquire() #LOCK
        print "two"
        time.sleep(1)
        mutex.release() #UNLOCK

root_mutex = threading.Lock()

threading.Thread(None, one, None, (root_mutex,)).start()
threading.Thread(None, two, None, (root_mutex,)).start()

Note: Assumes Python 2.7

And that's it. The program works now.
Like I said: mutex locks in Python are pretty damn easy.

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Programming Regs Lounge 2013-01-10 21:28:11 Reply

At 1/8/13 10:56 PM, Diki wrote: No argument here.

lol

And thanks for the previous post m8

Momo-the-Monkey
Momo-the-Monkey
  • Member since: Oct. 15, 2005
  • Offline.
Forum Stats
Member
Level 45
Musician
Response to Programming Regs Lounge 2013-01-11 14:26:46 Reply

Thinkin about starting to learn Java. What do you guys recommend as the best place I should start?


Hello, from the past!
[ PHP: Main ]

BBS Signature
egg82
egg82
  • Member since: Jun. 24, 2006
  • Online!
Forum Stats
Supporter
Level 05
Game Developer
Response to Programming Regs Lounge 2013-01-11 21:40:11 Reply

At 1/8/13 10:04 PM, pirateplatypus wrote: I stumbled across a nifty site. It's called Coding Bat.

The site has Python 2.x and Java. I don't speak Java so I have yet to try it out. I ran through the Python section. It was very easy but I think this is a pretty cool site for newbies.

solution to Java sleepIn:

return !weekday || vacation;

fuck your Python :P
(though in all seriousness, it's on my "to learn" list)


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Online!
Forum Stats
Moderator
Level 13
Programmer
Response to Programming Regs Lounge 2013-01-11 22:39:05 Reply

At 1/11/13 09:40 PM, egg82 wrote: solution to Java sleepIn:

return !weekday || vacation;

fuck your Python :P

But Python is far prettier:

def sleep_in(weekday, vacation):
    return not weekday or vacation

:)

pirateplatypus
pirateplatypus
  • Member since: Sep. 27, 2011
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to Programming Regs Lounge 2013-01-11 22:54:40 Reply

At 1/11/13 10:39 PM, Diki wrote: But Python is far prettier:

Which is my favourite thing about Python.


"If loving Python is crazy then I don't want to be sane." -Diki

lantaren
lantaren
  • Member since: Nov. 12, 2009
  • Offline.
Forum Stats
Member
Level 07
Musician
Response to Programming Regs Lounge 2013-01-12 14:06:04 Reply

So I was playing with the CodingBat Java stuff, and they write overly complicated code that could be far, far simplified >,<

Example:
Given a string, return true if the string starts with "hi" and false otherwise.

startHi("hi there") ât' true
startHi("hi") ât' true
startHi("hello hi") ât' false

their answer:
public boolean startHi(String str) {
// First test if the string is not at least length 2
// (so the substring() below does not go past the end).
if (str.length() < 2) return false;

// Pull out the string of the first two chars
String firstTwo = str.substring(0, 2);

// Test if it is equal to "hi"
if (firstTwo.equals("hi")) {
return true;
} else {
return false;
}
// This last part can be shortened to: return(firstTwo.equals("hi"));
}

my answer:
public boolean startHi(String str) {
if (str.startsWith("hi")) { return true; } else { return false; }
}

So... in short, they're good for practice, but the way that they do it in Java really doesn't make sense when there are operations to do the tasks they ask you to do already. Although if they said 'without str.startsWith' then it would've been harder, and their solution would make sense. But it's unneeded in the situation that they presented.


but in the end, what do I know? I am just a teenager after all.

pirateplatypus
pirateplatypus
  • Member since: Sep. 27, 2011
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to Programming Regs Lounge 2013-01-12 14:17:07 Reply

At 1/12/13 02:06 PM, lantaren wrote: So I was playing with the CodingBat Java stuff, and they write overly complicated code that could be far, far simplified >,<

That's how I felt about the Python answers they had.


"If loving Python is crazy then I don't want to be sane." -Diki

lantaren
lantaren
  • Member since: Nov. 12, 2009
  • Offline.
Forum Stats
Member
Level 07
Musician
Response to Programming Regs Lounge 2013-01-12 14:55:29 Reply

At 1/12/13 02:17 PM, pirateplatypus wrote: That's how I felt about the Python answers they had.

Really? I'm assuming that because it seems to be 'beginner' oriented that they believe that the more readable solution is better than the most logical/theoretically correct answer.

Since I've been messing around with my TI-84 recently, I'm used to the whole optimizations, such as:
{x+6(k=26)-6(k+24),y+6(k=25)-6(k=34)->Lpos() or something, which is basically stating that 'new position=current position, +/- 6 x depending on if left key or right key are pressed, and the current y position +/- 6y depending on if either up or down keys are pressed"

Like, all programming languages can do something similar, although no need to use lists as various positions, but for the TI and it's limited number of variables, it's good a really good + effecient system of holding multiple values, and technically it is a 100% logically sensible solution to the problem, but it's hardly legible lol.


but in the end, what do I know? I am just a teenager after all.

pirateplatypus
pirateplatypus
  • Member since: Sep. 27, 2011
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to Programming Regs Lounge 2013-01-12 15:07:42 Reply

At 1/12/13 02:55 PM, lantaren wrote:
At 1/12/13 02:17 PM, pirateplatypus wrote: That's how I felt about the Python answers they had.
Really? I'm assuming that because it seems to be 'beginner' oriented that they believe that the more readable solution is better than the most logical/theoretically correct answer.

I think that's exactly what it is. I actually think it's a good decision on their part. I think it's better for a newbie to learn from code that's easy to understand. They can always focus on optimization once they understand the basics.


"If loving Python is crazy then I don't want to be sane." -Diki

lantaren
lantaren
  • Member since: Nov. 12, 2009
  • Offline.
Forum Stats
Member
Level 07
Musician
Response to Programming Regs Lounge 2013-01-12 15:49:19 Reply

At 1/12/13 03:07 PM, pirateplatypus wrote: I think that's exactly what it is. I actually think it's a good decision on their part. I think it's better for a newbie to learn from code that's easy to understand. They can always focus on optimization once they understand the basics.

Right, but what really gets me is when they use a substring for a single character. I just don't know why anyone would do that, it's confusing (and, although not 'wrong' technically, that's just not very good practice). Also, it just doesn't make sense to re-write a native method :/ although the solutions always did make sense and were good solutions most of the time, easier, simpler, and more legible solutions were usually available (except on the 'warmup' logic ones, they got those pretty much.)


but in the end, what do I know? I am just a teenager after all.

pirateplatypus
pirateplatypus
  • Member since: Sep. 27, 2011
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to Programming Regs Lounge 2013-01-12 16:12:52 Reply

At 1/12/13 03:49 PM, lantaren wrote: Also, it just doesn't make sense to re-write a native method

In normal day-to-day coding I agree. Though I think you can learn a lot by re-inventing the wheel, as long as you also learn that said wheel exists and how to use it.

Otherwise, I completely agree. Some of their solutions were pretty bad.


"If loving Python is crazy then I don't want to be sane." -Diki

Diki
Diki
  • Member since: Jan. 31, 2004
  • Online!
Forum Stats
Moderator
Level 13
Programmer
Response to Programming Regs Lounge 2013-01-12 16:34:41 Reply

At 1/12/13 03:49 PM, lantaren wrote: Also, it just doesn't make sense to re-write a native method

It's useful for the purposes of learning and displaying competency.

lantaren
lantaren
  • Member since: Nov. 12, 2009
  • Offline.
Forum Stats
Member
Level 07
Musician
Response to Programming Regs Lounge 2013-01-12 17:52:54 Reply

As you've said, yes it is useful for showing competency and such, but they should've at least mentioned the str.startsWith method, I should think, considering that they do use it later on anyways (so it's not like they didn't know it was there)

I don't know, but to me it seems like 'if a string starts with substring' that the first thing you should do is use the native string methods first, and only if it doesn't exist or is only in an external library, should it be re/invented. Just my take on it, moreover just because it looks cleaner, is easier to type/remember, and makes sense. Regardless, I understand why and how they did the things they did, and it makes sense for a beginner to think through it, for maybe they might not know of the specific methods available to them.


but in the end, what do I know? I am just a teenager after all.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Online!
Forum Stats
Moderator
Level 13
Programmer
Response to Programming Regs Lounge 2013-01-13 17:45:44 Reply

God damn you weren't kidding about how terrible the CodingBat solutions are.
On this problem, for example, the given solution is:

def front_times(str, n):
  front_len = 3
  if front_len > len(str):
    front_len = len(str)
  front = str[:front_len]
  
  result = ""
  for i in range(n):
    result = result + front
  return result

Whereas mine is:

def front_times(str, n):
  return str[:3]*n

Or this other problem which has this solution:

def string_bits(str):
  result = ""
  for i in range(len(str)):
    if i % 2 == 0:
      result = result + str[i]
  return result

Compared to mine:

def string_bits(str):
  return str[::2]

It's like the people writing these don't even know Python.

kiwi-kiwi
kiwi-kiwi
  • Member since: Mar. 6, 2009
  • Offline.
Forum Stats
Member
Level 09
Programmer
Response to Programming Regs Lounge 2013-01-14 01:16:35 Reply

At 1/13/13 05:45 PM, Diki wrote: It's like the people writing these don't even know Python.

Yes they were most likely written by C programmers given the verbose checking of every input parameter

In other news Qt on mobile has really gotten somewhere, I have made some experiments and am quite impressed by the results. First of all, everything just works, you download the necessitas installer, click through the process and everything is setup for you. Then you can just create a build target for android devices for any project you have, click run and it just sends it to the device.

I'm gonna try some stuff with the debug build as well, but from what I've seen until now, it beats the shit out of the eclipse plugin.

Dean
Dean
  • Member since: Feb. 16, 2008
  • Offline.
Forum Stats
Moderator
Level 47
Gamer
Response to Programming Regs Lounge 2013-01-15 08:23:08 Reply

At 1/6/13 10:12 AM, Thegluestickman wrote: Rooted my phone and installed Cyanogen mod on it.

Is the process of rooting a phone and installing custom firmware like CyanogenMod pretty straight forward?

I'm probably going to do the same thing with my phone in the near future because the project (that I've yet to actually start coding) for my dissertation is meant to make use of ad-hoc Wi-Fi connections between two Android devices, which is currently not supported on the firmware I have (Gingerbread). I'm pretty sure I read in the past when I was doing my feasibility study of this project that CyanogenMod does support actually have support for this. I assume Android just have it disabled for security concerns or something.


BBS + Chat Moderator - Feel free to send me a PM if you have a problem!
Want to instant message me? [ Skype - DeanNewgrounds ]

BBS Signature
Thegluestickman
Thegluestickman
  • Member since: Mar. 16, 2007
  • Offline.
Forum Stats
Member
Level 13
Game Developer
Response to Programming Regs Lounge 2013-01-15 16:24:41 Reply

At 1/15/13 08:23 AM, Dean wrote:
At 1/6/13 10:12 AM, Thegluestickman wrote: Rooted my phone and installed Cyanogen mod on it.
Is the process of rooting a phone and installing custom firmware like CyanogenMod pretty straight forward?

Depending on what type of phone you have, HTCs require the bootloader being unlocked and getting S-Off access which is a pain but there is documentation on the CyanogenMod wiki. I recommend backing your texts, calls, contacts and apps up if you haven't already. It works much better than gingerbread does.


To protect the world from devastation.
kidd25 is the best Newgrounder ever.

BBS Signature
Dean
Dean
  • Member since: Feb. 16, 2008
  • Offline.
Forum Stats
Moderator
Level 47
Gamer
Response to Programming Regs Lounge 2013-01-15 16:44:37 Reply

At 1/15/13 04:24 PM, Thegluestickman wrote: Depending on what type of phone you have, HTCs require the bootloader being unlocked and getting S-Off access which is a pain but there is documentation on the CyanogenMod wiki.

Typical. I have a HTC Wildfire S. More of a budget smart phone but I don't use my phone enough to justify getting anything fancy. In fact, I could still live with a phone that doesn't even support Wi-Fi. As long as it can send text I'd be quite happy.

But yea, I'll hopefully get around do doing this sometime soon. I just have so many things that I want to do right now to get me back into the programming mindset. I was pretty lazy over the Christmas break and didn't so much besides watching Star Trek. Now I'm wanting to go back and finish of the simple forum that I started coding at the end of the summer. Been so long since I last touched that stuff that I'll need to familiarise myself with it all over again.

On top of that, I'm currently taking a Computer Games Programming class and the coursework for that will involve creating an arcade style game using C++. I'm also taking a class called Network Applications which offers a choice of coursework projects, although it's group work. I think we'll probably be building some kind of browser based instant messenger. They sound interesting, but it'll be a few weeks before I get into any of that.


BBS + Chat Moderator - Feel free to send me a PM if you have a problem!
Want to instant message me? [ Skype - DeanNewgrounds ]

BBS Signature
kiwi-kiwi
kiwi-kiwi
  • Member since: Mar. 6, 2009
  • Offline.
Forum Stats
Member
Level 09
Programmer
Response to Programming Regs Lounge 2013-01-15 17:22:51 Reply

At 1/15/13 04:44 PM, Dean wrote:
At 1/15/13 04:24 PM, Thegluestickman wrote: Depending on what type of phone you have, HTCs require the bootloader being unlocked and getting S-Off access which is a pain but there is documentation on the CyanogenMod wiki.
Typical. I have a HTC Wildfire S. More of a budget smart phone but I don't use my phone enough to justify getting anything fancy. In fact, I could still live with a phone that doesn't even support Wi-Fi. As long as it can send text I'd be quite happy.

There's nothing to worry about, I've rooted every device that has every gotten into my hands without any problem because I hate the bloatware that came preinstalled. And even if something doesn't work as expected, there's always xda-developers to help you out.

Dean
Dean
  • Member since: Feb. 16, 2008
  • Offline.
Forum Stats
Moderator
Level 47
Gamer
Response to Programming Regs Lounge 2013-01-23 12:23:36 Reply

I'm currently back to working on my PHP/MySQL forum again. A lot of the back end is in place, just have to add new queries to it whenever I realised I've missed something. Now I'm trying to hook it up to the (completely unstyled) front end. I'll start making use of a GitHub repository soon so that I can familiarise myself with that. If some of you guys had some time to spare, I'd be interested in receiving some feedback on how I've implemented/structured things. It's definitely the biggest PHP project I've taken on and the first time I've really used object orientated PHP so I've probably done some stuff that'd be considered bad practise or just plain stupid. Hopefully there's not too much of that though.

I'll post a link to that repository once I've got it set up, whenever that is. I want to clean up the code before I do that though. It's also not fully functional yet, but right now I'm more interested in making sure I've not got things set up badly.


BBS + Chat Moderator - Feel free to send me a PM if you have a problem!
Want to instant message me? [ Skype - DeanNewgrounds ]

BBS Signature
Dean
Dean
  • Member since: Feb. 16, 2008
  • Offline.
Forum Stats
Moderator
Level 47
Gamer
Response to Programming Regs Lounge 2013-01-23 18:04:23 Reply

Link: https://github.com/DeanThomson/simple-php-forum

It's still a long way from being done but if anyone has any comments, I'd like to hear them. Especially if they're alerting me to something I've done badly.


BBS + Chat Moderator - Feel free to send me a PM if you have a problem!
Want to instant message me? [ Skype - DeanNewgrounds ]

BBS Signature