Code me a Jam!!

7

1

On occasion I get a little blue and need a little music to cheer me up, but clicking the start button and finding my media player takes sooooooo long. I want a faster way to play a song.

The challenge:

Write a program that:

  • Choose a song from my home directory (so you don’t mix-up program sound files with music) (this includes sub folders of the home directory).

  • This song should be a random choice from all the media in my home folder. This doesn't need to be a perfect random, but I don't want to hear the same song multiple times. (unless that's my only song ;)

  • To make it simple you should only look for files ending in .mp3.

  • In the event that you find no music in my home dir, you should print Sorry Joe, no Jams! to stdout or closest equivalent (btw my name isn't Joe ;)

  • The layout of my home folder will contain no infinite loops

  • You should use system to play the song instead of using built in media playing APIs in your language (i.e. I want the song to play in the default player for my system).

  • I should be able to place this file anywhere on my computer (e.g. my desktop or start folder)

  • Your answer should work on windows, multi-platform is a (un-scored) bonus

This is code golf so the shortest program in bytes wins!!

J Atkin

Posted 2015-10-13T13:39:45.017

Reputation: 4 846

2What is your home dir? – Adám – 2015-10-13T13:41:59.953

It will differ from computer to computer, so you should use a builtin in your land to determine that. (e.g. in python os.path.expanduser('~')) – J Atkin – 2015-10-13T13:47:27.807

Lol, I meant lang, not land – J Atkin – 2015-10-13T13:54:39.490

2waiting for an answer in CJam :) – WizardOfMenlo – 2015-10-13T14:26:59.953

May I post a theoretical answer? I haven't used Windows in years, but I think I know an answer. It won't be golfed because I don't know that I can actually test it. – Addison Crump – 2015-10-13T15:17:10.587

Go ahead, If I know how to use it I can try to test it for you. Otherwise some passer by might be nice and test it ;-) – J Atkin – 2015-10-13T15:18:31.880

Answers

3

PowerShell, 59 52 bytes

ii(ls -r./*.mp4|random);(,"Sorry Joe, no Jams!")[$?]

-7 byte golf improvements, thanks to Jaykul:

  • Use ii (Invoke-Item) instead of saps
  • No need to use Get- in Get-noun commandlet names, it will be searched by default.
  • No space needed after -r

Previously at 59 bytes

saps(ls -R ~/*.mp3|Get-Random);(,"Sorry Joe, no Jams!")[$?]

It will throw an error if there are no files, but it will print the message too.

Explanation:

  • saps is an alias for Start-Process
  • ls is an alias for Get-ChildItem
  • ~ maps to the home directory, and -R is -Recurse
  • Get-Random selects a random item from the pipeline.
  • After trying to launch it, it uses the previous command exit value as an index into a two-item array which either does nothing, or returns the apology message.

TessellatingHeckler

Posted 2015-10-13T13:39:45.017

Reputation: 2 412

Wow that is short, very nice! – J Atkin – 2015-10-14T15:10:27.560

1You can shorten that to ii(ls -r./*.mp4|random);(,"Sorry Joe, no Jams!")[$?] or if you're not willing to assume PowerShell is in the home directory already (that's it's default), then add one character: ii(ls -r ~/*.mp4|random);(,"Sorry Joe, no Jams!")[$?] – Jaykul – 2015-12-21T19:13:35.977

@Jaykul Thanks! I can probably take Get- out of loads of my answers now. – TessellatingHeckler – 2015-12-21T21:18:44.107

save one more byte by replacing ii(ls -r./*.mp4|random); with ls -r./*.mp4|random|ii; – Clijsters – 2017-11-15T15:18:11.170

3

Dyalog APL, 78 75 73

{0::'Sorry Joe, no Jams!'⋄⎕SH⍵,{⍵⊃⍨?≢⍵}⎕SH∊'dir/b '⍵'*.mp3'}'%HOMEPATH%\'

Explanation:

{...} unnamed function, wherein represents whatever is to the right of the }
APL statements (separated by ) are executed leftmost first, but each statement is evaluated from right to left (no precedence rules), so each function gets whatever is to its right as argument
0:: sets a trap for all errors to return the string instead
∊'dir/b '⍵'*.mp3' makes the three strings into a single string
⎕SH passes its argument to cmd.exe
?≢⍵ random 1 ≤ integer ≤ count (or 0 < float < 1 if count is 0)
⍵⊃⍨ pick that element (floats are invalid indices, so an error is triggered here if count was 0)
⍵, prepends the home dir
⎕SH passes its argument to cmd.exe

Adám

Posted 2015-10-13T13:39:45.017

Reputation: 37 779

I may be missing somthing, but where does it print "Sorry Joe, no Jams!" if no media is found? (full disclosure, I have no idea how APL works ;) – J Atkin – 2015-10-13T14:26:49.407

@JAtkin Yeah, I forgot. Fixed. – Adám – 2015-10-13T14:27:24.040

@JAtkin Do you wan't an explanation now, or was "I have no idea how APL works" only a joke about my mistake? – Adám – 2015-10-13T14:41:53.633

No, that was for real, I have no clue how APL works. – J Atkin – 2015-10-13T14:56:34.597

1

Python 2, 178 bytes

import glob,os,random
try:os.startfile(random.choice([y for x in os.walk(os.getenv('HOMEPATH'))for y in glob.glob(os.path.join(x[0],'*.mp3'))]))
except:print"Sorry Joe, no Jams!"

Works on Windows, as per the spec.

Sam Cappleman-Lynes

Posted 2015-10-13T13:39:45.017

Reputation: 306

Nice. I can confirm this works. – J Atkin – 2015-10-13T14:29:18.207

You can probably get rid of the []. Also, if you only need it to work on Windows, can't you just replace the call to os.path.join with x[0]+'\*.mp3' or something like that? It looks overly portable for golf :) – Lynn – 2015-12-22T02:47:09.403

1

Linux shell utilities, 74

Ok, so this one's not going to work on windows, but in case you decide to use a real OS (), here's what you could do in Linux:

find ~ -name \*.mp3|shuf|(read l&&xdg-open "$l"||echo Sorry Joe, no Jams!)

Digital Trauma

Posted 2015-10-13T13:39:45.017

Reputation: 64 644

Hey! I actually like windows 10. But I also like bash... – J Atkin – 2015-10-13T17:54:12.723

I have a copy of bash and the unix utils installed on my windows 7 box. So this could work on windows if you have it set up right. – Jerry Jeremiah – 2015-12-21T21:43:28.710

Is it possible to decompile this to Batsh?

– user8397947 – 2016-06-15T18:11:25.220

@dorukayhan Probably, though I don't see what it would gain, given the dependencies on GNU find and shuf and xdg-utils xdg-open. – Digital Trauma – 2016-06-15T18:23:34.970