17
\$\begingroup\$

Challenge

Given the name of a PPCG member, output their PPCG ID number. If the user does not exist, you may report an error or return any non-positive number. If there are multiple members with this name, you may choose to output only one ID or all of them.

Test Cases

"musicman523" -> 69054
"Dennis" -> 12012
"xnor" -> 20260
"Leaky Nun" -> 48934
"fəˈnɛtɪk" -> 64505
"Jörg Hülsermann" -> 59107
"Community" -> -1
"Any user that does not exist" -> 0
"Alex" -> 69198 (this is one possible result)
"Leaky N" -> 0
"Jorge" -> 3716
\$\endgroup\$
17
  • 1
    \$\begingroup\$ Recommended test case: "Leaky N". Should return 0. \$\endgroup\$
    – Okx
    Aug 7, 2017 at 14:12
  • 5
    \$\begingroup\$ Can our program result in undefined behaviour for inexistent users (mine prints 48934, for example for a non-existing user)? I think this should be allowed, since errors are. \$\endgroup\$
    – Mr. Xcoder
    Aug 7, 2017 at 14:14
  • 4
    \$\begingroup\$ @Okx No, I don't. I am asking the OP if this behaviour is allowed. If it is not, I will delete or fix my answer. \$\endgroup\$
    – Mr. Xcoder
    Aug 7, 2017 at 14:16
  • 3
    \$\begingroup\$ @OliverNi Toxic? How? \$\endgroup\$
    – Okx
    Aug 7, 2017 at 14:22
  • 5
    \$\begingroup\$ @Okx He is asking a valid question to the OP and you are shooting him down immediately. Let the OP answer. \$\endgroup\$
    – Oliver Ni
    Aug 7, 2017 at 14:22

8 Answers 8

30
\$\begingroup\$

Stack Exchange Data Explorer, 56 54 53 51 46 bytes

-1 byte thanks to Hyper Neutrino. -5 bytes thanks to Giacomo Garabello.

SELECT ID FROM USERS WHERE##S##=DISPLAYNAME--S

Try it online!

Not sure if this is completely valid but... Input must be surrounded in single quotes '.

Also, I still don't get why SQL programmers like to shout but it's apparently good practise so... SELECT EVERYTHING FROM EVERYTHING WHERE EVERYTHING LIKE EVERYTHING!

Explanation

LET ME EXPLAIN.

SELECT ID FROM USERS WHERE##S##=DISPLAYNAME--S

                                           --S  -- DECLARE AN INPUT PARAMETER NAMED S
SELECT                                          -- FIND...
       ID                                       -- ID OF THE USERS...
          FROM USERS                            -- IN THE TABLE USERS...
                     WHERE                      -- THAT SATISFIES THE CONDITION...
                          ##S##=DISPLAYNAME     -- S EQUALS THE USERS' DISPLAY NAME
\$\endgroup\$
18
  • 13
    \$\begingroup\$ -2 BYTES BY REMOVING THE SPACES AROUND THE EQUALS SIGN \$\endgroup\$
    – hyper-neutrino
    Aug 7, 2017 at 15:54
  • 1
    \$\begingroup\$ NINJA'D YOUR NINJA IN CHAT XD \$\endgroup\$
    – hyper-neutrino
    Aug 7, 2017 at 15:55
  • 1
    \$\begingroup\$ -1 byte by reversing the order of operands around the equality operator \$\endgroup\$
    – hyper-neutrino
    Aug 7, 2017 at 16:00
  • 10
    \$\begingroup\$ +1 for the reference to SQL programmers liking to shout (and for a nice choice of language for your answer :) \$\endgroup\$
    – NH.
    Aug 7, 2017 at 23:31
  • 4
    \$\begingroup\$ why have you put the top 1 in this query? OP said "If there are multiple members with this name, you may choose to output only one ID or all of them"... \$\endgroup\$ Aug 8, 2017 at 10:20
5
\$\begingroup\$

JavaScript, 155 149 142 135 bytes

i=>fetch("//api.stackexchange.com/users?site=codegolf&inname="+i).then(r=>r.json()).then(r=>r.items.find(u=>u.display_name==i).user_id)

f=i=>fetch("//api.stackexchange.com/users?site=codegolf&inname="+i).then(r=>r.json()).then(r=>r.items.find(u=>u.display_name==i).user_id)
<input onchange="f(this.value).then(console.log)"><br>Fill input and press Enter

\$\endgroup\$
16
  • 1
    \$\begingroup\$ Does it support special characters like in Jörg Hülsermann? \$\endgroup\$
    – Arnauld
    Aug 7, 2017 at 15:32
  • 4
    \$\begingroup\$ This returned 0 for Oliver :( \$\endgroup\$
    – Oliver
    Aug 7, 2017 at 16:34
  • \$\begingroup\$ Save 7 bytes by replacing r=>r.items[0]).then(r with ({items:[r]}). Destructuring assignment ftw! \$\endgroup\$
    – kamoroso94
    Aug 7, 2017 at 18:55
  • \$\begingroup\$ You can use: i=>fetch('//api.stackexchange.com/users?site=codegolf&inname='+i).then(r=>r.json()).then(r=>r.items[0].user_id) as it'll return a promise error if it failed. You can also just do i=>fetch('/users?site=codegolf&inname='+i).then(r=>r.json()).then(r=>r.items[0].user_id) and say it needs to be run on the api domain \$\endgroup\$
    – Downgoat
    Aug 7, 2017 at 21:04
  • 2
    \$\begingroup\$ @Oliver WHAT another oliver???? THERE CAN BE ONLY ONE \$\endgroup\$
    – Oliver Ni
    Aug 8, 2017 at 12:21
5
\$\begingroup\$

Python 3 + requests, 196 bytes

Thanks @Wondercricket for -6 bytes!

from requests import*
x=lambda s:all([print(a['user_id'])if s==a['display_name']else''for a in get('http://api.stackexchange.com/users?inname=%s&site=codegolf'%utils.quote(s)).json()['items']])and d

Uses Stack Exchange API. Fixed the Leaky N and Jorge errors.

If there are multiple users with the same name, it prints all of them, which is allowed.

\$\endgroup\$
10
  • \$\begingroup\$ It gives me gzipped data.. \$\endgroup\$
    – Oliver Ni
    Aug 7, 2017 at 14:10
  • \$\begingroup\$ Fails on input Leaky N \$\endgroup\$
    – Okx
    Aug 7, 2017 at 14:13
  • \$\begingroup\$ @Okx Fixed. --- \$\endgroup\$
    – Oliver Ni
    Aug 7, 2017 at 14:23
  • \$\begingroup\$ Since you can "report an error or return 0" if the user does not exist, can't the last line be just print a['user_id'], which will throw a KeyError? \$\endgroup\$
    – Daniel
    Aug 7, 2017 at 14:25
  • 1
    \$\begingroup\$ fails for "Jorge" \$\endgroup\$ Aug 8, 2017 at 12:05
5
\$\begingroup\$

Python 2 + requests, 187 bytes

from requests import*
def f(x):t=get("http://api.stackexchange.com/users?inname="+utils.quote(x)+"&site=codegolf").json()["items"];print[k['user_id']for k in t if k['display_name']==x][0]

Returns the user ID if a single user exists, the first user which matches the requirements if more exist, and reports an error otherwise.

\$\endgroup\$
8
  • \$\begingroup\$ You can remove /2.2 from the API-url. \$\endgroup\$ Aug 7, 2017 at 14:39
  • \$\begingroup\$ @KevinCruijssen Thanks a lot \$\endgroup\$
    – Mr. Xcoder
    Aug 7, 2017 at 14:40
  • \$\begingroup\$ Hint: Do not try to run it with fəˈnɛtɪk, use \u{...} instead, because Python does not tolerate non-ASCII \$\endgroup\$
    – Mr. Xcoder
    Aug 7, 2017 at 15:05
  • \$\begingroup\$ Python 2, anyways. \$\endgroup\$ Aug 7, 2017 at 15:22
  • 3
    \$\begingroup\$ fails for "Jorge" \$\endgroup\$ Aug 8, 2017 at 12:05
3
\$\begingroup\$

Python 2 + requests, 173 bytes

lambda s:[i['user_id']for i in get('http://api.stackexchange.com/users?inname=%s&site=codegolf'%utils.quote(s)).json()['items']if i['display_name']==s]
from requests import*

Sample run

>>> f=\
... lambda s:[i['user_id']for i in get('http://api.stackexchange.com/users?inname=%s&site=codegolf'%utils.quote(s)).json()['items']if i['display_name']==s]
>>> from requests import*
>>>
>>> tests = ['musicman523', 'Dennis', 'xnor', 'Leaky Nun', 'Community', 'Any user that does not exist', 'Alex', 'Leaky N', 'Jorge']
>>> for i in tests: print '%-30r -> %s'%(i, f(i))
... 
'musicman523'                  -> [69054]
'Dennis'                       -> [12012, 13891, 14912, 24937]
'xnor'                         -> [20260]
'Leaky Nun'                    -> [48934]
'Community'                    -> [-1]
'Any user that does not exist' -> []
'Alex'                         -> [21536, 69198, 11192]
'Leaky N'                      -> []
'Jorge'                        -> [3716]

Fun fact: the result is sorted by reputation, highest first.

\$\endgroup\$
13
  • \$\begingroup\$ Fails on input Leaky N \$\endgroup\$
    – Okx
    Aug 7, 2017 at 14:12
  • \$\begingroup\$ Nice trick with %s. \$\endgroup\$
    – Mr. Xcoder
    Aug 7, 2017 at 14:12
  • \$\begingroup\$ @Okx Not for me, it doesn't. >>> f('Leaky N')\n48934 \$\endgroup\$ Aug 7, 2017 at 14:13
  • \$\begingroup\$ @totallyhuman It should return 0. Leaky N does not exist \$\endgroup\$
    – Okx
    Aug 7, 2017 at 14:14
  • \$\begingroup\$ @Okx Fixed. - - \$\endgroup\$ Aug 7, 2017 at 14:28
3
\$\begingroup\$

JavaScript, 128 119 bytes

-9 bytes thanks to Rogem.

n=>fetch("198.252.206.16/users?site=codegolf&inname="+n).then(r=>r.text()).then(t=>t.match(`\\/([^\\/]*)\\/`+n+`"`)[1])
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Think you'd save some bytes with the IPv4 address. (198.252.206.16 instead of api.stackexchange.com) \$\endgroup\$
    – user77406
    Dec 10, 2018 at 20:32
1
+100
\$\begingroup\$

APL (Dyalog Unicode), 169 bytes

a←⍞
y←(⎕JSON((⎕SE.SALT.New'HttpCommand').Get'https://api.stackexchange.com/users?site=codegolf&inname=',('\s+'⎕R'\%20')a).Data).items
{a≡1⊃y.display_name:y.user_id ⋄ 0}1

This took forever to figure out, lol.

Thanks to @Adám for saving me a lot of time, and a lot of bytes on the HTTP request.

Explanation

a←⍞                                 ⍝ Store string input
y←        ('\s+'⎕R'\%20')a          ⍝ Convert spaces to '%20' for query
  (⎕JSON((⎕SE.SALT.New'HttpCommand').Get'https://api.stackexchange.com/users?site=codegolf&inname=',).Data).items
                                    ⍝ Get and parse JSON from API call using ⎕SE (session namespace)
 a≡⊃y.display_name                  ⍝ Is the input equal to the unwrapped user id? 
                                    ⍝ (display name comes as an array of strings)
{                  :y.user_id ⋄ 0}  ⍝ If so, print the user id
                                    ⍝ Else print zero
                                    ⍝ Call function with placeholder arg 1
\$\endgroup\$
3
  • \$\begingroup\$ This can be golfed a lot. \$\endgroup\$
    – Adám
    Nov 17, 2020 at 7:33
  • \$\begingroup\$ @Adám curl again, I suppose. \$\endgroup\$
    – Razetime
    Nov 17, 2020 at 7:36
  • \$\begingroup\$ Not sure, as this one returns a compressed result, but you even have spaces around . \$\endgroup\$
    – Adám
    Nov 17, 2020 at 7:36
0
\$\begingroup\$

JavaScript (ES6) + HTML, 154 152 151 202 179 161 145 bytes

Sacrificed a few bytes to handle special characters.

Needs to be run under the api.stackexchange.com domain. Returns a Promise containing the ID or Throws an error in the Promise if the username can't be found.

s=>fetch(`/users?site=codegolf&inname=`+s).then(r=>r.json()).then(j=>j.items.find(i=>(o.innerHTML=i.display_name,o.innerText==s)).user_id)

<a id=o

Note: This solution was developed independently of Uriel's and its comments; if Uriel decides to use the find method, I'm happy to roll back to my longer, recursive version.

\$\endgroup\$
4
  • 3
    \$\begingroup\$ I've created a meta discussion on requiring a certain execution domain, since that does save quite a few bytes. \$\endgroup\$
    – Birjolaxew
    Aug 8, 2017 at 11:48
  • 1
    \$\begingroup\$ @Downvoter, please have the decency to leave a comment. \$\endgroup\$
    – Shaggy
    Dec 8, 2018 at 9:30
  • \$\begingroup\$ @Shaggy I would assume for the same reason as that meta discussion was started. \$\endgroup\$
    – user77406
    Dec 10, 2018 at 7:16
  • \$\begingroup\$ Downvoter, if you disagree with an established consensus (as @Rogem suggests), please downvote the relevant meta post rather than solutions that adhere to that consensus. \$\endgroup\$
    – Shaggy
    Dec 10, 2018 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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