Acronyms can really obviously narrow your message sensors

36

1

Because of this, you need a better way of working out if a phrase is an acronym of a word. You also think it would be worthwhile to see if the phrase and word in question are recursive acronyms.

Your task:

Given a word and then a phrase separated by a line, output if the phrase is an acronym and then if it is a recursive acronym. (The phrase contains what it stands for)

  • The input will compose of alphabetical characters as well as spaces.
  • Your program shouldn't be case sensitive.

Example Input/Output:

Case 1:

Input:

Acronyms
Acronyms can really obviously narrow your message sensors

Output:

True 
True

Case 2:

Input:

FAQ
frequently asked questions

Output:

True 
False

Case 3:

Input:

foo
bar baz

Output:

False
False

Case 4:

Input:

GNU
GNU is not Unix

Output:

False
False

Case 5:

Input:

Aha
A huge Aha

Output:

True
True

Blue

Posted 2015-10-19T16:58:11.417

Reputation: 26 661

69Acronyms Can Recurse? Oh! Now You're Making Sense. – Geobits – 2015-10-19T17:04:21.340

Do the newlines need to be present in the output? – Morgan Thrapp – 2015-10-19T17:49:57.960

2No, just as long as it's clear what the output is – Blue – 2015-10-19T18:05:52.687

9

This reminds me of an XKCD: http://xkcd.com/917

– ETHproductions – 2015-10-19T18:14:31.090

1Related. – Martin Ender – 2015-10-19T18:25:18.757

1New test case: Lolcat: Laughing over lolcat captions and tearing. Is this True/True or do recursive acronyms need to recurse at the first or last word? – Not that Charles – 2015-10-19T18:44:25.290

Yes it is True/True. – Blue – 2015-10-19T18:53:31.447

4ABCDE: Another basic clearly defined example. – John Dvorak – 2015-10-20T05:08:02.020

1What should be the output for GNU: Gnus nettle unicorns? – DLosc – 2015-10-20T16:59:04.073

True False. That probably breaks some stuff doesn't it? Let's say that case doesn't matter. – Blue – 2015-10-20T17:11:15.847

This reminds me of the Contracrostipunctus from Douglas Hofstadter's Gödel, Escher, Bach, which contains the acrostic "Hofstadter's Contracrostipunctus Acrostically Backwards Spells 'J.S. Bach'." – Neil – 2015-10-20T23:54:15.210

You should add the example PHP -> PHP Hypertext Preprocessor, since that's a real acronym. – mbomb007 – 2015-10-21T21:54:14.323

Answers

10

Pyth, 19 18

&pqJrz0hCKcrw0)}JK

This prints the result in a rather odd format, like: TrueFalse.

You can try it online or run the Test Suite.

Explanation:

&pqJrz0hCKcrw0)}JK      :
    rz0    rw0          : read two lines of input, and convert each to lower case
          c   )         : chop the second input on whitespace
   J     K              : store the first line in J and the chopped second line in K
  q    hC               : zip K and take the first element, check if it is the same as J
 p                      : print and return this value
&              }JK      : and the value with whether J is in K, implicit print

FryAmTheEggman

Posted 2015-10-19T16:58:11.417

Reputation: 16 206

15

Python 3, 89

Saved a bunch of bytes thanks to SOPython.

a=input().lower()
d=input().lower().split()
h=tuple(a)==next(zip(*d))
print(h,h&(a in d))

The most complicated part of this solution is h=tuple(a)==next(zip(*d)).
This unpacks the list d into zip and then calls next to return a tuple of the first element of each iterable passed into zip which is then compared against a tuple of each letter in a (tuple(a)).

Morgan Thrapp

Posted 2015-10-19T16:58:11.417

Reputation: 3 574

You can save 7 bytes by substituting [0]==l for .startswith(l). – Skyler – 2015-10-19T17:47:07.320

7

CJam, 21 20 bytes

qeuN/)S/_:c2$s=_p*&,

Try this fiddle in the CJam interpreter or verify all test cases at once.

How it works

qeu                  e# Read from STDIN and convert to uppercase.
   N/                e# Split at linenfeeds.
     )S/             e# Pop the second line form the array.
      S/             e# Split it at spaces.
        _:c          e# Push a copy and keep on the initial of each word.
           2$s       e# Push a copy of the line array and flatten it.
                     e# This pushes the first line.
              =      e# Check for equality.
               _p    e# Print a copy of the resulting Boolean.
                 *   e# Repeat the word array 1 or 0 times.
                  &  e# Intersect the result with the line array.
                   , e# Push the length of the result (1 or 0).

Dennis

Posted 2015-10-19T16:58:11.417

Reputation: 196 637

4

Scala, 135 110 108 bytes

val Array(x,y)=args.map(_.toLowerCase)
val z=y.split(" ").map(_(0)).mkString
print(z==x,z==x&&y.contains(z))

Saved a few bytes by using command line arguments (thanks to J Atkin for the hint), putting the booleans out as a tupel, using mkString instead of new String and print instead of println.

EDIT: Misinterpreted the question, and had to reimplement the solution

wastl

Posted 2015-10-19T16:58:11.417

Reputation: 151

4

Haskell, 81 80 bytes

import Data.Char
f[a,b]|c<-words b=(a==map(!!0)c,elem a c)
p=f.lines.map toLower

The output format is not strictly defined, so I return a pair of booleans, e.g. p "Aha\na huge arm" -> (True,False).

nimi

Posted 2015-10-19T16:58:11.417

Reputation: 34 639

Today I learned of pattern guards (<-)—thanks!

– wchargin – 2015-10-21T05:13:21.027

3

Python 3, 106 bytes

Well, at least it beat Scala ;)

x=input().lower()
y=input().lower().split()
g=all(x[i]==y[i][0]for i in range(len(y)))
print(g,g&(x in y))

Beta Decay

Posted 2015-10-19T16:58:11.417

Reputation: 21 478

1@muddyfish Possibly more explanation of each examples would prevent this from happening to other people. – Beta Decay – 2015-10-19T17:41:10.023

Would more time in the sandbox have helped? In my (obviously rather limited) experience of it, you only get responses whilst it's nearly at the top – Blue – 2015-10-19T18:07:55.747

@muddyfish Well I don't know how long you left it so, I don't know – Beta Decay – 2015-10-19T18:26:52.853

I left it there for about a day – Blue – 2015-10-19T18:28:04.863

@muddyfish A week is the recommended norm – Beta Decay – 2015-10-19T18:28:24.870

3

AppleScript, 302 301 297 293 Bytes

Aw, hell yeah. Not even bothered that I lose, this is competitive for AppleScript.

set x to(display dialog""default answer"")'s text returned
set y to(display dialog""default answer"")'s text returned's words
set n to y's items's number
repeat n
try
if not y's item n's character 1=(x as text)'s character n then return{false,false}
end
set n to n-1
end
return{true,x is in y}

Outputs as:

{true, false}

Or whatever the answer happens to be.

Addison Crump

Posted 2015-10-19T16:58:11.417

Reputation: 10 763

2

Ruby, 77 74 bytes

b=gets.chop.upcase
a=gets.upcase
p c=a.scan(/\b\w/)*''==b,c&&a.include?(b)

daniero

Posted 2015-10-19T16:58:11.417

Reputation: 17 193

2

PHP, 120 bytes

Not being case sensitive is a lot of weight (26 bytes). Passed all test cases:

foreach($c=explode(' ',strtolower($argv[2]))as$l)$m.=$l[0];var_dump($x=$m==$a=strtolower($argv[1]),$x&&in_array($a,$c));

Outputs two bool values in this form:

bool(true)
bool(false)

Reads two arguments from the command line, like:

a.php Acronyms "Acronym can really obviously narrow your message sensors"

Ungolfed

$acronym = strtolower($argv[1]);
$words = strtolower($argv[2]);
$words = explode(' ', $words);

foreach($words as $word) {
    $letters .= $word[0];
}

$isAcronym = $letters == $acronym;

var_dump(
    $isAcronym,
    $isAcronym && in_array($acronym, $words)
);

insertusernamehere

Posted 2015-10-19T16:58:11.417

Reputation: 4 551

1

Ruby, 52 bytes

p !!gets[/^#{x=gets.scan(/\b\w/)*""}$/i],!! ~/#{x}/i

Example:

$ echo "Aha
A huge AHA" | ruby acronym.rb
true
true

Ventero

Posted 2015-10-19T16:58:11.417

Reputation: 9 842

1

Matlab, 90 bytes

function z=f(r,s)
z=[sum(regexpi(s(regexpi(s,'(?<=(\s|^))\S')),r))>0 nnz(regexpi(s,r))>0];

Example (note that Matlab displays true/ false as 1/ 0):

>> f('Aha', 'A huge Aha')
ans =
     1     1

Luis Mendo

Posted 2015-10-19T16:58:11.417

Reputation: 87 464

1

JavaScript ES6, 95 92 bytes

(a,b)=>[(r=eval(`/^${a}$/i`)).test((s=b.split` `).map(c=>c[0]).join``),s.some(c=>r.test(c))]

Input both strings as parameters. Outputs an array with two values: one for each boolean.

Mwr247

Posted 2015-10-19T16:58:11.417

Reputation: 3 494

1I wouldn't have thought of using a regex in place of .indexOf. Nice work! Perhaps r=eval(`/^${a}$/i`) would work in place of your current r setup. – ETHproductions – 2015-10-21T21:29:42.440

@ETHproductions And I in turn wouldn't have thought of eval as a RegExp object shortener. Thanks for the tip! – Mwr247 – 2015-10-22T14:54:45.117

0

GNU sed, 118 bytes

Requires -r flag, included in score as +1. Note that I'm using \b for a word-boundary match, even though I can't find this documented in GNU sed. It Works For Me...

N
h
s/^(.*)\n.*\b\1\b.*/True/i
/\n/s/.*/False/
x
:
s/(.)(.*\n)\1[^ ]* ?/\2/i
t
/../s/.*/False/
/F/h
/F/!s/.*/True/
G

Expanded:

#!/bin/sed -rf

N
h

# Is it recursive?
s/^(.*)\n.*\b\1\b.*/True/i
# If no replacement, there's still a newline => false
/\n/s/.*/False/

x

# Is it an acronym?
# Repeatedly consume one letter and corresponding word
:
s/(.)(.*\n)\1[^ ]* ?/\2/i
t
# If more than just \n remain, then false
/../s/.*/False/
# And falsify recursive, too
/F/h
# !False => true
/F/!s/.*/True/

G

Toby Speight

Posted 2015-10-19T16:58:11.417

Reputation: 5 058

0

Groovy, 91 bytes

a=args*.toLowerCase()
println([a[1].split()*.charAt(0).join("")==a[0],a[1].contains(a[0])])

Output format is [bool, bool].This takes it's input from the command line args.

J Atkin

Posted 2015-10-19T16:58:11.417

Reputation: 4 846

0

Lua 5.3, 182 bytes

a=""b=io.read c=a.lower d=a.reverse e=d(c(b()))f=~e:len()h=a.sub g=d(c(b()))for m in g:gmatch"[^ ]+"do f=-~f if h(e,f,f)~=h(m,~0)then break end k=k or m==e end f=f>~1print(f,f and k)

MeepDarknessMeep

Posted 2015-10-19T16:58:11.417

Reputation: 31

0

R, 93 bytes

a=tolower(readLines(,2));cat(a[1]==gsub("([^ ])\\w* ?","\\1",a[2]),a[1]%in%scan(t=a[2],w=""))

Usage:

> a=tolower(readLines(,2));cat(a[1]==gsub("([^ ])\\w* ?","\\1",a[2]),a[1]%in%scan(t=a[2],w=""))
Aha
A huge Aha
Read 3 items
TRUE TRUE
> a=tolower(readLines(,2));cat(a[1]==gsub("([^ ])\\w* ?","\\1",a[2]),a[1]%in%scan(t=a[2],w=""))
Acronyms
Acronyms can really obviously narrow your message sensors
Read 8 items
TRUE TRUE
> a=tolower(readLines(,2));cat(a[1]==gsub("([^ ])\\w* ?","\\1",a[2]),a[1]%in%scan(t=a[2],w=""))
FAQ
frequently asked questions
Read 3 items
TRUE FALSE
> a=tolower(readLines(,2));cat(a[1]==gsub("([^ ])\\w* ?","\\1",a[2]),a[1]%in%scan(t=a[2],w=""))
foo
bar baz
Read 2 items
FALSE FALSE

plannapus

Posted 2015-10-19T16:58:11.417

Reputation: 8 610

0

awk 137 bytes

awk 'BEGIN{T="True";F="False"}NR*NF<2{a=tolower($1)}END{for(i=1;i<=NF;i++)b=b substr(tolower($i),1,1);print(a==b?T:F)"\n"(a==tolower($1)?T:F)}'
  • Initialize T="True";F="False" to simplify output.
  • NR*NF<2{a=tolower($1)}: set a only if the first line only has one field.
  • END{...}: assuming only two lines...
    • for(i=1;i<=NF;i++)b=b substr(tolower($i),1,1): construct recursive acronym.
    • print(a==b?T:F)"\n"(a==tolower($1)?T:F): print the output of both comparisons, a==b and a==tolower($1).

If anyone knows how to optimize the recursive acronym construction, feel free to suggest.

h.j.k.

Posted 2015-10-19T16:58:11.417

Reputation: 589

0

SpecBAS - 144 bytes

1 INPUT a$,b$: LET a$=UP$(a$),b$=UP$(b$),d$="": DIM c$(SPLIT b$,NOT " ")
2 FOR EACH w$ IN c$(): LET d$=d$+w$(1): NEXT w$
3 TEXT d$=a$'POS(a$,b$)>0

Converting the 2 x inputs to uppercase saves characters vs. lowercase conversion. Can now have multiple assignments done in one LET statement, which also helps. And TEXT saves one character over PRINT.

Uses 1/0 to show true/false (the apostrophe just moves output to the next line).

Brian

Posted 2015-10-19T16:58:11.417

Reputation: 1 209

0

Perl5, 90 bytes

($a,$b)=map{chomp;lc}<>;print((join"",map{substr($_,0,1)}split/ /,$b)ne $a?0:($b=~$a?2:1))

cheating a bit: 0 = all false, 1 = one true, 2 = both true. I'm not a golfer, but am upset perl is missing while browsing!

($a,$b)=map{chomp;lc}<>;              # get the two lines as lowercase
print((                               #
join"",map{substr($_,0,1)}split/ /,$b # collapse first letters of secondline
     ) ne $a  ? 0 : ( $b=~$a ? 2 : 1))# 0 nothing, 1 not recursive, or 2 

Will

Posted 2015-10-19T16:58:11.417

Reputation: 101

0

JavaScript (ES6), 89 96 95 bytes

(a,b)=>[p=(a=a[l='toLowerCase']())==(c=b[l]().split` `).map(x=>x[0]).join``,p&&c.some(x=>x==a)]

Shucks...I thought I had it all sorted out, but apparently I was wrong.

This defines an anonymous function which takes input as two strings, and returns and array of two boolean items. The first item is calculated by comparing the the first string in all lowercase with the first char of each word in the second string. The second item is calculated simply by checking if the second string contains the first.

Here's another solution for the second item; 2 bytes shorter, but very few browsers support it:

p&&c.includes(a)

ETHproductions

Posted 2015-10-19T16:58:11.417

Reputation: 47 880

Checking if the second string contains the first fails for GNU: Gnus nettle unicorns – edc65 – 2015-10-21T22:48:56.993

Please check again: tried it and does not even work: ReferenceError: l is not defined (missing l= before toLowerCase) – edc65 – 2015-10-21T22:54:14.150

... fixed that bug, it fails for 'GNU','GNU is not unix' (test case 4) should be false, false – edc65 – 2015-10-21T22:56:54.113

@edc65 Shucks, I erased the l= while looking for a bug and forgot to put it back. Thanks for bringing that up! The other test case should be fixed as well. – ETHproductions – 2015-10-21T23:09:43.240

0

JavaScript (ES6) 93

(w,s)=>s[L='toLowerCase'](w=w[L](z=y='')).replace(/\w+/g,v=>y+=v[z=z||v==w,0])&&[y=y==w,y&&z]

Test running the snippet below in any EcmaScript 6 compliant browser

f=(w,s)=>s[L='toLowerCase'](w=w[L](z=y='')).replace(/\w+/g,v=>y+=v[z=z||v==w,0])&&[y=y==w,y&&z]

// TEST

out=x=>O.innerHTML+=x+'\n';

;[
 ['Acronyms', 'Acronyms can really obviously narrow your message sensors', true, true]
,['FAQ', 'frequently asked questions', true, false]
,['foo', 'bar baz', false, false]
,['GNU', 'GNU is not Unix', false, false]
,['Aha', 'A huge Aha', true, true]
,['Lolcat', 'Laughing over lolcat captions and tearing.', true, true]
,['ABCDE', 'Another basic clearly defined example.', true, false]
,['GNU', 'Gnus nettle unicorns', true, false]
,['PHP', 'PHP Hypertext Preprocessor', true, true]
].forEach(([a,b,c,d]) => (
  [r,s]=f(a,b), 
  out(((r==c && s==d)?'OK ':'KO ') + a + ',' + b + ' -> ' + f(a,b))
))
<pre id=O></pre>

edc65

Posted 2015-10-19T16:58:11.417

Reputation: 31 086

0

Pyke (was untitled when posted), (noncompetitive), 20 bytes

l1c"jFh)J"iQl1qDji{&

You can find the source code here, language is completely unstable (first test challenge for it) so don't expect it to work in the future (commit 8)

Or 18 bytes (stableish)

l1idcmhsRl1jqDji{&

Try it here!

Blue

Posted 2015-10-19T16:58:11.417

Reputation: 26 661