Code Golf! Password Generator

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 4482 Views - Last Post: 30 June 2015 - 08:35 AM

#1 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4799
  • View blog
  • Posts: 8,534
  • Joined: 08-June 10

Code Golf! Password Generator

Post icon  Posted 09 June 2015 - 11:18 AM

Code Golf is a challenge in where you are given a task, and the goal is to solve it in as few total characters as possible.

Today's challenge:

Quote

Write a Password Generator. Requirements:

  • The resulting string must be exactly 16 characters.
  • The result must include at least one character from all "character classes"
  • The character classes are as follows:
    • !#$%&()*+,-./:;<=>? @[]^_{|}~ (note the ' ' (space) character between the ? and the @)
    • abcdefghijklmnopqrstuvwxyz
    • ABCDEFGHIJKLMNOPQRSTUVWXYZ
    • 0123456789

  • The result must not include any characters not in the character classes.
  • The generator must not repeatedly return the same password (this rule is to prevent you from just saying return "aaaabbbbccccdD0)";, don't worry about checking for this in your code.


When you submit your code, I will run it through my unit test project that has these rules defined as Asserts. If your code does not pass validation, I will reply with which rule you did not comply with (note I will only report one broken rule, if you fix that but still don't comply, I will respond with the new rule). In fact, I will be running your code through my unit test 10 times. Each run must pass. Also, I'm calling Distinct() on the ten results. There must not be just one result (i.e. don't just return a constant password, this must be a generator).

If, however, your code does pass unit test, it will be passed through the following:

private static void Compare()
{
    string templatePath = @"path\to\template.cs",
        submissionPath = @"path\to\submission.cs";
    var template = templatePath.LoadAndClean();
    var submission = submissionPath.LoadAndClean();
    Console.WriteLine("Stripped Template Size: {0} characters", template.Length);
    Console.WriteLine("Stripped Submission Size: {0} characters", submission.Length);
    Console.WriteLine("Score (lower is better): {0}", submission.Length - template.Length);
}



Extension methods used:

Spoiler


Your template is as follows:

using System;

namespace CodeGolf
{
    public class Generator
    {
        private static readonly Random r = new Random();

        public string Generate()
        {
            
        }
    }
}


As shown in the Compare method, I will strip all whitespace from your submission, then compare it against the template (also with whitespace stripped). Your score is the difference in characters. Everything counts; using statements, extra classes and functions; anything not in the template is counted against your score.

You may not modify the signature of the Generate method, the class name of the Generator class, or the namespace.

You may add using calls and references to built-in .NET Assemblies. You may not add code to other files in the solution without submitting them separately and including them in your result's score.

Submit your answers! I will post my answer after a few people have had a chance to answer.
Have fun!

Side note:
Spoiler


Second side note:

Feel free to submit your answers in other languages, however, they cannot be "scored" the same as the C# submissions. If you like this challenge, consider making it a challenge in the specific language forum that you like!

Last note:

When you submit your code, please use the following tags:

Posted Image

This will keep your code from giving other people ideas if they'd like to try first themselves.

Is This A Good Question/Topic? 4
  • +

Replies To: Code Golf! Password Generator

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4799
  • View blog
  • Posts: 8,534
  • Joined: 08-June 10

Re: Code Golf! Password Generator

Posted 09 June 2015 - 11:26 AM

My unit test project (hidden to keep you from getting ideas from it :P/> )

Spoiler


My first solution:

Spoiler


Score:
230
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 10590
  • View blog
  • Posts: 41,044
  • Joined: 12-June 08

Re: Code Golf! Password Generator

Posted 09 June 2015 - 11:49 AM

Are comments stripped when evaluating, or should I remove those myself?
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4799
  • View blog
  • Posts: 8,534
  • Joined: 08-June 10

Re: Code Golf! Password Generator

Posted 09 June 2015 - 12:03 PM

My regex strips nothing but whitespace, so please remove anything other than that you don't want considered as part of your score. If you'd like to submit one with comments included (as explanations) please add it in a second spoiler on your answer, since that would probably be greatly useful to others looking to get some ideas!
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 10590
  • View blog
  • Posts: 41,044
  • Joined: 12-June 08

Re: Code Golf! Password Generator

Posted 09 June 2015 - 12:10 PM

Fair enough.
Spoiler

Was This Post Helpful? 1
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4799
  • View blog
  • Posts: 8,534
  • Joined: 08-June 10

Re: Code Golf! Password Generator

Posted 09 June 2015 - 12:18 PM

Congratulations, your submission passed all the unit tests (though it did make me realize my template was missing the static keyword on the Random object, I've fixed that in the template and your submission). I also had to re-include the using System; to make it compile, so I've also added that to your submission.

Your score is:

404

edit: to get an idea of what my "LoadAndClean" function outputs (for scoring), here's your submission whitespace-stripped (copied directly from my debugger):

Spoiler


and here's the template stripped:

Spoiler

Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 10590
  • View blog
  • Posts: 41,044
  • Joined: 12-June 08

Re: Code Golf! Password Generator

Posted 09 June 2015 - 12:20 PM

Fun fun..

Can I get about 50 points shaved off that for admitting if I saw my code in production I would gleefully throw dirt clods at the creator? :D
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4799
  • View blog
  • Posts: 8,534
  • Joined: 08-June 10

Re: Code Golf! Password Generator

Posted 09 June 2015 - 12:24 PM

Haha. I guess I should clarify to newbies that this isn't an exercise in writing good code. This isn't something you should be aiming to do when you submit your code to your boss. This is more of a brain exercise: what's the smallest algorithm I can come up with to express this idea.

It's also easily and qualitatively score-able.
Was This Post Helpful? 0
  • +
  • -

#9 rgfirefly24  Icon User is online

  • D.I.C Lover
  • member icon


Reputation: 378
  • View blog
  • Posts: 1,886
  • Joined: 07-April 08

Re: Code Golf! Password Generator

Posted 09 June 2015 - 07:29 PM

I decided to go with a linq approach. If I come up with a better solution, am I allowed multiple submissions?

Spoiler

This post has been edited by rgfirefly24: 09 June 2015 - 07:32 PM

Was This Post Helpful? 1
  • +
  • -

#10 RoyTheReaper  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 09-June 15

Re: Code Golf! Password Generator

Posted 09 June 2015 - 08:19 PM

Hope this passes...

Spoiler

Was This Post Helpful? 0
  • +
  • -

#11 RoyTheReaper  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 09-June 15

Re: Code Golf! Password Generator

Posted 09 June 2015 - 08:40 PM

Crap...can I edit it? The .Substring was needed for a previous iteration of my generator.
This is my edited (if allowed) version.

Spoiler

Was This Post Helpful? 1
  • +
  • -

#12 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4799
  • View blog
  • Posts: 8,534
  • Joined: 08-June 10

Re: Code Golf! Password Generator

Posted 09 June 2015 - 10:00 PM

You guys can submit as many solutions as you want. I've unhidden my solution, it's the first reply in the thread. It also has the unit test I will be running so you can check your solutions.

I'll check your answers tomorrow morning. Thanks for participating!
Was This Post Helpful? 0
  • +
  • -

#13 bitspill  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 09-June 15

Re: Code Golf! Password Generator

Posted 09 June 2015 - 10:17 PM

I believe this meets all of the requirements however a few special characters are not included !#$%& <space>

Spoiler

Was This Post Helpful? 0
  • +
  • -

#14 bitspill  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 09-June 15

Re: Code Golf! Password Generator

Posted 10 June 2015 - 01:44 AM

In the case you feel the other may be stretching the rules this version should comply entirely.

Spoiler

Was This Post Helpful? 1
  • +
  • -

#15 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 6294
  • View blog
  • Posts: 13,459
  • Joined: 16-October 07

Re: Code Golf! Password Generator

Posted 10 June 2015 - 05:53 AM

I look forward to reviewing the others now. Nothing fancy here.

Fore:
Spoiler

Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3