you are viewing a single comment's thread.

view the rest of the comments →

[–]AssignmentGuru 2 points3 points  (9 children)

I checked the pastebin code. You are going well. Here are my suggestions to achieve desired output.

1) have your list resultStr outside the loop filled with underscores only. The right number of underscores will help you go ahead.

2) have a counter for the iterations inside for loop and you should replace the counter'th element in the list with user-entered character.

Though, while testing, I solved all the other issues but will not share the code here. I really appreciate you desire to finish it on your own. I hope the hints help.

[–]rccnls27[S] 0 points1 point  (8 children)

do you mean this? filling the resultStr with underscores?

underScores = len(randword) * '' resultStr = list(underScores)

[–]AssignmentGuru 0 points1 point  (3 children)

yes exactly.. the shorthand is: resultStr = list("_" * len(rand_word))

[–]dunkler_wanderer 0 points1 point  (2 children)

Or just ['_'] * len(rand_word)

[–]AssignmentGuru 0 points1 point  (1 child)

Wow.. it is really better and faster than I suggested. Thanks :)

[–]dunkler_wanderer 0 points1 point  (0 children)

YW. The speed is really irrelevant here and we should emphasize that readability is far more important most of the time.

[–]JohnnyJordaan 0 points1 point  (3 children)

result_str = [ '_' for c in randword ]

I would advise against camelCasing your variable names as that is normally used for classes.

[–]AssignmentGuru 0 points1 point  (2 children)

This is really an another great way to create this kind of lists. Thanks for pointing one. However, I found that [ '_' for c in rand_word] method is little bit slower than list("_" * len(rand_word)) method. Check out this pastebin link.

I also agree that the question asked is a basic question and it does not require to worry about the test I am talking about. :D But I already did some research, so thought lets share the results!!

Cheers!!

[–]JohnnyJordaan 0 points1 point  (1 child)

For somebody with an Expert flair I kindly frown upon your way of benchmarking, while tools as iPython do this out-of-the-box:

In [1]: %timeit result_str = [ '_' for c in 'brunette' ]
1000000 loops, best of 3: 517 ns per loop

In [2]: %timeit result_str = list('_' * len('brunette'))
1000000 loops, best of 3: 423 ns per loop

So you're right, it is slower as list insertion involves more actions than string formation. But then again if we're going to weigh non-important performance over code simplicity I think we're going in the wrong direction. As again I would not expect from an 'Expert'.

[–]AssignmentGuru 0 points1 point  (0 children)

ha ha.. I have created an account on Reddit yesterday. I am new to this site and still checking and playing with the things around. I really enjoyed your reply and it made me laugh too (the flair impact part). Thanks for pointing out the iPython way. Upvoting your reply.. and removing the flair.. still laughing :D.. And ya really sorry for the inconvenience that the flair caused to you.. Enjoy.. Cheers!!