I have so many secrets and nowhere to keep them!

The goal is simple: write a program that lets you save a string and have it be protected by a password.

The program will take an alias, password and (optional) secret as parameters.

If this is the first time the program is called with a given alias, then it will store/remember the secret, and output whatever you want.

If the program is called with an alias that has been used before, then it will output the secret for that alias iff the password is the same as the first time the program was run with that alias.

Other cases

  • If its the first time being called with a certain alias and no secret is given - store nothing.

  • If the program is called with an alias that has been used, and the password is incorrect - return any kind of error or nothing at all.

  • If the program is called with an alias that has been used, the password is correct and a new secret is supplied - output the old secret and replace it with the new one so that next time only the new secret is output.

  • If the program is called with an alias that has been used, the password is correct and no new secret is supplied - output the old secret and make sure it doesn't get replaced.

Note: These secrets/passwords do not need to be stored securely

Also note: any alphanumeric input for aliases passwords and secrets should be accepted

Standard rules apply, good luck!

  • 1
    \$\begingroup\$ Yes, use anything to store it - as for aliases I think it should accept any alphanumeric input \$\endgroup\$ – Quinn May 16 '19 at 21:05
  • 1
    \$\begingroup\$ May we use a function rather than a full program? \$\endgroup\$ – Arnauld May 16 '19 at 21:16
  • 1
    \$\begingroup\$ @Arnauld I'll allow it \$\endgroup\$ – Quinn May 16 '19 at 21:19
  • 1
    \$\begingroup\$ May we assume the password and secret are not the empty string? \$\endgroup\$ – xnor May 17 '19 at 2:14
  • 1
    \$\begingroup\$ May we output a null answer (like 0 or None) in any situation where we don't output the secret? \$\endgroup\$ – xnor May 17 '19 at 2:37

JavaScript (ES6),  60  50 bytes

Saved 10 bytes thanks to @JonasWilms!

Takes input as either (alias,password,secret) or (alias,password). Returns undefined the first time a secret is stored, or false if the password is incorrect.


Try a 1st test case online!

Try a 2nd test case online!


We define a named function \$f\$ whose underlying object is also used to store the passwords and the secrets.


f = (             // f = named function whose underlying object is used for storage
  a,              // a = alias
  p,              // p = password
  s,              // s = optional secret
  [P, v] = f[a]   // if f[a] is defined, retrieve the password P and the secret v
           || [p] // otherwise, copy p in P
) =>              //
  p == P && (     // if p is not equal to P, yield false; else:
    f[a] = [      //   update f[a]:
      P,          //     save the new password (or leave it unchanged)
      s || v      //     save the new secret if it's defined, or keep the previous one
    ],            //
    v             //   return the previous secret
  )               //
  • \$\begingroup\$ Took me a second to spot how you were storing things! \$\endgroup\$ – Shaggy May 16 '19 at 21:53
  • \$\begingroup\$ hmm, is this correct? \$\endgroup\$ – ngn May 17 '19 at 5:35
  • 1
    \$\begingroup\$ @ngn It's most likely incorrect indeed. Thanks for reporting this. Should be fixed now. \$\endgroup\$ – Arnauld May 17 '19 at 7:16
  • \$\begingroup\$ -7 bytes through storing a [password, value] array inside of the object. \$\endgroup\$ – Jonas Wilms May 20 '19 at 20:52
  • \$\begingroup\$ @JonasWilms Nicely done! I can't think of any situation where p&& is required. So, that's another -3 bytes. \$\endgroup\$ – Arnauld May 20 '19 at 21:42

Python 2, 94 93 bytes

def f(a,p,s=0,d={}):
 if q==p:d[a]=p,s or t;return t
 elif q<1<s:d[a]=p,s

Try it online!

For once, Python's weird default dict parameter works in my favor...

  • \$\begingroup\$ Wait, using a default argument makes it just one object instead of a new one on function call? Hmmm... This explains a lot of debugging I've had to do previously lol. \$\endgroup\$ – HyperNeutrino May 23 '19 at 1:46

Ruby, 64 bytes

Builds a hash for aliases to a single key-pair of password => secret. Probably could be more elegant.


Try it online!


Wolfram Language (Mathematica), 34 bytes


Try it online!


C# (Visual C# Interactive Compiler), 140 138 134 bytes

-2 bytes thanks to @Expired Data

a=>p=>s=>(a=P.ContainsKey(a)?P[a]:P[a]=new[]{p,s},o:p==a[0]?a[1]:p,s==""?s:p==a[0]?a[1]=s:s).o;var P=new Dictionary<string,string[]>()

Try it online!


Python 2, 77 bytes

def f(a,p,s=0,d={}):
 if p==P:
	if s>0:d[a]=p,s
	return S

Try it online!

Similar to Chas Brown's method.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.