# Post code( ), get Feeback( ) ;



## Paper Samurai (Oct 1, 2009)

Just a thread to post random bits of code that you're working on or have finished lately.

Feel free to ask questions on the stuff posted / critisize / say random things


----------



## Paper Samurai (Oct 1, 2009)

Here's a ruby program that I wrote in a couple of hours; and spent about the same time having to debug it - thanks out of date libraries !:mum

The program itself basically sorts a large list of data in a text file (about 5000 individual records) and puts them into an Excel spreadsheet,

Even though Ruby's suppose to be a pure OOP language, I thought to hell with that and just use some globals variables for the lols :b I also gutted the main logic twice, as I kept forgetting how strange the text file data was formatted. (I think they must have scrapped it from somewhere)

Anyway, without further ado:

https://github.com/darthur30/RubyScripts/blob/master/Convert Text to Excel/text2excel.rb


----------



## nullptr (Sep 21, 2012)

Here's a NTW mod manager in java I've been working on, I know it's a mess and it's not even finished. I'm hoping to finish it when I've gots me some time.



> import java.util.*;
> import java.io.*;
> 
> import javax.swing.JFrame;
> ...


----------



## Brasilia (Aug 23, 2012)




----------



## nullptr (Sep 21, 2012)

Brasilia said:


>


Aha you used tumblr http://www.tumblr.com/tagged/disco gif/.

I like this one


----------



## Brasilia (Aug 23, 2012)

^ I'm not even sure what this even thread means! :blush


----------



## nullptr (Sep 21, 2012)

Paper Samurai said:


> Here's a ruby program that I wrote in a couple of hours; and spent about the same time having to debug it - thanks out of date libraries !:mum
> 
> The program itself basically sorts a large list of data in a text file (about 5000 individual records) and puts them into an Excel spreadsheet,
> 
> ...


Wish I could critique but alas I don't know much ruby.


----------



## Paper Samurai (Oct 1, 2009)

Brasilia said:


> ^ I'm not even sure what this even thread means! :blush


Dammit, I missed the 'd' out of feedback :doh



galacticsenator said:


> Wish I could critique but alas I don't know much ruby.


Don't worry man, it seems to work fine and that's the main thing :b (I hope)

I like your Java code, I'm guessing the forum messed up the formatting perhaps ?


----------



## Brasilia (Aug 23, 2012)




----------



## nullptr (Sep 21, 2012)

Paper Samurai said:


> I like your Java code, I'm guessing the forum messed up the formatting perhaps ?


Yep, copy and pasting into it sucks. The code isn't close to being finished.



Brasilia said:


> ^ I'm not even sure what this even thread means! :blush


simple calls the post method and getFeedback. Master Samurai it's getFeedback, you know better than to put spaces in an identifier regardless of language.


----------



## Paper Samurai (Oct 1, 2009)

galacticsenator said:


> Yep, copy and pasting into it sucks. The code isn't close to being finished.


Have you tried Github ? It's kinda like a giant internet note pad for code. It's completely free and leaves all the nice formatting that you do untouched.

I have almost OCD when it comes to how I position some of the stuff on a page, so this pleases me greatly. :teeth



galacticsenator said:


> simple calls the post method and getFeedback. Master Samurai it's getFeedback, you know better than to put spaces in an identifier regardless of language.


It was just pseudo code I tells ya :b Camel case (what I usually do) may confuse some people I reckon.

someMethodWrittenInCamel( )


----------



## nullptr (Sep 21, 2012)

Paper Samurai said:


> Have you tried Github ? It's kinda like a giant internet note pad for code. It's completely free and leaves all the nice formatting that you do untouched.
> 
> I have almost OCD when it comes to how I position some of the stuff on a page, so this pleases me greatly. :teeth


Heard of it but I thought you needed linux to run git. I do have cygwin maybe I could try installing it there. I'll check it out, I actually use google code I was just too lazy it put it in there.


----------



## bobby. (Mar 29, 2011)

I'm quite pleased with how I implemented menus in my latest game.

Menu.hpp
Menu.cpp

MenuItem.hpp
MenuItem.cpp

Here's the settings menu as an example.

SettingsMenu.hpp
SettingsMenu.cpp

And here's the result.

And here's an example of how messy and horrible my code can get (in particular the populateMap() function).

Application.cpp

I always intended to tidy that up, but then I finished the project and couldn't be arsed :b


----------



## bobby. (Mar 29, 2011)

galacticsenator said:


> Here's a NTW mod manager in java I've been working on, I know it's a mess and it's not even finished. I'm hoping to finish it when I've gots me some time.


This looks a lot better than the crap I was writing at your age. I wish I'd started with Java instead of VB as it forces you to learn OOP and various good practices such as exception handling whereas in VB you can get away with writing really _really_ bad code.


----------



## nullptr (Sep 21, 2012)

Master samurai for you a thing I had worked on in php, it's a crappy type of posting system I did while bored.

http://codepad.org/VgRWKhDh

thanks for the site oob.



Oob said:


> This looks a lot better than the crap I was writing at your age. I wish I'd started with Java instead of VB as it forces you to learn OOP and various good practices such as exception handling whereas in VB you can get away with writing really _really_ bad code.


I actually started with python, thank god I didn't start with java, but I think it has to do a lot with what I learned by myself, I taught myself python to the point was I think I was a near expert, java came later but by that point I was already pretty good at programming.


----------



## Paper Samurai (Oct 1, 2009)

Oob said:


> I'm quite pleased with how I implemented menus in my latest game.
> 
> Menu.hpp
> Menu.cpp
> ...


I always like reading other people's C++ code for some reason  Although using it you tend to write longer lines, it seems to me at least to be really logical compared to some higher level languages - nothing is hidden under any abstractions and everything is presented up front.

I don't recognise the API's you're using I have to say (so I can't comment to much) but I do like your comment blocks - it's makes finding definitions a lot easier - which can be a right pain sometimes.



galacticsenator said:


> Master samurai for you a thing I had worked on in php, it's a crappy type of posting system I did while bored.
> 
> http://codepad.org/VgRWKhDh
> 
> thanks for the site oob.


No need for the extra formality, just call me Dylan if you want :yes. Your code does look good from what I can see, you're trying to prevent trolls posting malicious scripts to your server right. Where on the server are you storing user input though ? If you plan on storing to a database (and it's running PHP Version 5.10 or higher) you can use something call PDO that automatically screens input. Of course you can also just be extra paranoid and make only the minimum content on your site dynamic.


----------



## millenniumman75 (Feb 4, 2005)

Brasilia said:


>


Oh, wow - they let that on public television? Fit and Over Fifty

GalacticSenator, what code is that? I see scanner classes and file copy classes and methods.


----------



## bobby. (Mar 29, 2011)

Paper Samurai said:


> I don't recognise the API's you're using I have to say (so I can't comment to much) but I do like your comment blocks - it's makes finding definitions a lot easier - which can be a right pain sometimes.


Thanks. The only API I used was my 2d engine. The engine layer wraps all third-party APIs, meaning my games are highly portable - at least in theory.


----------



## nullptr (Sep 21, 2012)

millenniumman75 said:


> GalacticSenator, what code is that? I see scanner classes and file copy classes and methods.


It's a faction mod manager for a game I play, but terribly unfinished.


Paper Samurai said:


> No need for the extra formality, just call me Dylan if you want :yes. Your code does look good from what I can see, you're trying to prevent trolls posting malicious scripts to your server right. Where on the server are you storing user input though ? If you plan on storing to a database (and it's running PHP Version 5.10 or higher) you can use something call PDO that automatically screens input. Of course you can also just be extra paranoid and make only the minimum content on your site dynamic.


Ok d-y-l-an of sas praxeum you may call me admiral :b. Ya It does that. Actually I used a file, so hacker prone.


----------



## Paper Samurai (Oct 1, 2009)

millenniumman75 said:


> GalacticSenator, what code is that? I see scanner classes and file copy classes and methods.


It's Java btw. I remember you said you were a programmer back in your college days MM, Pascal and Delphi right ? (or have I got that completely wrong) Do you still dabble with source code ?



galacticsenator said:


> Ok d-y-l-an of sas praxeum you may call me admiral :b. Ya It does that. Actually I used a file, so hacker prone.


lolz, I'd rather someone call me by first name than master, I feel like I could be done for fraud at any moment with that sort of title :teeth - I'm pretty good at a wide range of different programming disciplines I'd say, but no expert at any one of them.


----------



## millenniumman75 (Feb 4, 2005)

Paper Samurai said:


> It's Java btw. I remember you said you were a programmer back in your college days MM, Pascal and Delphi right ? (or have I got that completely wrong) Do you still dabble with source code ?
> 
> lolz, I'd rather someone call me by first name than master, I feel like I could be done for fraud at any moment with that sort of title :teeth - I'm pretty good at a wide range of different programming disciplines I'd say, but no expert at any one of them.


 I know it's Java , but I am not familiar with those modules. There are methods Java supplies that are updated frequently.

I still dabble in C++ and PERL. Those are my main programming languages at the moment.


----------



## nullptr (Sep 21, 2012)

millenniumman75 said:


> I know it's Java , but I am not familiar with those modules. There are methods Java supplies that are updated frequently.


Oh I never thought they were that new. Scanner is just a simple standard library parser, and file is a file handle.


----------



## bobby. (Mar 29, 2011)

Here's a game of Reversi I made ages ago in Python http://goo.gl/qCWZS.

Anyone got any tips for how I can make the code nicer and more professional? I'm a complete Python n00b, and I have to make something for someone in Python so I want to make sure I'm following best practices.


----------



## nullptr (Sep 21, 2012)

Oob said:


> Here's a game of Reversi I made ages ago in Python http://goo.gl/qCWZS.
> 
> Anyone got any tips for how I can make the code nicer and more professional? I'm a complete Python n00b, and I have to make something for someone in Python so I want to make sure I'm following best practices.


Looks nice, I haven't used python in a long time though. I like your comment style.


----------



## NeuromorPhish (Oct 11, 2012)

Quick question about Python; is it really sensitive to the use of indentations in formatting, and if so, is it difficult to transfer code without messing it up?


----------



## nullptr (Sep 21, 2012)

NeuromorPhish said:


> Quick question about Python; is it really sensitive to the use of indentations in formatting, and if so, is it difficult to transfer code without messing it up?


Ya indentation is how blocks are grouped, they replace brackets. You can find a code paster thing to transer it formatted.


----------



## nullptr (Sep 21, 2012)

Bump, I like this thread.


----------



## NeuromorPhish (Oct 11, 2012)

Is anyone familiar with R?
I've been looking for a way to draw a line through an exponential distribution, but can't find a way that doesn't require a mathematical expression as an argument.
Here's what i've plotted;


using this code;

```
> area = c(0.0000000314, 0.000000071, 0.000000126, 0.000000196)
> resistans = c(10.8, 4.8, 2.8, 2.0)
>  plot(area,resistans, xlab = m^2, ylab = ohm)
```
Now, i just need that smooth, preferably red line...


----------



## Paper Samurai (Oct 1, 2009)

NeuromorPhish said:


> Is anyone familiar with R?
> I've been looking for a way to draw a line through an exponential distribution, but can't find a way that doesn't require a mathematical expression as an argument.
> Here's what i've plotted;
> 
> ...


I've never really used R, but a quick Google brought up this which may be helpful:

http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/abline.html


----------



## Paper Samurai (Oct 1, 2009)

I've recently done a prototype of a web interface that compilates various web services into one big data set.


**


*It's technically property of a small software company I'm contracted to atm, so no stealing lol :b


----------



## NeuromorPhish (Oct 11, 2012)

Paper Samurai said:


> I've never really used R, but a quick Google brought up this which may be helpful:
> 
> http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/abline.html


Yeah, i've tried the abline function, but apparently it's only capable of drawing straight lines which isn't exactly optimal for an exponential graph, hehe.


----------



## bobby. (Mar 29, 2011)

I posted the code for my 2d game engine and most recent game on Github, for anyone who wants to take a look.
http://goo.gl/J2Qzv

And at the moment I'm building an experiment for a friend who's doing a PhD in psychology.

After that, I'm gonna make just a couple more things before applying for programming jobs. I have no degree though, so I expect finding a job will be quite difficult..


----------



## Paper Samurai (Oct 1, 2009)

Oob said:


> I posted the code for my 2d game engine and most recent game on Github, for anyone who wants to take a look.
> http://goo.gl/J2Qzv
> 
> And at the moment I'm building an experiment for a friend who's doing a PhD in psychology.
> ...


I think you may be pleasantly surprised, 'cos if you have a decent portfolio (on Github etc.) there are a number of companies that are willing to take a risk - particularly so in your field (the more low level stuff). If you were trying to get into web development or possibly apps then you may find things a little more hard going - those areas are more saturated atm, and something like a degree could be the deciding factor between you and 20 other candidates.

Are you down in the South btw ? London & Brighton are apparently the 2 hotspots atm for tech and I reckon even I am going to have to relocate down there when my short term contract is up in a couple of weeks.


----------



## bobby. (Mar 29, 2011)

Paper Samurai said:


> I think you may be pleasantly surprised, 'cos if you have a decent portfolio (on Github etc.) there are a number of companies that are willing to take a risk - particularly so in your field (the more low level stuff). If you were trying to get into web development or possibly apps then you may find things a little more hard going - those areas are more saturated atm, and something like a degree could be the deciding factor between you and 20 other candidates.
> 
> Are you down in the South btw ? London & Brighton are apparently the 2 hotspots atm for tech and I reckon even I am going to have to relocate down there when my short term contract is up in a couple of weeks.


I live in Cambridge. There seem to be a lot of suitable software jobs available, but I haven't tried applying to any of them yet as I don't think my portfolio is impressive enough.


----------



## cavemanslaststand (Jan 6, 2011)

NeuromorPhish said:


> Is anyone familiar with R?
> I've been looking for a way to draw a line through an exponential distribution, but can't find a way that doesn't require a mathematical expression as an argument.
> Here's what i've plotted;
> 
> ...


NOTE: You can follow this official MATLAB document if it were an exponential decay:
http://www.mathworks.com/help/matla....html?searchHighlight=matlab+exponential+plot

... and that would be fine.

For certain ranges, yes inverse relationships and exponential decays do look similar, but technically, it's an inverse relationship curve fit from electrical properties:

Resistance ~ Constant / Area.

You can either calculate the constant as follows:

Area, 1/Area, Resistance, Constant
3.14E-08, 31847133.76, 10.8, 3.3912E-07
0.000000071, 14084507.04, 4.8, 3.408E-07
0.000000126, 7936507.937, 2.8, 3.528E-07
0.000000196, 5102040.816, 2.0, 3.92E-07

The cheap thing would be to average out the 4 constants above and simply plot the continuous function Y = Caverge / X along with your data in Matlab (Mucho cheap !), i.e.

Constant = <insert average of the four calculated constants above>;
xModel = 0.0000000314:0.0000000001:0.000000196;
yModel = Constant/xModel;
plot(xModel,yModel)

The more rigorous calculus least sum of square residuals calculation is:

Sum Square Residual Error = Sum (Ydata - Ymodel)^2 = Sum (Ydata - C/Xdata) ^ 2.

Take the derivate with respect to C, set the derivate to zero, and calculate C and use that C for the continuous Y = C/X plot.


----------



## nullptr (Sep 21, 2012)

cavemanslaststand said:


> NOTE: You can follow this official MATLAB document if it were an exponential decay:
> http://www.mathworks.com/help/matla....html?searchHighlight=matlab+exponential+plot
> 
> ... and that would be fine.
> ...


----------



## NeuromorPhish (Oct 11, 2012)

cavemanslaststand said:


> NOTE: You can follow this official MATLAB document if it were an exponential decay:
> http://www.mathworks.com/help/matla....html?searchHighlight=matlab+exponential+plot
> 
> ... and that would be fine.
> ...


Thanks for the informative reply! 
I think the similarity between exponential decay and inverse relationships has put me in a state of confusion.
However, i found the resistivity formula on wikipedia








where it becomes clear that we're dealing with an inverse relationship.
I also found some help with R in a Coursera discussion forum.


----------



## Paper Samurai (Oct 1, 2009)

Oob said:


> I live in Cambridge. There seem to be a lot of suitable software jobs available, but I haven't tried applying to any of them yet as I don't think my portfolio is impressive enough.


You may as well give it a go man & you get the added bonus of a potential employer telling you how you can improve even if they don't give you the job.



cavemanslaststand said:


> NOTE: You can follow this official MATLAB document if it were an exponential decay:
> http://www.mathworks.com/help/matla....html?searchHighlight=matlab+exponential+plot
> 
> ... and that would be fine.
> ...












DAT KNOWLEDGE !!!

*I'm always impressed by your technical posts Caveman !


----------



## mezzoforte (May 16, 2010)

Okay so, my first assignment for Java this year was to make these two programs. Pretty simple I think, but I'm still getting used to all the Java syntax. Any suggestions/corrections, since a lot of you guys seem to be pros? :b

Here are the directions I was given:
1.	Write a program that will compute the sum of the first n positive odd integers. For example, if n is 5, you should compute the sum of: 1 + 3+ 5+ 7+ 9.

2.	Write a program to read a list of nonnegative integers and to display the largest integer, the smallest integer, and the average of all integers. The user indicates the end of the input by entering a negative sentinel value that is not used in finding the largest, smallest, and average values. The average should be a value of type double so that it is computed with a fractional part.

*1.)*

public class SumofOddIntegers {
public static void main(String[] args) {
System.out.println("Please enter an integer");
Scanner scan = new Scanner (System.in);
int num = scan.nextInt();
int sum = 0;
int i = 1;
do
{
if (i % 2 != 0)
{
sum = sum + i;
}
i++;
}while (i <= (num*2));
System.out.println("Sum: " + sum);
}
}

*2.)*
public class MaxMinAvg {

public static void main(String[] args) {

System.out.println("Please enter a set of positive numbers. Enter a negative number when you are done.");
int num=0;
int max = 0;
int min=Integer.MAX_VALUE;
int temp = 0;
double i = 0;

do{
Scanner scan = new Scanner (System.in);
num = scan.nextInt();

if (num < 0)
{break;
}

if (num > max)
{max = num;
}

if (num < min) 
{min = num;}

i++;
num = num + temp;
temp = num;

}while (num > 0);

double avg = temp/i;

System.out.println("The max is " + max + ". The min is " + min + ". The average is " + avg + ".");

}
}


----------



## maninabox (Aug 23, 2012)

mezzoforte said:


> Okay so, my first assignment for Java this year was to make these two programs. Pretty simple I think, but I'm still getting used to all the Java syntax. Any suggestions/corrections, since a lot of you guys seem to be pros? :b
> 
> Here are the directions I was given:
> 1.	Write a program that will compute the sum of the first n positive odd integers. For example, if n is 5, you should compute the sum of: 1 + 3+ 5+ 7+ 9.
> ...


It looks like both of your programs should work. However, the first program lends itself more to a for loop.

Something like:

for(int x=0;x<num;x++)
{
sum = sum + i;// (or sum += i, not sure if you've learned that notation)
i = i +2;
}

I know you're just in a beginning class, but the do-while loop is a less efficient way of doing it. You have to loop through it twice as much because you're only adding 1 to your number, and it has to do the modulo operator (which is a lot slower than adding) each iteration of the loop to check if it's odd or even. Adding 2 to an odd number will always result in the next odd number.


----------



## mezzoforte (May 16, 2010)

maninabox said:


> It looks like both of your programs should work. However, the first program lends itself more to a for loop.
> 
> Something like:
> 
> ...


Thanks! I'll try it with a for loop


----------



## nullptr (Sep 21, 2012)

maninabox said:


> It looks like both of your programs should work. However, the first program lends itself more to a for loop.
> 
> Something like:
> 
> ...


I concur, I don't think I've ever used a do-while in my life, it's not intuitive enough for me.


----------



## maninabox (Aug 23, 2012)

galacticsenator said:


> I concur, I don't think I've ever used a do-while in my life, it's not intuitive enough for me.


Do-whiles can be useful in certain instances like menus, where you need something to at least run once. With a regular while loop, the statements in the loop may not run at all.


----------



## Paper Samurai (Oct 1, 2009)

You guys can quickly publish code here if ya want:

http://pastebin.com/

Seeing as this forum automatically mangles whitespace :afr


----------



## nullptr (Sep 21, 2012)

Paper Samurai said:


> You guys can quickly publish code here if ya want:
> 
> http://pastebin.com/
> 
> Seeing as this forum automatically mangles whitespace :afr




```
There's a code tag too that manages whitespace
```


----------



## Paper Samurai (Oct 1, 2009)

galacticsenator said:


> ```
> There's a code tag too that manages whitespace
> ```


Cheers man, didn't realise :teeth


----------



## Evo (Jan 1, 2012)

Here's a microcontroller DC motor control program I did in C. :b

http://codepad.org/UrknjEht


----------



## Paper Samurai (Oct 1, 2009)

Evo said:


> Here's a microcontroller DC motor control program I did in C. :b
> 
> http://codepad.org/UrknjEht


Looks impressive  I haven't done any micro-controller programming myself, but I can give you some very general code styling advice:


```
while (1==1)         {          }
```
This is your while loop at the end of main. Seeing as you want an infinite loop, you don't have to (or want to) evaluate a boolean expression with every iteration - You can just write the boolean literal 'true'. And seeing as the while loop has an empty code block attached to it, you can actually omit it to make the code cleaner by using a semi-colon.


```
while(true);
```


----------



## Evo (Jan 1, 2012)

Paper Samurai said:


> Looks impressive  I haven't done any micro-controller programming myself, but I can give you some very general code styling advice:
> 
> 
> ```
> ...


Thanks. I'll try that.


----------

