• 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

I'm stuck on a really annoying and probably simple problem. Since I am new to C++, I am unable to figure out how exactly to turn this copy constructor into a deep copy instead of a shallow one, specifcally because when I test it with pointers the copy will always be a shallow copy.

recDWmY.png


I have tried changing the statement in the loop to be

m_Data = new element(*original.m_Data);

and that doesn't seem to work.


sorry for the shameless question
 

Slavik81

Member
That's because element is a pointer. You don't want a new pointer, you want a new instance of the type the pointer points to.

Or maybe not. The instance being pointed to might not be of the same type as the pointer; it could be a subclass of the pointed to type. If that were the case, you'd have no way to correctly copy it based on the information you have available at compilation time.

Typically the way you would handle this is that you would leave the container code as it is and implement the copy constructor on the elements you insert into the container. std::shared_ptr is one example of such a wrapper that enforces some sort of policy upon copy.

Changing the container code as you are doing would also break it for use with non-pointer types.
 

iapetus

Scary Euro Man
In Java (well, in many languages), is it a good idea to check if two objects are null by saying (o1 == o2) assuming that o1 and o2 will never be the same object by design? It's something I haven't thought about until now, but it seems like a good way to save a very tiny bit of speed.

Things that seem like a good way to save a very tiny bit of speed are a bad idea with modern compilers and runtime environments. And although o1 and o2 can't currently be the same object by design, can you guarantee that future changes to the design won't change that? Because if it changes, suddenly things are going to start breaking magically for reasons that aren't apparent.

In Java (well, in many languages) it's a good idea to check if two objects are null by saing (o1 == null && o2 == null).

Knuth said:
Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.
 
Ah, thanks for those answers. In my instance, it was 100% guaranteed to be two different objects, but I can easily see how explicitly writing what you want with no shortcuts will always be fool proof and also readable. Having written code only for myself, I rarely ever focused on those ideals.

ipateus's quote is especially interesting because it truly is something I focus on too much.
 

Massa

Member
I don't agree. C might be a small language but it's not a simple language. The standard library is tiny (which means you have to write a lot of things yourself) and it lacks a ton of features that modern languages have. Also, the compiler does very little for you to protect you from doing horrible bad things (undefined behavior is everywhere) and you have to do error handling by yourself, explicitly. Not really relevant to a beginner, but C is also a security nightmare.

In contrast, Python, as an example, is "batteries included", so you can start doing cool stuff almost immediately because the standard library is very comprehensive, the syntax is even simpler than C and error handling is pretty easy with exceptions. The things C is good at (portability, low-level access to OS APIs, small footprint, runs on everything) aren't really that relevant to a newcomer to programming. You can always (and definitely should!) learn about pointers and contiguous memory and shared memory segments and what have you later.

That's the thing, I think learning things like why scanf requires a '&' to not crash is more important than being able to do "cool stuff". When I was a beginner I was writing simple programs and algorithms, things which mostly weren't affected by the problems you listed with C (and I agree with most of it, I don't use pure C in production for the most part).

That's not essential for a beginner but I'm glad I learned those things early, and I'm also glad I never got crazy about OO like some friends and coworkers. That can lead to ugliness when used improperly, and it's far too often used improperly. Designing a good OO program brings its own complications for a beginner, and creating an early habit of using it poorly is much more dangerous.

I guess the better question isn't which language you use, but what do you do to learn? Are you building a dynamic website or simple text based programs? For me it was solving *a lot* of small problems that took a simple input and did something simple on it. C was a pain to deal with sometimes, but I always learned something from it.
 
That's because element is a pointer. You don't want a new pointer, you want a new instance of the type the pointer points to.

Or maybe not. The instance being pointed to might not be of the same type as the pointer; it could be a subclass of the pointed to type. If that were the case, you'd have no way to correctly copy it based on the information you have available at compilation time.

Typically the way you would handle this is that you would leave the container code as it is and implement the copy constructor on the elements you insert into the container. std::shared_ptr is one example of such a wrapper that enforces some sort of policy upon copy.

Changing the container code as you are doing would also break it for use with non-pointer types.

Thanks a lot for the advice, greatly appreciated. Going into C++ with a few years Java experience and it's a whole different ballgame.
 

phoenixyz

Member
That's the thing, I think learning things like why scanf requires a '&' to not crash is more important than being able to do "cool stuff". When I was a beginner I was writing simple programs and algorithms, things which mostly weren't affected by the problems you listed with C (and I agree with most of it, I don't use pure C in production for the most part).

That's not essential for a beginner but I'm glad I learned those things early, and I'm also glad I never got crazy about OO like some friends and coworkers. That can lead to ugliness when used improperly, and it's far too often used improperly. Designing a good OO program brings its own complications for a beginner, and creating an early habit of using it poorly is much more dangerous.

I guess the better question isn't which language you use, but what do you do to learn? Are you building a dynamic website or simple text based programs? For me it was solving *a lot* of small problems that took a simple input and did something simple on it. C was a pain to deal with sometimes, but I always learned something from it.

Imho Python is ideal if you have to deal with many small problems. It's essentially just as good as a scripting language for small automation tasks as it is for big problems. Also you can introduce OO concepts into your scripts without rewriting all kinds of stuff as you would need in other languages which gives you a quicker insight on why to use OO at all (e.g. for encapsulation purposes). Also I don't agree that learning under-the-hood stuff (like the scanf example) should always come before being able to do "cool stuff". The earlier a beginner is able to realize the ideas he has in code the faster he will become comfortable with programming. After that it's much easier to grasp more complicated concepts in my opinion.
 

P44

Member
That's the thing, I think learning things like why scanf requires a '&' to not crash is more important than being able to do "cool stuff". When I was a beginner I was writing simple programs and algorithms, things which mostly weren't affected by the problems you listed with C (and I agree with most of it, I don't use pure C in production for the most part).

That's not essential for a beginner but I'm glad I learned those things early, and I'm also glad I never got crazy about OO like some friends and coworkers. That can lead to ugliness when used improperly, and it's far too often used improperly. Designing a good OO program brings its own complications for a beginner, and creating an early habit of using it poorly is much more dangerous.

I guess the better question isn't which language you use, but what do you do to learn? Are you building a dynamic website or simple text based programs? For me it was solving *a lot* of small problems that took a simple input and did something simple on it. C was a pain to deal with sometimes, but I always learned something from it.

Okay so I'm sort of doing FORTRAN and java at the same time (FORTRAN is strictly for a project at school) so, I'm wondering, what is a bad use of OO and a good use of OO and hell, what's a good resource to work all this out. After FORTAN I loved the idea of OO (I think it's a bit closer to how you would think through a problem if you get me) but what you've said has me a tad wary now.
 

oxrock

Gravity is a myth, the Earth SUCKS!
Lately I've been on a tangent of just coding whatever weird program comes into my mind. For exampIe, I had to change my yahoo account password the other day because apparently someone from china was trying to access my account, so instead of creating a password in a more traditional method, I whipped up an alphanumeric code generator. I even gave it a user ui with options to alter the code's properties such as length and case sensitivity. Now I'm most certainly not bragging, it's a simple thing to do, but adding things like that are totally redundant being that I can just run the code and change parameters as I wish manually.

With that silly project out of the way, I became curious as to just how long it would take for someone to brute force these wonderful passwords my generator was producing, so of course I had to create my own brute forcer complete with a timer that prints out how long the endeavor took to complete! My girlfriend saw what I was doing and is afraid I'm trying to be a hacker now, haha.

So I guess the question is, what silly things do you guys find yourselves coding for fun or on a whim?

P.S. I can crack a 5 digit alphanumeric code in 7.5 minutes, watch out! :p
 

tuffy

Member
So I guess the question is, what silly things do you guys find yourselves coding for fun or on a whim?
I threw together a little toy program for calculating Monster Hunter item drop chances in Haskell for fun. Like given 2, 3% chances of an item and a 4% chance of that item, it calculates the breakdown of all 4 possibilities like:
Code:
% ./chances 0.03x2 0.04
0 : 0.903264
1 : 0.093508
2 : 0.003192
3 : 0.000036
The whole thing came in at 55 lines (including comments) and worked out pretty well as a chance to try out more Haskell.
 

mltplkxr

Member
Ah, thanks for those answers. In my instance, it was 100% guaranteed to be two different objects, but I can easily see how explicitly writing what you want with no shortcuts will always be fool proof and also readable. Having written code only for myself, I rarely ever focused on those ideals.

ipateus's quote is especially interesting because it truly is something I focus on too much.
I like what iapetus quoted because it includes what I feel is the most important part: don't "waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts".

In this case, what you're micro-optizmizing might be called once in a blue moon. And so, in effect, the optimization would have no impact on the perceived or practical performance of your program. Applying the Pareto principle, 80% of your performance gains could be gained by optimizing 20% of your code. In other words, 80% of your use cases make use of 20% of your code. Those are the critical parts that you need to find or optimize, by profiling and testing.

Also, you're not writing assembler. Especially in Java, the compiler is going to optimize your code in ways you can't predict. In a future version of the JVM or if you change JVM, it might even be optimized differently.

Having said that, it's still important to be mindful of performance when you design and code, by choosing appropriate data structures, architectural decisions, etc.
 

Eoin

Member
So I guess the question is, what silly things do you guys find yourselves coding for fun or on a whim?
It's usually a game of some sort. Just made a 2d sidescroller, where you play as a member of my friends band and murder multiple Justin Biebers with their respective instruments. No idea how I ended up there, it started as just a generic level editor.
 
Lately I've been on a tangent of just coding whatever weird program comes into my mind. For exampIe, I had to change my yahoo account password the other day because apparently someone from china was trying to access my account, so instead of creating a password in a more traditional method, I whipped up an alphanumeric code generator. I even gave it a user ui with options to alter the code's properties such as length and case sensitivity. Now I'm most certainly not bragging, it's a simple thing to do, but adding things like that are totally redundant being that I can just run the code and change parameters as I wish manually.

With that silly project out of the way, I became curious as to just how long it would take for someone to brute force these wonderful passwords my generator was producing, so of course I had to create my own brute forcer complete with a timer that prints out how long the endeavor took to complete! My girlfriend saw what I was doing and is afraid I'm trying to be a hacker now, haha.

So I guess the question is, what silly things do you guys find yourselves coding for fun or on a whim?

P.S. I can crack a 5 digit alphanumeric code in 7.5 minutes, watch out! :p

A fun extension to your brute-force password cracker would be to try and make it multi-threaded, in theory you can get a really good speed-up with that. Depending on the language that can be a bit of a challenge, but I bet you'd learn a lot with that.

As for what I make.. I'm not very creative and I like trying out different programming languages, so I generally make the same project in multiple different languages. Like, I've made a L-System interpreter and renderer in C# (twice, actually), Scala, Clojure, Haskell and C++. I've made a lines of code counter (like cloc but less features, obviously) in Clojure, C++ and Java. I've also made two different TSP solvers (one using the Genetic Algorithm, written in Rust and another in C# with Simulated Annealing). I tend to abandon projects too quickly I think. I do "finish" some projects but they never get bigger than, say, 1000 lines.

I've got an interview for a software tester job tomorrow. Would be pretty happy to get that job, since a few friends of mine work at that company.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
This is kind of a catch-all coding thread so ask away.
 

oxrock

Gravity is a myth, the Earth SUCKS!
A fun extension to your brute-force password cracker would be to try and make it multi-threaded, in theory you can get a really good speed-up with that. Depending on the language that can be a bit of a challenge, but I bet you'd learn a lot with that.

As for what I make.. I'm not very creative and I like trying out different programming languages, so I generally make the same project in multiple different languages. Like, I've made a L-System interpreter and renderer in C# (twice, actually), Scala, Clojure, Haskell and C++. I've made a lines of code counter (like cloc but less features, obviously) in Clojure, C++ and Java. I've also made two different TSP solvers (one using the Genetic Algorithm, written in Rust and another in C# with Simulated Annealing). I tend to abandon projects too quickly I think. I do "finish" some projects but they never get bigger than, say, 1000 lines.

I've got an interview for a software tester job tomorrow. Would be pretty happy to get that job, since a few friends of mine work at that company.

I haven't done any multi-threading but with some optimizations I was able to cut the computation time almost in half, was quite amazed at that.
 

oxrock

Gravity is a myth, the Earth SUCKS!
Thanks to close to the edge I spent some time looking into multi threading yesterday and indeed was able to modify my brute force code cracking program to utilize multi-threading. The problem I have is that the code runs ridiculously slower now and I'm stumped as to why. I'm going to post the code and time it took to crack the simple code for both here and I'm hoping some of our resident programmers can offer some insight as to how I messed things up.

Original this completed in 4.92999982834 seconds

Code:
import itertools
import sys
import time

chars = ['a','A','b','B','c','C','d','D','e','E','f','F','g','G','h','H',
         'i','I','j','J','k','K','l','L','m','M','n','N','o','O','p','P',
         'q','Q','r','R','s','S','t','T','u','U','v','V','w','W','x','X',
         'y','Y','z','Z','1','2','3','4','5','6','7','8','9','0']
password = ""
print "Starting decryption, this could take a while"
t1 = time.time()

for length in range(0,20):
    for entry in itertools.product(chars,repeat = length):
        password = entry
        if password == ('w','4','c','9'):  #The code we're searching for
        #if password == ('a','b','c'):
            t2 = time.time()
            print 'That took ' + str(t2-t1) +' seconds'
            print ''.join(str(e) for e in password)
            raw_input("Press Enter to exit")
            sys.exit()
            
        else:
            password = []


Multi-Threading version This completed in 959.157999992 seconds

Code:
import itertools
import sys
import time
from Queue import Queue
from threading import Thread

password = ('w','4','c','9') # the code we're searching for


def do_stuff(q):     # this is the function that 10 threads will be running to clear the queue
  while True:
    i = q.get()
    if i == password:
        t2 = time.time()
        print "The password has been found. It took " + str(t2-t1) + " seconds to crack"
        raw_input("Press enter to exit")
        sys.exit(0)

    q.task_done()

chars = ['a','A','b','B','c','C','d','D','e','E','f','F','g','G','h','H',
         'i','I','j','J','k','K','l','L','m','M','n','N','o','O','p','P',
         'q','Q','r','R','s','S','t','T','u','U','v','V','w','W','x','X',
         'y','Y','z','Z','1','2','3','4','5','6','7','8','9','0']

print "Starting decryption, this could take a while"
q = Queue(maxsize=0)
num_threads = 10
t1 = time.time()
for i in range(num_threads):
  worker = Thread(target=do_stuff, args=(q,))
  worker.setDaemon(True)
  worker.start()


for length in range(0,20):
    for entry in itertools.product(chars,repeat = length):  #this is the queue producer
        q.put(entry)

q.join()

This is tested on python 2.7 and does work. For 3.x functionality you need to change the "from Queue import Queue" statement to simply "import Queue" and then it should work. Going from 5 second completion to 959 seconds with the "improved" code is quite frustrating. Insight as to why this may be happening and some advice would be great.
 

phoenixyz

Member
First of all, CPython has a global interpreter lock which means that threads are only meant for logical concurrency, they don't actually run in parallel on hardware. If you want that you need to use the multiprocessing module.

Also most of the logic is assembling the passwords which is done by your main thread and the other threads essentially just do a single compare of data which they consume from a threadsafe queue although it would be readily available to generate on the fly.


Profiling it with range(4) and num_threads = 1 on Python 3.4

python -m cProfile -s cumtime test.py

2508628 function calls (2508562 primitive calls) in 4.428 seconds

Ordered by: cumulative time

ncalls tottime percall cumtime percall filename:lineno(function)
8/1 0.000 0.000 4.428 4.428 {built-in method exec}
1 0.134 0.134 4.428 4.428 test.py:1(<module>)
242235 0.536 0.000 4.283 0.000 queue.py:118(put)
242236 0.091 0.000 3.100 0.000 threading.py:234(__enter__)
242236 3.009 0.000 3.009 0.000 {method '__enter__' of '_thread.lock' objects}
[...]

You see that most time is spend waiting for the put call to unlock (due to the thread safety).
 

traveler

Not Wario
I'm advancing to the final stage of an interview with a consulting company next week. The format is something I've never done before- a technical case interview. I've done some prepping on case interviews before, but the case questions I saw were always very high level, very business oriented. While there is a chance this interview could be the same way with a programming flavor draped over the top, I suspect it will be slightly more involved than that. Additionally, the interview is prep and present, rather than the Q&A format I think of when I think of case interviews. Not entirely sure how to structure and come up with a full whiteboard worth of material going in, but I'm practicing now to try and get the basics down so I'm not completely broadsided when I go into the interview. Anyways, I just wanted to check and see if anyone in here had done anything like this/knew of processes like this and had any pointers. Any advice is welcome at this stage.
 

Water

Member
The format is something I've never done before- a technical case interview.
...
Additionally, the interview is prep and present, rather than the Q&A format I think of when I think of case interviews. Not entirely sure how to structure and come up with a full whiteboard worth of material going in, but I'm practicing now to try and get the basics down so I'm not completely broadsided when I go into the interview.
So the prep is not homework, you'll work the problem and prepare a presentation at the interview?

No experience with case interviews, but in a less interactive interview format like that, I'd be especially concerned about answering the right problem.
What's the expected result - thorough analysis of the problem, outlining possible solutions, suggesting a solution, developing a solution...?
What abstraction levels and viewpoints are you expected to consider?
What is the audience you should present your findings to?
 

traveler

Not Wario
So the prep is not homework, you'll work the problem and prepare a presentation at the interview?

No experience with case interviews, but in a less interactive interview format like that, I'd be especially concerned about answering the right problem.
What's the expected result - thorough analysis of the problem, outlining possible solutions, suggesting a solution, developing a solution...?
What abstraction levels and viewpoints are you expected to consider?
What is the audience you should present your findings to?

The prep is not homework. I'll be given a stack of papers simulating a client's presentation and a couple hours to prepare a whiteboard presentation at the interview itself.

Expected result could be any and/or all of the things you list. They weren't explicitly clear on this and basically implied that they want the process to be a little unclear until I actually arrive in order to better analyze how I handle an unknown situation on the fly.

Same for your third question.

Audience is a panel of interviewers who will basically roleplay the client to whom I am to present the solution.

The position itself is an early level, though not quite entry, technical consulting role. My background is programming and a couple years of consulting, but my role in consulting wasn't particularly different from your standard programmer- I didn't architect solutions, take part in high level meetings with clients to determine a path forward for them, or any of the more management oriented activities you might expect, but the company interviewing me understands this, I think. They know I'm not super broad in terms of my technological expertise, and they even hinted that they run other candidates with some sort of quantitative background- be they engineers, statisticians, etc. non programmers basically- through this process as well. In the end, they want consultants with engineering background that also have functional management potential, so I think the case is intended to be a blend of the two, maybe even leaning towards the latter.

I guess the thing that really gets me is that I tend to think of case interviews as being back and forth dialogue between the candidate and the interviewer, not scenarios where the candidate lays out their thought process/solution/whatever it is I'm expected to show before even interacting with the interviewers, which seems to be the same point you're honing in on. Given that there's a decent amount of time allotted for the presentation, I'm expecting that some of the traditional q and a will show up there. It's just unusual to have to lay out an entire whiteboard's worth of thoughts/solutions preceding that.
 

Water

Member
The prep is not homework. I'll be given a stack of papers simulating a client's presentation and a couple hours to prepare a whiteboard presentation at the interview itself.
By "stack of papers simulating a client's presentation" do you mean simulating an assignment or request from a client?

It simplifies things a lot if at least the basic scenario is clear (interviewers are roleplaying a client -> you should make a presentation exactly like you would for a client rather than for a bunch of interviewers) and you don't have to guess that part.
 

traveler

Not Wario
By "stack of papers simulating a client's presentation" do you mean simulating an assignment or request from a client?

It simplifies things a lot if at least the basic scenario is clear (interviewers are roleplaying a client -> you should make a presentation exactly like you would for a client rather than for a bunch of interviewers) and you don't have to guess that part.

Request. They also implied that a good deal of the information might be extraneous, in order to better simulate the sort of situation that regularly occurs in consulting where clients might have a general idea of what they want, but might not be so sure on the specifics or might go overboard in detailing the request to the consultancy. Being able to read the papers and pinpoint the real meat amidst all the fat is part of the interviewing process.

I think your simplification is correct; it's just that there is typically some interaction and discussion with a client before presenting a formal situation, so this presentation will likely stride the line between that initial discussion and the first proposed solution a consultancy might present.
 
I am having a huge problem with AngularJS using the command: npm install

The installation froze so I had to close the Terminal.

Now when I tried to use "npm install" again, I get this error message:

npm ERR! install Couldn't read dependencies
npm ERR! package.json ENOENT, open '/Users/tripham/angularjs_projects/package.json'
npm ERR! package.json This is most likely not a problem with npm itself.
npm ERR! package.json npm can't find a package.json file in your current directory.

npm ERR! System Darwin 13.2.0
npm ERR! command "node" "/usr/local/bin/npm" "install"
npm ERR! cwd /Users/tripham/angularjs_projects
npm ERR! node -v v0.10.29
npm ERR! npm -v 1.4.14
npm ERR! path /Users/tripham/angularjs_projects/package.json
npm ERR! code ENOPACKAGEJSON
npm ERR! errno 34
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/tripham/angularjs_projects/npm-debug.log
npm ERR! not ok code 0

Help!

Sorry, but this is a repost from the Web Developer thread. I couldn't find an answer from Google yet..


Oh yeah, I needed a project with package.json first. :p
 
Hey guys! I'm looking into a project that would handle sensitive data (network traffic) and I would like to understand some encryption fundamentals. Does anybody here know of any references, books, or information I could get into?
 
Whatever you do, don't roll your own solution. Security/Encryption best practices are best practices for a reason.

I agree, at least for production systems that people actually use, you should use implementations by people who know what they're doing. Implementing your own crypto for fun is not harmful and can give you a better understanding of the algorithms and math involved.
 
I was unaware they had classes you could do at your own pace. Thank you!

I agree, at least for production systems that people actually use, you should use implementations by people who know what they're doing. Implementing your own crypto for fun is not harmful and can give you a better understanding of the algorithms and math involved.

Absolutely. I just know nothing about its implementation or best practices. Thanks for the advice though.
 

oxrock

Gravity is a myth, the Earth SUCKS!
This thread is rather stagnant at the moment so I don't feel so bad picking your very talented minds. I've rewritten my previous brute force cracking program to "utilize" python's multiprocessing module. However currently it's only actually setup to run as a single process. I've been racking my brain over how to get multiple iterators to work in tandom to actually utilize this ability and after many hours, the only semi plausible solution I can think of is to have one iterator start from the beginning as per usual while having the other work backwards starting from the highest possible output allowing the iterators to meet in the middle. Sadly I have no freaking idea how to make python's itertools.product work backwards! I guess I could write something myself to pull something like that off, but I'm thinking there's gotta be something like that available. Anyhow, hoping someone has some ideas.

I also want to reiterate to anyone who's reading this. This program was not written with the intent of actually putting it towards any nefarious purpose. I'm just using it as a tool for learning.

Here's what i'm working with currently:

Code:
from multiprocessing import Process
import itertools
import time


t1 = time.time()
chars = ['a','A','b','B','c','C','d','D','e','E','f','F','g','G','h','H',
         'i','I','j','J','k','K','l','L','m','M','n','N','o','O','p','P',
         'q','Q','r','R','s','S','t','T','u','U','v','V','w','W','x','X',
         'y','Y','z','Z','1','2','3','4','5','6','7','8','9','0']


def char_iterator(password):
    for length in range(0,20):
        for entry in itertools.product(chars,repeat = length):
            if entry == (password):
                t2 = time.time()
                print entry
                print 'I found that code in ' + str(t2-t1) + ' seconds'
                return

            elif entry == (password,):# this elif catches single letter passwords
                t2 = time.time()      # which are presented in a slightly different
                print entry           # format for some reason
                print 'I found that code in ' + str(t2-t1) + ' seconds'
                return
                

if __name__ == '__main__':
    p = Process(target=char_iterator, args=(('w','4','c','9'),))
    p.start()
    p.join()

Important to note, the multiprocessing module won't function in idle, it has to be run from a command shell. Also, this code was written in 2.7.8, honestly not sure what might need altering to be 3.x compatible.
 

Calvero

Banned
Hi programming GAF!
Just a few days ago I finally decided to attempt to learn about this. I was (am) so scared of it! Thinking that everything will go over my head, that I'll be useless, that my efforts will be in vain.
I have begun learning/studyingPython on codeacademy and am loving it! I'm not so far into it (only 20%) but I'm enjoying it! It's a whole new world for me! After i finish this, I'm signed up to take a course on Coursera on Python, and once done with that I'll see if I want to really chase after this.
 

Slavik81

Member
I have a similar problem. I have forgotten part of my password for a truecrypt store of mine, and I want to build a brute-force checker to crack it. The obvious way is, of course, to add my functionality on top of truecrypt itself. Unfortunately, it's actually a little tricky to build trucrypt from source (lots of dependencies to track down; had trouble finding all the right packages for Fedora). I could do it eventually, but it's annoying, boring work.

With the implosion of the truecrypt project, there may now be a few projects that can read truecrypt files, some of which might be easier to hack my cracker on top of. Any suggestions?

Now, oxrock, a couple things:
1. If you're on unix, it's trivial to use an external timing mechanism, which avoids cluttering your code with performance monitoring stuff. Just use "time python yourcode.py" If you want to keep it in python, at least put both the start and stop in main. This is minor stuff, admittedly, but keeping it out of the way lets you focus on the important things.

2. You've unfortunately not yet done the slow part of the check: checking the password is correct. Obviously, you know that if you were checking against a plaintext password like in your code there you wouldn't have to guess; you'd have it. In reality, your check would probably involve hashing each attempt and comparing it against a known hash, or attempting to use it as a key to decode some header and checking you got a valid squence. This might use an algorithm that's purposefully slow and difficult to compute. No human will notice a difference between 5ms and 5ns in validating their password, but using a hash that takes that long makes bruteforcing 1,000,000x times slower.

3. On the multi-processing part, I'm afraid you may have hit the limits of Itertools.Product. Ideally, you'd take the number of possible solutions, divide by the number of threads, and start each thread at an index that was a multiple of that value. e.g. for 13 threads, you'd have a thread starting at a, c, e, g, i, k, m, o, q, s, u, w, and y. (Or process them in even smaller batches in a similar way.) You may have to implement your own product function that allows starting from a later password guess.

That said, I'm no python expert. There might be something more clever you could do.
 
I have a similar problem. I have forgotten part of my password for a truecrypt store of mine, and I want to build a brute-force checker to crack it. The obvious way is, of course, to add my functionality on top of truecrypt itself. Unfortunately, it's actually a little tricky to build trucrypt from source (lots of dependencies to track down; had trouble finding all the right packages for Fedora). I could do it eventually, but it's annoying, boring work.

With the implosion of the truecrypt project, there may now be a few projects that can read truecrypt files, some of which might be easier to hack my cracker on top of. Any suggestions?

For TrueCrypt I can recommend using oclHashcat (openCL, GPGPU) which is quite fast and supports the decryption of TrueCrypt files.
 

phoenixyz

Member
I've been racking my brain over how to get multiple iterators to work in tandom to actually utilize this ability and after many hours, the only semi plausible solution I can think of is to have one iterator start from the beginning as per usual while having the other work backwards starting from the highest possible output allowing the iterators to meet in the middle. Sadly I have no freaking idea how to make python's itertools.product work backwards! I guess I could write something myself to pull something like that off, but I'm thinking there's gotta be something like that available.
3. On the multi-processing part, I'm afraid you may have hit the limits of Itertools.Product. Ideally, you'd take the number of possible solutions, divide by the number of threads, and start each thread at an index that was a multiple of that value. e.g. for 13 threads, you'd have a thread starting at a, c, e, g, i, k, m, o, q, s, u, w, and y. (Or process them in even smaller batches in a similar way.) You may have to implement your own product function that allows starting from a later password guess.

I would also recommend Slavik81's solution. The easiest way to implement this is to write your own generator which prefixes the results from itertools.product with the starting character(s) of your choice (like this <- only click if you want a solution).

I also want to reiterate to anyone who's reading this. This program was not written with the intent of actually putting it towards any nefarious purpose. I'm just using it as a tool for learning.
As there are highly efficient password crackers and exploit frameworks downloadable for everyone I don't think anyone here will suspect you to be an evil cracker :)
 

Tamanon

Banned
Hi programming GAF!
Just a few days ago I finally decided to attempt to learn about this. I was (am) so scared of it! Thinking that everything will go over my head, that I'll be useless, that my efforts will be in vain.
I have begun learning/studyingPython on codeacademy and am loving it! I'm not so far into it (only 20%) but I'm enjoying it! It's a whole new world for me! After i finish this, I'm signed up to take a course on Coursera on Python, and once done with that I'll see if I want to really chase after this.

Great that you're pumped for it! I'd suggest thinking of small projects that you can do while learning. Just to cement some of the concepts. What's helped me is just thinking of something small that I use a calculator or another program for and sketching it out, then trying a quick implementation. This has helped me to remember some off the idiosyncracies of Python or programming.

For instance, I wrote a quick IP address converter from base-10 to binary. My first pass was a bunch of if statements, but then I was able to reconfigure it to two nested for statements, which helped me understand how ranges work in Python.
 

Blackhead

Redarse
What's your setup? Does anyone here do programming on mobile devices? I'm looking for a IDE on Android or iOS (jailbroken?) so I can do some coding on my phone instead of wasting time on GAF...
 

BreakyBoy

o_O @_@ O_o
On the software side, I don't use full-blown IDEs much anymore, unless I'm working in either Java/Android, or C#. I haven't done much C or C++ in a while either. I use Sublime Text with a smattering of Vim via iTerm. For both, I use a variety of plugins to get some of the IDE-like features I might otherwise miss on occasion.

For reference, lately I mostly work in Ruby/Scala/Python/Go split 60/20/10/5, with the remaining 5% in random bits of other languages.

As for hardware, my usual is at work with a MacBook Pro hooked up to a Thunderbolt Display, with my Happy Hacker Keyboard at the ready. I'm still in the process of moving/renovating at home, so while that's in flux, I've been relegated to just my MacBook at home. On the road, I do sometimes just use an iPad for work. I have an Ubuntu box set up via Linode that I keep around for personal projects/services, and I often use that as a defacto remote workstation by SSHing into it.

Basically, I use an SSH client (my favorite one on iOS is Prompt) to connect to my Linode, which is setup to sync my project files from Dropbox, and has all my tools + screen + vim installed. That's really all I need to do my work 99% of the time. I'm also on call occasionally, and there has been a couple of instances where I was called, and I didn't have access to anything but my phone. It wasn't a problem though, as JuiceSSH lets me do the same thing I do from an iPad. It's just a wee bit cramped.

Honestly, I like the setup enough that after a co-worker pointed out this blog post to me, I've been thinking about just doing it full time. The only real issue is that as much as I do like vim, I still like Sublime Text just a little bit more.
 

maeh2k

Member
Does anyone want to practice/learn Test Driven Development?

I'm trying this TDD exercise "String Calculator" from Roy Osherove: http://osherove.com/tdd-kata-1/

I've done it four times by now (at least up to supporting different delimiters) and I'm curious about how others would solve it. If anyone wants to post their solution, I'd appreciate it.
I'm using Java, but I'd like to see a JavaScript version.

(I know there are quite a few solutions linked on the site, but I'm not sure I want to watch these long programming videos)
 
What's your setup? Does anyone here do programming on mobile devices? I'm looking for a IDE on Android or iOS (jailbroken?) so I can do some coding on my phone instead of wasting time on GAF...

Vim in terminals on Linux. Everytime I try using IDE's (Visual Studio, QtCreator, MonoDevelop) I just go back because I always feel like I'm being hindered by the tools put in place to help me. As BreakyBoy said, I'll just use a few plugins for what I want.

I've actually coded a project on my phone (C4Droid) and it was doable, but definitely not fun. However, it did make me feel like all the waiting time for Nintendo E3 stuff was justified since I did actual work.
 
What's your setup? Does anyone here do programming on mobile devices? I'm looking for a IDE on Android or iOS (jailbroken?) so I can do some coding on my phone instead of wasting time on GAF...

I have a Windows PC and a Thinkpad. When I'm not using an IDE (Visual Studio for .Net languages, IntelliJ for everything JVM), I generally use a Linux VM for programming because everything about Windows command line interfaces sucks. (well, Powershell is a little bit better, but it's still a lot worse than zsh or bash) As an editor, I use Sublime Text. I've tried out Emacs in the past and liked it, but it's kind of a hassle to set up and configure properly. ST3 comes with sensible defaults for everything, you can just install it and you're set.
 

usea

Member
Does anyone want to practice/learn Test Driven Development?

I'm trying this TDD exercise "String Calculator" from Roy Osherove: http://osherove.com/tdd-kata-1/

I've done it four times by now (at least up to supporting different delimiters) and I'm curious about how others would solve it. If anyone wants to post their solution, I'd appreciate it.
I'm using Java, but I'd like to see a JavaScript version.

(I know there are quite a few solutions linked on the site, but I'm not sure I want to watch these long programming videos)
As somebody who doesn't do TDD, I can't learn or practice it from this page. It's a small program description, which I can solve entirely in my brain without writing any code. I can't imagine how I would write this test-first, because just reading the problem description I already imagine the solution. Trying to make myself ignore the right answer is a frustrating exercise and feels pointless.

Even if the problem were more complicated and couldn't be solved as easily, I experience an extreme amount of frustration trying to stop myself from solving it just to write tests before code.

(Also the idea of katas seems kind of crazy to me. I guess they're just not for me)
 

Mexen

Member
Noob C++ programmer
Intermediate Java programmer

I don't know if I should go all out on Java or learn some C slash perfect my C++ some more. My vacation is just a month long and then a new semester begins.
 

mltplkxr

Member
Noob C++ programmer
Intermediate Java programmer

I don't know if I should go all out on Java or learn some C slash perfect my C++ some more. My vacation is just a month long and then a new semester begins.

Depends on what you want to do in the future. I'd say Java has broader applications : it touches big business as well as mobile with android and networked applications like servers. C and C++ is better if you want to work in video games, industrial systems, drivers, etc.
 
Noob C++ programmer
Intermediate Java programmer

I don't know if I should go all out on Java or learn some C slash perfect my C++ some more. My vacation is just a month long and then a new semester begins.
IMO, in school: breadth, not depth.

As long as you have the core concepts, you can (and will) become a subject matter expert on the job.

So: C++ and whatever else looks interesting
 

Calvero

Banned
So I'm still continuing with learning Python on codeacademy and my progress is slow but I'll still keep going. At about 34% through with it. I'm really enjoying it, I try to write down what I've gone through whenever I have free time to see if I can recall functions and datatypes with accuracy.
Been pretty busy with my summer homework for my last year of high school. (yay!)
 

BreakyBoy

o_O @_@ O_o
As somebody who doesn't do TDD, I can't learn or practice it from this page. It's a small program description, which I can solve entirely in my brain without writing any code. I can't imagine how I would write this test-first, because just reading the problem description I already imagine the solution. Trying to make myself ignore the right answer is a frustrating exercise and feels pointless.

Even if the problem were more complicated and couldn't be solved as easily, I experience an extreme amount of frustration trying to stop myself from solving it just to write tests before code.

(Also the idea of katas seems kind of crazy to me. I guess they're just not for me)

Not to single you out or anything, but in my (short) experience, this is the common issue for most when attempting to switch to TDD. And the truth is, there isn't really much special to TDD aside from the fact that it is a way to change the ingrained habits of how most developers do their work.

For most, you're presented with a problem, and you think of a broad solution for it. To get to that solution, you think of how to break it up into it's component parts. Traditionally, you dive straight in to coding up each part to head towards that grand solution. Then, maybe you write some tests after.

The problem is that those tests are often never written. They're only written when there already is a problem, and then maybe a test is written specifically to cover that edge case. Then code to solve the problem, until another problem comes up, and another test is written, etc etc.

With TDD, there's nothing special about the process aside from the fact that the code/test process is inverted. You don't write a single bit of that solution until you have a test that will check that the (part of the) problem is solved. So, you think of your solution, break it up into bits, and then write a test for the first bit that you want to write. Once you have your (failing) test, then you write your code that solves that bit of the problem. Then you think of the next bit, write the next failing test for that bit, then write the code to solve the next bit, etc etc.

Fundamentally, a good test suite is a good test suite regardless of if it was written before or after the code. Ultimately, you can get to the same place. The difference is that TDD enforces the idea that everything written must be thoroughly tested.

For small projects, I agree that it's overkill. I just finished writing a scraper to set up an automated aging report for our team's wiki. It took me a few hours, and about 200 lines of code. It's longer than it needed to be, but I decided to do it the OOP way as I figured we might want to leverage the classes I wrote in the future to automate more data movement between our stores and the wiki. I didn't write any tests for it though. That was just a bit more work than I thought was necessary for a 1-2 day project.

If we did start expanding on that work though? I'd want us to write tests for what I wrote, and then use TDD moving forward, as the code base will expand, and we can make sure that if we do any refactoring (which I can imagine we might, as I did take some shortcuts to optimize on API calls), we can make sure that existing functionality is retained, and that new functionality is ensured moving forward.

As with everything else, it's a tool. Use it when appropriate. And as for actually doing TDD, you get used to it. You have to break old habits and develop new ones.
 
In my experience, TDD is a guide:

* "write the test first" is the guideline.
* "write the test" is the rule.

You can get into serious trouble (as it, some seriously screwed up code design) when you're writing tests first for interfaces that are incomplete. I'd say the majority of tests I write are written after the code is.

As long as you stick with "when I check code in, it is well tested", you'll be ok.
 

BreakyBoy

o_O @_@ O_o
In my experience, TDD is a guide:

* "write the test first" is the guideline.
* "write the test" is the rule.

You can get into serious trouble (as it, some seriously screwed up code design) when you're writing tests first for interfaces that are incomplete. I'd say the majority of tests I write are written after the code is.

As long as you stick with "when I check code in, it is well tested", you'll be ok.

In practice, I would agree with that. When working on a (larger, 2+ person) project, I try not to commit until I have tests written. Often, if I'm in flow, and I don't want to stop coding a solution, I won't. But as you said, I don't commit to source control until I have tests to go along with what I wrote.
 
Top Bottom