Fibonacci distribution validator

8

1

Related: Hello world!!! Fibonacci distribution

Create a program that returns True if a given input meets the following specifications, and False otherwise:

  • The count of numeric characters (0-9) in the input matches a Fibonacci number.
  • The count of non-numeric characters !(0-9) in the input matches the Fibonacci number immediately preceding the count of numeric characters.

Additional Rules:

  • Your program must use the proper Fibonacci sequence, per OEIS - that is, the Fibonacci sequence must start with 0, 1, 1, 2, ...
  • If the numerics or non-numerics count is 1, the following must occur:
    • Numerics 1: Non-numeric count of 0 or 1 should be handled as True - all others False.
    • Non-Numerics 1: Numerics count of 1 or 2 should be handled as True - all others False.
  • Input may be taken however you like, but the program must be capable of handling any arbitrary text.
  • True/False are not case-sensitive, and can be substituted with 1/0 or T/F.
  • You may only hard-code up to two Fibonacci numbers.
  • Output may only be True/False or 1/0 or T/F. Any additional text or visible errors generated is unacceptable.

Iszi

Posted 2014-01-19T07:05:02.780

Reputation: 2 369

give some example IO – Shubanker – 2014-01-19T07:13:03.040

@Subhanker See the linked question for some example True cases. – Iszi – 2014-01-19T07:13:42.037

Relevant wikipedia article: http://en.wikipedia.org/wiki/Fibonacci_number#Recognizing_Fibonacci_numbers

– Justin – 2014-01-19T07:33:20.957

is T/F or T/nil acceptable as well? – John Dvorak – 2014-01-19T07:52:38.987

Am I right that we can either output or return? – Justin – 2014-01-19T08:08:58.213

@Quincunx I interpret it as "output". I believe a return value is acceptable. – John Dvorak – 2014-01-19T08:09:48.870

@JanDvorak I'm honestly not sure which language to use right now, so I'd like to keep my options open. I interpret the same as you. – Justin – 2014-01-19T08:10:56.253

@Quincrux Output preferably, but since there's already some answers I won't modify the challenge. – Iszi – 2014-01-20T00:24:29.323

@JanDvorak T/F is okay, but I wouldn't be too keen to accept T/nil. – Iszi – 2014-01-20T00:31:37.337

@Iszi T/nil is the standard representation of booleans in Lisp. That's why I'm asking. – John Dvorak – 2014-01-20T04:23:23.267

"You may only hard-code up to two Fibonacci numbers." - you're changing this rule too often (even one invalidating change counts as too often). Why not drop it entirely? – John Dvorak – 2014-01-20T05:46:33.043

@JanDvorak That seems to be a rather unique case I'd rather not mess with re-writing a rule (again) for. Sorry. As it is, I've added a fair bit since the start of this challenge already. I'd rather not edit it any more if I can get away with it. I thought this was going to be relatively simple, but perhaps it could have used some time in the Sandbox (AKA: Black Hole of My Ideas). – Iszi – 2014-01-20T05:46:41.553

@JanDvorak The original rule was to not hard-code Fibonacci numbers at all, which is a rather general statement of the rule's intent. However, some people (yourself included) took it a bit too literally and started writing disclaimers like "well, I had to hard-code a couple..." into their answers. I modified it to allow for the first two, because those are the bare essentials needed to start generation of the sequence. Then someone (oh, that was you again) requested a generalization of the rule to allow for the second and third numbers instead... – Iszi – 2014-01-20T05:52:36.750

...I'm done editing that rule. It's now strict enough to serve its purpose while still being just permissive enough to allow programs to be functional - which is how I like my rules to be in general anyway. – Iszi – 2014-01-20T05:54:05.227

You've still invalidated the first revision of my javascript solution. You might just say "you may not hard code the entire fibonacci sequence", but that's hardly beneficial because such sequence would be longer than the generator. – John Dvorak – 2014-01-20T05:56:09.723

@JanDvorak Your current revision is shorter, and fits the current version of the anti-hard-coding rule. I fail to understand why you would complain about that particular rule at this point. – Iszi – 2014-01-20T06:00:01.203

2Ugh, you changed the challenge. Now you say that the fibonacci sequence starts at 0 and give specific cases for 0. The other question that you linked to forbids 0, so I assumed the same. – Justin – 2014-01-21T04:47:03.067

Answers

4

Javascript, 92 88 86 characters

t=prompt()
for(b=c=1;c<t[l='length'];c=a+b)a=b,b=c
alert(c==t[l]&b==t.match(/\d/g)[l])

I hope you don't mind I've hard-coded the the first three Fibonacci numbers.

John Dvorak

Posted 2014-01-19T07:05:02.780

Reputation: 9 048

Does it handle multiple lines of text? – Justin – 2014-01-19T08:22:21.297

@Quincunx Chrome lets you copy/paste but not type newlines into the prompt input; haven't tested firefox. – John Dvorak – 2014-01-19T08:23:11.430

Guess I learn something new every day. – Justin – 2014-01-19T08:24:00.883

According to OEIS, the first three Fibonacci numbers are 0, 1, 1. In any case, you should only need to hard-code the first two - why did you do three? – Iszi – 2014-01-20T00:25:55.877

@Iszi you are right - a didn't need initialisation. As for why do I start at 1,2,3 - the poster of the initial challenge didn't accept 1 as immediately preceding 1. – John Dvorak – 2014-01-20T04:07:30.957

@JanDvorak Ah. Well, while the two are related this is indeed a separate challenge. I prefer to go by more proper definitions when dealing with a defined number series. – Iszi – 2014-01-20T04:36:25.137

@Iszi do you mind if I hard-code the second and third fibonacci number instead? – John Dvorak – 2014-01-20T04:41:13.653

So long as your program can also answer True for the case where the input is a single digit... which brings me to an interesting case I forgot to consider. If your numerics count is 1, then I suppose the program should output True if you have zero non-numerics or 1 non-numeric in the input. – Iszi – 2014-01-20T05:34:42.160

@Iszi but, what about the case I have 1 non-numeric (-1st fibonacci number), 0 numeric (0th fibonacci number) and 1 total (1st fibonacci number) characters? – John Dvorak – 2014-01-20T05:37:25.570

@JanDvorak Total characters is not a consideration for this program. Only the counts for numeric and non-numeric characters are of any concern. I'll clarify the ones situation in an edit shortly. – Iszi – 2014-01-20T05:40:07.917

4

Golfscript, 36

:?1 2{.@+.?,<}do?,=@{.48<\58<^},,@=*

Explanation:

  • :? stores the input into ?.
  • 1 2{.@+.?,<}do computes the last two fibonacci numbers until it hits the input length. The block reads: "duplicate the top, rotate the third value to the top, add them, duplicate the top, get the input, get its length, compare".
  • ?,= compares the last computed fibonacci number to the input length.
  • @ brings the input to the top
  • {.48<\58<^}, filters out only digits. The block reads "is the ASCII value below 48 XOR below 58?"
  • ,@= compares the filtered string length to the lower fibonacci number (count of digits)
  • * merges the two comparisons to provide a single boolean value.

Live demo: http://golfscript.apphb.com/?c=OyIvMDU5OiIKOj8xIDJ7LkArLj8sPH1kbz8sPUB7LjQ4PFw1ODxefSwsQD0q

John Dvorak

Posted 2014-01-19T07:05:02.780

Reputation: 9 048

3

Python - 128 125

import re
def v(s):
 l=len(re.sub("\d","",s));L=len(s)-l;a,b=1,2
 while a<L:
    if a==l and b==L:
     print 1;return
    b,a=a+b,b
 print 0

Really hope there is no problem with hardcoding the first few fibonacci numbers

Justin

Posted 2014-01-19T07:05:02.780

Reputation: 19 757

Isn't there... too much whitespace? – John Dvorak – 2014-01-19T08:39:38.527

@JanDvorak those four spaces were all tabs, so they were counted as 1 char per four spaces. I could alternate tabs and spaces, doing that now. – Justin – 2014-01-19T08:45:04.987

Looks like I should have been a bit more clear about hard-coding the numbers. Of course you'll need to prime the sequence, but you should only need the first two to do it. – Iszi – 2014-01-20T00:29:27.690

2

Perl, 92

$_=join"",<>;@_=1;$d=s/\d//g;push@_,$t=$_[-1]+$_[-2]while$t<$d;print$t==$d&$_[-2]==y///c?1:0

Usage:

cat fib-test 
print "Hello world%s"%("!"*int(3.141592653589793238462643383279502884197169399375105820))

perl -e '$_=join"",<>;@_=1;$d=s/\d//g;push@_,$t=$_[-1]+$_[-2]while$t<$d;print$t==$d&$_[-2]==y///c?1:0' fib-test
1

Dom Hastings

Posted 2014-01-19T07:05:02.780

Reputation: 16 415

2

Java - 147 145

boolean v(String s){int l=s.replaceAll("\\d","").length(),L=s.length()-l,a=1,b=2,c;while(a<L){if(a==l&&b==L)return 0<1;c=b;b+=a;a=c;}return 0>1;}

I'd say this is not bad for Java.

Edit: Thanks to Chris Hayes for suggesting 0>1 for false and 0<1 for true.

Justin

Posted 2014-01-19T07:05:02.780

Reputation: 19 757

4As long as we're using 1==0 to save on characters, you could use 0<1 in place of true, and 0>1 for false. – Chris Hayes – 2014-01-19T11:55:45.750

2

Python 3

(105 characters)

Script file name is passed to the program through the command line

import sys
a=[0]*2
for b in open(sys.argv[1]).read():a['/'<b<':']+=1
a,b=a
while a>0:a,b=b-a,a
print(b==1)

(87 characters)

Script must be wroted in file with name 's'

a=[0]*2
for b in open('s').read():a['/'<b<':']+=1
a,b=a
while a>0:a,b=b-a,a
print(b==1)

AMK

Posted 2014-01-19T07:05:02.780

Reputation: 506

1

APL, 34 chars/bytes*

{n←+/⍵∘.=⍞∊⎕D⋄n≡+\∘⌽⍣{≥/+/⊃⍺n}⍵}⍳2

Expects the input string on standard input and prints either 0 or 1 as required. ⎕IO must be set to 0 (the default is implementation-dependent.)

Ungolfed

s←⍞                ⍝ read input string
d←s∊⎕D             ⍝ vector with 1 for each digit, 0 for each non-digit in s
n←+/0 1∘.=d        ⍝ 2-vector with number of non-digits and number of digits in s
f←+\∘⌽             ⍝ a function that computes a fibonacci step (f 2 3 → 3 5)
t←f⍣{≥/+/⊃⍺n}0 1   ⍝ apply f repeatedly, starting with 0 1, until we get two fibonacci
                   ⍝   terms whose sum is ≥ the length of the input string (sum of n)
n≡t                ⍝ return 1 if the fibonacci terms match the no. of digits and non-digits

Examples

      {n←+/⍵∘.=⍞∊⎕D⋄n≡+\∘⌽⍣{≥/+/⊃⍺n}⍵}⍳2
%~n01234
1
      {n←+/⍵∘.=⍞∊⎕D⋄n≡+\∘⌽⍣{≥/+/⊃⍺n}⍵}⍳2
x'48656C6C6F20776F726C642121'||'!'
1
      {n←+/⍵∘.=⍞∊⎕D⋄n≡+\∘⌽⍣{≥/+/⊃⍺n}⍵}⍳2
[72 101 108 108 111 32 119 111 114 108 100 33 {.}2*]''+
1
      {n←+/⍵∘.=⍞∊⎕D⋄n≡+\∘⌽⍣{≥/+/⊃⍺n}⍵}⍳2
What?12345
0

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
*: APL can be written in its own (legacy) single-byte charset that maps APL symbols to the upper 128 byte values. Therefore, for the purpose of scoring, a program of N chars that only uses ASCII characters and APL symbols can be considered to be N bytes long.

Tobia

Posted 2014-01-19T07:05:02.780

Reputation: 5 455

0

Ruby, 85

d=-(n=(i=$<.read).gsub(/\d/,'').size)+i.size
a=b=1;while b<d;b=a+a=b end;p b==d&&a==n

Takes input either on STDIN or as a filename argument.

Output is either "true" or "false".

Darren Stone

Posted 2014-01-19T07:05:02.780

Reputation: 5 072