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

06-13-2015 , 04:23 PM
Code:
def vowel_start(word)
  if (word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u')
    return true
  else
    return false
  end
end
This is unidiomatic ruby in almost every way, so I'll try to help:

"vowel_start" -- Make your names sound as close to natural English as possible. "starts_with_vowel" is clearer than "vowel_start"

You should only use "return" when you need to exit early. Here, your function is a single expression, so you could remove both returns:

Code:
def starts_with_vowel(word)
  if (word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u')
    true
  else
    false
  end
end
But why stop there? You are returning "true" if the if clause is true, false otherwise. Just make your body the if clause itself:

Code:
def starts_with_vowel(word)
  (word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u')
end
Just looking at this, you can know with 95% certainty there is a better way. Ruby is optimized for conciseness. This kind of repetition for such a common task will have a shortcut:

Code:
def starts_with_vowel(word)
  'aeiou'.include? word[0]
end
This is now acceptable ruby. As for your other function with the until loop, I'll let you fix that, but know that using loops that way is nearly always a "writing C/java in ruby" code smell, and you can find a better way.
Programming homework and newbie help thread Quote
06-17-2015 , 07:33 PM
Quote:
Originally Posted by Low Key
Just discovered https://www.hackerrank.com/
Embarrassed how little progress I made. Excuse is its a new language.
I've been enjoying doing the project euler challenge section there. I have all the code saved I used when I started doing problems on projecteuler.net, so its been fun to go back and see how crappy and bad my code and process for solving problems was in doing the early euler problems (I used that site to get practice and learn Python), and then since those same problems on hackrank require a more efficient algorithm to solve the problem, rewriting them has really shown me how much more I know than when I started, which is a pretty satisfying.
Programming homework and newbie help thread Quote
06-19-2015 , 07:32 PM
Thought this was a good post, for all us aspiring programmers here

http://www.vikingcodeschool.com/post...s-so-damn-hard
Programming homework and newbie help thread Quote
06-20-2015 , 05:04 AM
You will always hit roadblocks, and you will find yourself more than once feeling like a total idiot when you know the answer is going to be stupid obvious once you figure it out. It is learning to go over those slumps and grinding forward that makes you better. After a few times, you say to yourself: "yeah, I'm struggling right now, so what?" The light at the end of the tunnel is always there, just have faith you'll make it.

If you find yourself working on something that doesn't offer you a real challenge (sorry, codecademy and other sources like that aren't a challenge), then you aren't really absorbing anything. It is from those moments you struggle, you fight, you lose 3 hours on a Friday night, that things really stick in. Sure, some things come from repetition, but that's not learning, that's regurgitation.
Programming homework and newbie help thread Quote
06-20-2015 , 04:53 PM
Couple questions:

Abstract classes in java, are those like templates in c++?

re: threads - if I have 2 methods that take 4 minutes each to complete, and 8 minutes total back to back, if they run concurrently in separate threads, that means both calcs are done in 4 minutes, yes?
Programming homework and newbie help thread Quote
06-20-2015 , 05:58 PM
If the cpu isn't overloaded by either of those methods then sure.
Also depending on what method is doing that may contradict other method to wait.
Programming homework and newbie help thread Quote
06-20-2015 , 07:28 PM
Quote:
Originally Posted by Low Key
Couple questions:

Abstract classes in java, are those like templates in c++?
No. Do you mean generic classes?
Quote:
Originally Posted by Low Key
re: threads - if I have 2 methods that take 4 minutes each to complete, and 8 minutes total back to back, if they run concurrently in separate threads, that means both calcs are done in 4 minutes, yes?
That depends on what resources the threads share. And as iosys said, only if they methods are independent. Half the time is the best case for your question.
Programming homework and newbie help thread Quote
06-20-2015 , 08:44 PM
A more complete answer is that low key is asking about parallel processes, which means the best case may be correct. Concurrency and parallelism are two different ideas, though relevant to each other. Concurrency describes the algorithms used to bring processes together and doesn't always denote parallel processing. Concurrent systems can exist on single processor systems.

Amdahl's Law describes the issues of max speed irt parallel systems: https://en.wikipedia.org/wiki/Amdahl%27s_law

Gustafson's Law gives a counterpoint to Amdahl's Law: https://en.wikipedia.org/wiki/Gustafson%27s_law

https://en.wikipedia.org/wiki/Concurrent_computing
Programming homework and newbie help thread Quote
06-20-2015 , 08:46 PM
total noob but how do i find someone to make me a program?
Programming homework and newbie help thread Quote
06-20-2015 , 09:36 PM
dave would be the type of guy to scare people away from CS
Programming homework and newbie help thread Quote
06-20-2015 , 10:03 PM
More information?
Programming homework and newbie help thread Quote
06-20-2015 , 10:19 PM
Quote:
No. Do you mean generic classes?
Possibly? Only two weeks into studying Java
Programming homework and newbie help thread Quote
06-20-2015 , 10:33 PM
Quote:
Originally Posted by iosys
dave would be the type of guy to scare people away from CS
Dave is pretty much correct in what he stated. Obviously if a multithreaded process is running on a single processor and the threads run at the same priority and the threads don't block until they run to completion the OS is going to schedule an equal amount of time for both, thus there isn't really any speed up execution time wise, in fact the OS overhead of switching between the two threads will increase execution time. Multi threading designs come into play generally speaking when a process has asynchronous operations that will block until an event occurs. A classic example is I/O calls to the OS. Do a disk read and your thread will block until the I/O operation is complete. When the thread blocks other threads run doing their intended processing. When the Disk read completes the OS schedules the blocked thread and the disk read is completed. When the thread retrieves the data from the OS the data read may queued for another thread to process the data. Access to such a queue is typically synchronized via OS allocated resources etc.
Programming homework and newbie help thread Quote
06-20-2015 , 10:48 PM
lol adios the post was a friendly poke at his character.

When I read his posts on anything specific, I get the impression he lives for every bit of detail he can get. Nothing wrong with it but if I had my first CS course with a professor or teacher that was as detailed seeking as him, I probably would have had an awful time.
Programming homework and newbie help thread Quote
06-20-2015 , 11:06 PM
iosys;

I take all of that as a compliment.
Programming homework and newbie help thread Quote
06-21-2015 , 09:28 AM
Quote:
Originally Posted by Low Key
Possibly? Only two weeks into studying Java
Generic classes in Java a similar to templates. The idea is you implement a class for different types, but each instance of the class can only handle one type.

Abstract classes (in both languages) are classes that have at least one abstract method (i. e. a method that isn't implemented). They're mainly used when you want to implement part of the functionality in different ways.

Interfaces in Java are similar to fully abstract classes in C++. They are only a 'description' of what the class should do. The reason you need them in Java is that the language doesn't support multiple inheritance, but a class can implement more than one interface.

But depending on how deep you dig, the previous three paragraphs are simplistic, sloppy or wrong. Adjust as your studies move ahead.
Programming homework and newbie help thread Quote
06-21-2015 , 11:03 AM
Simplistic is good for where I am. Appreciate it, and all the other responses

Never got to abstract class in c++, but it sounds similar to how polymorphism works in a way. Will have to look more of this stuff up.
Programming homework and newbie help thread Quote
06-22-2015 , 07:17 AM
It is polymorphism.

Let's say you have a shape class, defined like this:

Code:
class Shape {
public:
   Shape();
   virtual void Draw() = 0;
   virtual int Area() = 0;
}
The "= 0" part is called the "pure virtual" specifier in C++, in other languages like C# and Java we would just declare "abstract void Draw();". The difference between a virtual method and an abstract method is that a virtual method can be overridden, but an abstract one MUST be overridden (else the derived class is also abstract). Creating an instance of an abstract class is not allowed for the very good reason that we have no idea how to Draw it or calculate the Area. Once we subclass into Circle, Triangle etc. then we can implement those things, then I can for instance put them in a Shape[] and loop through and draw them all.

C++ doesn't have interfaces, but in C# I might do something like this:

Code:
public interface IDrawable {
    void Draw();
}

public abstract class Shape : IDrawable {
    public abstract void Draw();
    public abstract int Area();
}

public class AmusingSketchOfDickAndBalls : IDrawable {
    public void Draw() {
        //implementation
    }
}
Here Shape and AmusingSketch need share nothing in common other than they're both things that can be drawn. Rather than specify an inheritance hierachy, I'm specifying a behaviour my class is capable of. So both shapes and amusing sketches could be placed in an IDrawable[].
Programming homework and newbie help thread Quote
06-22-2015 , 04:04 PM
actually had covered virtual/pure virtual functions in cpp, so was able to following along with that pretty well for once. ^_^

Going over some of project Euler, one notices that a lot of the problems are super math focused, along the lines of:

Quote:
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?
Now, I understand the focus of that site is mostly math stuff, but a lot of loosely related sites have about the same focus.

I've always thought of that as the sort of thing you might study at university, lots and lots of math and not a lot of real-world CS implementation. Is that a fairly accurate view? Or do most, say, business-to-business or business-to-consumer solutions revolve around finding out how many pythagorean triples there are below 50 million?

I'm absolutely not against solving stuff like this to get a better grip on using the various bits of languages, just want to know if it's something I'll need to retain or more like a good workout for the mind.
Programming homework and newbie help thread Quote
07-20-2015 , 03:51 AM
Hey Guys

Having problems launching a simple Swing app using Java Web Start. I've successfully clean&built the project, but when I launch it using internet explorer I get a failed dialog box with the exception code below. Im using the latest version of Netbeans, JDK is up to date, and although there are a few threads on this issue on Stackoverflow there doesn't seem to be a workable solution.

Please help

Quote:

java.lang.NumberFormatException: For input string: "\Users\x\Documents\Open"
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.sun.deploy.security.DeployManifestChecker.veri fyCodebaseEx(Unknown Source)
at com.sun.deploy.security.DeployManifestChecker.veri fyCodebase(Unknown Source)
at com.sun.deploy.security.DeployManifestChecker.veri fy(Unknown Source)
at com.sun.deploy.security.DeployManifestChecker.veri fy(Unknown Source)
at com.sun.javaws.security.AppPolicy.grantUnrestricte dAccess(Unknown Source)
at com.sun.javaws.security.JNLPSignedResourcesHelper. checkSignedResourcesHelper(Unknown Source)
at com.sun.javaws.security.JNLPSignedResourcesHelper. checkSignedResources(Unknown Source)
at com.sun.javaws.Launcher.prepareResources(Unknown Source)
at com.sun.javaws.Launcher.prepareAllResources(Unknow n Source)
at com.sun.javaws.Launcher.prepareToLaunch(Unknown Source)
at com.sun.javaws.Launcher.prepareToLaunch(Unknown Source)
at com.sun.javaws.Launcher.launch(Unknown Source)
at com.sun.javaws.Main.launchApp(Unknown Source)
at com.sun.javaws.Main.continueInSecureThread(Unknown Source)
at com.sun.javaws.Main.access$000(Unknown Source)
at com.sun.javaws.Main$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Programming homework and newbie help thread Quote
07-22-2015 , 01:13 PM
Quote:
Originally Posted by Low Key
actually had covered virtual/pure virtual functions in cpp, so was able to following along with that pretty well for once. ^_^

Going over some of project Euler, one notices that a lot of the problems are super math focused, along the lines of:



Now, I understand the focus of that site is mostly math stuff, but a lot of loosely related sites have about the same focus.

I've always thought of that as the sort of thing you might study at university, lots and lots of math and not a lot of real-world CS implementation. Is that a fairly accurate view? Or do most, say, business-to-business or business-to-consumer solutions revolve around finding out how many pythagorean triples there are below 50 million?

I'm absolutely not against solving stuff like this to get a better grip on using the various bits of languages, just want to know if it's something I'll need to retain or more like a good workout for the mind.
your instinct is mostly right.

if you are writing day to day business applications, you will rarely be solving this kind of problem. it's much more important to be able to model real world business processes simply and flexibly, and to write maintainable code. it's a fairly different skill. that said, if you *can't* solve the simple and medium-difficulty euler problems well, it's unlikely you'll be good at writing business applications. there are some fundamental skills that carry over.
Programming homework and newbie help thread Quote
07-29-2015 , 11:54 AM
Hey guys,

I'm doing MIT 6.00 and this is problem set 2, maybe you guys could help me out. I'm trying to compute the value of a polynomical function. This is in python.

When I use the following code, I get no output at all. What am I doing wrong?

def evaluate_poly(poly,x):
total = 0.0
poly = [0.0, 0.0, 5.0, 9.3, 7.0]
x = 3
for i in xrange(len(poly)):
total += poly[i] * (x ** i)

return total
print total

Crap, indentation doesn't work, how can I copy my code to 2p2?
Programming homework and newbie help thread Quote
07-29-2015 , 12:12 PM
Put print total before return total
Programming homework and newbie help thread Quote
07-29-2015 , 12:53 PM
wrap your code in [ code ] tags. Or go to advanced and you see a little hash symbol.

There is so much wrong with your code, but:

The function takes 2 arguments. These arguments act as the variables you are initializing inside the function.

When you call your function, you call it like so:

Code:
ans = evaluate_poly([list of nums here] , exponent here)

print(ans)
I'm assuming you are using Python2?
Programming homework and newbie help thread Quote
08-03-2015 , 11:17 PM
Hi friends!
quick question,
i'm required to convert a simple fortran program to C most of it is straight forward, but i've never done fortran before and I saw this line:

PARAMETER( SCALE=2.**8 );

and I was wondering what it does, and whats the C equivalent of this.
Programming homework and newbie help thread Quote

      
m