• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Koren

Member
Damn didn't notice that but now its saying error: array type has incomplete element type 'struct Card' for the function header in the source file,
Are you sure that all three occurrence are the same? Because it works for me. Care to pastebin the whole file if you can't find the error?

Well I was originally but because once I have a order over the value of 10 I have to change it to a King, Ace and etc...
So, you'll replace it by a char* also for the value?

I still wouldn't do this. You'd better define a

Code:
typedef enum {
  Spades = 0,
  Diamonds = 1,
  Hearts = 2,
  Clovers = 3
} suits_t;

and a

Code:
const char *const suit_names[] = { "spades", "diamonds", "hearts", "clovers" };
(or better, string instead of char*)

and use
Code:
suits_t suit;

in your struct... and the same for the values...

You can get the string with

Code:
suit_names[ deck[i].suit ]

Granted, that's slightly more complex, but you'll avoid issues by manipulating integers instead of pointers for each card...
 

cyborg009

Banned
Are you sure that all three occurrence are the same? Because it works for me. Care to pastebin the whole file if you can't find the error?


So, you'll replace it by a char* also for the value?

I still wouldn't do this. You'd better define a

[/snip]

Granted, that's slightly more complex, but you'll avoid issues by manipulating integers instead of pointers for each card...

Thanks Got I'm going test that no and I've fixed the error its my first time with C.
 

Koren

Member
its my first time with C.
I see... I wish you a lot of fun learning it and playing with it...

I'll give you an advice: avoid pointers as much as you can. Really.

Of course, they're are useful. Really useful, in fact. But always try first to imagine a solution that avoid them.

If you're not against using (some) C++ instead of pure C, I'd suggest you to look at string in replacement of char*.
 
Doubly linked circular lists are fucking me up. My professor has some weird requirements that are really throwing me off.

Its technically a school assignment, so I'd rather discuss it in PM if anyone has the time.
 

NotBacon

Member
Doubly linked circular lists are fucking me up. My professor has some weird requirements that are really throwing me off.

Its technically a school assignment, so I'd rather discuss it in PM if anyone has the time.

I'll take a look, but yeah circular lists in general are a nuisance.
 

ssharm02

Banned
just a question

how hard is it to program the ghost AI in pacman? How did they do it back in the day, any short cuts we can take today?
 

Two Words

Member
I am getting an incredibly weird bug in my C program in a UNIX environment. I wonder if anybody could take a crack at this.

Real quick summary, my program takes command line arguments, I'll focus on a particular case. The following command line execution of the program should read input from input.txt and output it to output.txt

Code:
./program -f output.txt input.txt

If a line starts with "#include <someFileName>", the contents of that file is printed to output.txt instead of that line. This is handled recursively by my program. I create a new file stream to that file and send the FILE * to the same printing function. This works perfectly fine except for my really odd case.



Really odd bug:
Let's assume the command line execution above is used. If input.txt contains "#include <output.txt>", it works. I do flush the buffer before recursively printing the #include file. However, this breaks as soon the input file has more than seven "#include <output.txt>" lines in it. Seven or less works fine. Anything more causes a crazy infinite loop that causes output.txt to bloat to a 100+ MB file. I also tried this on my university's Linux box. It will allow up to 15 of these #include statements, but 16 or more gives the same bug. I cannot appropriately evoke EOF with ctrl + D either. I have to kill the execution with ctrl + C. My recursion is not going too deep to cause any issues either. It's only 1 level deep of recursion. The first block of code works fine, the second block breaks it.


This input.txt won't cause problems:
Code:
One
Two
#include <output.txt> 1
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;
#include <output.txt> 2 
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; 
#include <output.txt> 3
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;
#include <output.txt> 4
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;
#include <output.txt> 5
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;
#include <output.txt> 6
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;
#include <output.txt> 7


This input.txt will cause problems:
Code:
One
Two
#include <output.txt> 1
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;
#include <output.txt> 2
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;
#include <output.txt> 3
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;
#include <output.txt> 4
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;
#include <output.txt> 5
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;
#include <output.txt> 6
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;
#include <output.txt> 7
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;
#include <output.txt> 8




I'm not really sure about the rules on sharing code that is an assignment. Anybody have experience of that being an issue? I'm not sure how much help could be given without showing the whole source file.
 

Makai

Member
just a question

how hard is it to program the ghost AI in pacman? How did they do it back in the day, any short cuts we can take today?
They each have different personality. They all move towards the player but move in a random direction some percent of the time.
 
I am getting an incredibly weird bug in my C program in a UNIX environment. I wonder if anybody could take a crack at this.

Real quick summary, my program takes command line arguments, I'll focus on a particular case. The following command line execution of the program should read input from input.txt and output it to output.txt

Code:
./program -f output.txt input.txt

If a line starts with "#include <someFileName>", the contents of that file is printed to output.txt instead of that line. This is handled recursively by my program. I create a new file stream to that file and send the FILE * to the same printing function. This works perfectly fine except for my really odd case.



Really odd bug:
Let's assume the command line execution above is used. If input.txt contains "#include <output.txt>", it works. I do flush the buffer before recursively printing the #include file. However, this breaks as soon the input file has more than seven "#include <output.txt>" lines in it. Seven or less works fine. Anything more causes a crazy infinite loop that causes output.txt to bloat to a 100+ MB file. I also tried this on my university's Linux box. It will allow up to 15 of these #include statements, but 16 or more gives the same bug. I cannot appropriately evoke EOF with ctrl + D either. I have to kill the execution with ctrl + C. My recursion is not going too deep to cause any issues either. It's only 1 level deep of recursion. The first block of code works fine, the second block breaks it.

Have you run your program through a debugger and examined it at runtime? If you are in the process of learning UNIX and C then learning how to use the GNU Debugger is an essential skill. Set a breakpoint on the branchpoint where you decide to recurse and check the value of the condition each time the breakpoint is hit. Aside from that the best advice I can give you without examining the code itself is to add error checking to every IO operation. Every read, every write, every open and every close. Also make sure you are closing all file streams after you are finished with them. The kernel will only permit a process to have a finite number of simultaneous open file descriptors and while I don't think that is causing the problem here (it's possible but unlikely), failing to close a file descriptor or file stream is a problem that most beginners will run into eventually and is difficult to diagnose without correct error checking.

I'm not really sure about the rules on sharing code that is an assignment. Anybody have experience of that being an issue? I'm not sure how much help could be given without showing the whole source file.

If you are worried you can put the code into an anonymous private gist on github and PM the link to people and thus remain anonymous to any observers, but I think it is in the spirit of learning that you have come to this thread so I can't imagine any professors would not look down on it. You clearly have made a good attempt at the problem so far.
 

Two Words

Member
Have you run your program through a debugger and examined it at runtime? If you are in the process of learning UNIX and C then learning how to use the GNU Debugger is an essential skill. Set a breakpoint on the branchpoint where you decide to recurse and check the value of the condition each time the breakpoint is hit. Aside from that the best advice I can give you without examining the code itself is to add error checking to every IO operation. Every read, every write, every open and every close. Also make sure you are closing all file streams after you are finished with them. The kernel will only permit a process to have a finite number of simultaneous open file descriptors and while I don't think that is causing the problem here (it's possible but unlikely), failing to close a file descriptor or file stream is a problem that most beginners will run into eventually and is difficult to diagnose without correct error checking.



If you are worried you can put the code into an anonymous private gist on github and PM the link to people and thus remain anonymous to any observers, but I think it is in the spirit of learning that you have come to this thread so I can't imagine any professors would not look down on it. You clearly have made a good attempt at the problem so far.


I'm pretty sure I have closed files correctly. I am not great at using non-IDE debugging tools though, so I'm not sure the best angle to tackle that. I'll send you the code if you want to see it.
 
I'm pretty sure I have closed files correctly. I am not great at using non-IDE debugging tools though, so I'm not sure the best angle to tackle that. I'll send you the code if you want to see it.

Write your programs on Windows, after it works test it on linux. It's what i did in college
 

Koren

Member
I can also take a look if you want... Just send me a PM

(same for CorbBurrito if the problems remains)

just a question

how hard is it to program the ghost AI in pacman? How did they do it back in the day, any short cuts we can take today?
There's plently of links on the subject...
http://gameinternals.com/post/2072558330/understanding-pac-man-ghost-behavior

http://www.gamasutra.com/view/feature/3938/the_pacman_dossier.php?print=1

The "AI" is insanely basic, it's probably not even an AI, but it took them a really long time to find the best behaviors, and they achieve really a great result I think. Even with the bug.

In fact, with real AI, the game is unplayable. Even with 3 ghosts.
 

Two Words

Member
I can also take a look if you want... Just send me a PM

(same for CorbBurrito if the problems remains)


There's plently of links on the subject...
http://gameinternals.com/post/2072558330/understanding-pac-man-ghost-behavior

http://www.gamasutra.com/view/feature/3938/the_pacman_dossier.php?print=1

The "AI" is insanely basic, it's probably not even an AI, but it took them a really long time to find the best behaviors, and they achieve really a great result I think. Even with the bug.

In fact, with real AI, the game is unplayable. Even with 3 ghosts.

Thanks, appreciate it.
 

Koren

Member
Thanks, appreciate it.
I'm convinced now that what I was expaining in PM is correct...

Just use
Code:
setvbuf(stdout, 0, _IONBF, 0);
which disable write buffering for stdout and even a single include of output.txt in input.txt will give you an infinite loop.

So your infinite loop is perfectly normal, definitively not a bug, and the cases where it work is just a race between the reading and the write buffer filling.


Edit : BUFSIZ probably holds the size of the buffer. It's 1024 for me, so as soon as output.txt is over 1kb, you'll get the loop.

You can get the exact size of the buffer by using __fbufsize(stdout), but it's in stdio_ext.h, and I'm getting errors when I include it, I'm not sure It would be really useful to spend time looking why there's errors just to find it's indeed 1024...
 

luoapp

Member
I'm convinced now that what I was expaining in PM is correct...

Just use
Code:
setvbuf(stdout, 0, _IONBF, 0);
which disable write buffering for stdout and even a single include of output.txt in input.txt will give you an infinite loop.

So your infinite loop is perfectly normal, definitively not a bug, and the cases where it work is just a race between the reading and the write buffer filling.


Edit : BUFSIZ probably holds the size of the buffer. It's 1024 for me, so as soon as output.txt is over 1kb, you'll get the loop.

You can get the exact size of the buffer by using __fbufsize(stdout), but it's in stdio_ext.h, and I'm getting errors when I include it, I'm not sure It would be really useful to spend time looking why there's errors just to find it's indeed 1024...

Now I am intrigued. @Two Words, can you PM me a copy of your code? Thanks.
 
I sent it a bit ago. Did you not get my pm?

Yeah, apologies. I am a little slow on this :)

I am trying to reproduce your bug with a simple example and I am having a hard time doing it with trivial examples (simple text files that do not transitively include anything else). I have a simple text file with 10 lines of text and am able to include it hundreds of times and the program still terminates.

I am wondering what is the structure of the files you are testing this on? If you are testing it on complex C/C++ header files then you will almost certainly hit the problem of cyclic dependancies between the files which lead to a infinite loop using your algorithm. Indeed when I try to use the system headers on my Mac it quickly gets stuck in a infinite loop.

Are you able to advise on the structure of the files you are testing, and specifically if you know of any cyclic dependencies between them?
 

V_Arnold

Member
Hey folks! What language would you recommend me to focus on beside Javascript? I use JS now on serverside and on clientside as well, and i do not plan on stopping learning that language until I know everything behind the scenes.

But I am thinking about expanding my daily routine with an additional language.
My current targets are:
- Haskell
- C#
- Go
- Python
- or Rust

I have dabbled with Java, C++, AS before, but I would need something that is not a nightmare to manage (I do not get scared away by garbage collection, tbh, so that is okay), but have bindings to openGL/webGL or will have soon some to Vulkan. Being able to create modules that can be plugged into Node is also a plus. Any advice?
 

Koren

Member
Yeah, apologies. I am a little slow on this :)

I am trying to reproduce your bug with a simple example and I am having a hard time doing it with trivial examples
If you're talking about the "bug" I'm thinking about (although it's not a bug, actually), it happens when you try to include the output file in the output file.

Two words code is unbuffered, so sometimes it work, but only because fprintf is buffered (so that you read the whole output file before writing it). But as soon as the buffer is too short, it create a periodic, infinite file.

Hey folks! What language would you recommend me to focus on beside Javascript?
Out of your list, I'd say Python is pretty easy to manage (especially with pip, conda, etc.), and has plently of GL bindings. It's also usually preinstalled on Linux, so you'll get plently of use for anything more complex than a couple lines of scripting.

And there's plently of solutions to make Node and Python inter-operate...
 
Hey folks! What language would you recommend me to focus on beside Javascript? I use JS now on serverside and on clientside as well, and i do not plan on stopping learning that language until I know everything behind the scenes.

But I am thinking about expanding my daily routine with an additional language.
My current targets are:
- Haskell
- C#
- Go
- Python
- or Rust

I have dabbled with Java, C++, AS before, but I would need something that is not a nightmare to manage (I do not get scared away by garbage collection, tbh, so that is okay), but have bindings to openGL/webGL or will have soon some to Vulkan. Being able to create modules that can be plugged into Node is also a plus. Any advice?
Let me tell you some languages that you shouldn't use.

I think Haskell is fun and interesting but the purity and laziness really do get in the way sometimes. It has a huge barrier of entry and these days, I'm not sure if it's worth it anymore (credentials: half a decade of experience in it).

Go doesn't have generics. I know that's whatever for someone coming from a dynamically typed language but in a statically typed language it's really a basic feature that Go opted out of (to reduce binary sizes). No classes, either.

If you don't really care about performance than Rust isn't worth the effort.

I think you should learn Python or C#. You don't have a static language under your belt, so that makes C# valuable to learn. But Python is a great language and you will appreciate its pithiness.
 
Let me tell you some languages that you shouldn't use.

I think Haskell is fun and interesting but the purity and laziness really do get in the way sometimes. It has a huge barrier of entry and these days, I'm not sure if it's worth it anymore (credentials: half a decade of experience in it).

Go doesn't have generics. I know that's whatever for someone coming from a dynamically typed language but in a statically typed language it's really a basic feature that Go opted out of (to reduce binary sizes). No classes, either.

If you don't really care about performance than Rust isn't worth the effort.

I think you should learn Python or C#. You don't have a static language under your belt, so that makes C# valuable to learn. But Python is a great language and you will appreciate its pithiness.

Yeah, those are good recommendations. Because I'm a big fan of Rust, I'd recommend that as well but it'll have a much steeper learning curve than either C# or Python. It also depends a lot on what you want to do. If you want to continue doing web stuff, Python and C# are great. If you want to do something different, other languages might be better.
 

V_Arnold

Member
Thanks for the suggestion, folks.
A few years ago, I actually programmed a few things in C#, back when it was still exclusive to Windows(or was it?), but I mainly used de XNA library with it, and nothing else, so I only learned those things.

My longterm (i.e. 3-4+ years) plans are definitely machine learning, concurrency, neural networks, but I have a feeling that my stopped math studies will present a way bigger barrier of entry than any specific kind of programming language I would choose.
 

Kalnos

Banned
If you're doing web dev and you want to try a functional language try Elixir instead of Haskell IMO. It's fun and the Phoenix framework has quite a bit of buzz right now so it's a good time to jump in. Also since it uses the Erlang VM it's awesome for concurrency.

Every job that I have had to this point has used C# in some capacity though so learning it would probably be beneficial.
 

Koren

Member
Honestly, neural networks principles are really basic. I've already explained the principles to a 12 years old...

Granted, the back propagation formula can be hard to establish, and scheme that gives a quicker convergence are even harder to understand, but you don't need a lot of maths to use them.
 

Makai

Member
Thanks for the suggestion, folks.
A few years ago, I actually programmed a few things in C#, back when it was still exclusive to Windows(or was it?), but I mainly used de XNA library with it, and nothing else, so I only learned those things.

My longterm (i.e. 3-4+ years) plans are definitely machine learning, concurrency, neural networks, but I have a feeling that my stopped math studies will present a way bigger barrier of entry than any specific kind of programming language I would choose.
Do F#! It's Haskell + C#.
 

Two Words

Member
Yeah, apologies. I am a little slow on this :)

I am trying to reproduce your bug with a simple example and I am having a hard time doing it with trivial examples (simple text files that do not transitively include anything else). I have a simple text file with 10 lines of text and am able to include it hundreds of times and the program still terminates.

I am wondering what is the structure of the files you are testing this on? If you are testing it on complex C/C++ header files then you will almost certainly hit the problem of cyclic dependancies between the files which lead to a infinite loop using your algorithm. Indeed when I try to use the system headers on my Mac it quickly gets stuck in a infinite loop.

Are you able to advise on the structure of the files you are testing, and specifically if you know of any cyclic dependencies between them?

My original post explains how to replicate it. Though, it seems to have been thoroughly answered :p

I am getting an incredibly weird bug in my C program in a UNIX environment. I wonder if anybody could take a crack at this.

Real quick summary, my program takes command line arguments, I'll focus on a particular case. The following command line execution of the program should read input from input.txt and output it to output.txt

Code:
./program -f output.txt input.txt

If a line starts with "#include <someFileName>", the contents of that file is printed to output.txt instead of that line. This is handled recursively by my program. I create a new file stream to that file and send the FILE * to the same printing function. This works perfectly fine except for my really odd case.



Really odd bug:
Let's assume the command line execution above is used. If input.txt contains "#include <output.txt>", it works. I do flush the buffer before recursively printing the #include file. However, this breaks as soon the input file has more than seven "#include <output.txt>" lines in it. Seven or less works fine. Anything more causes a crazy infinite loop that causes output.txt to bloat to a 100+ MB file. I also tried this on my university's Linux box. It will allow up to 15 of these #include statements, but 16 or more gives the same bug. I cannot appropriately evoke EOF with ctrl + D either. I have to kill the execution with ctrl + C. My recursion is not going too deep to cause any issues either. It's only 1 level deep of recursion. The first block of code works fine, the second block breaks it.


This input.txt won't cause problems:
Code:
One
Two
#include <output.txt> 1
——————————————
#include <output.txt> 2 
—————————————— 
#include <output.txt> 3
——————————————
#include <output.txt> 4
——————————————
#include <output.txt> 5
——————————————
#include <output.txt> 6
——————————————
#include <output.txt> 7


This input.txt will cause problems:
Code:
One
Two
#include <output.txt> 1
——————————————
#include <output.txt> 2
——————————————
#include <output.txt> 3
——————————————
#include <output.txt> 4
——————————————
#include <output.txt> 5
——————————————
#include <output.txt> 6
——————————————
#include <output.txt> 7
——————————————
#include <output.txt> 8




I'm not really sure about the rules on sharing code that is an assignment. Anybody have experience of that being an issue? I'm not sure how much help could be given without showing the whole source file.
 
My original post explains how to replicate it. Though, it seems to have been thoroughly answered :p

Honestly, this is a good time to strap in and learn how to use a debugger. Imagine trying to debug 100,000 lines of code by just inspecting the source. Doesn't work. Whether you want to use Eclipse, GDB, or Visual Studio, there are lots of good tutorials on how to do the most basic debugging, and usually homework assignment bugs can be found in less than 1 minute using a debugger.
 
So I understand that particular input.txt correctly, are you having it take the output.txt file and feeding it again back to the same output.txt file?
 

Two Words

Member
Honestly, this is a good time to strap in and learn how to use a debugger. Imagine trying to debug 100,000 lines of code by just inspecting the source. Doesn't work. Whether you want to use Eclipse, GDB, or Visual Studio, there are lots of good tutorials on how to do the most basic debugging, and usually homework assignment bugs can be found in less than 1 minute using a debugger.

Yeah, I am decent with using a debugger with an IDE. I'm used to setting break points and using watches to observe the change in data line by line. Debugging our MIPS project last semester was a new experience. I had to swim in a pool of memory addresses the whole time.
 

Koren

Member
Honestly, this is a good time to strap in and learn how to use a debugger.
In this particular case, since the issue is buffering inside fprintf, I'm not sure that the debugger would be as useful (but I agree, it's a really valuable tool).

So I understand that particular input.txt correctly, are you having it take the output.txt file and feeding it again back to the same output.txt file?
Yes.

A strange idea, if you ask me, and I'd be curious to know whether it's part of the assignment, but still an interesting case to test.

It only works because, for short output.txt, the write flush happen after the read loop reach the current end of the file.

If I had to do this, I would write to a temp file (assuming it's too large for memory), and rename the result at the end, to avoid this kind of issues...
 
In this particular case, since the issue is buffering inside fprintf, I'm not sure that the debugger would be as useful (but I agree, it's a really valuable tool).
Haven't seen the code right? So for all we know it's a logic error. Change stdout to stderr to rule out buffering
 

Koren

Member
Haven't seen the code right?
Are you asking if I have seen the code? If so, yes, I've read it.

(Took me two minutes to read it and find the issue, and probably close to half an hour to convince myself I was right ^_^)

So for all we know it's a logic error. Change stdout to stderr to rule out buffering
He uses freopen, I'd like to know how that work for buffering... Does stderr stay unbuffered? The documentation says it's "like fclose then fopen", without anything about buffering, I wouldn't bet a lot on how it works...

I'd say using _IONBF is a better way to be sure that buffering is disabled, in this case (and you can still use stdio, which is handy since the code use stderr for console messages). And yes, it triggers the infinite-loop "bug", which isn't surprising.

In fact, I wouldn't totally trust the fact that stderr is unbuffered without checking it, if I haven't written the code myself, pretty sure that nothing prevents you from buffering it.
 
Sup, programmingGAF.

Question: I am a data entry specialist. I work with Excel and SQL, and I do batch commands to automate some work. Currently I am learning Tableau and Statistics.

Next year I plan on applying for Data Analyst jobs, the only thing is, a lot of the jobs
I want require knowledge of Python. I already have a Lynda.com playlist to teach me
throughout the year.

How difficult will it be for me (some one with no programming knowledge) to learn
Python? Also I'll be diving into R once a week since its another sought after skill.

Thanks in advance.
 

ssharm02

Banned
I can also take a look if you want... Just send me a PM

(same for CorbBurrito if the problems remains)


There's plently of links on the subject...
http://gameinternals.com/post/2072558330/understanding-pac-man-ghost-behavior

http://www.gamasutra.com/view/feature/3938/the_pacman_dossier.php?print=1

The "AI" is insanely basic, it's probably not even an AI, but it took them a really long time to find the best behaviors, and they achieve really a great result I think. Even with the bug.

In fact, with real AI, the game is unplayable. Even with 3 ghosts.
wow thnx for the links
 

NotBacon

Member
[Agent]ZeroNine;196899107 said:
Sup, programmingGAF.
How difficult will it be for me (some one with no programming knowledge) to learn
Python? Also I'll be diving into R once a week since its another sought after skill.

Thanks in advance.

To learn Python? Anybody can learn the syntax fairly easily. It'll probably take a couple weeks to nail the essentials.

To learn programming? It's quite the hurdle for many newcomers. For some it just clicks, for others it takes relentless study and concentration. You'll probably end up in the middle. If you really want to dive in, my advice: Get a good foundation (from a good book/website/etc.), and then once you have the basics down, run with it. Program, program, program. For hours. The only way to truly learn is by doing.

Good links:
Official Python Tutorial.
Learn Python the Hard Way.
 
To learn Python? Anybody can learn the syntax fairly easily. It'll probably take a couple weeks to nail the essentials.

To learn programming? It's quite the hurdle for many newcomers. For some it just clicks, for others it takes relentless study and concentration. You'll probably end up in the middle. If you really want to dive in, my advice: Get a good foundation (from a good book/website/etc.), and then once you have the basics down, run with it. Program, program, program. For hours. The only way to truly learn is by doing.

Good links:
Official Python Tutorial.
Learn Python the Hard Way.

LPTHW is really not that great. There are better courses out there, from MITs 6.001x on EdX (which still uses python 2) or the Python For Everyone courses on Coursera to various free online books like Invent Your Own Computer Games with Python or How To Think Like A Computer Scientist.
 

Koren

Member
LPTHW is really not that great.
That's interesting to hear...

I see it quoted quite often, and while I'm clearly not the intended target (the author clearly states this), I'm still interested by this kind of introductions both because I have to train beginners myself, and because I'm often asked for this kind of ressources.

I don't agree with several of the author's choices, and overall I felt unconvinced, and I was wondering whether I was just me (or if I had felt differently as a beginner). I'm also a bit surprised that someone that admit he doesn't like coding anymore write a course.

That being said, I don't think there's a book/course that'll fit everyone, so it's a difficult question.
 
That's interesting to hear...

I see it quoted quite often, and while I'm clearly not the intended target (the author clearly states this), I'm still interested by this kind of introductions both because I have to train beginners myself, and because I'm often asked for this kind of ressources.

I don't agree with several of the author's choices, and overall I felt unconvinced, and I was wondering whether I was just me (or if I had felt differently as a beginner). I'm also a bit surprised that someone that admit he doesn't like coding anymore write a course.

That being said, I don't think there's a book/course that'll fit everyone, so it's a difficult question.

Yeah, its quoted quite often, dunno why though, except that its free? As i said, i just feel there are better free materials out there.

I dont really like how LPTHW says not to use Python 3. People should use Python 3 unless there is a specific reason not to but i dont think that people new to python will run into that issue (MIT 6.001x MOOC still uses 2.7 so thats a valid reason).
 
I honestly think for all the time people spend worrying about which book is optimal, you could just pick one and read it. It's not like the other books are going anywhere. So just pick one, whether it's Programming Python, LPTHW, Essential Python, Python Cookbook, or whatever.
 
I think programming is the most frustrating thing I have ever attempted in my life. I'm in week 7 of my class. Next week is the final. These last two weeks have dealt with objects/drivers and every time I think I finally understand what's going on I don't actually know. So my assignment this week the instructor gave us a driver and the assignment is to create an object class for it. I think I am missing some basic understanding of how methods work.

Any ways here's the situation. I created my Constructors, Accessors and Mutators. I'm having trouble setting up methods for changing an objects variables. Here's the driver.

Code:
import java.util.Scanner;

public class BeersCharlesWeek7Prog
{



	public static void main (String[] args)
	{
		  Scanner stdIn = new Scanner(System.in);
		  String name;    //Auxiliar ComputerMicrobe name
		  String dNACode;   //Auxiliar ComputerMicrobe DNA Code
		  ComputerMicrobe a, b, c; // ComputerMicrobe objects

		  System.out.println("Enter name of first ComputerMicrobe");
		  name = stdIn.next();
		  System.out.println("Enter DNA Code for first ComputerMicrobe");
		  dNACode = stdIn.next();
		  a = new ComputerMicrobe(name, dNACode);

		  System.out.println("Enter name of second ComputerMicrobe");
		  name = stdIn.next();
		  System.out.println("Enter DNA Code for second ComputerMicrobe");
		  dNACode = stdIn.next();
		  b = new ComputerMicrobe(name, dNACode);

		  System.out.println("Initial set of ComputerMicrobes");
		  System.out.println(a);
		  System.out.println(b);

		  System.out.println("ComputerMicrobe a after mutation");
		  a.mutate();
		  System.out.println(a);

		  System.out.println("ComputerMicrobe b after reverse");
		  b.reverse();
		  System.out.println(b);



	} // end main
} // end class

There is more to the driver, but atm I just need to know how to apply my method to an object. Here is my object class so far.

Code:
public class ComputerMicrobe
{
private String name;
private String dNACode;



	//Constructors

	public ComputerMicrobe (String dNACode, String name)
	{
		this.setName(name);
		this.setDNACode(dNACode);

	}
	public ComputerMicrobe()
	{
		this("", "");
	}
	//Accessor Methods

	public String getName()
	{
		return this.name;
	}

	public String getDNACode()
	{
		return this.dNACode;
	}



	// Mutator Methods

	public void setName(String name)
	{
		this.name = name;
	}

	public void setDNACode(String dNACode)
	{
		this.dNACode = dNACode;
	}



	//Method for mutate
	public String mutate()
	{
		return this.getDNACode();
	}
       
        //Method for reverse
	public String reverse()

	{
	   return this.getDNACode();
	}


	//
	public String toString()
    {
    	return this.getName() + " " + this.getDNACode();
    }


}

So in the driver it's calling the mutate or reverse method with the a.mutate and b.reverse. I have tried a few different methods and and all the examples I've seen return a value such as I had return reverse; at one point, but that doesn't reverse DNACode of ComputerMicrobe b per this instruction.

reverse &#8211; This method will take no parameters. It will reverse the String for the dNACode/ For example if the dNACode is &#8220;ABC&#8221;, after this method the dNACode is &#8220;CBA&#8221;. The method will return the current object (return this).

I had something like this in the reverse method with different variables.

Code:
if(source == null || source.isEmpty()){
            return source;
        }      
        String reverse = "";
        for(int i = source.length() -1; i>=0; i--){
            reverse = reverse + source.charAt(i);
        }
     
        return reverse;

It seems like no matter what I put in the reverse method I get the Microbe printed out per inputs no changes.


Hopefully I explained this well enough for a little help.
 

NotBacon

Member
1. You're passing in the strings backwards when you create the objects in the driver.

2. If you want to actually change a String within your object, you'll need to reassign it. So do that reversal code, and then this.dnaCode = theReveresedString. And then return your object like the instructions say.


Strings in Java are immutable, so you have to assign a new String whenever you want it changed.
 
Thanks. My issue was not knowing how to return the object. Just as a test I made a simple method to make sure it was returning.

Code:
public String mutate()
	{
		this.dNACode = "123X5";
		return this.dNACode;
	}

	public String reverse()

	{

        this.dNACode = "09876";
        return this.dNACode;

	}

Now that I know it's returning I just need to do the fun math part. By fun I mean frustrating.
 

Koren

Member
I honestly think for all the time people spend worrying about which book is optimal, you could just pick one and read it. It's not like the other books are going anywhere. So just pick one, whether it's Programming Python, LPTHW, Essential Python, Python Cookbook, or whatever.
I mostly agree, but it can have big consequences.

I tried learning Python in the late 90s. The explanations were awful (tand made the language appear far worse than it is), enough to nullify my interest in the language. Since I didn't needed it, I went study something else.

A half a dozen years later, I tried again, and the experience was completely different.

So, I won't spend weeks choosing a book, I can even try one on a feeling in a bookstore, but I'm always interested in advices on which book is technically sound and interesting.
 

JeTmAn81

Member
Thanks. My issue was not knowing how to return the object. Just as a test I made a simple method to make sure it was returning.

Code:
public String mutate()
	{
		this.dNACode = "123X5";
		return this.dNACode;
	}

	public String reverse()

	{

        this.dNACode = "09876";
        return this.dNACode;

	}

Now that I know it's returning I just need to do the fun math part. By fun I mean frustrating.

It looked to me like your reverse method was correct so as long as you're selecting the correct string for the source it should work.

A word about immutable strings. When a string is created, the computer has to find a space in memory the exact size of the string to fit it into, so when you add a new character to the string it has to search for a whole new space which fits the new size of the string, and redoes the process to allocate the contents of your string to that new space.

That's why they're called immutable, because you don't get to just change one character of a string even if you want. I mean, you can change that character but underneath the language is actually allocating a whole new space for it rather than just finding where the existing string lives in memory and adjusting the part that corresponds to the character you changed.

So all of that is a bit of what goes on behind the scenes. It's not something you need to know in order to reverse a string, but it's good to know a bit more about why things tend to be done in certain ways. Most industrial implementations of string reversal would not be done the way you're doing it, but would use other methods that try to avoid having to reallocate the whole string every time you change something. But what you're doing is fine for a beginner.

Lastly, what's so tough about the mutate method? It's hard to imagine that they would be asking you to do a very complex transformation in the middle of an elementary programming assignment.
 
It looked to me like your reverse method was correct so as long as you're selecting the correct string for the source it should work.

A word about immutable strings. When a string is created, the computer has to find a space in memory the exact size of the string to fit it into, so when you add a new character to the string it has to search for a whole new space which fits the new size of the string, and redoes the process to allocate the contents of your string to that new space.

That's why they're called immutable, because you don't get to just change one character of a string even if you want. I mean, you can change that character but underneath the language is actually allocating a whole new space for it rather than just finding where the existing string lives in memory and adjusting the part that corresponds to the character you changed.

So all of that is a bit of what goes on behind the scenes. It's not something you need to know in order to reverse a string, but it's good to know a bit more about why things tend to be done in certain ways. Most industrial implementations of string reversal would not be done the way you're doing it, but would use other methods that try to avoid having to reallocate the whole string every time you change something. But what you're doing is fine for a beginner.

Lastly, what's so tough about the mutate method? It's hard to imagine that they would be asking you to do a very complex transformation in the middle of an elementary programming assignment.

Everything sees to be a challenge for me. I'm actually completely stuck now. I'm still getting a 92% in the class, but I feel like I don't understand what's being asked alot of the time or why I'm doing the things I'm doing. The instructions for the mutate are

mutate – This method will take no parameters. It will select at random (use the Math.random method) a position for a character in its dNACode and replace it with the character ‘X’. The method will return the current object (return this).

I feel like I'm close with this.

Code:
int r1 = (int) (Math.random() * dNACode.length());
		dNACode.replace(dNACode.charAt(r1), 'X');
		this.dNACode = dNACode;
		return this.dNACode;

but probably not. I know I need to make a new string.

The next step after reverse is to create a new ComputerMicrobe.

reproduce – This method takes another ComputerMicrobe as a parameter and creates a new ComputerMicrobe. This new ComputerMicrobe will have a name that is the concatenation of the name of “this” ComputerMicrobe with the name of the other ComputerMicrobe. The dNACode of the new ComputerMicrobe will take half of the dNACode of “this” ComputerMicrobe concatenated with half of the dNACode of the other ComputerMicrobe. The method will return the newly created ComputerMicrobe.

The driver won't compile if the class is wrong, so I've been using that to help steer me in the right direction. I can't even find any examples of the "reproduce" method. I guess that's what programming is about. I'm not worried about passing the class. I'm worried about moving from here to Programming Fundamentals and being completely lost.
 
Top Bottom