Problem of the Day
A new programming or logic puzzle every Mon-Fri

Random YouTube Video

The last day of July is here!

YouTube videos are identified by 11 character alphanumeric strings (including "-"). Your goal for today is to generate a link for a random YouTube video (https://www.youtube.com/watch?v=YOUR_STRING_HERE). For bonus points create a programmatic way to test that the link actually works.

Permalink: http://problemotd.com/problem/random-youtube-video/

Comments:

  • Nick Krichevsky - 10 years, 4 months ago

    import requests
    from random import choice,getrandbits
    
    chars = 'abcdefghijklmnopqrstuvwxyz1234567890-'
    baseUrl = "http://youtube.com/watch?v="
    url = str(baseUrl)
    for i in range (11):
        c = choice(chars)
        if c.isalpha():
            if (bool(getrandbits(1))):
                url+=c.upper()
        url+=c
    print url
    q = requests.get(url)
    if 'This video is unavailable.' in q.text:
        print "Video does not exist"
    

    reply permalink

  • Max Burstein - 10 years, 4 months ago

    Good stuff!

    reply permalink

Content curated by @MaxBurstein