What's my PPCG ID?

16

2

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

musicman523

Posted 2017-08-07T13:53:41.653

Reputation: 4 472

Sandbox – musicman523 – 2017-08-07T13:54:09.910

If there are multiple users with the same name, can we output any one of them? – Okx – 2017-08-07T13:56:16.210

@Okx Yes, any of them is fine – musicman523 – 2017-08-07T13:58:01.373

Related – Oliver – 2017-08-07T14:07:38.763

1Recommended test case: "Leaky N". Should return 0. – Okx – 2017-08-07T14:12:16.760

5Can 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. – Mr. Xcoder – 2017-08-07T14:14:49.133

1@Mr.Xcoder The question states that If the user does not exist, you may report an error or return 0. I think you have your answer. – Okx – 2017-08-07T14:16:03.793

4@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. – Mr. Xcoder – 2017-08-07T14:16:38.727

@Mr.Xcoder Sorry, how does that not answer your question? – Okx – 2017-08-07T14:19:20.360

1@Okx Sorry, can you please not be so toxic here? I see your point, but you could be a little nicer about it. – Oliver Ni – 2017-08-07T14:21:26.770

3@OliverNi Toxic? How? – Okx – 2017-08-07T14:22:00.533

5@Okx He is asking a valid question to the OP and you are shooting him down immediately. Let the OP answer. – Oliver Ni – 2017-08-07T14:22:32.930

@Mr.Xcoder You can do anything besides return a positive integer. This includes (but is not limited to) throwing an exception, returning 0, returning a negative number, etc. – musicman523 – 2017-08-07T15:07:47.357

@musicman I fixed my answer. – Mr. Xcoder – 2017-08-07T15:09:59.793

I wonder if this could be answered with a Stack Exchange Data Explorer query...

– Stevoisiak – 2017-08-07T15:30:52.177

@StevenVascellaro Done and done.

– totallyhuman – 2017-08-07T15:44:28.683

1Recommended test case: "Jorge". Should return 3716 (as of right now) – Felipe Nardi Batista – 2017-08-08T12:06:51.060

Answers

30

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

totallyhuman

Posted 2017-08-07T13:53:41.653

Reputation: 15 378

13-2 BYTES BY REMOVING THE SPACES AROUND THE EQUALS SIGN – HyperNeutrino – 2017-08-07T15:54:47.523

1NINJA'D YOUR NINJA IN CHAT XD – HyperNeutrino – 2017-08-07T15:55:33.947

1-1 byte by reversing the order of operands around the equality operator – HyperNeutrino – 2017-08-07T16:00:34.863

1-1 byte: 1Id (the space is unnecessary) – HyperNeutrino – 2017-08-07T16:02:48.047

Wait, is SQL's whitespace requirement the same as Python's? o0 – totallyhuman – 2017-08-07T16:03:16.480

Apparently so ¯\(ツ) – HyperNeutrino – 2017-08-07T16:04:13.463

Don't think it's quite fair to require the input include quotes. +2 bytes to add the quotes back into your answer: SELECT TOP 1ID FROM USERS WHERE'##S##'=DISPLAYNAME--S – BradC – 2017-08-07T20:38:06.120

It's accepted for Python to do it, so I don't see why ---SQL--- SEDE should be disallowed from using it. – totallyhuman – 2017-08-07T20:39:48.867

10+1 for the reference to SQL programmers liking to shout (and for a nice choice of language for your answer :) – NH. – 2017-08-07T23:31:04.963

4why 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"... – Giacomo Garabello – 2017-08-08T10:20:38.093

Ohh, I must've missed that. Thanks for spotting that! – totallyhuman – 2017-08-08T12:33:12.913

i am stupid i searched something about stackexchange api and this was second in the results and i didnt see it – Oliver Ni – 2017-08-08T12:36:45.800

Seems to fail for every username? – stevefestl – 2018-01-01T04:18:24.150

@SteveFest Still works fine for me. Are you surrounding the input with single quotes? – totallyhuman – 2018-01-01T04:20:16.520

No. I just typed SteveFest. I also tried your username and it fails too. – stevefestl – 2018-01-01T04:26:13.750

@SteveFest You should be doing it lol. The answer says so. – totallyhuman – 2018-01-01T04:27:10.040

Oops, my fault. – stevefestl – 2018-01-01T04:29:05.993

Not sure if the Data Explorer has changed since this was written, but I don't think you need the --S at the end any more, it correctly prompts for the parameter with only the ##S##. – BradC – 2018-12-07T21:40:42.197

5

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.

Oliver Ni

Posted 2017-08-07T13:53:41.653

Reputation: 9 650

It gives me gzipped data.. – Oliver Ni – 2017-08-07T14:10:41.617

Fails on input Leaky N – Okx – 2017-08-07T14:13:15.970

@Okx Fixed. --- – Oliver Ni – 2017-08-07T14:23:26.610

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? – Daniel – 2017-08-07T14:25:31.600

No, since Leaky N would return something. – Oliver Ni – 2017-08-07T14:26:24.073

@Dopapp Looking at the code, you see that it searches with the parameter inname. Thus, SE is only checking that it's in the name. – Okx – 2017-08-07T14:27:04.527

@Okx, oh yeah I see that now – Daniel – 2017-08-07T14:28:54.153

You can take advantage of a module within request and replace s.replace(' ','%20') with utils.quote(s) for 173 bytes – Wondercricket – 2017-08-07T17:00:25.183

1fails for "Jorge" – Felipe Nardi Batista – 2017-08-08T12:05:51.807

@FelipeNardiBatista Fixed. – Oliver Ni – 2017-08-09T02:21:03.163

5

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.

Mr. Xcoder

Posted 2017-08-07T13:53:41.653

Reputation: 39 774

You can remove /2.2 from the API-url. – Kevin Cruijssen – 2017-08-07T14:39:28.327

@KevinCruijssen Thanks a lot – Mr. Xcoder – 2017-08-07T14:40:43.733

Hint: Do not try to run it with fəˈnɛtɪk, use \u{...} instead, because Python does not tolerate non-ASCII – Mr. Xcoder – 2017-08-07T15:05:38.543

Python 2, anyways. – totallyhuman – 2017-08-07T15:22:33.710

Mentioned this as a comment to Oliver's answer, but you can shed 6 bytes by replacing s.replace(' ','%20') with utils.quote(s); utils being a module within requests – Wondercricket – 2017-08-07T17:12:22.483

@Wondercricket Thanks a lot! – Mr. Xcoder – 2017-08-07T17:14:56.760

3fails for "Jorge" – Felipe Nardi Batista – 2017-08-08T12:05:58.217

Ok, hopefully all the issues are fixed now. – Mr. Xcoder – 2017-12-31T20:54:44.713

5

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

Uriel

Posted 2017-08-07T13:53:41.653

Reputation: 11 708

1Does it support special characters like in Jörg Hülsermann? – Arnauld – 2017-08-07T15:32:48.517

4This returned 0 for Oliver :( – Oliver – 2017-08-07T16:34:16.330

Save 7 bytes by replacing r=>r.items[0]).then(r with ({items:[r]}). Destructuring assignment ftw!

– kamoroso94 – 2017-08-07T18:55:32.673

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 – Downgoat – 2017-08-07T21:04:24.913

Version that fixes bug reported by @Oliver and saves 7 bytes: i=>fetch(\//api.stackexchange.com/users?site=codegolf&inname=`+i).then(r=>r.json()).then(r=>alert(r.items.find(u=>u.display_name==i).user_id))` - be careful of StackExchange's zero-width spaces when copy-pasting. – Birjolaxew – 2017-08-08T06:43:42.930

You can save a few bytes by just returning the Promise, rather than alerting the result from it. – Shaggy – 2017-08-08T09:58:26.687

@Downgoat how can I show the result of the promise? – Uriel – 2017-08-08T11:08:29.423

@Uriel, call the function like so: f("Shaggy").then(console.log). See here for more on that consensus. (Note that your solution still fails on usernames containing special characters)

– Shaggy – 2017-08-08T11:10:05.497

2@Oliver WHAT another oliver???? THERE CAN BE ONLY ONE – Oliver Ni – 2017-08-08T12:21:28.473

@OliverNi you are Oliver Ni, he's Oliver – Uriel – 2017-08-08T13:04:30.857

@OliverNi Ironically you were the reason Oliver used to return 0 :P Apparently "Oliver Ni" is a better match than "Oliver" when searching for "Oliver". – Birjolaxew – 2017-08-08T13:52:57.017

@Birjolaxew, it's because the default sort order for this query is by reputation descending which, as of this writing, places OliverNi at the top of the list. – Shaggy – 2017-08-08T16:30:45.030

@Shaggy would be .then(::console.log) – Downgoat – 2017-08-08T17:10:41.060

No need for the colons in this case, @Downgoat; give it a try in the consensus I linked to above. – Shaggy – 2017-08-08T17:47:16.360

@Shaggy not sure what you mean. Code would error without colons – Downgoat – 2017-08-08T17:50:14.443

@Downgoat: try it here

– Shaggy – 2017-08-08T18:10:07.253

3

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.

totallyhuman

Posted 2017-08-07T13:53:41.653

Reputation: 15 378

Fails on input Leaky N – Okx – 2017-08-07T14:12:45.470

Nice trick with %s. – Mr. Xcoder – 2017-08-07T14:12:54.513

@Okx Not for me, it doesn't. >>> f('Leaky N')\n48934 – totallyhuman – 2017-08-07T14:13:54.713

@totallyhuman It should return 0. Leaky N does not exist – Okx – 2017-08-07T14:14:40.860

@Okx Fixed. - - – totallyhuman – 2017-08-07T14:28:05.520

s==t['display_name']and instead of t['display_name']==s and for -1 byte – Mr. Xcoder – 2017-08-07T14:29:42.233

Oh, love abusing Python's whitespace requirements. Thanks! – totallyhuman – 2017-08-07T14:30:10.613

Oh, so is the import for requests not required? – Okx – 2017-08-07T14:32:26.967

@Okx Oh, my bad. – totallyhuman – 2017-08-07T14:37:15.223

The OP replied: Your 147 byte approach is invalid. – Mr. Xcoder – 2017-08-07T15:10:51.053

Aw, maaan... ;-; – totallyhuman – 2017-08-07T15:27:59.577

Fails for "Jorge" – Felipe Nardi Batista – 2017-08-08T12:09:22.200

@FelipeNardiBatista Fixed. – totallyhuman – 2017-08-08T14:57:33.940

3

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])

Oliver

Posted 2017-08-07T13:53:41.653

Reputation: 7 160

1Think you'd save some bytes with the IPv4 address. (198.252.206.16 instead of api.stackexchange.com) – None – 2018-12-10T20:32:30.837

-1

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.

Shaggy

Posted 2017-08-07T13:53:41.653

Reputation: 24 623

2

I've created a meta discussion on requiring a certain execution domain, since that does save quite a few bytes.

– Birjolaxew – 2017-08-08T11:48:08.617

1@Downvoter, please have the decency to leave a comment. – Shaggy – 2018-12-08T09:30:00.073

@Shaggy I would assume for the same reason as that meta discussion was started. – None – 2018-12-10T07:16:50.953

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. – Shaggy – 2018-12-10T12:05:46.880