The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   The Coat Room (http://hintsforums.macworld.com/forumdisplay.php?f=8)
-   -   How many Fs? (http://hintsforums.macworld.com/showthread.php?t=87061)

EatsWithFingers 03-11-2008 06:28 AM

Quote:

Originally Posted by J Christopher (Post 457285)
It would be much more interesting to see who could write the least efficient program to count the Fs in that sentence series of words quoted in the OP.

This reminded me of the International Obfuscated C Code Contest, where the aim is to write a program such that is impossible to work out what it does.

Unfortunately, my programming teacher taught me too well, and I refuse to go down that path...! :D


EDIT: It was too tempting. Below is C code which, although only slightly obfuscated, computes the result using a divide-and-conquer approach. The first parameter is the sentence, and the second is the letter to count (e.g. $>./a.out 'hello there' e). It prints out -1 if not called correctly.

Code:

int f(char *s, char l, int a, int b) { return a==b?s[a]==l:f(s,l,a,(a+b)/2)+f(s,l,((a+b)/2)+1,b); }
int main(int argc, char *argv[]) { printf("%d\n",argc==3?f(argv[1],*argv[2],0,strlen(argv[1])-1):-1); }


Felix_MC 03-11-2008 07:38 PM

Couldn't find this for OS X (not that I look that much :P), but if you got a PC around, or Bootcamp and such, heres an easier way ;)
http://www.softpedia.com/get/Office-...Software.shtml

Maybe I'll put some long and useless javascript code later, I gtg right now..:P

fazstp 03-11-2008 08:20 PM

Much as I've enjoyed the coding challenge that has ensued the original post was to illustrate a quirk of the brain. Apparently most people only see 3 Fs.

Now code-on :D . I am interested to see where this might lead.

Felix_MC 03-11-2008 10:05 PM

Well, I tried it in javascript, but it got too messy, and too complicated to debug.. :p

Besides, who needs code anyway, when you could do this in Quartz Composer in under 5 mins? :)
Below is the QC file, containing only 3 base patches: String Components, Structure Count, and Mathematical Expression. Then I added a Image with string and Sprite patches so the result can be seen in the viewer window. :p
I've also build a "codeless" cocoa app in Xcode, using the QC file as the "logic".
And my app can count any letter, symbol, word, or phrase the user wants, so it's already better than all of y'all's :D
Download here :P

tw 03-11-2008 11:34 PM

Quote:

Originally Posted by Felix_MC (Post 457505)
Besides, who needs code anyway, when you could do this in Quartz Composer in under 5 mins? :)

maybe you should turn this into the world's most useless screensaver... ;)

NovaScotian 03-12-2008 10:24 AM

Quote:

Originally Posted by Mikey-San (Post 457216)
Okay, come back in 6 months and determine the intent of this code. Better comment it. ;)

I would know in 6 months.

Code:

-- AppleScript can discern class within a list.
set tList to {"Alpha", "Beta", 6, {27, 36}, "Gamma", 4, {1, "B", 3}, {A:"Adam", E:"Eve"}}
-- each of these produces a list:
set S to strings of tList --> {"Alpha", "Beta", "Gamma"}
set N to integers of tList --> {6, 4}
set L to lists of tList --> {{27, 36}, {1, "B", 3}}
set M to integers of item 1 of L --> {1, 3}
set R to records of tList --> {{A:"Adam", E:"Eve"}}
-- etc.


Wee_Guy 03-12-2008 03:07 PM

I find that the simplest way to find the fs was to hit [Command]+[F] and hit 'Next' a few times.

The answer was indeed, 6.

However, when i counted manually before hitting 'Find', I failed to count the 'F's in the 'of's.

Felix_MC 03-12-2008 04:27 PM

Quote:

Originally Posted by tw
maybe you should turn this into the world's most useless screensaver...

Nah, you're just jealous of my skills XD :D

BTW, does the app at least work properly? Anyone tried it:p?
I know it has a few bugs with main window when opening and closing the app, but does it count right?:)

capitalj 03-12-2008 05:34 PM

Quote:

Originally Posted by Felix_MC (Post 457657)
BTW, does the app at least work properly? Anyone tried it:p?
I know it has a few bugs with main window when opening and closing the app, but does it count right?:)

After reading that, I thought "What the heck..." and downloaded it.

It counted correctly, and I noticed the search is case sensitive, too.

I searched "Dimple monkey twice the pudding octopi for tango man" for first "i" (4) then "I" (0).

Felix_MC 03-12-2008 07:58 PM

Okay, so I fixed some changed some stuff around. First I added a checkbox that asks you whether you want the search to be case sensitive or not. I had to triple the number of patches just for that;). Then I fixed one bug that under some circumstances, when the main text field was empty, and the search field contained something, the frequency would show -1. I added a quick conditional patch that displays "0" whenever the output displays a negative number. I also did some small interface changes, and stuff like that..
Download qc file and app here for anyone that's interested.
That was actually pretty fun :D
There should be more small, silly, programming contests on this forum, kinda like some forums have photoshop contests or what not :D

Now I'm going to go and try to write a "F" counter program for my TI-84+ Silver Edition :p:)

EatsWithFingers 03-13-2008 08:49 AM

Probabilistic Method
 
Quote:

Originally Posted by fazstp (Post 457487)
Much as I've enjoyed the coding challenge that has ensued the original post was to illustrate a quirk of the brain. Apparently most people only see 3 Fs.

Even knowing that there were 6, it still took me the best part of two minutes to find them all!


Quote:

Originally Posted by fazstp (Post 457487)
Now code-on :D . I am interested to see where this might lead.

Below is code which returns an answer based on probability. A random character is chosen, then checked for equivalence with the target letter. Over time, the ratio of hits/(misses + hits) will tend towards the ratio of 'target letters'/total.

Code:

#define VAL_UPDATE_BOUND 1000000

#include <stdlib.h>
#include <time.h>


/* Chooses a random character in the input string and tests for equality with *
 * the target letter. The number of hits and misses are used to calculate    *
 * probabilistically the number of occurrences of the target letter in the    *
 * input string. The loops exists when no change in the guessed answer occurs *
 * for a given number of iterations, or when the number of hits or misses are *
 * about to overflow. Shorter strings have a larger bound for the number of  *
 * iterations required for each guess, since guesses are more volatile when  *
 * the number of characters is low.                                          */
void count_letters_prob(char *str, char letter)
{
  int length    = strlen(str);
  int num_loops = 0;
  int max_loops = VAL_UPDATE_BOUND / length;

  unsigned int hits  = 0;
  unsigned int misses = 0;

  int curr_guess = 0;
  int new_guess  = 0;

  while (num_loops++ < max_loops) {
    int idx = length * ((float)(rand())/(RAND_MAX + 1.0));

    /* Check Character for Match */
    if (str[idx] == letter) {
      if (hits == UINT_MAX) break; else hits++;
    }
    else {
      if (misses == UINT_MAX) break; else misses++;
    }

    new_guess = (length * ((float)hits / (hits + misses))) + 0.5;

    /* Update Variables if Guess has Changed */
    if (new_guess != curr_guess) {
      // printf("%d\n", new_guess);
      curr_guess = new_guess;
      num_loops = 0;
    }
  }

  printf("HITS:\t%d\nMISSES:\t%d\nGUESS:\t%d\n", hits, misses, curr_guess);
}


int main(int argc, char *argv[])
{
  if (argc != 3) { return 1; }

  /* Initialise Random Number Generator                        *
  * (srand is called twice because if this is not done, for    *
  *  small ranges, the first random number is always the same) */
  srand(time(0));
  srand(rand());

  count_letters_prob(argv[1], *argv[2]);

  return 0;
}

Example run:

$> ./a.out 'FINISHED FILES ARE THE RESULT OF YEARS OF SCIENTIFIC STUDY COMBINED WITH THE EXPERIENCE OF YEARS' F
HITS: 739
MISSES: 10962
GUESS: 6


Not as compact as my previous entries though.... :p

Jay Carr 03-13-2008 11:42 AM

Good grief! I had myself wholly convinced that you all were nutters and the sentence in fact only had 3 F's in it. I was going to write an extended rant about how you all are idiots, then I decided to run a simple test on my own. I put the line into a text editor and starting deleting every letter that wasn't an F.

Didn't take me long to see that there were in fact 6 Fs, and that for some reason my mind was completely skipping over the Fs in the world "of". How ridiculous! Why on earth does that happen? Fastzp, is there an explanation for this?

cwtnospam 03-13-2008 12:20 PM

It's becuase we do nto raed letetrs, we raed wrods!

seeker777 03-13-2008 12:39 PM

I just wanted to pipe up and say that I have enjoyed this thread even though I did not participate (might have something to do with being ancient and having learned practically dead languages back in the day...Cobol anyone? :p How about Fortran IV?...)

Anyway, Felix_MC, shouldn't you be working on homework, not fooling around here:D I seem to remember some last minute crises (hmm, interviews...losing your a$$ in stock games...):rolleyes::)

iampete 03-13-2008 12:58 PM

Quote:

Originally Posted by seeker777 (Post 457826)
. . . I have enjoyed this thread . . .

Ditto, even though most of it was way above my head.:o

Quote:

Originally Posted by seeker777 (Post 457826)
. . . Felix_MC . . . losing your a$$ in stock games...):rolleyes::)

Ah, but he still has his a$$, since all of his losses were imaginary:), unlike most of the rest of us whose (paper) lo$$es were for real.:(

NovaScotian 03-13-2008 01:52 PM

Quote:

Originally Posted by cwtnospam (Post 457818)
It's becuase we do nto raed letetrs, we raed wrods!

Or even: "Acocdrnig to an elgnsih unviesitry sutdy the oredr of letetrs in a wrod dosen't mttaer, the olny thnig thta's iopmrantt is that the frsit and lsat ltteer of eevry word is in the crcreot ptoision. The rset can be jmbueld and one is stlil able to raed the txet wiohtut dclftfuity"

Felix_MC 03-13-2008 03:45 PM

Quote:

Originally Posted by seeker777
Anyway, Felix_MC, shouldn't you be working on homework, not fooling around here I seem to remember some last minute crises (hmm, interviews...losing your a$$ in stock games...)

Right now I'm slacking off a bit. As some of might now, this weekend I moved from a big city in Northern Virginia (Sterling), to a smaller city in Central Virginia. School is a bit easier, and teachers aren't counting any tests or hw for this week, since I'm new. The school is also a bit older, and therefore has less computers and stuff like that. Also since I moved, I won't be able to participate in the Stock Market game, but I can still check on my group, since I have the username and password :p The good thing is, I got promoted to Algebra II, which is considered 11th grade math here in Virginia, and I'm only in 8th grade :). Also, girls are a bit hotter here, and way more "nicer":rolleyes:. And since I now take 11th grade math, I have to take a bus in the middle of the day to go to the local highschool to take Algebra II. Highschool girls are sweet..:p Maybe I should create a new threat about it, I need help with something anyway..
Ah... Math and girls... What more could you ask for?:D

Quote:

Originally Posted by iampete
Ah, but he still has his a$$, since all of his losses were imaginary, unlike most of the rest of us whose (paper) lo$$es were for real.

Maybe you should sign up for the Stock Market Game too :)
Last time I checked the fee was $15 :D

Quote:

Originally Posted by NovaScotian
Or even: "Acocdrnig to an elgnsih unviesitry sutdy the oredr of letetrs in a wrod dosen't mttaer, the olny thnig thta's iopmrantt is that the frsit and lsat ltteer of eevry word is in the crcreot ptoision. The rset can be jmbueld and one is stlil able to raed the txet wiohtut dclftfuity"

I bleive taht we leanred taht in shoocl too :)

fazstp 03-13-2008 08:17 PM

Quote:

Originally Posted by Zalister (Post 457797)
Didn't take me long to see that there were in fact 6 Fs, and that for some reason my mind was completely skipping over the Fs in the world "of". How ridiculous! Why on earth does that happen? Fastzp, is there an explanation for this?

I have no idea why. My wife found the question on an Alzheimer's website but there wasn't much in the way of explanation. I just found it interesting because I was pretty convinced there were 4 Fs. It seems like such a simple task for the brain.

tw 03-13-2008 09:05 PM

Quote:

Originally Posted by fazstp (Post 457924)
I have no idea why. My wife found the question on an Alzheimer's website but there wasn't much in the way of explanation. I just found it interesting because I was pretty convinced there were 4 Fs. It seems like such a simple task for the brain.

I think its because the brain pays less attention to connector words than to substance words. we don't really think word by word, we think phrase by phrase, and those 'of's are just there to tie the phrases together.

fazstp 03-14-2008 01:08 AM

Quote:

Originally Posted by EatsWithFingers (Post 457773)
Below is code which returns an answer based on probability. A random character is chosen, then checked for equivalence with the target letter. Over time, the ratio of hits/(misses + hits) will tend towards the ratio of 'target letters'/total.

That's crazy enough to work :D . I found 10000 passes gives you a pretty accurate count.


All times are GMT -5. The time now is 04:33 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.